src/notes/tests/api/note.py
changeset 85 e17899ced2b8
parent 74 043477fd5c5c
child 119 8ff8e2aee0f9
equal deleted inserted replaced
84:bf35a7737f94 85:e17899ced2b8
    11 from rest_framework.test import APITransactionTestCase
    11 from rest_framework.test import APITransactionTestCase
    12 
    12 
    13 from notes.models import Session, Note
    13 from notes.models import Session, Note
    14 
    14 
    15 logger = logging.getLogger(__name__)
    15 logger = logging.getLogger(__name__)
       
    16 
    16 
    17 
    17 class NoteApiTests(APITransactionTestCase):
    18 class NoteApiTests(APITransactionTestCase):
    18 
    19 
    19     def setUp(self):
    20     def setUp(self):
    20         User = get_user_model()
    21         User = get_user_model()
    53             description="Description 3",
    54             description="Description 3",
    54             protocol="[]",
    55             protocol="[]",
    55             owner=user3
    56             owner=user3
    56         )
    57         )
    57 
    58 
    58         Note.objects.create(
    59         self.note1 = Note.objects.create(
    59             tc_start=timezone.now(),
    60             tc_start=timezone.now(),
    60             tc_end=timezone.now(),
    61             tc_end=timezone.now(),
    61             session=self.session1,
    62             session=self.session1,
    62             plain="example note 1",
    63             plain="example note 1",
    63             html="<i>example note 1</i>",
    64             html="<i>example note 1</i>",
    75             raw="<i>example note</i>",
    76             raw="<i>example note</i>",
    76             margin_note="margin note",
    77             margin_note="margin note",
    77             categorization="[]"
    78             categorization="[]"
    78         )
    79         )
    79 
    80 
    80 
       
    81     def test_create_note_no_user(self):
    81     def test_create_note_no_user(self):
    82         url = reverse('notes:notes-list', kwargs={'session_ext_id': self.session1.ext_id})
    82         url = reverse('notes:notes-list',
       
    83                       kwargs={'session_ext_id': self.session1.ext_id})
    83         response = self.client.post(url, {
    84         response = self.client.post(url, {
    84             'tc_start': timezone.now(),
    85             'tc_start': timezone.now(),
    85             'tc_end': timezone.now(),
    86             'tc_end': timezone.now(),
    86             'plain': "example note 2",
    87             'plain': "example note 2",
    87             'html': "<i>example note</i>",
    88             'html': "<i>example note</i>",
    90             'categorization': "[]"
    91             'categorization': "[]"
    91         }, format='json')
    92         }, format='json')
    92 
    93 
    93         self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
    94         self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
    94 
    95 
    95 
       
    96     def test_create_note(self):
    96     def test_create_note(self):
    97         url = reverse('notes:notes-list', kwargs={'session_ext_id': self.session1.ext_id})
    97         url = reverse('notes:notes-list',
       
    98                       kwargs={'session_ext_id': self.session1.ext_id})
    98         self.client.login(username='test_user1', password='top_secret')
    99         self.client.login(username='test_user1', password='top_secret')
    99         response = self.client.post(url, {
   100         response = self.client.post(url, {
   100             'tc_start': timezone.now(),
   101             'tc_start': timezone.now(),
   101             'tc_end': timezone.now(),
   102             'tc_end': timezone.now(),
   102             'plain': "example note 2",
   103             'plain': "example note 2",
   107         }, format='json')
   108         }, format='json')
   108 
   109 
   109         self.assertEqual(response.status_code, status.HTTP_201_CREATED)
   110         self.assertEqual(response.status_code, status.HTTP_201_CREATED)
   110         json = response.json()
   111         json = response.json()
   111         self.assertIn('ext_id', json)
   112         self.assertIn('ext_id', json)
   112         note = Note.objects.get(ext_id=json['ext_id'], session__id=self.session1.id)
   113         note = Note.objects.get(
       
   114             ext_id=json['ext_id'], session__id=self.session1.id)
   113         self.assertTrue(note)
   115         self.assertTrue(note)
   114 
   116 
   115     def test_create_note_with_ext_id(self):
   117     def test_create_note_with_ext_id(self):
   116         url = reverse('notes:notes-list', kwargs={'session_ext_id': self.session1.ext_id})
   118         url = reverse('notes:notes-list',
       
   119                       kwargs={'session_ext_id': self.session1.ext_id})
   117         self.client.login(username='test_user1', password='top_secret')
   120         self.client.login(username='test_user1', password='top_secret')
   118         ext_id = str(uuid4())
   121         ext_id = str(uuid4())
   119         response = self.client.post(url, {
   122         response = self.client.post(url, {
   120             'ext_id': ext_id,
   123             'ext_id': ext_id,
   121             'tc_start': timezone.now(),
   124             'tc_start': timezone.now(),
   131         json = response.json()
   134         json = response.json()
   132         self.assertIn('ext_id', json)
   135         self.assertIn('ext_id', json)
   133         self.assertEqual(json['ext_id'], ext_id)
   136         self.assertEqual(json['ext_id'], ext_id)
   134         note = Note.objects.get(ext_id=ext_id)
   137         note = Note.objects.get(ext_id=ext_id)
   135         self.assertTrue(note)
   138         self.assertTrue(note)
       
   139 
       
   140     def test_update_note(self):
       
   141         url = reverse('notes:notes-detail',
       
   142                       kwargs={'session_ext_id': self.session1.ext_id, 'ext_id': self.note1.ext_id})
       
   143         self.client.login(username='test_user1', password='top_secret')
       
   144         response = self.client.put(url, {
       
   145             'plain': "example note 1 modified",
       
   146             'html': "<i>example note modified</i>",
       
   147             'raw': "<i>example note modified</i>",
       
   148             'margin_note': "margin note",
       
   149             'categorization': "[]"
       
   150         }, format='json')
       
   151 
       
   152         self.assertEqual(response.status_code, status.HTTP_200_OK)
       
   153         json = response.json()
       
   154         self.assertIn('plain', json)
       
   155         self.assertEqual("example note 1 modified", json['plain'])
       
   156         note = Note.objects.get(
       
   157             ext_id=json['ext_id'], session__id=self.session1.id)
       
   158         self.assertTrue(note)
       
   159         self.assertEqual("example note 1 modified", note.plain)
       
   160 
       
   161     #TODO: Fail if a tc_start, tc_end, session, ext_id updated, created are provided on update ?
       
   162     # def test_update_note_tc_start(self):
       
   163     #     url = reverse('notes:notes-detail',
       
   164     #                   kwargs={'session_ext_id': self.session1.ext_id, 'ext_id': self.note1.ext_id})
       
   165     #     self.client.login(username='test_user1', password='top_secret')
       
   166     #     response = self.client.put(url, {
       
   167     #         'tc_start': timezone.now(),
       
   168     #         'tc_end': timezone.now(),
       
   169     #         'plain': "example note 1 modified",
       
   170     #         'html': "<i>example note modified</i>",
       
   171     #         'raw': "<i>example note modified</i>",
       
   172     #         'margin_note': "margin note",
       
   173     #         'categorization': "[]"
       
   174     #     }, format='json')
       
   175 
       
   176     #     self.assertEqual(response.status_code, status.HTTP_200_OK)