ingredient test finished
This commit is contained in:
parent
fdcdf6a026
commit
d049cf6d3d
106
cookbook/tests/pytest/api/test_api_ingredient.py
Normal file
106
cookbook/tests/pytest/api/test_api_ingredient.py
Normal file
@ -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()
|
@ -7,7 +7,7 @@ from django.contrib import auth
|
|||||||
from django.contrib.auth.models import User, Group
|
from django.contrib.auth.models import User, Group
|
||||||
from django_scopes import scopes_disabled
|
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
|
# hack from https://github.com/raphaelm/django-scopes to disable scopes for all fixtures
|
||||||
@ -40,10 +40,9 @@ def space_2():
|
|||||||
|
|
||||||
# ---------------------- OBJECT FIXTURES ---------------------
|
# ---------------------- OBJECT FIXTURES ---------------------
|
||||||
|
|
||||||
@pytest.fixture()
|
def get_random_recipe(space_1, u1_s1):
|
||||||
def recipe_1_s1(space_1, u1_s1):
|
r = Recipe.objects.create(
|
||||||
return Recipe.objects.create(
|
name=uuid.uuid4(),
|
||||||
name='recipe_1_s1',
|
|
||||||
waiting_time=20,
|
waiting_time=20,
|
||||||
working_time=20,
|
working_time=20,
|
||||||
servings=4,
|
servings=4,
|
||||||
@ -51,6 +50,43 @@ def recipe_1_s1(space_1, u1_s1):
|
|||||||
space=space_1
|
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 -----------------------
|
# ---------------------- USER FIXTURES -----------------------
|
||||||
# maybe better with factories but this is very explict so ...
|
# maybe better with factories but this is very explict so ...
|
||||||
|
Loading…
Reference in New Issue
Block a user