From 69d5e628c5f4911786bd03008a2ceb7e721e41fb Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Wed, 8 Jul 2020 23:40:53 +0200 Subject: [PATCH] sync admin view + tests --- cookbook/serializer.py | 4 +- cookbook/tests/api/test_api_syn_log.py | 51 ++++++++++++++++++++++++++ cookbook/tests/api/test_api_sync.py | 2 +- cookbook/urls.py | 1 + cookbook/views/api.py | 10 ++++- 5 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 cookbook/tests/api/test_api_syn_log.py diff --git a/cookbook/serializer.py b/cookbook/serializer.py index 90d7b5b13..224a54760 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -59,13 +59,13 @@ class StorageSerializer(serializers.ModelSerializer): class SyncSerializer(serializers.ModelSerializer): class Meta: 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 Meta: model = SyncLog - fields = '__all__' + fields = ('id', 'sync', 'status', 'msg', 'created_at') class KeywordSerializer(UniqueFieldsMixin, serializers.ModelSerializer): diff --git a/cookbook/tests/api/test_api_syn_log.py b/cookbook/tests/api/test_api_syn_log.py new file mode 100644 index 000000000..a0995a010 --- /dev/null +++ b/cookbook/tests/api/test_api_syn_log.py @@ -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) diff --git a/cookbook/tests/api/test_api_sync.py b/cookbook/tests/api/test_api_sync.py index 92024d6e5..1b5963cd7 100644 --- a/cookbook/tests/api/test_api_sync.py +++ b/cookbook/tests/api/test_api_sync.py @@ -46,7 +46,7 @@ class TestApiSync(TestViews): self.assertEqual(r.status_code, 200) self.assertEqual(response['path'], 'new') - def test_storage_delete(self): + 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) diff --git a/cookbook/urls.py b/cookbook/urls.py index 895c4254a..aa1291b38 100644 --- a/cookbook/urls.py +++ b/cookbook/urls.py @@ -13,6 +13,7 @@ router.register(r'user-name', api.UserNameViewSet, basename='username') router.register(r'user-preference', api.UserPreferenceViewSet) router.register(r'storage', api.StorageViewSet) router.register(r'sync', api.SyncViewSet) +router.register(r'sync-log', api.SyncLogViewSet) router.register(r'unit', api.UnitViewSet) router.register(r'food', api.FoodViewSet) diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 1d0f5df8a..d048eca93 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -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.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.nextcloud import Nextcloud 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): @@ -83,6 +83,12 @@ class SyncViewSet(viewsets.ModelViewSet): permission_classes = [CustomIsAdmin, ] +class SyncLogViewSet(viewsets.ReadOnlyModelViewSet): + queryset = SyncLog.objects.all() + serializer_class = SyncLogSerializer + permission_classes = [CustomIsAdmin, ] + + class RecipeBookViewSet(RetrieveModelMixin, UpdateModelMixin, ListModelMixin, viewsets.GenericViewSet): queryset = RecipeBook.objects.all() serializer_class = RecipeBookSerializer