new keyword api test working
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
from django.test import utils
|
from django.test import utils
|
||||||
from django_scopes import scopes_disabled
|
from django_scopes import scopes_disabled
|
||||||
|
|
||||||
# disables scoping error in all queries used inside the test functions
|
# disables scoping error in all queries used inside the test FUNCTIONS
|
||||||
# fixtures need to have their own scopes_disabled
|
# FIXTURES need to have their own scopes_disabled!!
|
||||||
|
# This is done by hook pytest_fixture_setup in conftest.py for all non yield fixtures
|
||||||
utils.setup_databases = scopes_disabled()(utils.setup_databases)
|
utils.setup_databases = scopes_disabled()(utils.setup_databases)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from django.test import utils
|
from django.test import utils
|
||||||
from django_scopes import scopes_disabled
|
from django_scopes import scopes_disabled
|
||||||
|
|
||||||
# disables scoping error in all queries used inside the test functions
|
# disables scoping error in all queries used inside the test FUNCTIONS
|
||||||
# fixtures need to have their own scopes_disabled
|
# FIXTURES need to have their own scopes_disabled!!
|
||||||
|
# This is done by hook pytest_fixture_setup in conftest.py for all non yield fixtures
|
||||||
utils.setup_databases = scopes_disabled()(utils.setup_databases)
|
utils.setup_databases = scopes_disabled()(utils.setup_databases)
|
||||||
|
@ -9,13 +9,13 @@ from django.urls import reverse
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def keyword_1():
|
def obj_1(space_1):
|
||||||
return Keyword.objects.get_or_create(name='test_1')[0]
|
return Keyword.objects.get_or_create(name='test_1', space=space_1)[0]
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def keyword_2():
|
def obj_2(space_1):
|
||||||
return Keyword.objects.get_or_create(name='test_2')[0]
|
return Keyword.objects.get_or_create(name='test_2', space=space_1)[0]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("arg", [
|
@pytest.mark.parametrize("arg", [
|
||||||
@ -29,66 +29,108 @@ def test_list_permission(arg, request):
|
|||||||
assert c.get(reverse('api:keyword-list')).status_code == arg[1]
|
assert c.get(reverse('api:keyword-list')).status_code == arg[1]
|
||||||
|
|
||||||
|
|
||||||
def test_list_filter(keyword_1, keyword_2, u1_s1):
|
def test_list_filter(obj_1, obj_2, u1_s1):
|
||||||
# verify storage is returned
|
# verify storage is returned
|
||||||
r = u1_s1.get(reverse('api:keyword-list'))
|
r = u1_s1.get(reverse('api:keyword-list'))
|
||||||
assert r.status_code == 200
|
assert r.status_code == 200
|
||||||
response = json.loads(r.content)
|
response = json.loads(r.content)
|
||||||
assert len(response) == 2
|
assert len(response) == 2
|
||||||
assert response[0]['name'] == keyword_1.name
|
assert response[0]['name'] == obj_1.name
|
||||||
|
|
||||||
response = json.loads(u1_s1.get(f'{reverse("api:keyword-list")}?limit=1'))
|
response = json.loads(u1_s1.get(f'{reverse("api:keyword-list")}?limit=1').content)
|
||||||
assert len(response) == 1
|
assert len(response) == 1
|
||||||
|
|
||||||
response = json.loads(u1_s1.get(f'{reverse("api:keyword-list")}?query=chicken'))
|
response = json.loads(u1_s1.get(f'{reverse("api:keyword-list")}?query=chicken').content)
|
||||||
assert len(response) == 0
|
assert len(response) == 0
|
||||||
|
|
||||||
response = json.loads(u1_s1.get(f'{reverse("api:keyword-list")}?query={keyword_1.name[4:]}'))
|
response = json.loads(u1_s1.get(f'{reverse("api:keyword-list")}?query={obj_1.name[4:]}').content)
|
||||||
assert len(response) == 1
|
assert len(response) == 1
|
||||||
|
|
||||||
#
|
|
||||||
# def test_keyword_update(self):
|
@pytest.mark.parametrize("arg", [
|
||||||
# r = self.user_client_1.patch(
|
['a_u', 403],
|
||||||
# reverse(
|
['g1_s1', 403],
|
||||||
# 'api:keyword-detail',
|
['u1_s1', 200],
|
||||||
# args={self.keyword_1.id}
|
['a1_s1', 200],
|
||||||
# ),
|
['g1_s2', 403],
|
||||||
# {'name': 'new'},
|
['u1_s2', 404],
|
||||||
# content_type='application/json'
|
['a1_s2', 404],
|
||||||
# )
|
])
|
||||||
# response = json.loads(r.content)
|
def test_update(arg, request, obj_1):
|
||||||
# self.assertEqual(r.status_code, 200)
|
c = request.getfixturevalue(arg[0])
|
||||||
# self.assertEqual(response['name'], 'new')
|
r = c.patch(
|
||||||
#
|
reverse(
|
||||||
#
|
'api:keyword-detail',
|
||||||
# def test_keyword_add(self):
|
args={obj_1.id}
|
||||||
# r = self.user_client_1.post(
|
),
|
||||||
# reverse('api:keyword-list'),
|
{'name': 'new'},
|
||||||
# {'name': 'test'},
|
content_type='application/json'
|
||||||
# content_type='application/json'
|
)
|
||||||
# )
|
response = json.loads(r.content)
|
||||||
# response = json.loads(r.content)
|
assert r.status_code == arg[1]
|
||||||
# self.assertEqual(r.status_code, 201)
|
if r.status_code == 200:
|
||||||
# self.assertEqual(response['name'], 'test')
|
assert response['name'] == 'new'
|
||||||
#
|
|
||||||
#
|
|
||||||
# def test_keyword_add_duplicate(self):
|
@pytest.mark.parametrize("arg", [
|
||||||
# r = self.user_client_1.post(
|
['a_u', 403],
|
||||||
# reverse('api:keyword-list'),
|
['g1_s1', 403],
|
||||||
# {'name': self.keyword_1.name},
|
['u1_s1', 201],
|
||||||
# content_type='application/json'
|
['a1_s1', 201],
|
||||||
# )
|
])
|
||||||
# response = json.loads(r.content)
|
def test_keyword_add(arg, request, u1_s2):
|
||||||
# self.assertEqual(r.status_code, 201)
|
c = request.getfixturevalue(arg[0])
|
||||||
# self.assertEqual(response['name'], self.keyword_1.name)
|
r = c.post(
|
||||||
#
|
reverse('api:keyword-list'),
|
||||||
#
|
{'name': 'test'},
|
||||||
# def test_keyword_delete(self):
|
content_type='application/json'
|
||||||
# r = self.user_client_1.delete(
|
)
|
||||||
# reverse(
|
response = json.loads(r.content)
|
||||||
# 'api:keyword-detail',
|
assert r.status_code == arg[1]
|
||||||
# args={self.keyword_1.id}
|
if r.status_code == 201:
|
||||||
# )
|
assert response['name'] == 'test'
|
||||||
# )
|
r = c.get(reverse('api:keyword-detail', args={response['id']}))
|
||||||
# self.assertEqual(r.status_code, 204)
|
assert r.status_code == 200
|
||||||
# self.assertEqual(Keyword.objects.count(), 1)
|
r = u1_s2.get(reverse('api:keyword-detail', args={response['id']}))
|
||||||
|
assert r.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_duplicate(u1_s1, u1_s2, obj_1):
|
||||||
|
r = u1_s1.post(
|
||||||
|
reverse('api:keyword-list'),
|
||||||
|
{'name': obj_1.name},
|
||||||
|
content_type='application/json'
|
||||||
|
)
|
||||||
|
response = json.loads(r.content)
|
||||||
|
assert r.status_code == 201
|
||||||
|
assert response['id'] == obj_1.id
|
||||||
|
|
||||||
|
r = u1_s2.post(
|
||||||
|
reverse('api:keyword-list'),
|
||||||
|
{'name': obj_1.name},
|
||||||
|
content_type='application/json'
|
||||||
|
)
|
||||||
|
response = json.loads(r.content)
|
||||||
|
assert r.status_code == 201
|
||||||
|
assert response['id'] != obj_1.id
|
||||||
|
|
||||||
|
|
||||||
|
def test_keyword_delete(u1_s1, u1_s2, obj_1):
|
||||||
|
r = u1_s2.delete(
|
||||||
|
reverse(
|
||||||
|
'api:keyword-detail',
|
||||||
|
args={obj_1.id}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert r.status_code == 404
|
||||||
|
|
||||||
|
r = u1_s1.delete(
|
||||||
|
reverse(
|
||||||
|
'api:keyword-detail',
|
||||||
|
args={obj_1.id}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
assert r.status_code == 204
|
||||||
|
with scopes_disabled():
|
||||||
|
assert Keyword.objects.count() == 0
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import copy
|
import copy
|
||||||
|
import inspect
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@ -8,6 +9,17 @@ from django_scopes import scopes_disabled
|
|||||||
from cookbook.models import Space
|
from cookbook.models import Space
|
||||||
|
|
||||||
|
|
||||||
|
# hack from https://github.com/raphaelm/django-scopes to disable scopes for all fixtures
|
||||||
|
# does not work on yield fixtures as only one yield can be used per fixture (i think)
|
||||||
|
@pytest.hookimpl(hookwrapper=True)
|
||||||
|
def pytest_fixture_setup(fixturedef, request):
|
||||||
|
if inspect.isgeneratorfunction(fixturedef.func):
|
||||||
|
yield
|
||||||
|
else:
|
||||||
|
with scopes_disabled():
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def enable_db_access_for_all_tests(db):
|
def enable_db_access_for_all_tests(db):
|
||||||
pass
|
pass
|
||||||
|
@ -152,7 +152,8 @@ class SupermarketViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
|||||||
permission_classes = [CustomIsUser]
|
permission_classes = [CustomIsUser]
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return self.queryset.filter(space=self.request.user.userpreference.space)
|
self.queryset = self.queryset.filter(space=self.request.user.userpreference.space)
|
||||||
|
return super().get_queryset()
|
||||||
|
|
||||||
|
|
||||||
class KeywordViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
class KeywordViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
||||||
@ -169,7 +170,8 @@ class KeywordViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
|||||||
permission_classes = [CustomIsUser]
|
permission_classes = [CustomIsUser]
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return self.queryset.filter(space=self.request.user.userpreference.space)
|
self.queryset = self.queryset.filter(space=self.request.user.userpreference.space)
|
||||||
|
return super().get_queryset()
|
||||||
|
|
||||||
|
|
||||||
class UnitViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
class UnitViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
||||||
@ -178,7 +180,8 @@ class UnitViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
|||||||
permission_classes = [CustomIsUser]
|
permission_classes = [CustomIsUser]
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return self.queryset.filter(space=self.request.user.userpreference.space)
|
self.queryset = self.queryset.filter(space=self.request.user.userpreference.space)
|
||||||
|
return super().get_queryset()
|
||||||
|
|
||||||
|
|
||||||
class FoodViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
class FoodViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
||||||
@ -187,7 +190,8 @@ class FoodViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
|||||||
permission_classes = [CustomIsUser]
|
permission_classes = [CustomIsUser]
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return self.queryset.filter(space=self.request.user.userpreference.space)
|
self.queryset = self.queryset.filter(space=self.request.user.userpreference.space)
|
||||||
|
return super().get_queryset()
|
||||||
|
|
||||||
|
|
||||||
class RecipeBookViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
class RecipeBookViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
||||||
@ -196,7 +200,8 @@ class RecipeBookViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
|||||||
permission_classes = [CustomIsOwner]
|
permission_classes = [CustomIsOwner]
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return self.queryset.filter(created_by=self.request.user).filter(space=self.request.user.userpreference.space)
|
self.queryset = self.queryset.filter(created_by=self.request.user).filter(space=self.request.user.userpreference.space)
|
||||||
|
return super().get_queryset()
|
||||||
|
|
||||||
|
|
||||||
class RecipeBookEntryViewSet(viewsets.ModelViewSet, viewsets.GenericViewSet):
|
class RecipeBookEntryViewSet(viewsets.ModelViewSet, viewsets.GenericViewSet):
|
||||||
|
Reference in New Issue
Block a user