-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add remote_repo_create/3 function with TestDouble fixes #9
- Loading branch information
Showing
7 changed files
with
178 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import Config | ||
|
||
# set this to false if you want to hit the actual endpoints during development: | ||
config :gogs, | ||
# client_id: "d6fca75c63daa014c187", | ||
# client_secret: "8eeb143935d1a505692aaef856db9b4da8245f3c", | ||
httpoison_mock: false | ||
httpoison_mock: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,117 +6,156 @@ defmodule Gogs.HTTPoisonMock do | |
and return things in the way we expect, | ||
so that we can check the pipeline in ElixirAuthGithub.github_auth | ||
""" | ||
@remote_repo_create_response_body %{ | ||
clone_url: "https://gogs-server.fly.dev/myorg/replacethis.git", | ||
created_at: "0001-01-01T00:00:00Z", | ||
default_branch: "", | ||
description: "replacethis", | ||
empty: false, | ||
fork: false, | ||
forks_count: 0, | ||
full_name: "myorg/replacethis", | ||
html_url: "https://gogs-server.fly.dev/myorg/replacethis", | ||
id: 17, | ||
mirror: false, | ||
name: "test-repo450", | ||
open_issues_count: 0, | ||
owner: %{ | ||
avatar_url: "https://gogs-server.fly.dev/avatars/2", | ||
email: "", | ||
full_name: "", | ||
id: 2, | ||
login: "myorg", | ||
username: "myorg" | ||
}, | ||
parent: nil, | ||
permissions: %{admin: true, pull: true, push: true}, | ||
private: false, | ||
size: 0, | ||
ssh_url: "ssh://[email protected]:10022/myorg/replacethis.git", | ||
stars_count: 0, | ||
updated_at: "0001-01-01T00:00:00Z", | ||
watchers_count: 0, | ||
website: "" | ||
} | ||
|
||
# make a valid response body for testing | ||
def make_repo_create_post_response_body(repo_name) do | ||
Map.merge(@remote_repo_create_response_body, %{ | ||
clone_url: "https://gogs-server.fly.dev/myorg/#{repo_name}.git", | ||
description: repo_name, | ||
full_name: "myorg/#{repo_name}", | ||
html_url: "https://gogs-server.fly.dev/myorg/#{repo_name}", | ||
ssh_url: "ssh://[email protected]:10022/myorg/#{repo_name}.git", | ||
}) | ||
end | ||
|
||
@doc """ | ||
`get/3` stubs the HTTPoison get! function when parameters match test vars. | ||
`post/3` stubs the HTTPoison post function when parameters match test vars. | ||
feel free refactor this if you can make it pretty. | ||
""" | ||
@valid_body %{ | ||
access_token: "12345", | ||
login: "test_user", | ||
name: "Testy McTestface", | ||
email: "[email protected]", | ||
avatar_url: "https://avatars3.githubusercontent.com/u/10835816", | ||
id: "19" | ||
} | ||
def post("https://gogs-server.fly.dev/api/v1/org/myorg/repos", body, _headers) do | ||
# IO.inspect("Gogs.HTTPoisonMock.post/3 called!") | ||
body_map = Jason.decode!(body) |> Useful.atomize_map_keys() | ||
response_body = | ||
make_repo_create_post_response_body(body_map.name) | ||
|> Jason.encode!() | ||
{:ok, %{body: response_body}} | ||
end | ||
|
||
@body_email_nil %{ | ||
access_token: "12345", | ||
login: "test_user", | ||
name: "Testy McTestface", | ||
email: nil, | ||
avatar_url: "https://avatars3.githubusercontent.com/u/10835816", | ||
id: "28" | ||
} | ||
|
||
|
||
@emails [ | ||
%{ | ||
"email" => "[email protected]", | ||
"verified" => true, | ||
"primary" => false, | ||
"visibility" => "private" | ||
}, | ||
%{ | ||
"email" => "[email protected]", | ||
"verified" => true, | ||
"primary" => true, | ||
"visibility" => "private" | ||
} | ||
] | ||
|
||
def get!(url, headers \\ [], options \\ []) | ||
|
||
def get!( | ||
"https://api.github.com/user", | ||
[ | ||
{"User-Agent", "ElixirAuthGithub"}, | ||
{"Authorization", "token 123"} | ||
], | ||
_options | ||
) do | ||
%{body: "{\"error\": \"test error\"}"} | ||
end | ||
# @body_email_nil %{ | ||
# access_token: "12345", | ||
# login: "test_user", | ||
# name: "Testy McTestface", | ||
# email: nil, | ||
# avatar_url: "https://avatars3.githubusercontent.com/u/10835816", | ||
# id: "28" | ||
# } | ||
|
||
def get!( | ||
"https://api.github.com/user", | ||
[ | ||
{"User-Agent", "ElixirAuthGithub"}, | ||
{"Authorization", "token 42"} | ||
], | ||
_options | ||
) do | ||
%{body: Jason.encode!(@body_email_nil)} | ||
end | ||
# @emails [ | ||
# %{ | ||
# "email" => "[email protected]", | ||
# "verified" => true, | ||
# "primary" => false, | ||
# "visibility" => "private" | ||
# }, | ||
# %{ | ||
# "email" => "[email protected]", | ||
# "verified" => true, | ||
# "primary" => true, | ||
# "visibility" => "private" | ||
# } | ||
# ] | ||
|
||
# user emails | ||
def get!( | ||
"https://api.github.com/user/emails", | ||
[ | ||
{"User-Agent", "ElixirAuthGithub"}, | ||
{"Authorization", "token 42"} | ||
], | ||
_options | ||
) do | ||
%{body: Jason.encode!(@emails)} | ||
end | ||
# def get!(url, headers \\ [], options \\ []) | ||
|
||
def get!(_url, _headers, _options) do | ||
%{body: Jason.encode!(@valid_body)} | ||
end | ||
# def get!( | ||
# "https://api.github.com/user", | ||
# [ | ||
# {"User-Agent", "ElixirAuthGithub"}, | ||
# {"Authorization", "token 123"} | ||
# ], | ||
# _options | ||
# ) do | ||
# %{body: "{\"error\": \"test error\"}"} | ||
# end | ||
|
||
@doc """ | ||
`post/3` stubs the HTTPoison post! function when parameters match test vars. | ||
""" | ||
def post!(url, body, headers \\ [], options \\ []) | ||
|
||
def post!( | ||
"https://github.com/login/oauth/access_token?client_id=TEST_ID&client_secret=TEST_SECRET&code=1234", | ||
_body, | ||
_headers, | ||
_options | ||
) do | ||
%{body: "error=error"} | ||
end | ||
# def get!( | ||
# "https://api.github.com/user", | ||
# [ | ||
# {"User-Agent", "ElixirAuthGithub"}, | ||
# {"Authorization", "token 42"} | ||
# ], | ||
# _options | ||
# ) do | ||
# %{body: Jason.encode!(@body_email_nil)} | ||
# end | ||
|
||
def post!( | ||
"https://github.com/login/oauth/access_token?client_id=TEST_ID&client_secret=TEST_SECRET&code=123", | ||
_body, | ||
_headers, | ||
_options | ||
) do | ||
%{body: "access_token=123"} | ||
end | ||
# # user emails | ||
# def get!( | ||
# "https://api.github.com/user/emails", | ||
# [ | ||
# {"User-Agent", "ElixirAuthGithub"}, | ||
# {"Authorization", "token 42"} | ||
# ], | ||
# _options | ||
# ) do | ||
# %{body: Jason.encode!(@emails)} | ||
# end | ||
|
||
def post!( | ||
"https://github.com/login/oauth/access_token?client_id=TEST_ID&client_secret=TEST_SECRET&code=42", | ||
_body, | ||
_headers, | ||
_options | ||
) do | ||
%{body: "access_token=42"} | ||
end | ||
# def get!(_url, _headers, _options) do | ||
# %{body: Jason.encode!(@valid_body)} | ||
# end | ||
|
||
# for some reason GitHub's Post returns a URI encoded string | ||
def post!(_url, _body, _headers, _options) do | ||
%{body: URI.encode_query(@valid_body)} | ||
end | ||
# @doc """ | ||
# `post/3` stubs the HTTPoison post! function when parameters match test vars. | ||
# """ | ||
# def post!(url, body, headers \\ [], options \\ []) | ||
|
||
# def post!( | ||
# "https://github.com/login/oauth/access_token?client_id=TEST_ID&client_secret=TEST_SECRET&code=1234", | ||
# _body, | ||
# _headers, | ||
# _options | ||
# ) do | ||
# %{body: "error=error"} | ||
# end | ||
|
||
# def post!( | ||
# "https://github.com/login/oauth/access_token?client_id=TEST_ID&client_secret=TEST_SECRET&code=123", | ||
# _body, | ||
# _headers, | ||
# _options | ||
# ) do | ||
# %{body: "access_token=123"} | ||
# end | ||
|
||
|
||
|
||
# # for some reason GitHub's Post returns a URI encoded string | ||
# def post!(_url, _body, _headers, _options) do | ||
# %{body: URI.encode_query(@valid_body)} | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defmodule GogsHelpersTest do | ||
use ExUnit.Case | ||
doctest GogsHelpers | ||
|
||
test "GogsHelpers.api_base_url/0 returns the API URL for the Gogs Server" do | ||
assert GogsHelpers.api_base_url() == "https://gogs-server.fly.dev/api/v1/" | ||
end | ||
end |