Skip to content

Commit

Permalink
precommit
Browse files Browse the repository at this point in the history
  • Loading branch information
JJFlorian committed Apr 10, 2024
1 parent 6cecad9 commit d737bea
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 39 deletions.
9 changes: 4 additions & 5 deletions api/tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import pytest

from django.contrib.auth.models import User


from api import models as api_models
from gmn import models as gmn_models
from gmw import models as gmw_models
Expand All @@ -18,15 +16,15 @@ def organisation():
)
return organisation


@pytest.fixture
def user():
user = User.objects.create_user(
username='test_user',
email='[email protected]',
password='test_password'
username="test_user", email="[email protected]", password="test_password"
)
return user


@pytest.fixture
def userprofile(user, organisation):
userprofile = api_models.UserProfile.objects.create(
Expand All @@ -35,6 +33,7 @@ def userprofile(user, organisation):
)
return userprofile


@pytest.fixture
def gmn(organisation):
return gmn_models.GMN(
Expand Down
85 changes: 51 additions & 34 deletions api/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from rest_framework import status
from unittest.mock import patch

import pytest
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient
import pytest
from api.tests import fixtures

from api import models as api_models
from unittest.mock import patch
from api.tests import fixtures

user = fixtures.user
organisation = fixtures.organisation # imported, even though not used in this file, because required for userprofile fixture
organisation = fixtures.organisation # imported, even though not used in this file, because required for userprofile fixture
userprofile = fixtures.userprofile


Expand All @@ -24,6 +26,7 @@ def test_host_redirect_view(api_client):

assert r.status_code == status.HTTP_302_FOUND


@pytest.mark.django_db
def test_user_view_set_list(api_client, user):
"""Test the UserViewSet listview"""
Expand All @@ -34,6 +37,7 @@ def test_user_view_set_list(api_client, user):

assert response.status_code == status.HTTP_200_OK


@pytest.mark.django_db
def test_user_view_set_logged_in(api_client, user, userprofile):
"""Test the logged_in action of UserViewSet with a logged in user"""
Expand All @@ -59,6 +63,7 @@ def test_user_view_set_logged_in(api_client, user, userprofile):
}
assert response.data == expected_data


@pytest.mark.django_db
def test_user_view_set_not_logged_in(api_client):
"""Test the logged_in action of UserViewSet with no logged in user"""
Expand All @@ -81,6 +86,7 @@ def test_user_view_set_not_logged_in(api_client):
}
assert response.data == expected_data


@pytest.mark.django_db
def test_importtask_view_set_list_not_logged_in(api_client):
"""Test the importtask list endpoint"""
Expand All @@ -89,6 +95,7 @@ def test_importtask_view_set_list_not_logged_in(api_client):

assert response.status_code == status.HTTP_403_FORBIDDEN


