diff --git a/src/phi/api/rest.py b/src/phi/api/rest.py index c9c2a61..5161545 100644 --- a/src/phi/api/rest.py +++ b/src/phi/api/rest.py @@ -11,6 +11,7 @@ log = get_logger(__name__) class User(View): async def get(self): uid = self.request.match_info.get('uid', None) + if uid is None: return HTTPUnprocessableEntity() diff --git a/src/phi/api/routes.py b/src/phi/api/routes.py index fb7de51..5854991 100644 --- a/src/phi/api/routes.py +++ b/src/phi/api/routes.py @@ -4,5 +4,7 @@ from phi.api.rest import User api_routes = [ + route('*', '/user', User), + route('*', '/user/', User), route('*', '/user/{uid}', User) ] diff --git a/test/conftest.py b/test/conftest.py index cb33e1c..65f087a 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,5 +1,6 @@ import pytest import phi.ldap.client +import phi.api.app @pytest.fixture @@ -15,3 +16,19 @@ def ldap_client(): client.open() yield client client.close() + + +@pytest.fixture +def api_app(): + return phi.api.app.api_app({ + 'ldap': { + 'host': 'localhost', + 'port': 389, + 'encryption': 'TLSv1.2', + 'ciphers': 'HIGH', + 'validate': 'False', + 'username': 'uid=phi,ou=Services,dc=unit,dc=macaomilano,dc=org', + 'password': 'phi', + 'base_dn': 'dc=unit,dc=macaomilano,dc=org', + 'attribute_id': 'uid', + 'attribute_mail': 'mail'}}) diff --git a/test/test_api.py b/test/test_api.py new file mode 100644 index 0000000..918e67b --- /dev/null +++ b/test/test_api.py @@ -0,0 +1,15 @@ +async def test_user_request_not_valid(test_client, api_app): + client = await test_client(api_app) + + resp = await client.get('/user') + assert resp.status == 422 + resp = None + + resp = await client.get('/user/') + assert resp.status == 422 + + +async def test_user_not_found(test_client, api_app): + client = await test_client(api_app) + resp = await client.get('/user/nonexistent') + assert resp.status == 404