mirror of
https://github.com/valitydev/api-key-mgmt-v2.git
synced 2024-11-06 02:15:19 +00:00
TD-635: fix issue for list method (#15)
Co-authored-by: anatoliy.losev <losto@nix>
This commit is contained in:
parent
17fb36a860
commit
cc42e72073
@ -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 }}
|
||||
|
@ -99,7 +99,7 @@ prepare(
|
||||
#{
|
||||
'partyId' := PartyID,
|
||||
'limit' := Limit,
|
||||
'status' := Status0,
|
||||
'status' := Status,
|
||||
continuationToken := ContinuationToken0
|
||||
},
|
||||
Context,
|
||||
@ -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),
|
||||
|
@ -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
|
||||
@ -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) ->
|
||||
|
@ -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">>).
|
||||
@ -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
|
||||
]}
|
||||
].
|
||||
|
||||
@ -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
|
||||
@ -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;
|
||||
@ -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),
|
||||
|
||||
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user