more tests working but scopes broken
This commit is contained in:
3
.idea/recipes.iml
generated
3
.idea/recipes.iml
generated
@ -29,4 +29,7 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="TestRunnerService">
|
||||
<option name="PROJECT_TEST_RUNNER" value="pytest" />
|
||||
</component>
|
||||
</module>
|
@ -1,93 +0,0 @@
|
||||
import json
|
||||
|
||||
from cookbook.models import Keyword
|
||||
from cookbook.tests.views.test_views import TestViews
|
||||
from django.urls import reverse
|
||||
|
||||
class TestApiKeyword(TestViews):
|
||||
|
||||
def setUp(self):
|
||||
super(TestApiKeyword, self).setUp()
|
||||
self.keyword_1 = Keyword.objects.create(
|
||||
name='meat'
|
||||
)
|
||||
self.keyword_2 = Keyword.objects.create(
|
||||
name='veggies'
|
||||
)
|
||||
|
||||
def test_keyword_list(self):
|
||||
# verify view permissions are applied accordingly
|
||||
self.batch_requests(
|
||||
[
|
||||
(self.anonymous_client, 403),
|
||||
(self.guest_client_1, 403),
|
||||
(self.user_client_1, 200),
|
||||
(self.admin_client_1, 200),
|
||||
(self.superuser_client, 200)
|
||||
],
|
||||
reverse('api:keyword-list')
|
||||
)
|
||||
|
||||
# verify storage is returned
|
||||
r = self.user_client_1.get(reverse('api:keyword-list'))
|
||||
self.assertEqual(r.status_code, 200)
|
||||
response = json.loads(r.content)
|
||||
self.assertEqual(len(response), 2)
|
||||
self.assertEqual(response[0]['name'], self.keyword_1.name)
|
||||
|
||||
r = self.user_client_1.get(f'{reverse("api:keyword-list")}?limit=1')
|
||||
response = json.loads(r.content)
|
||||
self.assertEqual(len(response), 1)
|
||||
|
||||
r = self.user_client_1.get(
|
||||
f'{reverse("api:keyword-list")}?query=chicken'
|
||||
)
|
||||
response = json.loads(r.content)
|
||||
self.assertEqual(len(response), 0)
|
||||
|
||||
r = self.user_client_1.get(f'{reverse("api:keyword-list")}?query=MEAT')
|
||||
response = json.loads(r.content)
|
||||
self.assertEqual(len(response), 1)
|
||||
|
||||
def test_keyword_update(self):
|
||||
r = self.user_client_1.patch(
|
||||
reverse(
|
||||
'api:keyword-detail',
|
||||
args={self.keyword_1.id}
|
||||
),
|
||||
{'name': 'new'},
|
||||
content_type='application/json'
|
||||
)
|
||||
response = json.loads(r.content)
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertEqual(response['name'], 'new')
|
||||
|
||||
def test_keyword_add(self):
|
||||
r = self.user_client_1.post(
|
||||
reverse('api:keyword-list'),
|
||||
{'name': 'test'},
|
||||
content_type='application/json'
|
||||
)
|
||||
response = json.loads(r.content)
|
||||
self.assertEqual(r.status_code, 201)
|
||||
self.assertEqual(response['name'], 'test')
|
||||
|
||||
def test_keyword_add_duplicate(self):
|
||||
r = self.user_client_1.post(
|
||||
reverse('api:keyword-list'),
|
||||
{'name': self.keyword_1.name},
|
||||
content_type='application/json'
|
||||
)
|
||||
response = json.loads(r.content)
|
||||
self.assertEqual(r.status_code, 201)
|
||||
self.assertEqual(response['name'], self.keyword_1.name)
|
||||
|
||||
def test_keyword_delete(self):
|
||||
r = self.user_client_1.delete(
|
||||
reverse(
|
||||
'api:keyword-detail',
|
||||
args={self.keyword_1.id}
|
||||
)
|
||||
)
|
||||
self.assertEqual(r.status_code, 204)
|
||||
self.assertEqual(Keyword.objects.count(), 1)
|
6
cookbook/tests/pytest/api/__init__.py
Normal file
6
cookbook/tests/pytest/api/__init__.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.test import utils
|
||||
from django_scopes import scopes_disabled
|
||||
|
||||
# disables scoping error in all queries used inside the test functions
|
||||
# fixtures need to have their own scopes_disabled
|
||||
utils.setup_databases = scopes_disabled()(utils.setup_databases)
|
94
cookbook/tests/pytest/api/test_api_keyword.py
Normal file
94
cookbook/tests/pytest/api/test_api_keyword.py
Normal file
@ -0,0 +1,94 @@
|
||||
import json
|
||||
|
||||
import pytest
|
||||
from django_scopes import scopes_disabled
|
||||
|
||||
from cookbook.models import Keyword
|
||||
from cookbook.tests.views.test_views import TestViews
|
||||
from django.urls import reverse
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def keyword_1():
|
||||
return Keyword.objects.get_or_create(name='test_1')[0]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def keyword_2():
|
||||
return Keyword.objects.get_or_create(name='test_2')[0]
|
||||
|
||||
|
||||
@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('api:keyword-list')).status_code == arg[1]
|
||||
|
||||
|
||||
def test_list_filter(keyword_1, keyword_2, u1_s1):
|
||||
# verify storage is returned
|
||||
r = u1_s1.get(reverse('api:keyword-list'))
|
||||
assert r.status_code == 200
|
||||
response = json.loads(r.content)
|
||||
assert len(response) == 2
|
||||
assert response[0]['name'] == keyword_1.name
|
||||
|
||||
response = json.loads(u1_s1.get(f'{reverse("api:keyword-list")}?limit=1'))
|
||||
assert len(response) == 1
|
||||
|
||||
response = json.loads(u1_s1.get(f'{reverse("api:keyword-list")}?query=chicken'))
|
||||
assert len(response) == 0
|
||||
|
||||
response = json.loads(u1_s1.get(f'{reverse("api:keyword-list")}?query={keyword_1.name[4:]}'))
|
||||
assert len(response) == 1
|
||||
|
||||
#
|
||||
# def test_keyword_update(self):
|
||||
# r = self.user_client_1.patch(
|
||||
# reverse(
|
||||
# 'api:keyword-detail',
|
||||
# args={self.keyword_1.id}
|
||||
# ),
|
||||
# {'name': 'new'},
|
||||
# content_type='application/json'
|
||||
# )
|
||||
# response = json.loads(r.content)
|
||||
# self.assertEqual(r.status_code, 200)
|
||||
# self.assertEqual(response['name'], 'new')
|
||||
#
|
||||
#
|
||||
# def test_keyword_add(self):
|
||||
# r = self.user_client_1.post(
|
||||
# reverse('api:keyword-list'),
|
||||
# {'name': 'test'},
|
||||
# content_type='application/json'
|
||||
# )
|
||||
# response = json.loads(r.content)
|
||||
# self.assertEqual(r.status_code, 201)
|
||||
# self.assertEqual(response['name'], 'test')
|
||||
#
|
||||
#
|
||||
# def test_keyword_add_duplicate(self):
|
||||
# r = self.user_client_1.post(
|
||||
# reverse('api:keyword-list'),
|
||||
# {'name': self.keyword_1.name},
|
||||
# content_type='application/json'
|
||||
# )
|
||||
# response = json.loads(r.content)
|
||||
# self.assertEqual(r.status_code, 201)
|
||||
# self.assertEqual(response['name'], self.keyword_1.name)
|
||||
#
|
||||
#
|
||||
# def test_keyword_delete(self):
|
||||
# r = self.user_client_1.delete(
|
||||
# reverse(
|
||||
# 'api:keyword-detail',
|
||||
# args={self.keyword_1.id}
|
||||
# )
|
||||
# )
|
||||
# self.assertEqual(r.status_code, 204)
|
||||
# self.assertEqual(Keyword.objects.count(), 1)
|
@ -1,5 +1,4 @@
|
||||
import copy
|
||||
import inspect
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
@ -14,55 +13,112 @@ def enable_db_access_for_all_tests(db):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_password():
|
||||
return 'strong-test-pass'
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def create_user(db, django_user_model, test_password):
|
||||
def make_user(**kwargs):
|
||||
kwargs['password'] = test_password
|
||||
if 'username' not in kwargs:
|
||||
kwargs['username'] = str(uuid.uuid4())
|
||||
return django_user_model.objects.create_user(**kwargs)
|
||||
|
||||
return make_user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@pytest.fixture()
|
||||
def space_1():
|
||||
with scopes_disabled():
|
||||
return Space.objects.get_or_create(name='space_1')[0]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@pytest.fixture()
|
||||
def space_2():
|
||||
with scopes_disabled():
|
||||
return Space.objects.get_or_create(name='space_2')[0]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def user_1(client, space_1):
|
||||
# ---------------------- USER FIXTURES -----------------------
|
||||
# maybe better with factories but this is very explict so ...
|
||||
|
||||
def create_user(client, space, **kwargs):
|
||||
c = copy.deepcopy(client)
|
||||
with scopes_disabled():
|
||||
print(f'creating user 1 with space {space_1}')
|
||||
user = User.objects.create(username='user_1')
|
||||
user.groups.add(Group.objects.get(name='user'))
|
||||
user.userpreference.space = space_1
|
||||
group = kwargs.pop('group', None)
|
||||
username = kwargs.pop('username', uuid.uuid4())
|
||||
|
||||
user = User.objects.create(username=username, **kwargs)
|
||||
if group:
|
||||
user.groups.add(Group.objects.get(name=group))
|
||||
|
||||
user.userpreference.space = space
|
||||
user.userpreference.save()
|
||||
c.force_login(user)
|
||||
return c
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def user_2(client, space_2):
|
||||
c = copy.deepcopy(client)
|
||||
with scopes_disabled():
|
||||
print(f'creating user 2 with space {space_2}')
|
||||
user = User.objects.create(username='user_2')
|
||||
user.groups.add(Group.objects.get(name='user'))
|
||||
user.userpreference.space = space_2
|
||||
user.userpreference.save()
|
||||
c.force_login(user)
|
||||
return c
|
||||
# anonymous user
|
||||
@pytest.fixture()
|
||||
def a_u(client):
|
||||
return copy.deepcopy(client)
|
||||
|
||||
|
||||
# users without any group
|
||||
@pytest.fixture()
|
||||
def ng1_s1(client, space_1):
|
||||
return create_user(client, space_1)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def ng1_s2(client, space_2):
|
||||
return create_user(client, space_2)
|
||||
|
||||
|
||||
# guests
|
||||
@pytest.fixture()
|
||||
def g1_s1(client, space_1):
|
||||
return create_user(client, space_1, group='guest')
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def g2_s1(client, space_1):
|
||||
return create_user(client, space_1, group='guest')
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def g1_s2(client, space_2):
|
||||
return create_user(client, space_2, group='guest')
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def g2_s2(client, space_2):
|
||||
return create_user(client, space_2, group='guest')
|
||||
|
||||
|
||||
# users
|
||||
@pytest.fixture()
|
||||
def u1_s1(client, space_1):
|
||||
return create_user(client, space_1, group='user')
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def u2_s1(client, space_1):
|
||||
return create_user(client, space_1, group='user')
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def u1_s2(client, space_2):
|
||||
return create_user(client, space_2, group='user')
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def u2_s2(client, space_2):
|
||||
return create_user(client, space_2, group='user')
|
||||
|
||||
|
||||
# admins
|
||||
@pytest.fixture()
|
||||
def a1_s1(client, space_1):
|
||||
return create_user(client, space_1, group='admin')
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def a2_s1(client, space_1):
|
||||
return create_user(client, space_1, group='admin')
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def a1_s2(client, space_2):
|
||||
return create_user(client, space_2, group='admin')
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def a2_s2(client, space_2):
|
||||
return create_user(client, space_2, group='admin')
|
||||
|
@ -10,16 +10,11 @@ from cookbook.models import Keyword, Space
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_user_create(user_2, user_1):
|
||||
r = user_1.post(reverse('api:keyword-list'), {'name': 'test', 'space': 1}, content_type='application/json')
|
||||
def test_user_create(u1_s1,u1_s2):
|
||||
r = u1_s1.post(reverse('api:keyword-list'), {'name': 'test', 'space': 1}, content_type='application/json')
|
||||
response = json.loads(r.content)
|
||||
assert r.status_code == 201
|
||||
assert response['name'] == 'test'
|
||||
|
||||
print('DEEEEEBUUUUUGGGG')
|
||||
print(user_1.get(reverse('view_test')).content)
|
||||
print(user_2.get(reverse('view_test')).content)
|
||||
print(user_2)
|
||||
|
||||
r = user_2.get(reverse('api:keyword-detail', args={response['id']}), content_type='application/json')
|
||||
r = u1_s2.get(reverse('api:keyword-detail', args={response['id']}), content_type='application/json')
|
||||
assert r.status_code == 404
|
||||
|
Reference in New Issue
Block a user