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(),
#{
sample_license_info => #{
value => #{
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"
}
value => sample_license_info_response()
}
}
)
@ -93,13 +83,36 @@ schema("/license/upload") ->
}
),
responses => #{
200 => <<"ok">>,
400 => emqx_dashboard_swagger:error_codes([?BAD_REQUEST], <<"bad request">>),
404 => emqx_dashboard_swagger:error_codes([?NOT_FOUND], <<"file not found">>)
200 => emqx_dashboard_swagger:schema_with_examples(
map(),
#{
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 = maps:from_list(emqx_license_checker:dump()),
{200, License}.
@ -111,20 +124,28 @@ schema("/license/upload") ->
msg => "license_file_not_found",
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} ->
?SLOG(error, #{
msg => "bad_license_file",
reason => Error,
path => Filepath
}),
{400, <<"bad request">>};
{400, error_msg(?BAD_REQUEST, <<"Bad license file">>)};
{ok, _} ->
?SLOG(info, #{
msg => "updated_license_file",
path => Filepath
}),
{200, <<"ok">>}
License = maps:from_list(emqx_license_checker:dump()),
{200, License}
end;
'/license/upload'(post, #{body := #{<<"key">> := Key}}) ->
case emqx_license:update_key(Key) of
@ -133,10 +154,11 @@ schema("/license/upload") ->
msg => "bad_license_key",
reason => Error
}),
{400, <<"bad request">>};
{400, error_msg(?BAD_REQUEST, <<"Bad license key">>)};
{ok, _} ->
?SLOG(info, #{msg => "updated_license_key"}),
{200, <<"ok">>}
License = maps:from_list(emqx_license_checker:dump()),
{200, License}
end;
'/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",
ok = file:write_file(Path, NewKey),
try
Res = request(
post,
uri(["license", "upload"]),
#{file => Path}
),
?assertMatch({ok, 200, _}, Res),
{ok, 200, Payload} = Res,
?assertEqual(
{ok, 200, <<"ok">>},
request(
post,
uri(["license", "upload"]),
#{file => Path}
)
#{
<<"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(
#{max_connections := 999},
@ -136,13 +149,20 @@ t_license_upload_file_success(_Config) ->
end.
t_license_upload_file_not_found(_Config) ->
Res = request(
post,
uri(["license", "upload"]),
#{file => "/tmp/inexistent.lic"}
),
?assertMatch({ok, 404, _}, Res),
{ok, 404, Payload} = Res,
?assertEqual(
{ok, 404, <<"file not found">>},
request(
post,
uri(["license", "upload"]),
#{file => "/tmp/inexistent.lic"}
)
#{
<<"code">> => <<"NOT_FOUND">>,
<<"message">> => <<"File not found">>
},
emqx_json:decode(Payload, [return_maps])
),
assert_untouched_license(),
ok.
@ -150,13 +170,19 @@ t_license_upload_file_not_found(_Config) ->
t_license_upload_file_reading_error(_Config) ->
%% eisdir
Path = "/tmp/",
Res = request(
post,
uri(["license", "upload"]),
#{file => Path}
),
?assertMatch({ok, 400, _}, Res),
{ok, 400, Payload} = Res,
?assertEqual(
{ok, 400, <<"bad request">>},
request(
post,
uri(["license", "upload"]),
#{file => Path}
)
#{
<<"code">> => <<"BAD_REQUEST">>,
<<"message">> => <<"Illegal operation on a directory">>
},
emqx_json:decode(Payload, [return_maps])
),
assert_untouched_license(),
ok.
@ -165,13 +191,19 @@ t_license_upload_file_bad_license(_Config) ->
Path = "/tmp/bad.lic",
ok = file:write_file(Path, <<"bad key">>),
try
Res = request(
post,
uri(["license", "upload"]),
#{file => Path}
),
?assertMatch({ok, 400, _}, Res),
{ok, 400, Payload} = Res,
?assertEqual(
{ok, 400, <<"bad request">>},
request(
post,
uri(["license", "upload"]),
#{file => Path}
)
#{
<<"code">> => <<"BAD_REQUEST">>,
<<"message">> => <<"Bad license file">>
},
emqx_json:decode(Payload, [return_maps])
),
assert_untouched_license(),
ok
@ -182,13 +214,26 @@ t_license_upload_file_bad_license(_Config) ->
t_license_upload_key_success(_Config) ->
NewKey = emqx_license_test_lib:make_license(#{max_connections => "999"}),
Res = request(
post,
uri(["license", "upload"]),
#{key => NewKey}
),
?assertMatch({ok, 200, _}, Res),
{ok, 200, Payload} = Res,
?assertEqual(
{ok, 200, <<"ok">>},
request(
post,
uri(["license", "upload"]),
#{key => NewKey}
)
#{
<<"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(
#{max_connections := 999},
@ -198,13 +243,19 @@ t_license_upload_key_success(_Config) ->
t_license_upload_key_bad_key(_Config) ->
BadKey = <<"bad key">>,
Res = request(
post,
uri(["license", "upload"]),
#{key => BadKey}
),
?assertMatch({ok, 400, _}, Res),
{ok, 400, Payload} = Res,
?assertEqual(
{ok, 400, <<"bad request">>},
request(
post,
uri(["license", "upload"]),
#{key => BadKey}
)
#{
<<"code">> => <<"BAD_REQUEST">>,
<<"message">> => <<"Bad license key">>
},
emqx_json:decode(Payload, [return_maps])
),
assert_untouched_license(),
ok.