From c0c3f1ae08e8de15e1b4012e52a4eff07a5915a5 Mon Sep 17 00:00:00 2001
From: Jean-Baptiste <bleme@pm.me>
Date: Sat, 13 Jul 2019 13:56:06 +0200
Subject: [PATCH] update: add unit test for #154 and fix it

---
 djangoldp/serializers.py        |  2 +-
 djangoldp/tests/models.py       |  3 +--
 djangoldp/tests/runner.py       |  4 ++--
 djangoldp/tests/tests_save.py   | 25 ++++++++++++++++++++++++-
 djangoldp/tests/tests_temp.py   | 26 +-------------------------
 djangoldp/tests/tests_update.py |  5 ++++-
 6 files changed, 33 insertions(+), 32 deletions(-)

diff --git a/djangoldp/serializers.py b/djangoldp/serializers.py
index 75940ba9..72155ecb 100644
--- a/djangoldp/serializers.py
+++ b/djangoldp/serializers.py
@@ -285,7 +285,7 @@ class LDPSerializer(HyperlinkedModelSerializer):
                     object_list = dictionary["@graph"]
                     if self.parent.instance is None:
                         obj = next(filter(
-                            lambda o: not hasattr(o, self.parent.url_field_name) or "./" in o[self.url_field_name],
+                            lambda o: not self.parent.url_field_name in o or "./" in o[self.parent.url_field_name],
                             object_list))
                         value = super().get_value(obj)
                     else:
diff --git a/djangoldp/tests/models.py b/djangoldp/tests/models.py
index f3c6e88c..8bda453d 100644
--- a/djangoldp/tests/models.py
+++ b/djangoldp/tests/models.py
@@ -98,9 +98,8 @@ class Post(Model):
     author = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True)
     peer_user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, related_name="peers_post")
 
-
     class Meta:
         auto_author = 'author'
 
 
-get_user_model()._meta.serializer_fields = ['@id', 'username', 'first_name', 'last_name', 'email', 'userprofile', 'conversation_set']
+get_user_model()._meta.serializer_fields = ['@id', 'username', 'first_name', 'last_name', 'email', 'userprofile', 'conversation_set',]
diff --git a/djangoldp/tests/runner.py b/djangoldp/tests/runner.py
index 82859e21..c3d2bf25 100644
--- a/djangoldp/tests/runner.py
+++ b/djangoldp/tests/runner.py
@@ -33,8 +33,8 @@ settings.configure(DEBUG=True,
                            "delete": "acl:Delete",
                            "control": "acl:Control"
                        }
-                   }
-                   ,
+                   },
+                   AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend', 'guardian.backends.ObjectPermissionBackend'),
                    ROOT_URLCONF='djangoldp.urls',
                    DJANGOLDP_PACKAGES=['djangoldp.tests'],
                    INSTALLED_APPS=('django.contrib.auth',
diff --git a/djangoldp/tests/tests_save.py b/djangoldp/tests/tests_save.py
index d0acfadf..c4ca1399 100644
--- a/djangoldp/tests/tests_save.py
+++ b/djangoldp/tests/tests_save.py
@@ -3,7 +3,7 @@ from rest_framework.utils import json
 
 from djangoldp.models import Model
 from djangoldp.serializers import LDPSerializer
-from djangoldp.tests.models import Skill, JobOffer, Invoice, Message
+from djangoldp.tests.models import Skill, JobOffer, Invoice, LDPDummy
 
 
 class Save(TestCase):
@@ -214,3 +214,26 @@ class Save(TestCase):
         self.assertEqual(response.status_code, 201)
         self.assertEqual(response.data['peer_user'], None)
 
+    def test_save_sub_object_in_new_object_with_reverse_1to1_relation(self):
+        dummy = LDPDummy.objects.create(some="foo")
+
+        body = [
+            {
+                '@id': "_:b216",
+                'http://happy-dev.fr/owl/#description': "user update",
+                'http://happy-dev.fr/owl/#ddummy': {
+                    "@id": "https://happy-dev.fr{}{}/".format(Model.container_id(dummy), dummy.id)
+                }
+            },
+            {
+                '@id': './',
+                "http://happy-dev.fr/owl/#first_name": "Alexandre",
+                "http://happy-dev.fr/owl/#last_name": "Bourlier",
+                "http://happy-dev.fr/owl/#username": "alex",
+                'http://happy-dev.fr/owl/#userprofile': {'@id': "_:b216"}
+            }
+        ]
+        response = self.client.post('/users/', data=json.dumps(body),
+                                    content_type='application/ld+json')
+        self.assertEqual(response.status_code, 201)
+        self.assertIn('userprofile', response.data)
diff --git a/djangoldp/tests/tests_temp.py b/djangoldp/tests/tests_temp.py
index 03dfb977..1864f386 100644
--- a/djangoldp/tests/tests_temp.py
+++ b/djangoldp/tests/tests_temp.py
@@ -14,31 +14,7 @@ class TestTemp(TestCase):
         self.client = APIClient()
         self.user = User.objects.create_user(username='john', email='jlennon@beatles.com', password='glass onion')
 
-
     def tearDown(self):
         pass
 
-    def test_post_should_accept_missing_field_id_nullable(self):
-        body = [
-            {
-                '@id': "./",
-                'http://happy-dev.fr/owl/#content': "post update",
-            }
-        ]
-        response = self.client.post('/posts/', data=json.dumps(body),
-                                   content_type='application/ld+json')
-        self.assertEqual(response.status_code, 201)
-        self.assertIn('peer_user', response.data)
-
-    def test_post_should_accept_empty_field_if_nullable(self):
-        body = [
-            {
-                '@id': "./",
-                'http://happy-dev.fr/owl/#content': "post update",
-                'http://happy-dev.fr/owl/#peer_user': ""
-            }
-        ]
-        response = self.client.post('/posts/', data=json.dumps(body),
-                                   content_type='application/ld+json')
-        self.assertEqual(response.status_code, 201)
-        self.assertEqual(response.data['peer_user'], None)
+
diff --git a/djangoldp/tests/tests_update.py b/djangoldp/tests/tests_update.py
index 4e31ea7d..c1458988 100644
--- a/djangoldp/tests/tests_update.py
+++ b/djangoldp/tests/tests_update.py
@@ -280,7 +280,10 @@ class Update(TestCase):
         body = [
             {
                 '@id': "_:b975",
-                'http://happy-dev.fr/owl/#description': "user description"
+                'http://happy-dev.fr/owl/#description': "user description",
+                'http://happy-dev.fr/owl/#dummy':  {
+                    '@id' : './'
+                }
             },
             {
                 '@id': '/users/{}/'.format(user.pk),
-- 
GitLab