Skip to content
Snippets Groups Projects
Commit d4c6f053 authored by Tore.Brede's avatar Tore.Brede
Browse files

Merge branch 'GREG-5_additional_apis' into 'master'

GREG-5: Adding some basic endpoints for consents, sponsors, and organizational units

See merge request !14
parents 2b8dcaba 9596b664
No related branches found
No related tags found
1 merge request!14GREG-5: Adding some basic endpoints for consents, sponsors, and organizational units
Pipeline #88451 passed
from rest_framework.serializers import ModelSerializer
from greg.models import Consent
class ConsentSerializer(ModelSerializer):
class Meta:
model = Consent
fields = "__all__"
from rest_framework.serializers import ModelSerializer
from greg.models import OrganizationalUnit
class OrganizationalUnitSerializer(ModelSerializer):
class Meta:
model = OrganizationalUnit
fields = "__all__"
from rest_framework import serializers
from greg.models import Sponsor
class SponsorSerializer(serializers.ModelSerializer):
class Meta:
model = Sponsor
fields = ["id", "feide_id"]
......@@ -8,17 +8,23 @@ from drf_spectacular.views import (
SpectacularSwaggerView,
)
from greg.api.views.consent import ConsentViewSet
from greg.api.views.organizational_unit import OrganizationalUnitViewSet
from greg.api.views.person import (
PersonRoleViewSet,
PersonViewSet,
)
from greg.api.views.role import RoleViewSet
from greg.api.views.health import Health
from greg.api.views.sponsor import SponsorViewSet
router = DefaultRouter()
router.register(r"persons", PersonViewSet, basename="person")
router.register(r"roles", RoleViewSet, basename="role")
router.register(r"consents", ConsentViewSet, basename="consent")
router.register(r"sponsors", SponsorViewSet, basename="sponsor")
router.register(r"orgunit", OrganizationalUnitViewSet, basename="orgunit")
urlpatterns = router.urls
......
from rest_framework import viewsets
from greg.api.pagination import PrimaryKeyCursorPagination
from greg.api.serializers.consent import ConsentSerializer
from greg.models import Consent
class ConsentViewSet(viewsets.ModelViewSet):
"""Consent API"""
queryset = Consent.objects.all().order_by("id")
serializer_class = ConsentSerializer
pagination_class = PrimaryKeyCursorPagination
lookup_field = "id"
from rest_framework import viewsets
from greg.api.pagination import PrimaryKeyCursorPagination
from greg.api.serializers.organizational_unit import OrganizationalUnitSerializer
from greg.models import OrganizationalUnit
class OrganizationalUnitViewSet(viewsets.ModelViewSet):
"""OrganizationalUnit API"""
queryset = OrganizationalUnit.objects.all().order_by("id")
serializer_class = OrganizationalUnitSerializer
pagination_class = PrimaryKeyCursorPagination
lookup_field = "id"
from rest_framework.viewsets import ReadOnlyModelViewSet
from greg.api.pagination import PrimaryKeyCursorPagination
from greg.api.serializers.sponsor import SponsorSerializer
from greg.models import Sponsor
class SponsorViewSet(ReadOnlyModelViewSet):
"""Sponsor API"""
queryset = Sponsor.objects.all().order_by("id")
serializer_class = SponsorSerializer
pagination_class = PrimaryKeyCursorPagination
lookup_field = "id"
import pytest
from rest_framework import status
from rest_framework.reverse import reverse
from greg.models import Consent
@pytest.fixture
def consent_foo() -> Consent:
return Consent.objects.create(
type="test_consent",
consent_name_en="Test1",
consent_name_nb="Test2",
consent_description_en="Test description",
consent_description_nb="Test beskrivelse",
consent_link_en="https://example.org",
consent_link_nb="https://example.org",
valid_from="2018-01-20",
user_allowed_to_change=True,
)
@pytest.mark.django_db
def test_get_consent(client, consent_foo):
resp = client.get(reverse("consent-detail", kwargs={"id": consent_foo.id}))
assert resp.status_code == status.HTTP_200_OK
data = resp.json()
assert data.get("id") == consent_foo.id
assert data.get("type") == consent_foo.type
assert data.get("consent_name_en") == consent_foo.consent_name_en
import pytest
from django.contrib.auth import get_user_model
from rest_framework import status
from rest_framework.authtoken.models import Token
from rest_framework.reverse import reverse
from rest_framework.status import HTTP_200_OK
from rest_framework.test import APIClient
from greg.models import Person
@pytest.fixture
def client() -> APIClient:
user, _ = get_user_model().objects.get_or_create(username="test")
token, _ = Token.objects.get_or_create(user=user)
client = APIClient()
client.credentials(HTTP_AUTHORIZATION=f"Token {token.key}")
return client
@pytest.fixture
def person_foo() -> Person:
return Person.objects.create(
......
import logging
from rest_framework.authtoken.models import Token
from rest_framework.test import APIClient
from django.contrib.auth import get_user_model
# faker spams the logs with localisation warnings
# see https://github.com/joke2k/faker/issues/753
......@@ -19,3 +23,12 @@ def setup_db_test_data(django_db_setup, django_db_blocker):
with django_db_blocker.unblock():
database_seeder = DatabasePopulation()
database_seeder.populate_database()
@pytest.fixture
def client() -> APIClient:
user, _ = get_user_model().objects.get_or_create(username="test")
token, _ = Token.objects.get_or_create(user=user)
client = APIClient()
client.credentials(HTTP_AUTHORIZATION=f"Token {token.key}")
return client
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment