delete model check fixes + sync log
This commit is contained in:
17
cookbook/migrations/0112_remove_synclog_space.py
Normal file
17
cookbook/migrations/0112_remove_synclog_space.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# Generated by Django 3.1.7 on 2021-03-16 23:21
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('cookbook', '0111_space_created_by'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='synclog',
|
||||||
|
name='space',
|
||||||
|
),
|
||||||
|
]
|
@ -220,8 +220,7 @@ class SyncLog(models.Model, PermissionModelMixin):
|
|||||||
msg = models.TextField(default="")
|
msg = models.TextField(default="")
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
space = models.ForeignKey(Space, on_delete=models.CASCADE)
|
objects = ScopedManager(space='sync__space')
|
||||||
objects = ScopedManager(space='space')
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.created_at}:{self.sync} - {self.status}"
|
return f"{self.created_at}:{self.sync} - {self.status}"
|
||||||
|
@ -27,7 +27,7 @@ class Dropbox(Provider):
|
|||||||
try:
|
try:
|
||||||
recipes = r.json()
|
recipes = r.json()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
log_entry = SyncLog(status='ERROR', msg=str(r), sync=monitor, space=monitor.space)
|
log_entry = SyncLog(status='ERROR', msg=str(r), sync=monitor)
|
||||||
log_entry.save()
|
log_entry.save()
|
||||||
return r
|
return r
|
||||||
|
|
||||||
@ -51,7 +51,6 @@ class Dropbox(Provider):
|
|||||||
status='SUCCESS',
|
status='SUCCESS',
|
||||||
msg='Imported ' + str(import_count) + ' recipes',
|
msg='Imported ' + str(import_count) + ' recipes',
|
||||||
sync=monitor,
|
sync=monitor,
|
||||||
space=monitor.space,
|
|
||||||
)
|
)
|
||||||
log_entry.save()
|
log_entry.save()
|
||||||
|
|
||||||
|
@ -33,7 +33,6 @@ class Local(Provider):
|
|||||||
status='SUCCESS',
|
status='SUCCESS',
|
||||||
msg='Imported ' + str(import_count) + ' recipes',
|
msg='Imported ' + str(import_count) + ' recipes',
|
||||||
sync=monitor,
|
sync=monitor,
|
||||||
space=monitor.space,
|
|
||||||
)
|
)
|
||||||
log_entry.save()
|
log_entry.save()
|
||||||
|
|
||||||
|
@ -49,7 +49,6 @@ class Nextcloud(Provider):
|
|||||||
status='SUCCESS',
|
status='SUCCESS',
|
||||||
msg='Imported ' + str(import_count) + ' recipes',
|
msg='Imported ' + str(import_count) + ' recipes',
|
||||||
sync=monitor,
|
sync=monitor,
|
||||||
space=monitor.space
|
|
||||||
)
|
)
|
||||||
log_entry.save()
|
log_entry.save()
|
||||||
|
|
||||||
|
@ -1,70 +0,0 @@
|
|||||||
import json
|
|
||||||
|
|
||||||
from cookbook.models import Storage, Sync, SyncLog
|
|
||||||
from cookbook.tests.views.test_views import TestViews
|
|
||||||
from django.contrib import auth
|
|
||||||
from django.urls import reverse
|
|
||||||
|
|
||||||
|
|
||||||
class TestApiSyncLog(TestViews):
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
super(TestApiSyncLog, 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'
|
|
||||||
)
|
|
||||||
|
|
||||||
self.sync_log = SyncLog.objects.create(
|
|
||||||
sync=self.sync, status='success'
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_sync_log_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:synclog-list')
|
|
||||||
)
|
|
||||||
|
|
||||||
# verify log entry is returned
|
|
||||||
r = self.admin_client_1.get(reverse('api:synclog-list'))
|
|
||||||
self.assertEqual(r.status_code, 200)
|
|
||||||
response = json.loads(r.content)
|
|
||||||
self.assertEqual(len(response), 1)
|
|
||||||
self.assertEqual(response[0]['status'], self.sync_log.status)
|
|
||||||
|
|
||||||
def test_sync_log_update(self):
|
|
||||||
# read only view
|
|
||||||
r = self.admin_client_1.patch(
|
|
||||||
reverse(
|
|
||||||
'api:synclog-detail',
|
|
||||||
args={self.sync.id}
|
|
||||||
),
|
|
||||||
{'path': 'new'},
|
|
||||||
content_type='application/json'
|
|
||||||
)
|
|
||||||
self.assertEqual(r.status_code, 405)
|
|
||||||
|
|
||||||
def test_sync_log_delete(self):
|
|
||||||
# read only view
|
|
||||||
r = self.admin_client_1.delete(
|
|
||||||
reverse(
|
|
||||||
'api:synclog-detail',
|
|
||||||
args={self.sync.id})
|
|
||||||
)
|
|
||||||
self.assertEqual(r.status_code, 405)
|
|
@ -129,4 +129,4 @@ def test_delete(u1_s1, u1_s2, obj_1):
|
|||||||
|
|
||||||
assert r.status_code == 204
|
assert r.status_code == 204
|
||||||
with scopes_disabled():
|
with scopes_disabled():
|
||||||
assert Food.objects.count() == 0
|
assert MealType.objects.count() == 0
|
||||||
|
@ -119,4 +119,4 @@ def test_delete(a1_s1, a1_s2, obj_1):
|
|||||||
|
|
||||||
assert r.status_code == 204
|
assert r.status_code == 204
|
||||||
with scopes_disabled():
|
with scopes_disabled():
|
||||||
assert RecipeBook.objects.count() == 0
|
assert Storage.objects.count() == 0
|
||||||
|
@ -127,4 +127,4 @@ def test_delete(u1_s1, u1_s2, obj_1):
|
|||||||
|
|
||||||
assert r.status_code == 204
|
assert r.status_code == 204
|
||||||
with scopes_disabled():
|
with scopes_disabled():
|
||||||
assert RecipeBook.objects.count() == 0
|
assert Supermarket.objects.count() == 0
|
||||||
|
@ -112,4 +112,4 @@ def test_delete(a1_s1, a1_s2, obj_1):
|
|||||||
|
|
||||||
assert r.status_code == 204
|
assert r.status_code == 204
|
||||||
with scopes_disabled():
|
with scopes_disabled():
|
||||||
assert RecipeBook.objects.count() == 0
|
assert Sync.objects.count() == 0
|
||||||
|
105
cookbook/tests/pytest/api/test_api_sync_log.py
Normal file
105
cookbook/tests/pytest/api/test_api_sync_log.py
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
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, SyncLog
|
||||||
|
|
||||||
|
LIST_URL = 'api:synclog-list'
|
||||||
|
DETAIL_URL = 'api:synclog-detail'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def obj_1(space_1, u1_s1):
|
||||||
|
s = Storage.objects.create(name='Test Storage 1', username='test', password='password', token='token', url='url', created_by=auth.get_user(u1_s1), space=space_1, )
|
||||||
|
sy = Sync.objects.create(storage=s, path='path', space=space_1, )
|
||||||
|
return SyncLog.objects.create(sync=sy, status=1)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def obj_2(space_1, u1_s1):
|
||||||
|
s = Storage.objects.create(name='Test Storage 2', username='test', password='password', token='token', url='url', created_by=auth.get_user(u1_s1), space=space_1, )
|
||||||
|
sy = Sync.objects.create(storage=s, path='path', space=space_1, )
|
||||||
|
return SyncLog.objects.create(sync=sy, status=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.sync.space = space_2
|
||||||
|
obj_1.sync.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', 405],
|
||||||
|
['g1_s2', 403],
|
||||||
|
['u1_s2', 403],
|
||||||
|
['a1_s2', 405],
|
||||||
|
])
|
||||||
|
def test_update(arg, request, obj_1):
|
||||||
|
c = request.getfixturevalue(arg[0])
|
||||||
|
r = c.patch(
|
||||||
|
reverse(
|
||||||
|
DETAIL_URL,
|
||||||
|
args={obj_1.id}
|
||||||
|
),
|
||||||
|
{'msg': 'new'},
|
||||||
|
content_type='application/json'
|
||||||
|
)
|
||||||
|
assert r.status_code == arg[1]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("arg", [
|
||||||
|
['a_u', 403],
|
||||||
|
['g1_s1', 403],
|
||||||
|
['u1_s1', 403],
|
||||||
|
['a1_s1', 405],
|
||||||
|
])
|
||||||
|
def test_add(arg, request, a1_s2, obj_1):
|
||||||
|
c = request.getfixturevalue(arg[0])
|
||||||
|
r = c.post(
|
||||||
|
reverse(LIST_URL),
|
||||||
|
{'msg': 'test'},
|
||||||
|
content_type='application/json'
|
||||||
|
)
|
||||||
|
response = json.loads(r.content)
|
||||||
|
assert r.status_code == arg[1]
|
||||||
|
|
||||||
|
|
||||||
|
def test_delete(a1_s1, a1_s2, obj_1):
|
||||||
|
r = a1_s2.delete(
|
||||||
|
reverse(
|
||||||
|
DETAIL_URL,
|
||||||
|
args={obj_1.id}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert r.status_code == 405
|
||||||
|
|
||||||
|
r = a1_s1.delete(
|
||||||
|
reverse(
|
||||||
|
DETAIL_URL,
|
||||||
|
args={obj_1.id}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
assert r.status_code == 405
|
@ -136,7 +136,7 @@ class SyncLogViewSet(viewsets.ReadOnlyModelViewSet):
|
|||||||
permission_classes = [CustomIsAdmin, ]
|
permission_classes = [CustomIsAdmin, ]
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return self.queryset.filter(space=self.request.user.userpreference.space)
|
return self.queryset.filter(sync__space=self.request.user.userpreference.space)
|
||||||
|
|
||||||
|
|
||||||
class SupermarketViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
class SupermarketViewSet(viewsets.ModelViewSet, StandardFilterMixin):
|
||||||
|
Reference in New Issue
Block a user