From a25109e16c7adc9f710eb5713016645b130bc02a Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Wed, 17 Mar 2021 00:03:36 +0100 Subject: [PATCH] added sync api test --- cookbook/tests/api/test_api_sync.py | 68 ------------ cookbook/tests/pytest/api/test_api_sync.py | 115 +++++++++++++++++++++ 2 files changed, 115 insertions(+), 68 deletions(-) delete mode 100644 cookbook/tests/api/test_api_sync.py create mode 100644 cookbook/tests/pytest/api/test_api_sync.py diff --git a/cookbook/tests/api/test_api_sync.py b/cookbook/tests/api/test_api_sync.py deleted file mode 100644 index 26c84df15..000000000 --- a/cookbook/tests/api/test_api_sync.py +++ /dev/null @@ -1,68 +0,0 @@ -import json - -from cookbook.models import Storage, Sync -from cookbook.tests.views.test_views import TestViews -from django.contrib import auth -from django.urls import reverse - - -class TestApiSync(TestViews): - - def setUp(self): - super(TestApiSync, self).setUp() - self.storage = Storage.objects.create( - name='Test Storage', - username='test', - password='password', - token='token', - url='url', - created_by=auth.get_user(self.admin_client_1) - ) - - self.sync = Sync.objects.create( - storage=self.storage, - path='path' - ) - - def test_sync_list(self): - # verify view permissions are applied accordingly - self.batch_requests( - [ - (self.anonymous_client, 403), - (self.guest_client_1, 403), - (self.user_client_1, 403), - (self.admin_client_1, 200), - (self.superuser_client, 200) - ], - reverse('api:sync-list') - ) - - # verify sync is returned - r = self.admin_client_1.get(reverse('api:sync-list')) - self.assertEqual(r.status_code, 200) - response = json.loads(r.content) - self.assertEqual(len(response), 1) - storage_response = response[0] - self.assertEqual(storage_response['path'], self.sync.path) - - def test_sync_update(self): - # can update sync as admin - r = self.admin_client_1.patch( - reverse( - 'api:sync-detail', - args={self.sync.id} - ), - {'path': 'new'}, - content_type='application/json' - ) - response = json.loads(r.content) - self.assertEqual(r.status_code, 200) - self.assertEqual(response['path'], 'new') - - def test_sync_delete(self): - # can delete sync as admin - r = self.admin_client_1.delete( - reverse('api:sync-detail', args={self.sync.id}) - ) - self.assertEqual(r.status_code, 204) - self.assertEqual(Sync.objects.count(), 0) diff --git a/cookbook/tests/pytest/api/test_api_sync.py b/cookbook/tests/pytest/api/test_api_sync.py new file mode 100644 index 000000000..90bd75ae7 --- /dev/null +++ b/cookbook/tests/pytest/api/test_api_sync.py @@ -0,0 +1,115 @@ +import json + +import pytest +from django.contrib import auth +from django.urls import reverse +from django_scopes import scopes_disabled + +from cookbook.models import RecipeBook, Storage, Sync + +LIST_URL = 'api:sync-list' +DETAIL_URL = 'api:sync-detail' + + +@pytest.fixture() +def obj_1(space_1, u1_s1): + s = Storage.objects.create(name='Test Storage', username='test', password='password', token='token', url='url', created_by=auth.get_user(u1_s1), space=space_1, ) + return Sync.objects.create(storage=s, path='path', space=space_1, ) + + +@pytest.fixture +def obj_2(space_1, u1_s1): + s = Storage.objects.create(name='Test Storage', username='test', password='password', token='token', url='url', created_by=auth.get_user(u1_s1), space=space_1, ) + return Sync.objects.create(storage=s, path='path', space=space_1, ) + + +@pytest.mark.parametrize("arg", [ + ['a_u', 403], + ['g1_s1', 403], + ['u1_s1', 403], + ['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, a1_s1, a1_s2, space_2): + assert len(json.loads(a1_s1.get(reverse(LIST_URL)).content)) == 2 + assert len(json.loads(a1_s2.get(reverse(LIST_URL)).content)) == 0 + + obj_1.space = space_2 + obj_1.save() + + assert len(json.loads(a1_s1.get(reverse(LIST_URL)).content)) == 1 + assert len(json.loads(a1_s2.get(reverse(LIST_URL)).content)) == 1 + + +@pytest.mark.parametrize("arg", [ + ['a_u', 403], + ['g1_s1', 403], + ['u1_s1', 403], + ['a1_s1', 200], + ['g1_s2', 403], + ['u1_s2', 403], + ['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} + ), + {'path': 'new'}, + content_type='application/json' + ) + response = json.loads(r.content) + assert r.status_code == arg[1] + if r.status_code == 200: + assert response['path'] == 'new' + + +@pytest.mark.parametrize("arg", [ + ['a_u', 403], + ['g1_s1', 403], + ['u1_s1', 403], + ['a1_s1', 201], +]) +def test_add(arg, request, a1_s2, obj_1): + c = request.getfixturevalue(arg[0]) + r = c.post( + reverse(LIST_URL), + {'storage': obj_1.storage.pk, 'path': 'test'}, + content_type='application/json' + ) + response = json.loads(r.content) + print(r.content) + assert r.status_code == arg[1] + if r.status_code == 201: + assert response['path'] == 'test' + r = c.get(reverse(DETAIL_URL, args={response['id']})) + assert r.status_code == 200 + r = a1_s2.get(reverse(DETAIL_URL, args={response['id']})) + assert r.status_code == 404 + + +def test_delete(a1_s1, a1_s2, obj_1): + r = a1_s2.delete( + reverse( + DETAIL_URL, + args={obj_1.id} + ) + ) + assert r.status_code == 404 + + r = a1_s1.delete( + reverse( + DETAIL_URL, + args={obj_1.id} + ) + ) + + assert r.status_code == 204 + with scopes_disabled(): + assert RecipeBook.objects.count() == 0