From d049cf6d3d2881eceb9d6636ab93aae438656136 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Thu, 11 Mar 2021 21:50:54 +0100 Subject: [PATCH] ingredient test finished --- cookbook/tests/api/test_api_ingredient.py | 0 .../tests/pytest/api/test_api_ingredient.py | 106 ++++++++++++++++++ cookbook/tests/pytest/conftest.py | 46 +++++++- 3 files changed, 147 insertions(+), 5 deletions(-) delete mode 100644 cookbook/tests/api/test_api_ingredient.py create mode 100644 cookbook/tests/pytest/api/test_api_ingredient.py diff --git a/cookbook/tests/api/test_api_ingredient.py b/cookbook/tests/api/test_api_ingredient.py deleted file mode 100644 index e69de29b..00000000 diff --git a/cookbook/tests/pytest/api/test_api_ingredient.py b/cookbook/tests/pytest/api/test_api_ingredient.py new file mode 100644 index 00000000..2fbacbed --- /dev/null +++ b/cookbook/tests/pytest/api/test_api_ingredient.py @@ -0,0 +1,106 @@ +import json + +import pytest +from django.urls import reverse +from django_scopes import scopes_disabled + +from cookbook.models import Food, Ingredient + +LIST_URL = 'api:ingredient-list' +DETAIL_URL = 'api:ingredient-detail' + + +@pytest.mark.parametrize("arg", [ + ['a_u', 403], + ['g1_s1', 403], + ['u1_s1', 200], + ['a1_s1', 200], +]) +def test_list_permission(arg, request): + c = request.getfixturevalue(arg[0]) + assert c.get(reverse(LIST_URL)).status_code == arg[1] + + +def test_list_space(recipe_1_s1, u1_s1, u1_s2, space_2): + assert len(json.loads(u1_s1.get(reverse(LIST_URL)).content)) == 10 + assert len(json.loads(u1_s2.get(reverse(LIST_URL)).content)) == 0 + + with scopes_disabled(): + recipe_1_s1.space = space_2 + recipe_1_s1.save() + + assert len(json.loads(u1_s1.get(reverse(LIST_URL)).content)) == 0 + assert len(json.loads(u1_s2.get(reverse(LIST_URL)).content)) == 10 + + +@pytest.mark.parametrize("arg", [ + ['a_u', 403], + ['g1_s1', 403], + ['u1_s1', 200], + ['a1_s1', 200], + ['g1_s2', 403], + ['u1_s2', 404], + ['a1_s2', 404], +]) +def test_update(arg, request, recipe_1_s1): + with scopes_disabled(): + i = recipe_1_s1.steps.first().ingredients.first() + c = request.getfixturevalue(arg[0]) + r = c.patch( + reverse( + DETAIL_URL, + args={i.id} + ), + {'note': 'new'}, + content_type='application/json' + ) + response = json.loads(r.content) + assert r.status_code == arg[1] + if r.status_code == 200: + assert response['note'] == 'new' + + +@pytest.mark.parametrize("arg", [ + ['a_u', 403], + ['g1_s1', 403], + ['u1_s1', 201], + ['a1_s1', 201], +]) +def test_add(arg, request, u1_s2): + c = request.getfixturevalue(arg[0]) + r = c.post( + reverse(LIST_URL), + {'food': {'name': 'test'}, 'unit': {'name': 'test'}, 'amount': 1}, + content_type='application/json' + ) + response = json.loads(r.content) + print(r) + assert r.status_code == arg[1] + if r.status_code == 201: + assert response['id'] == 1 + r = c.get(reverse(DETAIL_URL, args={response['id']})) + assert r.status_code == 404 # ingredient is not linked to a recipe and therefore cannot be accessed + r = u1_s2.get(reverse(DETAIL_URL, args={response['id']})) + assert r.status_code == 404 + + +def test_delete(u1_s1, u1_s2, recipe_1_s1): + with scopes_disabled(): + i = recipe_1_s1.steps.first().ingredients.first() + r = u1_s2.delete( + reverse( + DETAIL_URL, + args={i.id} + ) + ) + assert r.status_code == 404 + + r = u1_s1.delete( + reverse( + DETAIL_URL, + args={i.id} + ) + ) + + assert r.status_code == 204 + assert not Ingredient.objects.filter(pk=i.id).exists() diff --git a/cookbook/tests/pytest/conftest.py b/cookbook/tests/pytest/conftest.py index 75f0bb38..c9ffc423 100644 --- a/cookbook/tests/pytest/conftest.py +++ b/cookbook/tests/pytest/conftest.py @@ -7,7 +7,7 @@ from django.contrib import auth from django.contrib.auth.models import User, Group from django_scopes import scopes_disabled -from cookbook.models import Space, Recipe +from cookbook.models import Space, Recipe, Step, Ingredient, Food, Unit # hack from https://github.com/raphaelm/django-scopes to disable scopes for all fixtures @@ -40,10 +40,9 @@ def space_2(): # ---------------------- OBJECT FIXTURES --------------------- -@pytest.fixture() -def recipe_1_s1(space_1, u1_s1): - return Recipe.objects.create( - name='recipe_1_s1', +def get_random_recipe(space_1, u1_s1): + r = Recipe.objects.create( + name=uuid.uuid4(), waiting_time=20, working_time=20, servings=4, @@ -51,6 +50,43 @@ def recipe_1_s1(space_1, u1_s1): space=space_1 ) + s1 = Step.objects.create(name=uuid.uuid4(), instruction=uuid.uuid4(), ) + s2 = Step.objects.create(name=uuid.uuid4(), instruction=uuid.uuid4(), ) + + r.steps.add(s1) + r.steps.add(s2) + + for x in range(5): + s1.ingredients.add( + Ingredient.objects.create( + amount=1, + food=Food.objects.create(name=uuid.uuid4(), space=space_1,), + unit=Unit.objects.create(name=uuid.uuid4(), space=space_1,), + note=uuid.uuid4(), + ) + ) + + s2.ingredients.add( + Ingredient.objects.create( + amount=1, + food=Food.objects.create(name=uuid.uuid4(), space=space_1,), + unit=Unit.objects.create(name=uuid.uuid4(), space=space_1,), + note=uuid.uuid4(), + ) + ) + + return r + + +@pytest.fixture +def recipe_1_s1(space_1, u1_s1): + return get_random_recipe(space_1, u1_s1) + + +@pytest.fixture +def recipe_2_s1(space_1, u1_s1): + return get_random_recipe(space_1, u1_s1) + # ---------------------- USER FIXTURES ----------------------- # maybe better with factories but this is very explict so ...