Skip to content

Commit

Permalink
TD-635: fix issue for list method
Browse files Browse the repository at this point in the history
  • Loading branch information
anatoliy.losev committed Aug 27, 2023
1 parent 17fb36a commit 1935e6b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apps/akm/priv/mails/request_revoke.dtl
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ From: no-reply@empayre.com
To: You


To revoke key, go to link: {{ url }}/apikeys/v2/orgs/revoke_party/revoke-api-key/{{ api_key_id }}?apiKeyRevokeToken={{ revoke_token }}
To revoke key, go to link: {{ url }}/apikeys/v2/orgs/{{ party_id }}/revoke-api-key/{{ api_key_id }}?apiKeyRevokeToken={{ revoke_token }}
3 changes: 1 addition & 2 deletions apps/akm/src/akm_apikeys_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ prepare(
#{
'partyId' := PartyID,
'limit' := Limit,
'status' := Status0,
'status' := Status,
continuationToken := ContinuationToken0
},
Context,
Expand All @@ -110,7 +110,6 @@ prepare(
Resolution = akm_auth:authorize_operation(Prototypes, Context),
{ok, Resolution}
end,
Status = genlib:define(Status0, <<"active">>),
ContinuationToken = erlang:binary_to_integer(genlib:define(ContinuationToken0, <<"0">>)),
Process = fun() ->
{ok, Response} = akm_apikeys_processing:list_api_keys(PartyID, Status, Limit, ContinuationToken),
Expand Down
22 changes: 16 additions & 6 deletions apps/akm/src/akm_apikeys_processing.erl
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,7 @@ get_api_key(ApiKeyId) ->

-spec list_api_keys(binary(), binary(), non_neg_integer(), non_neg_integer()) -> {ok, list_keys_response()}.
list_api_keys(PartyId, Status, Limit, Offset) ->
{ok, Columns, Rows} = epgsql_pool:query(
main_pool,
"SELECT id, name, status, metadata, created_at FROM apikeys where party_id = $1 AND status = $2 "
"ORDER BY created_at DESC LIMIT $3 OFFSET $4",
[PartyId, Status, Limit, Offset]
),
{ok, Columns, Rows} = get_keys(PartyId, Status, Limit, Offset),
case erlang:length(Rows) < Limit of
true ->
% last piece of data
Expand Down Expand Up @@ -164,6 +159,21 @@ get_full_api_key(ApiKeyId) ->
{ok, ApiKey}
end.

get_keys(PartyId, undefined, Limit, Offset) ->
epgsql_pool:query(
main_pool,
"SELECT id, name, status, metadata, created_at FROM apikeys where party_id = $1 "
"ORDER BY created_at DESC LIMIT $2 OFFSET $3",
[PartyId, Limit, Offset]
);
get_keys(PartyId, Status, Limit, Offset) ->
epgsql_pool:query(
main_pool,
"SELECT id, name, status, metadata, created_at FROM apikeys where party_id = $1 AND status = $2 "
"ORDER BY created_at DESC LIMIT $3 OFFSET $4",
[PartyId, Status, Limit, Offset]
).

%% Encode/Decode

to_marshalled_maps(Columns, Rows) ->
Expand Down
53 changes: 48 additions & 5 deletions apps/akm/test/akm_basic_test_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
-export([list_keys_test/1]).
-export([revoke_key_w_email_error_test/1]).
-export([revoke_key_test/1]).
-export([list_keys_w_status_test/1]).

%% also defined in ct hook module akm_cth.erl
-define(ACCESS_TOKEN, <<"some.access.token">>).
Expand Down Expand Up @@ -45,7 +46,8 @@ groups() ->
get_unknown_key_test,
list_keys_test,
revoke_key_w_email_error_test,
revoke_key_test
revoke_key_test,
list_keys_w_status_test
]}
].

Expand All @@ -60,12 +62,15 @@ init_per_testcase(revoke_key_w_email_error_test, C) ->
end
),
C;
init_per_testcase(revoke_key_test, C) ->
init_per_testcase(Name, C) when
Name =:= revoke_key_test;
Name =:= list_keys_w_status_test
->
meck:expect(
gen_smtp_client,
send,
fun({_, _, Msg}, _, CallbackFun) ->
application:set_env(akm, email_msg_revoke_key_test, Msg),
application:set_env(akm, Name, Msg),
P = spawn(fun() -> CallbackFun({ok, <<"success">>}) end),
{ok, P}
end
Expand All @@ -77,7 +82,8 @@ init_per_testcase(_Name, C) ->
-spec end_per_testcase(test_case_name(), config()) -> _.
end_per_testcase(Name, C) when
Name =:= revoke_key_w_email_error_test;
Name =:= revoke_key_test
Name =:= revoke_key_test;
Name =:= list_keys_w_status_test
->
meck:unload(gen_smtp_client),
C;
Expand Down Expand Up @@ -201,7 +207,7 @@ revoke_key_test(Config) ->
%% check success request revoke
{204, _, _} = akm_client:request_revoke_key(Host, Port, PartyId, ApiKeyId),

RevokePath = extract_revoke_path(email_msg_revoke_key_test),
RevokePath = extract_revoke_path(revoke_key_test),
RevokeWithBadApiKeyId = break_api_key_id(RevokePath, ApiKeyId),
RevokeWithBadRevokeToken = break_revoke_token(RevokePath),

Expand All @@ -214,6 +220,43 @@ revoke_key_test(Config) ->
%% check success revoke
{204, _, _} = akm_client:revoke_key(Host, Port, RevokePath).

-spec list_keys_w_status_test(config()) -> test_result().
list_keys_w_status_test(Config) ->
Host = akm_ct_utils:lookup_config(akm_host, Config),
Port = akm_ct_utils:lookup_config(akm_port, Config),
PartyId = <<"list-keys-w-status-party">>,

#{
<<"apiKey">> := #{
<<"id">> := RevokingApiKeyId
} = RevokingApiKey
} = akm_client:issue_key(Host, Port, PartyId, #{name => <<"RevokingApiKey">>}),
#{
<<"apiKey">> := ActiveApiKey
} = akm_client:issue_key(Host, Port, PartyId, #{name => <<"ActiveApiKey">>}),

{204, _, _} = akm_client:request_revoke_key(Host, Port, PartyId, RevokingApiKeyId),
RevokePath = extract_revoke_path(list_keys_w_status_test),
{204, _, _} = akm_client:revoke_key(Host, Port, RevokePath),
RevokedApiKey = RevokingApiKey#{<<"status">> => <<"revoked">>},

%% check full list by default
#{
<<"results">> := [ActiveApiKey, RevokedApiKey]
} = akm_client:list_keys(Host, Port, PartyId),

%% check list of active keys
#{
<<"results">> := [ActiveApiKey]
} = akm_client:list_keys(Host, Port, PartyId, [{<<"status">>, <<"active">>}, {<<"limit">>, <<"1000">>}]),

%% check list of revoked keys
#{
<<"results">> := [RevokedApiKey]
} = akm_client:list_keys(Host, Port, PartyId, [{<<"status">>, <<"revoked">>}, {<<"limit">>, <<"1000">>}]).

%% Internal functions

get_list_keys(Host, Port, PartyId, Limit, #{<<"results">> := ListKeys, <<"continuationToken">> := Cont}, Acc) ->
Params = [{<<"limit">>, Limit}, {<<"continuationToken">>, Cont}],
get_list_keys(Host, Port, PartyId, Limit, akm_client:list_keys(Host, Port, PartyId, Params), Acc ++ ListKeys);
Expand Down

0 comments on commit 1935e6b

Please sign in to comment.