sync admin view + tests

This commit is contained in:
vabene1111 2020-07-08 23:40:53 +02:00
parent e65c8436ef
commit 69d5e628c5
5 changed files with 63 additions and 5 deletions

View File

@ -59,13 +59,13 @@ class StorageSerializer(serializers.ModelSerializer):
class SyncSerializer(serializers.ModelSerializer): class SyncSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Sync model = Sync
fields = ('storage', 'path', 'active', 'last_checked', 'created_at', 'updated_at') fields = ('id', 'storage', 'path', 'active', 'last_checked', 'created_at', 'updated_at')
class SyncLogSerializer(serializers.ModelSerializer): class SyncLogSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = SyncLog model = SyncLog
fields = '__all__' fields = ('id', 'sync', 'status', 'msg', 'created_at')
class KeywordSerializer(UniqueFieldsMixin, serializers.ModelSerializer): class KeywordSerializer(UniqueFieldsMixin, serializers.ModelSerializer):

View File

@ -0,0 +1,51 @@
import json
from django.contrib import auth
from django.db.models import ProtectedError
from django.urls import reverse
from cookbook.models import Storage, Sync, SyncLog
from cookbook.tests.views.test_views import TestViews
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, 403)
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, 403)

View File

@ -46,7 +46,7 @@ class TestApiSync(TestViews):
self.assertEqual(r.status_code, 200) self.assertEqual(r.status_code, 200)
self.assertEqual(response['path'], 'new') self.assertEqual(response['path'], 'new')
def test_storage_delete(self): def test_sync_delete(self):
# can delete sync as admin # can delete sync as admin
r = self.admin_client_1.delete(reverse('api:sync-detail', args={self.sync.id})) r = self.admin_client_1.delete(reverse('api:sync-detail', args={self.sync.id}))
self.assertEqual(r.status_code, 204) self.assertEqual(r.status_code, 204)

View File

@ -13,6 +13,7 @@ router.register(r'user-name', api.UserNameViewSet, basename='username')
router.register(r'user-preference', api.UserPreferenceViewSet) router.register(r'user-preference', api.UserPreferenceViewSet)
router.register(r'storage', api.StorageViewSet) router.register(r'storage', api.StorageViewSet)
router.register(r'sync', api.SyncViewSet) router.register(r'sync', api.SyncViewSet)
router.register(r'sync-log', api.SyncLogViewSet)
router.register(r'unit', api.UnitViewSet) router.register(r'unit', api.UnitViewSet)
router.register(r'food', api.FoodViewSet) router.register(r'food', api.FoodViewSet)

View File

@ -23,11 +23,11 @@ from rest_framework.response import Response
from cookbook.helper.permission_helper import group_required, CustomIsOwner, CustomIsAdmin, CustomIsUser, CustomIsGuest from cookbook.helper.permission_helper import group_required, CustomIsOwner, CustomIsAdmin, CustomIsUser, CustomIsGuest
from cookbook.helper.recipe_url_import import get_from_html from cookbook.helper.recipe_url_import import get_from_html
from cookbook.models import Recipe, Sync, Storage, CookLog, MealPlan, MealType, ViewLog, UserPreference, RecipeBook, Ingredient, Food, Step, Keyword, Unit from cookbook.models import Recipe, Sync, Storage, CookLog, MealPlan, MealType, ViewLog, UserPreference, RecipeBook, Ingredient, Food, Step, Keyword, Unit, SyncLog
from cookbook.provider.dropbox import Dropbox from cookbook.provider.dropbox import Dropbox
from cookbook.provider.nextcloud import Nextcloud from cookbook.provider.nextcloud import Nextcloud
from cookbook.serializer import MealPlanSerializer, MealTypeSerializer, RecipeSerializer, ViewLogSerializer, UserNameSerializer, UserPreferenceSerializer, RecipeBookSerializer, IngredientSerializer, FoodSerializer, StepSerializer, \ from cookbook.serializer import MealPlanSerializer, MealTypeSerializer, RecipeSerializer, ViewLogSerializer, UserNameSerializer, UserPreferenceSerializer, RecipeBookSerializer, IngredientSerializer, FoodSerializer, StepSerializer, \
KeywordSerializer, RecipeImageSerializer, StorageSerializer, SyncSerializer KeywordSerializer, RecipeImageSerializer, StorageSerializer, SyncSerializer, SyncLogSerializer
class UserNameViewSet(viewsets.ReadOnlyModelViewSet): class UserNameViewSet(viewsets.ReadOnlyModelViewSet):
@ -83,6 +83,12 @@ class SyncViewSet(viewsets.ModelViewSet):
permission_classes = [CustomIsAdmin, ] permission_classes = [CustomIsAdmin, ]
class SyncLogViewSet(viewsets.ReadOnlyModelViewSet):
queryset = SyncLog.objects.all()
serializer_class = SyncLogSerializer
permission_classes = [CustomIsAdmin, ]
class RecipeBookViewSet(RetrieveModelMixin, UpdateModelMixin, ListModelMixin, viewsets.GenericViewSet): class RecipeBookViewSet(RetrieveModelMixin, UpdateModelMixin, ListModelMixin, viewsets.GenericViewSet):
queryset = RecipeBook.objects.all() queryset = RecipeBook.objects.all()
serializer_class = RecipeBookSerializer serializer_class = RecipeBookSerializer