Skip to content

Commit

Permalink
test: add fetch_new_token_on_401
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-kivra committed Nov 1, 2019
1 parent adc6c25 commit 9dc00ba
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions test/oauth2c_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
-include_lib("stdlib/include/assert.hrl").

-define(AUTH_URL, <<"https://authurl.com">>).
-define(INVALID_TOKEN_AUTH_URL, <<"https://invalidauthurl.com">>).
-define(REQUEST_URL, <<"https://requesturl.com">>).
-define(CLIENT_CREDENTIALS_GRANT, <<"client_credentials">>).
-define(VALID_TOKEN, <<"iamanaccesstoken">>).
-define(HEADERS(AccessToken),
[{<<"Authorization">>, <<"bearer ", AccessToken/binary>>}]).

-define(GET_BODY, [{<<"a">>, <<"b">>}]).

Expand All @@ -15,6 +19,7 @@ groups() -> [].
all() -> [ retrieve_access_token
, fetch_access_token_on_request
, fetch_access_token_on_request
, fetch_new_token_on_401
].

init_per_suite(Config) -> Config.
Expand Down Expand Up @@ -53,14 +58,39 @@ fetch_access_token_on_request(_Config) ->
?assert(meck:called(restc, request,
[post, percent, ?AUTH_URL, '_', '_', '_', '_'])).

fetch_new_token_on_401(_Config) ->
{ok, _, Client} = oauth2c:retrieve_access_token(?CLIENT_CREDENTIALS_GRANT,
?INVALID_TOKEN_AUTH_URL,
<<"ID">>,
<<"SECRET">>),
?assert(1 =:= meck:num_calls(restc, request,
[ post, percent,
?INVALID_TOKEN_AUTH_URL, '_', '_', '_', '_'
])),

Response = oauth2c:request(get, json, ?REQUEST_URL, [], [], [], [], Client),
?assertMatch({{ok, 401, _, _}, Client}, Response),
?assert(2 =:= meck:num_calls(restc, request,
[ post, percent,
?INVALID_TOKEN_AUTH_URL, '_', '_', '_', '_'
])).

mock_http_requests() ->
meck:expect(restc, request,
fun(post, percent, ?AUTH_URL, [200], _, _, _) ->
Body = [{<<"access_token">>, <<"iamanaccesstoken">>},
{<<"token_type">>, <<"iamatokentype">>}],
Body = [{<<"access_token">>, ?VALID_TOKEN},
{<<"token_type">>, <<"bearer">>}],
{ok, 200, [], Body};
(post, percent, ?INVALID_TOKEN_AUTH_URL, [200], _, _, _) ->
Body = [{<<"access_token">>, <<"invalid">>},
{<<"token_type">>, <<"bearer">>}],
{ok, 200, [], Body};
(get, json, _, _, _, _, _) ->
{ok, 200, [], [{<<"a">>, <<"b">>}]}
(get, json, _, _, Headers, _, _) ->
ValidToken = ?HEADERS(?VALID_TOKEN),
case Headers of
ValidToken -> {ok, 200, [], [{<<"a">>, <<"b">>}]};
_ -> {ok, 401, [], []}
end
end).

%%%_* Editor ===================================================================
Expand Down

0 comments on commit 9dc00ba

Please sign in to comment.