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 1/2] 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 From 6925327da5db7abbdfca61bff22bb356a1bc76c2 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste <bleme@pm.me> Date: Mon, 18 Mar 2019 17:35:31 +0100 Subject: [PATCH 2/2] syntax: Fix CI --- djangoldp/tests/runner.py | 28 +++++++++++++++++++++++++++- djangoldp/tests/tests_auto_author.py | 1 - 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/djangoldp/tests/runner.py b/djangoldp/tests/runner.py index 6a54bedc..0a663cc1 100644 --- a/djangoldp/tests/runner.py +++ b/djangoldp/tests/runner.py @@ -8,7 +8,33 @@ settings.configure(DEBUG=True, 'ENGINE': 'django.db.backends.sqlite3', } }, - LDP_RDF_CONTEXT = 'https://cdn.happy-dev.fr/owl/hdcontext.jsonld', + LDP_RDF_CONTEXT = { + "@context": { + "@vocab": "http://happy-dev.fr/owl/#", + "foaf": "http://xmlns.com/foaf/0.1/", + "doap": "http://usefulinc.com/ns/doap#", + "ldp": "http://www.w3.org/ns/ldp#", + "rdfs": "http://www.w3.org/2000/01/rdf-schema#", + "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", + "xsd": "http://www.w3.org/2001/XMLSchema#", + "geo": "http://www.w3.org/2003/01/geo/wgs84_pos#", + "acl": "http://www.w3.org/ns/auth/acl#", + "name": "rdfs:label", + "website": "foaf:homepage", + "deadline": "xsd:dateTime", + "lat": "geo:lat", + "lng": "geo:long", + "jabberID": "foaf:jabberID", + "permissions": "acl:accessControl", + "mode": "acl:mode", + "view": "acl:Read", + "change": "acl:Write", + "add": "acl:Append", + "delete": "acl:Delete", + "control": "acl:Control" + } + } + , ROOT_URLCONF='djangoldp.tests.urls', DJANGOLDP_PACKAGES=['djangoldp.tests'], INSTALLED_APPS=('django.contrib.auth', diff --git a/djangoldp/tests/tests_auto_author.py b/djangoldp/tests/tests_auto_author.py index d2d1064e..5ed5505a 100644 --- a/djangoldp/tests/tests_auto_author.py +++ b/djangoldp/tests/tests_auto_author.py @@ -16,7 +16,6 @@ class TestAutoAuthor(APITestCase): 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') -- GitLab