Skip to content

Commit

Permalink
Merge branch 'main' into healthcheck-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
reinout authored Apr 10, 2024
2 parents 6278ae3 + 0b6bace commit ab795a5
Show file tree
Hide file tree
Showing 7 changed files with 493 additions and 30 deletions.
8 changes: 7 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Changelog for BROStar API

## 0.4 (unreleased)
## 0.5 (unreleased)


- Nothing changed yet.


## 0.4 (2024-04-10)


- Hotfix: importtasks endpoint was accidentaly renamed to ImportTaskViewSet. Back to importtasks
- added tests for api views and serializers

## 0.3 (2024-04-09)


Expand Down
64 changes: 64 additions & 0 deletions api/tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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


@pytest.fixture
def organisation():
organisation = api_models.Organisation.objects.create(
name="Nieuwegein",
kvk_number="12345678",
bro_user_token="secret",
bro_user_password="secret",
)
return organisation


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


@pytest.fixture
def userprofile(user, organisation):
userprofile = api_models.UserProfile.objects.create(
user=user,
organisation=organisation,
)
return userprofile


@pytest.fixture
def gmn(organisation):
return gmn_models.GMN(
data_owner=organisation,
bro_id="GMN123456789",
)


@pytest.fixture
def gmw(organisation):
return gmw_models.GMW(
data_owner=organisation,
bro_id="GMW123456789",
)


@pytest.fixture
def upload_task_valid_input_data(organisation):
return {
"data_owner": organisation,
"bro_domain": "GMN",
"project_number": "1",
"registration_type": "GMN_StartRegistration",
"request_type": "registration",
"status": "PENDING",
"metadata": {},
"sourcedocument_data": {},
}
32 changes: 5 additions & 27 deletions api/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
import pytest

from api import models as api_models
from api.tests import fixtures
from gmn import models as gmn_models
from gmw import models as gmw_models


@pytest.fixture
def organisation():
organisation = api_models.Organisation.objects.create(
name="Nieuwegein",
kvk_number="12345678",
bro_user_token="secret",
bro_user_password="secret",
)
organisation.save()
return organisation


@pytest.fixture
def gmn(organisation):
return gmn_models.GMN(
data_owner=organisation,
bro_id="GMN123456789",
)


@pytest.fixture
def gmw(organisation):
return gmw_models.GMW(
data_owner=organisation,
bro_id="GMW123456789",
)
# this setup is chosen because ruff removes the fixture imports in other methods
organisation = fixtures.organisation
gmw = fixtures.gmw
gmn = fixtures.gmn


@pytest.mark.django_db
Expand Down
103 changes: 103 additions & 0 deletions api/tests/test_serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import pytest
from rest_framework.exceptions import ValidationError

from api import models as api_models
from api import serializers as api_serializers
from api.tests import fixtures

# this setup is chosen because ruff removes the fixture imports in other methods
organisation = fixtures.organisation


@pytest.mark.django_db
def test_valid_data_importtask_serialization(organisation):
data = {
"data_owner": organisation.uuid,
"kvk_number": organisation.kvk_number,
"bro_domain": "GMN",
"status": "PENDING",
}
serializer = api_serializers.ImportTaskSerializer(data=data)
assert serializer.is_valid(), serializer.errors
serialized_data = serializer.data
assert serialized_data["bro_domain"] == "GMN"


@pytest.mark.django_db
def test_valid_data_importtask_deserialization(organisation):
data = {
"data_owner": organisation,
"kvk_number": organisation.kvk_number,
"bro_domain": "GMN",
"status": "PENDING",
}
upload_task = api_models.ImportTask.objects.create(**data)
serializer = api_serializers.ImportTaskSerializer(instance=upload_task)
serialized_data = serializer.data
assert serialized_data["bro_domain"] == "GMN"


@pytest.mark.django_db
def test_invalid_data_importtask_validation(organisation):
data = {
"data_owner": organisation,
"kvk_number": organisation.kvk_number,
"bro_domain": "Non-existing-domain",
"status": "PENDING",
}
serializer = api_serializers.ImportTaskSerializer(data=data)
with pytest.raises(ValidationError):
serializer.is_valid(raise_exception=True)


@pytest.mark.django_db
def test_valid_data_uploadtask_serialization(organisation):
data = {
"data_owner": organisation.uuid,
"bro_domain": "GMN",
"project_number": "1",
"registration_type": "GMN_StartRegistration",
"request_type": "registration",
"status": "PENDING",
"metadata": {},
"sourcedocument_data": {},
}
serializer = api_serializers.UploadTaskSerializer(data=data)
assert serializer.is_valid(), serializer.errors
serialized_data = serializer.data
assert serialized_data["bro_domain"] == "GMN"


@pytest.mark.django_db
def test_valid_data_uploadtask_deserialization(organisation):
data = {
"data_owner": organisation,
"bro_domain": "GMN",
"project_number": "1",
"registration_type": "GMN_StartRegistration",
"request_type": "registration",
"status": "PENDING",
"metadata": {},
"sourcedocument_data": {},
}
upload_task = api_models.UploadTask.objects.create(**data)
serializer = api_serializers.UploadTaskSerializer(instance=upload_task)
serialized_data = serializer.data
assert serialized_data["bro_domain"] == "GMN"


@pytest.mark.django_db
def test_invalid_data_uploadtask_validation(organisation):
data = {
"data_owner": organisation,
"bro_domain": "Non-existing-domain",
"project_number": "1",
"registration_type": "GMN_StartRegistration",
"request_type": "registration",
"status": "PENDING",
"metadata": {},
"sourcedocument_data": {},
}
serializer = api_serializers.UploadTaskSerializer(data=data)
with pytest.raises(ValidationError):
serializer.is_valid(raise_exception=True)
Loading

0 comments on commit ab795a5

Please sign in to comment.