@pytest.mark.django_db
def test_importtask_view_set_list_logged_in(api_client, user, userprofile):
"""Test the importtask list endpoint
Expand All @@ -100,6 +107,7 @@ def test_importtask_view_set_list_logged_in(api_client, user, userprofile):

assert response.status_code == status.HTTP_200_OK


@pytest.mark.django_db
def test_importtask_view_post_valid_data(api_client, user, userprofile, organisation):
"""Test posting on the importtask enpoint
Expand All @@ -111,13 +119,14 @@ def test_importtask_view_post_valid_data(api_client, user, userprofile, organisa
data = {
"bro_domain": "GMN",
"kvk_number": organisation.kvk_number,
"data_owner": organisation.uuid
"data_owner": organisation.uuid,
}

response = api_client.post(url, data, format='json')
response = api_client.post(url, data, format="json")

assert response.status_code == status.HTTP_201_CREATED


@pytest.mark.django_db
def test_importtask_view_post_invalid_data(api_client, user, userprofile, organisation):
"""Test posting on the importtask enpoint
Expand All @@ -129,10 +138,10 @@ def test_importtask_view_post_invalid_data(api_client, user, userprofile, organi
data = {
"bro_domain": "non-existing",
"kvk_number": organisation.kvk_number,
"data_owner": organisation.uuid
"data_owner": organisation.uuid,
}

response = api_client.post(url, data, format='json')
response = api_client.post(url, data, format="json")

assert response.status_code == status.HTTP_400_BAD_REQUEST

Expand All @@ -145,6 +154,7 @@ def test_uploadtask_view_set_list_not_logged_in(api_client):

assert response.status_code == status.HTTP_403_FORBIDDEN


@pytest.mark.django_db
def test_uploadtask_view_set_list_logged_in(api_client, user, userprofile):
"""Test the uploadtask list endpoint
Expand All @@ -156,6 +166,7 @@ def test_uploadtask_view_set_list_logged_in(api_client, user, userprofile):

assert response.status_code == status.HTTP_200_OK


@pytest.mark.django_db
def test_uploadtask_view_post_valid_data(api_client, user, userprofile, organisation):
"""Test posting on the uploadtask enpoint
Expand All @@ -171,13 +182,14 @@ def test_uploadtask_view_post_valid_data(api_client, user, userprofile, organisa
"request_type": "registration",
"metadata": {},
"sourcedocument_data": {},
"data_owner": organisation.uuid
"data_owner": organisation.uuid,
}

response = api_client.post(url, data, format='json')
response = api_client.post(url, data, format="json")

assert response.status_code == status.HTTP_201_CREATED


@pytest.mark.django_db
def test_uploadtask_view_post_invalid_data(api_client, user, userprofile, organisation):
"""Test posting on the uploadtasks enpoint
Expand All @@ -193,33 +205,38 @@ def test_uploadtask_view_post_invalid_data(api_client, user, userprofile, organi
"request_type": "non-existing",
"metadata": {},
"sourcedocument_data": {},
"data_owner": organisation.uuid
"data_owner": organisation.uuid,
}

response = api_client.post(url, data, format='json')
response = api_client.post(url, data, format="json")

assert response.status_code == status.HTTP_400_BAD_REQUEST


@pytest.mark.django_db
@patch('api.bro_upload.utils.check_delivery_status')
def test_uploadtask_check_status(mock_check_delivery_status, api_client, user, userprofile, organisation):
@patch("api.bro_upload.utils.check_delivery_status")
def test_uploadtask_check_status(
mock_check_delivery_status, api_client, user, userprofile, organisation
):
"""Test post on uploadtasks/check-status endpoint
Note: userprofile needs to be used as fixture for this test
"""
api_client.force_authenticate(user=user)

upload_task_instance = api_models.UploadTask.objects.create(
bro_domain = "GMN",
project_number = "1",
registration_type = "GMN_StartRegistration",
request_type = "registration",
metadata = {},
sourcedocument_data = {},
data_owner = organisation,
status="PENDING"
bro_domain="GMN",
project_number="1",
registration_type="GMN_StartRegistration",
request_type="registration",
metadata={},
sourcedocument_data={},
data_owner=organisation,
status="PENDING",
)

url = reverse("api:uploadtask-check-status", kwargs={'uuid': upload_task_instance.uuid})
url = reverse(
"api:uploadtask-check-status", kwargs={"uuid": upload_task_instance.uuid}
)

# Check 201 response for status = PENDING
response = api_client.post(url)
Expand Down Expand Up @@ -253,12 +270,12 @@ def test_uploadtask_check_status(mock_check_delivery_status, api_client, user, u
mock_check_delivery_status.return_value = {
"brondocuments": [
{
"errors": ["ERROR!!!"],
"errors": ["ERROR!!!"],
"status": "OPGENOMEN_LVBRO",
"broId":"GMN1234",
"broId": "GMN1234",
}
],
"status": "DOORGELEVERD"
"status": "DOORGELEVERD",
}

response = api_client.post(url)
Expand All @@ -268,12 +285,12 @@ def test_uploadtask_check_status(mock_check_delivery_status, api_client, user, u
mock_check_delivery_status.return_value = {
"brondocuments": [
{
"errors": [],
"errors": [],
"status": "OPGENOMEN_LVBRO",
"broId":"GMN1234",
"broId": "GMN1234",
}
],
"status": "DOORGELEVERD"
"status": "DOORGELEVERD",
}

response = api_client.post(url)
Expand All @@ -283,13 +300,13 @@ def test_uploadtask_check_status(mock_check_delivery_status, api_client, user, u
mock_check_delivery_status.return_value = {
"brondocuments": [
{
"errors": [],
"errors": [],
"status": "NOTFINISHEDYET",
"broId":"GMN1234",
"broId": "GMN1234",
}
],
"status": "DOORGELEVERD"
"status": "DOORGELEVERD",
}

response = api_client.post(url)
assert response.status_code == status.HTTP_304_NOT_MODIFIED
assert response.status_code == status.HTTP_304_NOT_MODIFIED

0 comments on commit d737bea

Please sign in to comment.