From 9ade9615cdd183d36f0733fb5ea59f4e3ff01471 Mon Sep 17 00:00:00 2001 From: Mehmet Baran Geylani Date: Fri, 19 Jul 2024 14:00:08 +0300 Subject: [PATCH] Add tests for service endpoint Signed-off-by: Mehmet Baran Geylani --- tests/conftest.py | 38 +++++++++++++- tests/test_service/__init__.py | 0 tests/test_service/test_service_create.py | 59 ++++++++++++++++++++++ tests/test_service/test_service_list.py | 32 ++++++++++++ tests/test_service/test_service_lookup.py | 34 +++++++++++++ tests/test_service/test_service_refresh.py | 36 +++++++++++++ tests/test_service/test_service_revoke.py | 36 +++++++++++++ tests/test_service/test_service_update.py | 37 ++++++++++++++ tests/test_user/test_user_get.py | 8 --- tests/test_user/test_user_lookup.py | 8 +-- 10 files changed, 272 insertions(+), 16 deletions(-) create mode 100644 tests/test_service/__init__.py create mode 100644 tests/test_service/test_service_create.py create mode 100644 tests/test_service/test_service_list.py create mode 100644 tests/test_service/test_service_lookup.py create mode 100644 tests/test_service/test_service_refresh.py create mode 100644 tests/test_service/test_service_revoke.py create mode 100644 tests/test_service/test_service_update.py diff --git a/tests/conftest.py b/tests/conftest.py index 05e5858..d0d4ef6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,10 +5,12 @@ import pytest # noqa: F401 from webhook_to_fedora_messaging.database import db # noqa: F401 - +from sqlalchemy_helpers import get_or_create from webhook_to_fedora_messaging.main import create_app from os import environ from os.path import abspath, basename +from webhook_to_fedora_messaging.models.user import User +from webhook_to_fedora_messaging.models.service import Service @pytest.fixture(scope="session") @@ -19,3 +21,37 @@ def client(): with open(root.replace(basename(root), "tests/data/w2fm-test.db"), "rb") as srce: dest.write(srce.read()) return create_app().test_client() + + +@pytest.fixture(autouse=False, scope="function") +def create_user(client): + with client.application.app_context(): + # Setup code to create the object in the database + user, is_created = get_or_create(db.session, User, username="mehmet") # Adjust fields as necessary + db.session.commit() + + yield + + with client.application.app_context(): + # Teardown code to remove the object from the database + db.session.query(User).filter_by(username="mehmet").delete() + db.session.commit() + + +@pytest.fixture(autouse=False, scope="function") +def create_service(client): + with client.application.app_context(): + # Setup code to create the object in the database + user, created = get_or_create(db.session, User, username="mehmet") # Adjust fields as necessary + get_or_create(db.session, Service, name="Github Demo", type="Github", desc="description", user_id=user.id) + + db.session.commit() + + yield + + with client.application.app_context(): + # Teardown code to remove the object from the database + db.session.query(User).filter_by(username="mehmet").delete() + db.session.query(Service).filter_by(name="Github Demo").delete() + db.session.commit() + diff --git a/tests/test_service/__init__.py b/tests/test_service/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_service/test_service_create.py b/tests/test_service/test_service_create.py new file mode 100644 index 0000000..92a258d --- /dev/null +++ b/tests/test_service/test_service_create.py @@ -0,0 +1,59 @@ +import pytest + + +@pytest.mark.parametrize( + "data, code", + [ + pytest.param( + {"username": "mehmet", + "type": "Github", + "desc": "Github Demo", + "name": "My Github"}, + 201, + id="SERVICE Endpoint - 201 Created", + ), + pytest.param( + {"username": "baran", + "type": "Github", + "desc": "Github Demo", + "name": "My Github"}, + 404, + id="SERVICE Endpoint - 404 Not Found", + ), + pytest.param( + {"username": "mehmet", + "type": "Github", + "desc": "Github Demo"}, + 400, + id="SERVICE Endpoint - 400 Bad Request" + ), + pytest.param( + None, + 415, + id="SERVICE Endpoint - 415 Unsupported Media Type" + ) + ] +) +@pytest.mark.usefixtures("create_user") +def test_service_create(client, data, code): + response = client.post("/service/", json=data) + assert response.status_code == code + +@pytest.mark.parametrize( + "data, code", + [ + pytest.param( + {"username": "mehmet", + "type": "Github", + "desc": "Github Demo", + "name": "My Github"}, + 409, + id="SERVICE Endpoint - 409 Conflict", + ) + ] +) +@pytest.mark.usefixtures("create_user") +def test_service_conflict(client, data, code): + response = client.post("/service/", json=data) + response = client.post("/service/", json=data) + assert response.status_code == code diff --git a/tests/test_service/test_service_list.py b/tests/test_service/test_service_list.py new file mode 100644 index 0000000..473d694 --- /dev/null +++ b/tests/test_service/test_service_list.py @@ -0,0 +1,32 @@ +import pytest + + +@pytest.mark.parametrize( + "data, code", + [ + pytest.param( + {"username": "mehmet"}, + 200, + id="SERVICE Endpoint - 200 Success", + ), + pytest.param( + {"username": "baran"}, + 404, + id="SERVICE Endpoint - 404 Not Found", + ), + pytest.param( + {"user": "mehmet"}, + 400, + id="SERVICE Endpoint - 400 Bad Request" + ), + pytest.param( + None, + 415, + id="SERVICE Endpoint - 415 Unsupported Media Type" + ) + ] +) +@pytest.mark.usefixtures("create_user") +def test_service_list(client, data, code): + response = client.get("/service/search", json=data) + assert response.status_code == code diff --git a/tests/test_service/test_service_lookup.py b/tests/test_service/test_service_lookup.py new file mode 100644 index 0000000..3179ee1 --- /dev/null +++ b/tests/test_service/test_service_lookup.py @@ -0,0 +1,34 @@ +import pytest + + +@pytest.mark.parametrize( + "data, code", + [ + pytest.param( + {'service_uuid': 1}, + 200, + id="SERVICE Endpoint - 200 Success", + ), + pytest.param( + {"service_uuid": 2039}, + 404, + id="SERVICE Endpoint - 404 Not Found", + ), + pytest.param( + {"username": "mehmet", + "type": "Github", + "desc": "Github Demo"}, + 400, + id="SERVICE Endpoint - 400 Bad Request" + ), + pytest.param( + None, + 415, + id="SERVICE Endpoint - 415 Unsupported Media Type" + ) + ] +) +@pytest.mark.usefixtures("create_service") +def test_service_lookup(client, data, code): + response = client.get("/service/", json=data) + assert response.status_code == code diff --git a/tests/test_service/test_service_refresh.py b/tests/test_service/test_service_refresh.py new file mode 100644 index 0000000..2626349 --- /dev/null +++ b/tests/test_service/test_service_refresh.py @@ -0,0 +1,36 @@ +import pytest + + +@pytest.mark.parametrize( + "data, code", + [ + pytest.param( + {'service_uuid': 1, + 'username': 'mehmet'}, + 200, + id="SERVICE Endpoint - 200 Success", + ), + pytest.param( + {"service_uuid": 2039, + 'username': "baran"}, + 404, + id="SERVICE Endpoint - 404 Not Found", + ), + pytest.param( + {"username": "mehmet", + "type": "Github", + "desc": "Github Demo"}, + 400, + id="SERVICE Endpoint - 400 Bad Request" + ), + pytest.param( + None, + 415, + id="SERVICE Endpoint - 415 Unsupported Media Type" + ) + ] +) +@pytest.mark.usefixtures("create_service") +def test_service_refresh(client, data, code): + response = client.post("/service/token", json=data) + assert response.status_code == code diff --git a/tests/test_service/test_service_revoke.py b/tests/test_service/test_service_revoke.py new file mode 100644 index 0000000..f053602 --- /dev/null +++ b/tests/test_service/test_service_revoke.py @@ -0,0 +1,36 @@ +import pytest + + +@pytest.mark.parametrize( + "data, code", + [ + pytest.param( + {'service_uuid': 1, + 'username': 'mehmet'}, + 200, + id="SERVICE Endpoint - 200 Success", + ), + pytest.param( + {"service_uuid": 2039, + 'username': "baran"}, + 404, + id="SERVICE Endpoint - 404 Not Found", + ), + pytest.param( + {"username": "mehmet", + "type": "Github", + "desc": "Github Demo"}, + 400, + id="SERVICE Endpoint - 400 Bad Request" + ), + pytest.param( + None, + 415, + id="SERVICE Endpoint - 415 Unsupported Media Type" + ) + ] +) +@pytest.mark.usefixtures("create_service") +def test_service_revoke(client, data, code): + response = client.put("/service/revoke", json=data) + assert response.status_code == code diff --git a/tests/test_service/test_service_update.py b/tests/test_service/test_service_update.py new file mode 100644 index 0000000..bea1c5c --- /dev/null +++ b/tests/test_service/test_service_update.py @@ -0,0 +1,37 @@ +import pytest + + +@pytest.mark.parametrize( + "data, code", + [ + pytest.param( + {'service_uuid': 1, + 'username': 'mehmet', + 'name': 'new name'}, + 200, + id="SERVICE Endpoint - 200 Success", + ), + pytest.param( + {"service_uuid": 2039, + 'username': "baran"}, + 404, + id="SERVICE Endpoint - 404 Not Found", + ), + pytest.param( + {"username": "mehmet", + "type": "Github", + "desc": "Github Demo"}, + 400, + id="SERVICE Endpoint - 400 Bad Request" + ), + pytest.param( + None, + 415, + id="SERVICE Endpoint - 415 Unsupported Media Type" + ) + ] +) +@pytest.mark.usefixtures("create_service") +def test_service_update(client, data, code): + response = client.put("/service/", json=data) + assert response.status_code == code diff --git a/tests/test_user/test_user_get.py b/tests/test_user/test_user_get.py index eb1428a..fc1cf19 100644 --- a/tests/test_user/test_user_get.py +++ b/tests/test_user/test_user_get.py @@ -31,11 +31,3 @@ def test_user_get(client, data, code): response = client.get("/user/search", json=data) assert response.status_code == code - - -@pytest.fixture -def create_user(client): - data = {'username': 'mehmet'} - client.post("/user/", json=data) - print("created user") - diff --git a/tests/test_user/test_user_lookup.py b/tests/test_user/test_user_lookup.py index 6483f2a..cdf2cfa 100644 --- a/tests/test_user/test_user_lookup.py +++ b/tests/test_user/test_user_lookup.py @@ -27,14 +27,8 @@ ] ) + @pytest.mark.usefixtures("create_user") def test_user_lookup(client, data, code): response = client.get("/user/", json=data) assert response.status_code == code - - -@pytest.fixture -def create_user(client): - data = {'username': 'mehmet'} - client.post("/user/", json=data) -