chore(license): change api responses after review

This commit is contained in:
Thales Macedo Garitezi 2022-08-01 09:27:41 -03:00
parent b19e8fb3cd
commit 1a66e53c49
2 changed files with 129 additions and 56 deletions

View File

@ -47,17 +47,7 @@ schema("/license") ->
map(), map(),
#{ #{
sample_license_info => #{ sample_license_info => #{
value => #{ value => sample_license_info_response()
customer => "Foo",
customer_type => 10,
deployment => "bar-deployment",
email => "contact@foo.com",
expiry => false,
expiry_at => "2295-10-27",
max_connections => 10,
start_at => "2022-01-11",
type => "trial"
}
} }
} }
) )
@ -93,13 +83,36 @@ schema("/license/upload") ->
} }
), ),
responses => #{ responses => #{
200 => <<"ok">>, 200 => emqx_dashboard_swagger:schema_with_examples(
400 => emqx_dashboard_swagger:error_codes([?BAD_REQUEST], <<"bad request">>), map(),
404 => emqx_dashboard_swagger:error_codes([?NOT_FOUND], <<"file not found">>) #{
sample_license_info => #{
value => sample_license_info_response()
}
}
),
400 => emqx_dashboard_swagger:error_codes([?BAD_REQUEST], <<"Bad license key">>),
404 => emqx_dashboard_swagger:error_codes([?NOT_FOUND], <<"File not found">>)
} }
} }
}. }.
sample_license_info_response() ->
#{
customer => "Foo",
customer_type => 10,
deployment => "bar-deployment",
email => "contact@foo.com",
expiry => false,
expiry_at => "2295-10-27",
max_connections => 10,
start_at => "2022-01-11",
type => "trial"
}.
error_msg(Code, Msg) ->
#{code => Code, message => emqx_misc:readable_error_msg(Msg)}.
'/license'(get, _Params) -> '/license'(get, _Params) ->
License = maps:from_list(emqx_license_checker:dump()), License = maps:from_list(emqx_license_checker:dump()),
{200, License}. {200, License}.
@ -111,20 +124,28 @@ schema("/license/upload") ->
msg => "license_file_not_found", msg => "license_file_not_found",
path => Filepath path => Filepath
}), }),
{404, <<"file not found">>}; {404, error_msg(?NOT_FOUND, <<"File not found">>)};
{error, Error} when is_atom(Error) ->
?SLOG(error, #{
msg => "bad_license_file",
reason => Error,
path => Filepath
}),
{400, error_msg(?BAD_REQUEST, emqx_misc:explain_posix(Error))};
{error, Error} -> {error, Error} ->
?SLOG(error, #{ ?SLOG(error, #{
msg => "bad_license_file", msg => "bad_license_file",
reason => Error, reason => Error,
path => Filepath path => Filepath
}), }),
{400, <<"bad request">>}; {400, error_msg(?BAD_REQUEST, <<"Bad license file">>)};
{ok, _} -> {ok, _} ->
?SLOG(info, #{ ?SLOG(info, #{
msg => "updated_license_file", msg => "updated_license_file",
path => Filepath path => Filepath
}), }),
{200, <<"ok">>} License = maps:from_list(emqx_license_checker:dump()),
{200, License}
end; end;
'/license/upload'(post, #{body := #{<<"key">> := Key}}) -> '/license/upload'(post, #{body := #{<<"key">> := Key}}) ->
case emqx_license:update_key(Key) of case emqx_license:update_key(Key) of
@ -133,10 +154,11 @@ schema("/license/upload") ->
msg => "bad_license_key", msg => "bad_license_key",
reason => Error reason => Error
}), }),
{400, <<"bad request">>}; {400, error_msg(?BAD_REQUEST, <<"Bad license key">>)};
{ok, _} -> {ok, _} ->
?SLOG(info, #{msg => "updated_license_key"}), ?SLOG(info, #{msg => "updated_license_key"}),
{200, <<"ok">>} License = maps:from_list(emqx_license_checker:dump()),
{200, License}
end; end;
'/license/upload'(post, _Params) -> '/license/upload'(post, _Params) ->
{400, <<"bad request">>}. {400, error_msg(?BAD_REQUEST, <<"Invalid request params">>)}.

View File

@ -117,13 +117,26 @@ t_license_upload_file_success(_Config) ->
Path = "/tmp/new.lic", Path = "/tmp/new.lic",
ok = file:write_file(Path, NewKey), ok = file:write_file(Path, NewKey),
try try
?assertEqual( Res = request(
{ok, 200, <<"ok">>},
request(
post, post,
uri(["license", "upload"]), uri(["license", "upload"]),
#{file => Path} #{file => Path}
) ),
?assertMatch({ok, 200, _}, Res),
{ok, 200, Payload} = Res,
?assertEqual(
#{
<<"customer">> => <<"Foo">>,
<<"customer_type">> => 10,
<<"deployment">> => <<"bar-deployment">>,
<<"email">> => <<"contact@foo.com">>,
<<"expiry">> => false,
<<"expiry_at">> => <<"2295-10-27">>,
<<"max_connections">> => 999,
<<"start_at">> => <<"2022-01-11">>,
<<"type">> => <<"trial">>
},
emqx_json:decode(Payload, [return_maps])
), ),
?assertMatch( ?assertMatch(
#{max_connections := 999}, #{max_connections := 999},
@ -136,13 +149,20 @@ t_license_upload_file_success(_Config) ->
end. end.
t_license_upload_file_not_found(_Config) -> t_license_upload_file_not_found(_Config) ->
?assertEqual( Res = request(
{ok, 404, <<"file not found">>},
request(
post, post,
uri(["license", "upload"]), uri(["license", "upload"]),
#{file => "/tmp/inexistent.lic"} #{file => "/tmp/inexistent.lic"}
) ),
?assertMatch({ok, 404, _}, Res),
{ok, 404, Payload} = Res,
?assertEqual(
#{
<<"code">> => <<"NOT_FOUND">>,
<<"message">> => <<"File not found">>
},
emqx_json:decode(Payload, [return_maps])
), ),
assert_untouched_license(), assert_untouched_license(),
ok. ok.
@ -150,13 +170,19 @@ t_license_upload_file_not_found(_Config) ->
t_license_upload_file_reading_error(_Config) -> t_license_upload_file_reading_error(_Config) ->
%% eisdir %% eisdir
Path = "/tmp/", Path = "/tmp/",
?assertEqual( Res = request(
{ok, 400, <<"bad request">>},
request(
post, post,
uri(["license", "upload"]), uri(["license", "upload"]),
#{file => Path} #{file => Path}
) ),
?assertMatch({ok, 400, _}, Res),
{ok, 400, Payload} = Res,
?assertEqual(
#{
<<"code">> => <<"BAD_REQUEST">>,
<<"message">> => <<"Illegal operation on a directory">>
},
emqx_json:decode(Payload, [return_maps])
), ),
assert_untouched_license(), assert_untouched_license(),
ok. ok.
@ -165,13 +191,19 @@ t_license_upload_file_bad_license(_Config) ->
Path = "/tmp/bad.lic", Path = "/tmp/bad.lic",
ok = file:write_file(Path, <<"bad key">>), ok = file:write_file(Path, <<"bad key">>),
try try
?assertEqual( Res = request(
{ok, 400, <<"bad request">>},
request(
post, post,
uri(["license", "upload"]), uri(["license", "upload"]),
#{file => Path} #{file => Path}
) ),
?assertMatch({ok, 400, _}, Res),
{ok, 400, Payload} = Res,
?assertEqual(
#{
<<"code">> => <<"BAD_REQUEST">>,
<<"message">> => <<"Bad license file">>
},
emqx_json:decode(Payload, [return_maps])
), ),
assert_untouched_license(), assert_untouched_license(),
ok ok
@ -182,13 +214,26 @@ t_license_upload_file_bad_license(_Config) ->
t_license_upload_key_success(_Config) -> t_license_upload_key_success(_Config) ->
NewKey = emqx_license_test_lib:make_license(#{max_connections => "999"}), NewKey = emqx_license_test_lib:make_license(#{max_connections => "999"}),
?assertEqual( Res = request(
{ok, 200, <<"ok">>},
request(
post, post,
uri(["license", "upload"]), uri(["license", "upload"]),
#{key => NewKey} #{key => NewKey}
) ),
?assertMatch({ok, 200, _}, Res),
{ok, 200, Payload} = Res,
?assertEqual(
#{
<<"customer">> => <<"Foo">>,
<<"customer_type">> => 10,
<<"deployment">> => <<"bar-deployment">>,
<<"email">> => <<"contact@foo.com">>,
<<"expiry">> => false,
<<"expiry_at">> => <<"2295-10-27">>,
<<"max_connections">> => 999,
<<"start_at">> => <<"2022-01-11">>,
<<"type">> => <<"trial">>
},
emqx_json:decode(Payload, [return_maps])
), ),
?assertMatch( ?assertMatch(
#{max_connections := 999}, #{max_connections := 999},
@ -198,13 +243,19 @@ t_license_upload_key_success(_Config) ->
t_license_upload_key_bad_key(_Config) -> t_license_upload_key_bad_key(_Config) ->
BadKey = <<"bad key">>, BadKey = <<"bad key">>,
?assertEqual( Res = request(
{ok, 400, <<"bad request">>},
request(
post, post,
uri(["license", "upload"]), uri(["license", "upload"]),
#{key => BadKey} #{key => BadKey}
) ),
?assertMatch({ok, 400, _}, Res),
{ok, 400, Payload} = Res,
?assertEqual(
#{
<<"code">> => <<"BAD_REQUEST">>,
<<"message">> => <<"Bad license key">>
},
emqx_json:decode(Payload, [return_maps])
), ),
assert_untouched_license(), assert_untouched_license(),
ok. ok.