test with args/extra/type

This commit is contained in:
Kay0u 2020-01-08 23:08:59 +07:00
parent c603a4e41b
commit b6cb1b5e7e
No known key found for this signature in database
GPG key ID: 7FF262C033518333
3 changed files with 114 additions and 0 deletions

View file

@ -75,6 +75,29 @@ testauth:
- all - all
authenticator: ldap authenticator: ldap
with_arg:
api: GET /test-auth/with_arg/<super_arg>
arguments:
super_arg:
help: Super Arg
with_extra_str_only:
api: GET /test-auth/with_extra_str_only/<only_a_str>
arguments:
only_a_str:
help: Only a String
extra:
pattern:
- !!str ^[a-zA-Z]
- "pattern_only_a_str"
with_type_int:
api: GET /test-auth/with_type_int/<only_an_int>
arguments:
only_an_int:
help: Only an Int
type: int
subcategories: subcategories:
subcat: subcat:
actions: actions:

View file

@ -38,5 +38,17 @@ def testauth_ldap():
return "some_data_from_ldap" return "some_data_from_ldap"
def testauth_with_arg(super_arg):
return super_arg
def testauth_with_extra_str_only(only_a_str):
return only_a_str
def testauth_with_type_int(only_an_int):
return only_an_int
def yoloswag_version(*args, **kwargs): def yoloswag_version(*args, **kwargs):
return "666" return "666"

View file

@ -170,6 +170,43 @@ class TestAuthAPI:
== '"some_data_from_ldap"' == '"some_data_from_ldap"'
) )
def test_request_with_arg(self, moulinette_webapi, capsys):
self.login(moulinette_webapi)
assert (
moulinette_webapi.get("/test-auth/with_arg/yoloswag", status=200).text
== '"yoloswag"'
)
def test_request_arg_with_extra(self, moulinette_webapi, caplog, mocker):
self.login(moulinette_webapi)
assert (
moulinette_webapi.get(
"/test-auth/with_extra_str_only/YoLoSwAg", status=200
).text
== '"YoLoSwAg"'
)
error = "error_message"
mocker.patch("moulinette.Moulinette18n.n", return_value=error)
moulinette_webapi.get("/test-auth/with_extra_str_only/12345", status=400)
assert any("doesn't match pattern" in message for message in caplog.messages)
def test_request_arg_with_type(self, moulinette_webapi, caplog, mocker):
self.login(moulinette_webapi)
assert (
moulinette_webapi.get("/test-auth/with_type_int/12345", status=200).text
== "12345"
)
error = "error_message"
mocker.patch("moulinette.Moulinette18n.g", return_value=error)
moulinette_webapi.get("/test-auth/with_type_int/yoloswag", status=400)
class TestAuthCLI: class TestAuthCLI:
def test_login(self, moulinette_cli, capsys, mocker): def test_login(self, moulinette_cli, capsys, mocker):
@ -262,3 +299,45 @@ class TestAuthCLI:
message = capsys.readouterr() message = capsys.readouterr()
assert "cannot get value from callback method" in message.err assert "cannot get value from callback method" in message.err
def test_request_with_arg(self, moulinette_cli, capsys, mocker):
mocker.patch("getpass.getpass", return_value="default")
moulinette_cli.run(["testauth", "with_arg", "yoloswag"], output_as="plain")
message = capsys.readouterr()
assert "yoloswag" in message.out
def test_request_arg_with_extra(self, moulinette_cli, capsys, mocker):
mocker.patch("getpass.getpass", return_value="default")
moulinette_cli.run(
["testauth", "with_extra_str_only", "YoLoSwAg"], output_as="plain"
)
message = capsys.readouterr()
assert "YoLoSwAg" in message.out
error = "error_message"
mocker.patch("moulinette.Moulinette18n.n", return_value=error)
with pytest.raises(MoulinetteError):
moulinette_cli.run(
["testauth", "with_extra_str_only", "12345"], output_as="plain"
)
message = capsys.readouterr()
assert "doesn't match pattern" in message.err
def test_request_arg_with_type(self, moulinette_cli, capsys, mocker):
mocker.patch("getpass.getpass", return_value="default")
moulinette_cli.run(["testauth", "with_type_int", "12345"], output_as="plain")
message = capsys.readouterr()
assert "12345" in message.out
mocker.patch("sys.exit")
with pytest.raises(MoulinetteError):
moulinette_cli.run(
["testauth", "with_type_int", "yoloswag"], output_as="plain"
)
message = capsys.readouterr()
assert "invalid int value" in message.err