Skip to content

Commit

Permalink
Merge pull request #23 from maykinmedia/feature/18-authentication
Browse files Browse the repository at this point in the history
Feature/18 authentication
  • Loading branch information
Floris272 authored Sep 19, 2024
2 parents e5a15e4 + 499ad8c commit eb32e9a
Show file tree
Hide file tree
Showing 14 changed files with 306 additions and 285 deletions.
8 changes: 7 additions & 1 deletion src/open_producten/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# 'django.contrib.sitemaps',
# External applications.
# Project applications.
"rest_framework.authtoken",
"localflavor",
"treebeard",
"open_producten.accounts",
Expand Down Expand Up @@ -56,8 +57,13 @@
#
# Django rest framework
#

REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework.authentication.TokenAuthentication",
],
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework.permissions.IsAuthenticated",
],
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
"DEFAULT_PARSER_CLASSES": [
"rest_framework.parsers.JSONParser",
Expand Down
36 changes: 21 additions & 15 deletions src/open_producten/products/tests/api/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from freezegun import freeze_time
from rest_framework.exceptions import ErrorDetail
from rest_framework.test import APIClient

from open_producten.products.models import Data, Product
from open_producten.products.tests.factories import DataFactory, ProductFactory
Expand Down Expand Up @@ -47,6 +48,7 @@ def product_to_dict(product):
class TestProduct(BaseApiTestCase):

def setUp(self):
super().setUp()
self.product_type = ProductTypeFactory.create()
self.data = {
"product_type_id": self.product_type.id,
Expand All @@ -57,7 +59,11 @@ def setUp(self):
}
self.path = "/api/v1/products/"

def create_product(self):
def test_read_product_without_credentials_returns_error(self):
response = APIClient().get(self.path)
self.assertEqual(response.status_code, 401)

def _create_product(self):
return ProductFactory.create(bsn="111222333")

def test_create_product(self):
Expand Down Expand Up @@ -212,7 +218,7 @@ def test_create_product_with_data_for_field_not_part_of_product_type_returns_err
self.assertEqual(Field.objects.count(), 1)

def test_update_product(self):
product = self.create_product()
product = self._create_product()

data = self.data | {"end_date": datetime.date(2025, 12, 31)}
response = self.put(product.id, data)
Expand All @@ -221,7 +227,7 @@ def test_update_product(self):
self.assertEqual(Product.objects.count(), 1)

def test_update_product_without_bsn_or_kvk(self):
product = self.create_product()
product = self._create_product()

data = self.data.copy()
data.pop("bsn")
Expand All @@ -244,7 +250,7 @@ def test_update_product_data(self):
field = FieldFactory.create(
product_type=self.product_type, type=FieldTypes.TEXTFIELD, is_required=True
)
product = self.create_product()
product = self._create_product()
data_instance = DataFactory.create(product=product, field=field, value="abc")

data = self.data | {"data": [{"id": data_instance.id, "value": "123"}]}
Expand All @@ -261,7 +267,7 @@ def test_update_product_with_duplicate_data_ids_returns_error(self):
field = FieldFactory.create(
product_type=self.product_type, type=FieldTypes.TEXTFIELD, is_required=True
)
product = self.create_product()
product = self._create_product()
data_instance = DataFactory.create(product=product, field=field, value="abc")

data = self.data | {
Expand Down Expand Up @@ -292,9 +298,9 @@ def test_update_product_with_data_not_part_of_product_returns_error(
field = FieldFactory.create(
product_type=self.product_type, type=FieldTypes.TEXTFIELD, is_required=True
)
product = self.create_product()
product = self._create_product()
data_instance = DataFactory.create(
product=self.create_product(), field=field, value="abc"
product=self._create_product(), field=field, value="abc"
)

data = self.data | {
Expand Down Expand Up @@ -323,7 +329,7 @@ def test_update_product_with_no_existing_data_object_returns_error(
FieldFactory.create(
product_type=self.product_type, type=FieldTypes.TEXTFIELD, is_required=True
)
product = self.create_product()
product = self._create_product()

dummy_id = uuid.uuid4()

Expand Down Expand Up @@ -351,7 +357,7 @@ def test_update_product_with_invalid_data_returns_error(self):
field = FieldFactory.create(
product_type=self.product_type, type=FieldTypes.NUMBER, is_required=True
)
product = self.create_product()
product = self._create_product()
data_instance = DataFactory.create(product=product, field=field, value="123")

data = self.data | {
Expand All @@ -375,7 +381,7 @@ def test_update_product_with_invalid_data_returns_error(self):
)

def test_partial_update_product(self):
product = self.create_product()
product = self._create_product()

data = {"end_date": datetime.date(2025, 12, 31)}
response = self.patch(product.id, data)
Expand All @@ -387,7 +393,7 @@ def test_partial_update_product_data(self):
field = FieldFactory.create(
product_type=self.product_type, type=FieldTypes.TEXTFIELD, is_required=True
)
product = self.create_product()
product = self._create_product()
data_instance = DataFactory.create(product=product, field=field, value="abc")

data = {"data": [{"id": data_instance.id, "value": "123"}]}
Expand All @@ -401,23 +407,23 @@ def test_partial_update_product_data(self):
self.assertEqual(data_instance.value, "123")

def test_read_products(self):
product = self.create_product()
product = self._create_product()

response = self.get()

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [product_to_dict(product)])

def test_read_product(self):
product = self.create_product()
product = self._create_product()

response = self.get(product.id)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, product_to_dict(product))

def test_read_product_with_data(self):
product = self.create_product()
product = self._create_product()
field = FieldFactory.create(
product_type=self.product_type, is_required=True, type="textfield"
)
Expand All @@ -427,7 +433,7 @@ def test_read_product_with_data(self):
self.assertEqual(response.data, product_to_dict(product))

def test_delete_product(self):
product = self.create_product()
product = self._create_product()
response = self.delete(product.id)

self.assertEqual(response.status_code, 204)
Expand Down
6 changes: 6 additions & 0 deletions src/open_producten/producttypes/tests/api/test_category.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.forms import model_to_dict

from rest_framework.exceptions import ErrorDetail
from rest_framework.test import APIClient

from open_producten.producttypes.models import Category, Link
from open_producten.producttypes.tests.factories import (
Expand Down Expand Up @@ -35,12 +36,17 @@ def category_to_dict(category):
class TestCategoryViewSet(BaseApiTestCase):

def setUp(self):
super().setUp()
self.data = {
"name": "test-category",
"parent_category": None,
}
self.path = "/api/v1/categories/"

def test_read_category_without_credentials_returns_error(self):
response = APIClient().get(self.path)
self.assertEqual(response.status_code, 401)

def test_create_minimal_category(self):
response = self.post(self.data)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.forms import model_to_dict

from rest_framework.test import APIClient

from open_producten.producttypes.models import Category, Question
from open_producten.producttypes.tests.factories import CategoryFactory, QuestionFactory
from open_producten.utils.tests.cases import BaseApiTestCase
Expand All @@ -14,13 +16,18 @@ def question_to_dict(question):
class TestCategoryQuestion(BaseApiTestCase):

def setUp(self):
super().setUp()
self.category = CategoryFactory.create()
self.data = {"question": "18?", "answer": "eligible"}
self.path = f"/api/v1/categories/{self.category.id}/questions/"

def create_question(self):
def _create_question(self):
return QuestionFactory.create(category=self.category)

def test_read_question_without_credentials_returns_error(self):
response = APIClient().get(self.path)
self.assertEqual(response.status_code, 401)

def test_create_question(self):
response = self.post(self.data)

Expand All @@ -29,7 +36,7 @@ def test_create_question(self):
self.assertEqual(self.category.questions.first().question, "18?")

def test_update_question(self):
question = self.create_question()
question = self._create_question()

data = self.data | {"question": "21?"}
response = self.put(question.id, data)
Expand All @@ -39,7 +46,7 @@ def test_update_question(self):
self.assertEqual(Category.objects.first().questions.first().question, "21?")

def test_partial_update_question(self):
question = self.create_question()
question = self._create_question()

data = {"question": "21?"}
response = self.patch(question.id, data)
Expand All @@ -49,23 +56,23 @@ def test_partial_update_question(self):
self.assertEqual(Category.objects.first().questions.first().question, "21?")

def test_read_questions(self):
question = self.create_question()
question = self._create_question()

response = self.get()

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [question_to_dict(question)])

def test_read_question(self):
question = self.create_question()
question = self._create_question()

response = self.get(question.id)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, question_to_dict(question))

def test_delete_question(self):
question = self.create_question()
question = self._create_question()
response = self.delete(question.id)

self.assertEqual(response.status_code, 204)
Expand Down
18 changes: 11 additions & 7 deletions src/open_producten/producttypes/tests/api/test_condition.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from rest_framework.test import APIClient

from open_producten.producttypes.models import Condition
from open_producten.utils.tests.cases import BaseApiTestCase
from open_producten.utils.tests.helpers import model_to_dict_with_id
Expand All @@ -12,6 +14,7 @@ def condition_to_dict(condition):
class TestProductTypeCondition(BaseApiTestCase):

def setUp(self):
super().setUp()
self.data = {
"name": "test condition",
"question": "?",
Expand All @@ -20,8 +23,9 @@ def setUp(self):
}
self.path = "/api/v1/conditions/"

def create_condition(self):
return ConditionFactory.create()
def test_read_condition_without_credentials_returns_error(self):
response = APIClient().get(self.path)
self.assertEqual(response.status_code, 401)

def test_create_condition(self):
response = self.post(self.data)
Expand All @@ -31,7 +35,7 @@ def test_create_condition(self):
self.assertEqual(Condition.objects.first().name, "test condition")

def test_update_condition(self):
condition = self.create_condition()
condition = ConditionFactory.create()

data = self.data | {"name": "updated"}
response = self.put(condition.id, data)
Expand All @@ -41,7 +45,7 @@ def test_update_condition(self):
self.assertEqual(Condition.objects.first().name, "updated")

def test_partial_update_condition(self):
condition = self.create_condition()
condition = ConditionFactory.create()

data = {"name": "updated"}
response = self.patch(condition.id, data)
Expand All @@ -51,23 +55,23 @@ def test_partial_update_condition(self):
self.assertEqual(Condition.objects.first().name, "updated")

def test_read_conditions(self):
condition = self.create_condition()
condition = ConditionFactory.create()

response = self.get()

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [condition_to_dict(condition)])

def test_read_condition(self):
condition = self.create_condition()
condition = ConditionFactory.create()

response = self.get(condition.id)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, condition_to_dict(condition))

def test_delete_condition(self):
condition = self.create_condition()
condition = ConditionFactory.create()
response = self.delete(condition.id)

self.assertEqual(response.status_code, 204)
Expand Down
Loading

0 comments on commit eb32e9a

Please sign in to comment.