Skip to content

Commit

Permalink
Refactor GitHub api layer for easier and more flexible testing
Browse files Browse the repository at this point in the history
- removed bypass from all tests except lib/code_corps/github/api_test.exs
- introduced `with_mock_api` macro to mock out an api layer module for usage in misc github tests
- implementd proper bypass tests in `api_test`
- removed github_case since it's bypass facilities are now only existing in `api_test`
- fixed several minor bugs and issues revealed through this process
  • Loading branch information
begedin committed Sep 4, 2017
1 parent 6e7960e commit 833dce1
Show file tree
Hide file tree
Showing 50 changed files with 749 additions and 528 deletions.
1 change: 1 addition & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ config :code_corps, :corsica_log_level, [rejected: :warn]
{:ok, pem} = (System.get_env("GITHUB_APP_PEM") || "") |> Base.decode64()

config :code_corps,
github: CodeCorps.GitHub.API,
github_app_id: System.get_env("GITHUB_APP_ID"),
github_app_client_id: System.get_env("GITHUB_APP_CLIENT_ID"),
github_app_client_secret: System.get_env("GITHUB_APP_CLIENT_SECRET"),
Expand Down
4 changes: 3 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ pem = case System.get_env("GITHUB_APP_PEM") do
encoded_pem -> encoded_pem |> Base.decode64!
end

config :code_corps, github_app_pem: pem
config :code_corps,
github: CodeCorps.GitHub.SuccessAPI,
github_app_pem: pem
228 changes: 0 additions & 228 deletions lib/code_corps/github.ex

This file was deleted.

41 changes: 41 additions & 0 deletions lib/code_corps/github/api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule CodeCorps.GitHub.API do
alias CodeCorps.{
GitHub,
GitHub.APIError,
GitHub.HTTPClientError
}

@spec request(GitHub.method, String.t, GitHub.headers, GitHub.body, list) :: GitHub.response
def request(method, url, headers, body, options) do
method
|> :hackney.request(url, headers, body, options)
|> marshall_response()
end

@typep http_success :: {:ok, integer, [{String.t, String.t}], String.t}
@typep http_failure :: {:error, term}

@spec marshall_response(http_success | http_failure) :: GitHub.response
defp marshall_response({:ok, status, _headers, body}) when status in 200..299 do
case body |> Poison.decode do
{:ok, json} ->
{:ok, json}
{:error, {_decoding_error, _decoding_value}} ->
marshall_response({:error, :body_decoding_error})
end
end
defp marshall_response({:ok, 404, _headers, body}) do
{:error, APIError.new({404, %{"message" => body}})}
end
defp marshall_response({:ok, status, _headers, body}) when status in 400..599 do
case body |> Poison.decode do
{:ok, json} ->
{:error, APIError.new({status, json})}
{:error, {_decoding_error, _decoding_value}} ->
marshall_response({:error, :body_decoding_error})
end
end
defp marshall_response({:error, reason}) do
{:error, HTTPClientError.new(reason: reason)}
end
end
12 changes: 0 additions & 12 deletions lib/code_corps/github/bypass.ex

This file was deleted.

0 comments on commit 833dce1

Please sign in to comment.