src/notes/tests/api/session.py
changeset 142 56850f5c73f6
parent 131 adad5563603c
--- a/src/notes/tests/api/session.py	Tue Jul 10 15:18:03 2018 +0200
+++ b/src/notes/tests/api/session.py	Wed Jul 18 17:32:09 2018 +0200
@@ -3,6 +3,7 @@
 """
 import datetime
 import logging
+from unittest import skip
 from uuid import uuid4
 
 from django.contrib.auth import get_user_model
@@ -12,7 +13,8 @@
 from rest_framework import status
 from rest_framework.test import APITransactionTestCase
 
-from notes.models import Session, Note, GroupProfile
+from notes import constants
+from notes.models import GroupProfile, Note, Session
 
 logger = logging.getLogger(__name__)
 
@@ -50,7 +52,6 @@
         self.session1 = Session.objects.create(
             title="a new session 1",
             description="Description 1",
-            protocol="[]",
             group=self.user1.profile.default_group,
             owner=self.user1
         )
@@ -58,7 +59,6 @@
         self.session2 = Session.objects.create(
             title="a new session 2",
             description="Description 2",
-            protocol="[]",
             group=self.user2.profile.default_group,
             owner=self.user2
         )
@@ -66,7 +66,6 @@
         self.session3 = Session.objects.create(
             title="a new session 3",
             description="Description 3",
-            protocol="[]",
             group=user3.profile.default_group,
             owner=user3
         )
@@ -74,7 +73,6 @@
         self.session4 = Session.objects.create(
             title="a new session 4",
             description="Description 4",
-            protocol="[]",
             group=user3.profile.default_group,
             owner=user3
         )
@@ -166,17 +164,110 @@
         response = self.client.post(url, {
             'title': "a new session",
             'description': "description of the session",
-            'protocol': "[]",
             'group': 'group1'
         }, format='json')
 
-        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
+        self.assertEqual(response.status_code, status.HTTP_201_CREATED, "error msg: %r" % response.content)
+        json = response.json()
+        self.assertIn('ext_id', json)
+
+        session = Session.objects.get(ext_id=json['ext_id'])
+        self.assertIsNotNone(session)
+        self.assertEqual(self.group1, session.group)
+        self.assertEqual(self.group1.profile.protocol, session.protocol)
+
+
+    def test_create_session_protocol(self):
+        url = reverse('notes:session-list')
+        self.client.login(username='test_user1', password='top_secret')
+        response = self.client.post(url, {
+            'title': "a new session",
+            'description': "description of the session",
+            'group': 'group1',
+            'protocol': {}
+        }, format='json')
+
+        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.json())
+        json = response.json()
+        self.assertIn('ext_id', json)
+
+        session = Session.objects.get(ext_id=json['ext_id'])
+        self.assertIsNotNone(session)
+        self.assertEqual(self.group1, session.group)
+        self.assertNotEqual(self.group1.profile.protocol, session.protocol)
+
+
+    def test_create_session_protocol_as_str(self):
+        url = reverse('notes:session-list')
+        self.client.login(username='test_user1', password='top_secret')
+        group1 = Group.objects.get(id=self.group1.id)
+        response = self.client.post(url, {
+            'title': "a new session",
+            'description': "description of the session",
+            'group': 'group1',
+            'protocol': group1.profile.protocol
+        }, format='json')
+
+        self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.json())
         json = response.json()
         self.assertIn('ext_id', json)
 
         session = Session.objects.get(ext_id=json['ext_id'])
         self.assertIsNotNone(session)
         self.assertEqual(self.group1, session.group)
+        self.assertEqual(self.group1.profile.protocol, session.protocol)
+
+
+    def test_create_session_protocol_as_str_unknown(self):
+        url = reverse('notes:session-list')
+        self.client.login(username='test_user1', password='top_secret')
+        group1 = Group.objects.get(id=self.group1.id)
+        response = self.client.post(url, {
+            'title': "a new session",
+            'description': "description of the session",
+            'group': 'group1',
+            'protocol': constants.PROTOCOL_URN_PREFIX + str(uuid4()) + ".1"
+        }, format='json')
+
+        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.json())
+        json = response.json()
+        self.assertIn('protocol', json)
+        self.assertIn('Incorrect protocol', json['protocol'][0])
+        self.assertIn('does not exists', json['protocol'][0])
+
+
+    def test_create_session_protocol_as_str_bad(self):
+        url = reverse('notes:session-list')
+        self.client.login(username='test_user1', password='top_secret')
+        group1 = Group.objects.get(id=self.group1.id)
+        response = self.client.post(url, {
+            'title': "a new session",
+            'description': "description of the session",
+            'group': 'group1',
+            'protocol': constants.PROTOCOL_URN_PREFIX + "foo"
+        }, format='json')
+
+        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.json())
+        json = response.json()
+        self.assertIn('protocol', json)
+        self.assertEqual('Incorrect format. Expected `urn:protocol:<ext_id>.<version>`.', json['protocol'][0])
+
+
+    def test_create_session_protocol_as_other_type_bad(self):
+        url = reverse('notes:session-list')
+        self.client.login(username='test_user1', password='top_secret')
+        group1 = Group.objects.get(id=self.group1.id)
+        response = self.client.post(url, {
+            'title': "a new session",
+            'description': "description of the session",
+            'group': 'group1',
+            'protocol': True
+        }, format='json')
+
+        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.json())
+        json = response.json()
+        self.assertIn('protocol', json)
+        self.assertEqual('Incorrect format. Expected `urn:protocol:<ext_id>.<version>` or dict.', json['protocol'][0])
 
 
     def test_create_session_no_group(self):
@@ -184,8 +275,7 @@
         self.client.login(username='test_user1', password='top_secret')
         response = self.client.post(url, {
             'title': "a new session",
-            'description': "description of the session",
-            'protocol': "[]",
+            'description': "description of the session"
         }, format='json')
 
         self.assertEqual(response.status_code, status.HTTP_201_CREATED, "Error when creating session %r" % response.json())
@@ -196,32 +286,14 @@
         self.assertIsNotNone(session)
         user1_personal_group = Group.objects.get(profile__owner_personal=self.user1)
         self.assertEqual(user1_personal_group, session.group)
+        self.assertEqual(user1_personal_group.profile.protocol, session.protocol)
 
     def test_create_session_no_group_default(self):
         url = reverse('notes:session-list')
         self.client.login(username='test_user2', password='top_secret')
         response = self.client.post(url, {
             'title': "a new session",
-            'description': "description of the session",
-            'protocol': "[]",
-        }, format='json')
-
-        self.assertEqual(response.status_code, status.HTTP_201_CREATED, "Error when creating session %r" % response.json())
-        json = response.json()
-        self.assertIn('ext_id', json)
-
-        session = Session.objects.get(ext_id=json['ext_id'])
-        self.assertIsNotNone(session)
-        self.assertEqual(self.group1, session.group)
-
-
-    def test_create_session_no_group_default(self):
-        url = reverse('notes:session-list')
-        self.client.login(username='test_user2', password='top_secret')
-        response = self.client.post(url, {
-            'title': "a new session",
-            'description': "description of the session",
-            'protocol': "[]",
+            'description': "description of the session"
         }, format='json')
 
         self.assertEqual(response.status_code, status.HTTP_201_CREATED, "Error when creating session %r" % response.json())
@@ -239,7 +311,7 @@
         response = self.client.post(url, {
             'title': "a new session",
             'description': "description of the session",
-            'protocol': "[]",
+            'protocol': { 'owner': 'group1' },
             'group': "group1"
         }, format='json')
 
@@ -257,11 +329,10 @@
             'ext_id': ext_id,
             'title': "a new session",
             'description': "description of the session",
-            'group': 'group1',
-            'protocol': "[]"
+            'group': 'group1'
         }, format='json')
 
-        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
+        self.assertEqual(response.status_code, status.HTTP_201_CREATED, "response content: %r" % response.content)
         json = response.json()
         self.assertIn('ext_id', json)
         self.assertEqual(json['ext_id'], ext_id)
@@ -351,3 +422,44 @@
         for session_json in json['results']:
             self.assertIn(session_json.get('title'), ['a new session 3', 'a new session 4'])
 
+    def test_update_session(self):
+        url = reverse('notes:session-detail', kwargs={'ext_id': self.session1.ext_id})
+
+        self.client.login(username='test_user1', password='top_secret')
+        response = self.client.put(url, {
+            'title': "a new session",
+            'description': "description of the session",
+            'group': 'group1'
+        }, format='json')
+
+        self.assertEqual(response.status_code, status.HTTP_200_OK, "response content: %r" % response.content)
+        json = response.json()
+        self.assertIn('ext_id', json)
+
+        session = Session.objects.get(ext_id=json['ext_id'])
+        self.assertIsNotNone(session)
+        self.assertEqual(self.group1, session.group)
+        self.assertEqual(self.group1.profile.protocol, session.protocol)
+
+    @skip("Protocol update not yet implemented")
+    def test_update_session_protocol(self):
+        url = reverse('notes:session-detail', kwargs={'ext_id': self.session1.ext_id})
+
+        self.client.login(username='test_user1', password='top_secret')
+        response = self.client.put(url, {
+            'title': "an updated session",
+            'description': "description of the session",
+            'group': 'group1',
+            'protocol': "urn:protocol:845ac623-b85a-4ddd-91ee-a2deb1a9b54f.3"
+        }, format='json')
+
+        json = response.json()
+        # self.assertIn('ext_id', json)
+        session = Session.objects.get(ext_id=self.session1.ext_id)
+        self.assertEqual(response.status_code, status.HTTP_200_OK, "response content: %r" % response.content)
+
+
+        # session = Session.objects.get(ext_id=json['ext_id'])
+        # self.assertIsNotNone(session)
+        # self.assertEqual(self.group1, session.group)
+        # self.assertEqual(self.group1.profile.protocol, session.protocol)