Skip to content

Commit

Permalink
Add tests for service endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Mehmet Baran Geylani <[email protected]>
  • Loading branch information
brngylni authored and gridhead committed Jul 22, 2024
1 parent 65b1113 commit 9ade961
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 16 deletions.
38 changes: 37 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()

Empty file added tests/test_service/__init__.py
Empty file.
59 changes: 59 additions & 0 deletions tests/test_service/test_service_create.py
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions tests/test_service/test_service_list.py
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions tests/test_service/test_service_lookup.py
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions tests/test_service/test_service_refresh.py
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions tests/test_service/test_service_revoke.py
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions tests/test_service/test_service_update.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 0 additions & 8 deletions tests/test_user/test_user_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

8 changes: 1 addition & 7 deletions tests/test_user/test_user_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 9ade961

Please sign in to comment.