diff --git a/djangoldp/tests/models.py b/djangoldp/tests/models.py index e576eadb04959bfd632e5e0a075e0a5bf1d562d7..502e3cdcf4bb795699e4525353d53a998bf3a4fa 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 2d3f2e433db6d47c54426ebbcc944bd01286123b..0a663cc11bf0e9aa5f03212a8df8479dc96d4502 100644 --- a/djangoldp/tests/runner.py +++ b/djangoldp/tests/runner.py @@ -8,6 +8,33 @@ settings.configure(DEBUG=True, 'ENGINE': 'django.db.backends.sqlite3', } }, + 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', @@ -31,6 +58,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 79db62f3b40f18e55ac48f2d74b7ad2bfa06115d..f1ace746ec4e04039ec774f329ba300f90b5c1e0 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 0000000000000000000000000000000000000000..5ed5505afab78ffb3f164338322826c598561ea9 --- /dev/null +++ b/djangoldp/tests/tests_auto_author.py @@ -0,0 +1,24 @@ +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 = { + '@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 6b1e8ad5c1012fd75a35095037b3af110e6a4490..dd63fa18253d31d41c1cedf456ea2273ec0f5712 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)