From b6fade5c3c7232ed6208823c94e23f4be947c3ac Mon Sep 17 00:00:00 2001 From: Jean-Baptiste <bleme@pm.me> Date: Mon, 18 Mar 2019 17:02:47 +0100 Subject: [PATCH] update: No auto_author is request.user is not an User --- djangoldp/tests/models.py | 8 ++++++ djangoldp/tests/runner.py | 2 ++ .../tests/tests_anonymous_permissions.py | 3 ++- djangoldp/tests/tests_auto_author.py | 25 +++++++++++++++++++ djangoldp/views.py | 3 ++- 5 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 djangoldp/tests/tests_auto_author.py diff --git a/djangoldp/tests/models.py b/djangoldp/tests/models.py index e576eadb..502e3cdc 100644 --- a/djangoldp/tests/models.py +++ b/djangoldp/tests/models.py @@ -68,3 +68,11 @@ class Task(models.Model): class Meta: serializer_fields = ['@id', 'title', 'batch'] + + +class Post(Model): + content = models.CharField(max_length=255) + author = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True) + + class Meta: + auto_author = 'author' diff --git a/djangoldp/tests/runner.py b/djangoldp/tests/runner.py index 2d3f2e43..6a54bedc 100644 --- a/djangoldp/tests/runner.py +++ b/djangoldp/tests/runner.py @@ -8,6 +8,7 @@ settings.configure(DEBUG=True, 'ENGINE': 'django.db.backends.sqlite3', } }, + LDP_RDF_CONTEXT = 'https://cdn.happy-dev.fr/owl/hdcontext.jsonld', ROOT_URLCONF='djangoldp.tests.urls', DJANGOLDP_PACKAGES=['djangoldp.tests'], INSTALLED_APPS=('django.contrib.auth', @@ -31,6 +32,7 @@ failures = test_runner.run_tests([ 'djangoldp.tests.tests_user_permissions', 'djangoldp.tests.tests_anonymous_permissions', 'djangoldp.tests.tests_update', + 'djangoldp.tests.tests_auto_author', ]) if failures: sys.exit(failures) diff --git a/djangoldp/tests/tests_anonymous_permissions.py b/djangoldp/tests/tests_anonymous_permissions.py index 79db62f3..f1ace746 100644 --- a/djangoldp/tests/tests_anonymous_permissions.py +++ b/djangoldp/tests/tests_anonymous_permissions.py @@ -10,6 +10,7 @@ from djangoldp.views import LDPViewSet import json + class TestAnonymousUserPermissions(TestCase): def setUp(self): self.factory = APIRequestFactory() @@ -49,4 +50,4 @@ class TestAnonymousUserPermissions(TestCase): nested_fields=["skills"], permission_classes=[AnonymousReadOnly]) response = my_view(request, pk=self.job.pk) - self.assertEqual(response.status_code, 403) \ No newline at end of file + self.assertEqual(response.status_code, 403) diff --git a/djangoldp/tests/tests_auto_author.py b/djangoldp/tests/tests_auto_author.py new file mode 100644 index 00000000..d2d1064e --- /dev/null +++ b/djangoldp/tests/tests_auto_author.py @@ -0,0 +1,25 @@ +import json + +from django.contrib.auth.models import User +from rest_framework.test import APIRequestFactory, APIClient, APITestCase + + +class TestAutoAuthor(APITestCase): + + def setUp(self): + self.factory = APIRequestFactory() + self.client = APIClient() + self.user = User.objects.create_user(username='john', email='jlennon@beatles.com', password='glass onion') + + def tearDown(self): + self.user.delete() + + def test_save_with_anonymous_user(self): + post = { + '@context': "http://owl.openinitiative.com/oicontext.jsonld", + '@graph': [{'http://happy-dev.fr/owl/#content': "post content"}]} + + response = self.client.post('/posts/', data=json.dumps(post), content_type='application/ld+json') + self.assertEqual(response.status_code, 201) + self.assertNotIn('author', response.data) + self.assertEquals(response.data['content'], "post content") diff --git a/djangoldp/views.py b/djangoldp/views.py index 6b1e8ad5..dd63fa18 100644 --- a/djangoldp/views.py +++ b/djangoldp/views.py @@ -1,6 +1,7 @@ from django.apps import apps from django.conf import settings from django.conf.urls import url, include +from django.contrib.auth.models import User from django.core.exceptions import FieldDoesNotExist from django.core.urlresolvers import get_resolver from django.db.utils import OperationalError @@ -119,7 +120,7 @@ class LDPViewSet(LDPViewSetGenerator): return type(LDPSerializer)(model_name + 'Serializer', (LDPSerializer,), {'Meta': meta_class}) def perform_create(self, serializer, **kwargs): - if hasattr(self.model._meta, 'auto_author'): + if hasattr(self.model._meta, 'auto_author') and isinstance(self.request.user, User): kwargs[self.model._meta.auto_author] = self.request.user serializer.save(**kwargs) -- GitLab