Merge pull request #7898 from zhongwencool/replace-undefined-to-infinity

fix: infinity means never expire
This commit is contained in:
zhongwencool 2022-05-09 16:43:54 +08:00 committed by GitHub
commit 5bae3e4907
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 12 deletions

View File

@ -120,12 +120,12 @@ fields(app) ->
)},
{expired_at,
hoconsc:mk(
hoconsc:union([undefined, emqx_datetime:epoch_second()]),
hoconsc:union([infinity, emqx_datetime:epoch_second()]),
#{
desc => "No longer valid datetime",
example => <<"2021-12-05T02:01:34.186Z">>,
required => false,
default => undefined
default => infinity
}
)},
{created_at,
@ -219,7 +219,7 @@ api_key_by_name(put, #{bindings := #{name := Name}, body := Body}) ->
format(App = #{expired_at := ExpiredAt0, created_at := CreateAt}) ->
ExpiredAt =
case ExpiredAt0 of
undefined -> <<"undefined">>;
infinity -> <<"infinity">>;
_ -> list_to_binary(calendar:system_time_to_rfc3339(ExpiredAt0))
end,
App#{
@ -228,4 +228,4 @@ format(App = #{expired_at := ExpiredAt0, created_at := CreateAt}) ->
}.
ensure_expired_at(#{<<"expired_at">> := ExpiredAt}) when is_integer(ExpiredAt) -> ExpiredAt;
ensure_expired_at(_) -> undefined.
ensure_expired_at(_) -> infinity.

View File

@ -108,10 +108,10 @@ t_update(_Config) ->
),
Unexpired1 = maps:without([expired_at], Change),
{ok, Update2} = update_app(Name, Unexpired1),
?assertEqual(<<"undefined">>, maps:get(<<"expired_at">>, Update2)),
Unexpired2 = Change#{expired_at => <<"undefined">>},
?assertEqual(<<"infinity">>, maps:get(<<"expired_at">>, Update2)),
Unexpired2 = Change#{expired_at => <<"infinity">>},
{ok, Update3} = update_app(Name, Unexpired2),
?assertEqual(<<"undefined">>, maps:get(<<"expired_at">>, Update3)),
?assertEqual(<<"infinity">>, maps:get(<<"expired_at">>, Update3)),
?assertEqual({error, {"HTTP/1.1", 404, "Not Found"}}, update_app(<<"Not-Exist">>, Change)),
ok.
@ -160,9 +160,9 @@ t_authorize(_Config) ->
},
?assertMatch({ok, #{<<"api_key">> := _, <<"enable">> := true}}, update_app(Name, Expired)),
?assertEqual(Unauthorized, emqx_mgmt_api_test_util:request_api(get, BanPath, BasicHeader)),
UnExpired = #{expired_at => undefined},
UnExpired = #{expired_at => infinity},
?assertMatch(
{ok, #{<<"api_key">> := _, <<"expired_at">> := <<"undefined">>}},
{ok, #{<<"api_key">> := _, <<"expired_at">> := <<"infinity">>}},
update_app(Name, UnExpired)
),
{ok, _Status1} = emqx_mgmt_api_test_util:request_api(get, BanPath, BasicHeader),
@ -172,9 +172,9 @@ t_create_unexpired_app(_Config) ->
Name1 = <<"EMQX-UNEXPIRED-API-KEY-1">>,
Name2 = <<"EMQX-UNEXPIRED-API-KEY-2">>,
{ok, Create1} = create_unexpired_app(Name1, #{}),
?assertMatch(#{<<"expired_at">> := <<"undefined">>}, Create1),
{ok, Create2} = create_unexpired_app(Name2, #{expired_at => <<"undefined">>}),
?assertMatch(#{<<"expired_at">> := <<"undefined">>}, Create2),
?assertMatch(#{<<"expired_at">> := <<"infinity">>}, Create1),
{ok, Create2} = create_unexpired_app(Name2, #{expired_at => <<"infinity">>}),
?assertMatch(#{<<"expired_at">> := <<"infinity">>}, Create2),
ok.
list_app() ->