-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathfailure_api.ex
More file actions
36 lines (28 loc) · 1.36 KB
/
failure_api.ex
File metadata and controls
36 lines (28 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
defmodule CodeCorps.GitHub.FailureAPI do
@moduledoc ~S"""
A basic GitHub API mock which returns a 401 forbidden for all requests.
Should be good enough for any tests that simply assert a piece of code is able
to recover from a generic request error.
For any tests that cover handling of specific errors, a non-default API should
be defined inline.
Since our GitHub requests are often forced to start with an installation
access token request, that one is set to succeed here as well.
"""
import CodeCorps.GitHub.TestHelpers
alias CodeCorps.GitHub.SuccessAPI
def request(method, url, body, headers, options) do
case {method, url} |> for_access_token?() do
true -> SuccessAPI.request(method, url, body, headers, options)
false ->
send(self(), {method, url, body, headers, options})
{:ok, body} = load_endpoint_fixture("forbidden") |> Poison.encode
{:ok, %HTTPoison.Response{status_code: 401, body: body}}
end
end
defp for_access_token?({:post, url}), do: url |> access_token_url?()
defp for_access_token?({_method, _url}), do: false
defp access_token_url?("https://api.github.com/" <> path), do: path |> String.split("/") |> access_token_parts?()
defp access_token_url?(_), do: false
defp access_token_parts?(["installations", _, "access_tokens"]), do: true
defp access_token_parts?(_), do: false
end