test unit api
This commit is contained in:
parent
a7b7272bec
commit
e8f7caebd1
@ -1,69 +0,0 @@
|
|||||||
import json
|
|
||||||
|
|
||||||
from cookbook.models import Unit
|
|
||||||
from cookbook.tests.views.test_views import TestViews
|
|
||||||
from django.urls import reverse
|
|
||||||
|
|
||||||
|
|
||||||
class TestApiUnit(TestViews):
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
super(TestApiUnit, self).setUp()
|
|
||||||
self.unit_1 = Unit.objects.create(
|
|
||||||
name='kg'
|
|
||||||
)
|
|
||||||
self.unit_2 = Unit.objects.create(
|
|
||||||
name='g'
|
|
||||||
)
|
|
||||||
|
|
||||||
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:unit-list')
|
|
||||||
)
|
|
||||||
|
|
||||||
# verify storage is returned
|
|
||||||
r = self.user_client_1.get(reverse('api:unit-list'))
|
|
||||||
self.assertEqual(r.status_code, 200)
|
|
||||||
response = json.loads(r.content)
|
|
||||||
self.assertEqual(len(response), 2)
|
|
||||||
self.assertEqual(response[0]['name'], self.unit_1.name)
|
|
||||||
|
|
||||||
r = self.user_client_1.get(f'{reverse("api:unit-list")}?limit=1')
|
|
||||||
response = json.loads(r.content)
|
|
||||||
self.assertEqual(len(response), 1)
|
|
||||||
|
|
||||||
r = self.user_client_1.get(f'{reverse("api:unit-list")}?query=m')
|
|
||||||
response = json.loads(r.content)
|
|
||||||
self.assertEqual(len(response), 0)
|
|
||||||
|
|
||||||
r = self.user_client_1.get(f'{reverse("api:unit-list")}?query=kg')
|
|
||||||
response = json.loads(r.content)
|
|
||||||
self.assertEqual(len(response), 1)
|
|
||||||
|
|
||||||
def test_keyword_update(self):
|
|
||||||
r = self.user_client_1.patch(
|
|
||||||
reverse(
|
|
||||||
'api:unit-detail',
|
|
||||||
args={self.unit_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_delete(self):
|
|
||||||
r = self.user_client_1.delete(
|
|
||||||
reverse('api:unit-detail', args={self.unit_1.id})
|
|
||||||
)
|
|
||||||
self.assertEqual(r.status_code, 204)
|
|
||||||
self.assertEqual(Unit.objects.count(), 1)
|
|
149
cookbook/tests/pytest/api/test_api_unit.py
Normal file
149
cookbook/tests/pytest/api/test_api_unit.py
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from django.urls import reverse
|
||||||
|
from django_scopes import scopes_disabled
|
||||||
|
|
||||||
|
from cookbook.models import Food, Unit
|
||||||
|
|
||||||
|
LIST_URL = 'api:unit-list'
|
||||||
|
DETAIL_URL = 'api:unit-detail'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def obj_1(space_1):
|
||||||
|
return Unit.objects.get_or_create(name='test_1', space=space_1)[0]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def obj_2(space_1):
|
||||||
|
return Unit.objects.get_or_create(name='test_2', space=space_1)[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(LIST_URL)).status_code == arg[1]
|
||||||
|
|
||||||
|
|
||||||
|
def test_list_space(obj_1, obj_2, u1_s1, u1_s2, space_2):
|
||||||
|
assert len(json.loads(u1_s1.get(reverse(LIST_URL)).content)) == 2
|
||||||
|
assert len(json.loads(u1_s2.get(reverse(LIST_URL)).content)) == 0
|
||||||
|
|
||||||
|
obj_1.space = space_2
|
||||||
|
obj_1.save()
|
||||||
|
|
||||||
|
assert len(json.loads(u1_s1.get(reverse(LIST_URL)).content)) == 1
|
||||||
|
assert len(json.loads(u1_s2.get(reverse(LIST_URL)).content)) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_list_filter(obj_1, obj_2, u1_s1):
|
||||||
|
# verify storage is returned
|
||||||
|
r = u1_s1.get(reverse(LIST_URL))
|
||||||
|
assert r.status_code == 200
|
||||||
|
response = json.loads(r.content)
|
||||||
|
assert len(response) == 2
|
||||||
|
assert response[0]['name'] == obj_1.name
|
||||||
|
|
||||||
|
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?limit=1').content)
|
||||||
|
assert len(response) == 1
|
||||||
|
|
||||||
|
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?query=chicken').content)
|
||||||
|
assert len(response) == 0
|
||||||
|
|
||||||
|
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?query={obj_1.name[4:]}').content)
|
||||||
|
assert len(response) == 1
|
||||||
|
|
||||||
|
|
||||||
|
@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, obj_1):
|
||||||
|
c = request.getfixturevalue(arg[0])
|
||||||
|
r = c.patch(
|
||||||
|
reverse(
|
||||||
|
DETAIL_URL,
|
||||||
|
args={obj_1.id}
|
||||||
|
),
|
||||||
|
{'name': 'new'},
|
||||||
|
content_type='application/json'
|
||||||
|
)
|
||||||
|
response = json.loads(r.content)
|
||||||
|
assert r.status_code == arg[1]
|
||||||
|
if r.status_code == 200:
|
||||||
|
assert response['name'] == '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),
|
||||||
|
{'name': 'test'},
|
||||||
|
content_type='application/json'
|
||||||
|
)
|
||||||
|
response = json.loads(r.content)
|
||||||
|
assert r.status_code == arg[1]
|
||||||
|
if r.status_code == 201:
|
||||||
|
assert response['name'] == 'test'
|
||||||
|
r = c.get(reverse(DETAIL_URL, args={response['id']}))
|
||||||
|
assert r.status_code == 200
|
||||||
|
r = u1_s2.get(reverse(DETAIL_URL, args={response['id']}))
|
||||||
|
assert r.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_duplicate(u1_s1, u1_s2, obj_1):
|
||||||
|
r = u1_s1.post(
|
||||||
|
reverse(LIST_URL),
|
||||||
|
{'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(LIST_URL),
|
||||||
|
{'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_delete(u1_s1, u1_s2, obj_1):
|
||||||
|
r = u1_s2.delete(
|
||||||
|
reverse(
|
||||||
|
DETAIL_URL,
|
||||||
|
args={obj_1.id}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert r.status_code == 404
|
||||||
|
|
||||||
|
r = u1_s1.delete(
|
||||||
|
reverse(
|
||||||
|
DETAIL_URL,
|
||||||
|
args={obj_1.id}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
assert r.status_code == 204
|
||||||
|
with scopes_disabled():
|
||||||
|
assert Food.objects.count() == 0
|
Loading…
Reference in New Issue
Block a user