src/notes/tests/api/note.py
author ymh <ymh.work@gmail.com>
Wed, 19 Jul 2017 15:57:13 +0200
changeset 119 8ff8e2aee0f9
parent 85 e17899ced2b8
child 120 892980a3af09
permissions -rw-r--r--
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
"""
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
Tests the core api for sessions
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
"""
119
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
     4
import datetime
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
import logging
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
from uuid import uuid4
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
from django.contrib.auth import get_user_model
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
from django.urls import reverse
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
from django.utils import timezone
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
from rest_framework import status
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
from rest_framework.test import APITransactionTestCase
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
from notes.models import Session, Note
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
logger = logging.getLogger(__name__)
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
85
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    18
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
class NoteApiTests(APITransactionTestCase):
119
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    20
    '''
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    21
    Test Note api
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    22
    '''
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    def setUp(self):
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
        User = get_user_model()
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
        user1 = User.objects.create_user(
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
            username='test_user1',
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
            email='test_user@emial.com',
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
            password='top_secret'
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        )
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
        user2 = User.objects.create_user(
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
            username='test_user2',
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
            email='test_user@emial.com',
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
            password='top_secret'
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        )
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        user3 = User.objects.create_user(
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
            username='test_user3',
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
            email='test_user@emial.com',
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
            password='top_secret'
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        )
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        self.session1 = Session.objects.create(
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
            title="a new session 1",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
            description="Description 1",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
            protocol="[]",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
            owner=user1
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        )
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        self.session2 = Session.objects.create(
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
            title="a new session 2",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
            description="Description 2",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
            protocol="[]",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            owner=user2
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        )
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        Session.objects.create(
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
            title="a new session 3",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
            description="Description 3",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
            protocol="[]",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
            owner=user3
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        )
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
85
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    63
        self.note1 = Note.objects.create(
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
            tc_start=timezone.now(),
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
            tc_end=timezone.now(),
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
            session=self.session1,
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
            plain="example note 1",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
            html="<i>example note 1</i>",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
            raw="<i>example note 1</i>",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
            margin_note="margin note 1",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
            categorization="[]"
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        )
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
119
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    74
        self.note2 = Note.objects.create(
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    75
            tc_start=timezone.now(),
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    76
            tc_end=timezone.now(),
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    77
            session=self.session1,
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    78
            plain="example note 1.1",
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    79
            html="<i>example note 1,1</i>",
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    80
            raw="<i>example note 1.1</i>",
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    81
            margin_note="margin note 1.1",
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    82
            categorization="[]"
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    83
        )
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    84
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    85
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
        Note.objects.create(
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
            tc_start=timezone.now(),
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
            tc_end=timezone.now(),
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
            session=self.session2,
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
            plain="example note 2",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
            html="<i>example note</i>",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
            raw="<i>example note</i>",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
            margin_note="margin note",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
            categorization="[]"
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
        )
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
    def test_create_note_no_user(self):
119
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    98
        url = reverse('notes:notes-session-list',
85
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    99
                      kwargs={'session_ext_id': self.session1.ext_id})
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
        response = self.client.post(url, {
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
            'tc_start': timezone.now(),
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
            'tc_end': timezone.now(),
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
            'plain': "example note 2",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
            'html': "<i>example note</i>",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
            'raw': "<i>example note</i>",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
            'margin_note': "margin note",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
            'categorization': "[]"
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
        }, format='json')
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
    def test_create_note(self):
119
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   113
        url = reverse('notes:notes-session-list',
85
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   114
                      kwargs={'session_ext_id': self.session1.ext_id})
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
        self.client.login(username='test_user1', password='top_secret')
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
        response = self.client.post(url, {
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
            'tc_start': timezone.now(),
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
            'tc_end': timezone.now(),
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
            'plain': "example note 2",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
            'html': "<i>example note</i>",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
            'raw': "<i>example note</i>",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
            'margin_note': "margin note",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
            'categorization': "[]"
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
        }, format='json')
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
        json = response.json()
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
        self.assertIn('ext_id', json)
85
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   129
        note = Note.objects.get(
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   130
            ext_id=json['ext_id'], session__id=self.session1.id)
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
        self.assertTrue(note)
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
    def test_create_note_with_ext_id(self):
119
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   134
        url = reverse('notes:notes-session-list',
85
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   135
                      kwargs={'session_ext_id': self.session1.ext_id})
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
        self.client.login(username='test_user1', password='top_secret')
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
        ext_id = str(uuid4())
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
        response = self.client.post(url, {
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
            'ext_id': ext_id,
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
            'tc_start': timezone.now(),
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
            'tc_end': timezone.now(),
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
            'plain': "example note 2",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
            'html': "<i>example note</i>",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
            'raw': "<i>example note</i>",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
            'margin_note': "margin note",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
            'categorization': "[]"
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
        }, format='json')
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
        json = response.json()
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
        self.assertIn('ext_id', json)
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
        self.assertEqual(json['ext_id'], ext_id)
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
        note = Note.objects.get(ext_id=ext_id)
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
        self.assertTrue(note)
85
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   155
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   156
    def test_update_note(self):
119
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   157
        url = reverse('notes:notes-session-detail',
85
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   158
                      kwargs={'session_ext_id': self.session1.ext_id, 'ext_id': self.note1.ext_id})
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   159
        self.client.login(username='test_user1', password='top_secret')
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   160
        response = self.client.put(url, {
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   161
            'plain': "example note 1 modified",
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   162
            'html': "<i>example note modified</i>",
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   163
            'raw': "<i>example note modified</i>",
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   164
            'margin_note': "margin note",
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   165
            'categorization': "[]"
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   166
        }, format='json')
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   167
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   168
        self.assertEqual(response.status_code, status.HTTP_200_OK)
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   169
        json = response.json()
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   170
        self.assertIn('plain', json)
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   171
        self.assertEqual("example note 1 modified", json['plain'])
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   172
        note = Note.objects.get(
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   173
            ext_id=json['ext_id'], session__id=self.session1.id)
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   174
        self.assertTrue(note)
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   175
        self.assertEqual("example note 1 modified", note.plain)
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   176
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   177
    #TODO: Fail if a tc_start, tc_end, session, ext_id updated, created are provided on update ?
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   178
    # def test_update_note_tc_start(self):
119
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   179
    #     url = reverse('notes:notes-session-detail',
85
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   180
    #                   kwargs={'session_ext_id': self.session1.ext_id, 'ext_id': self.note1.ext_id})
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   181
    #     self.client.login(username='test_user1', password='top_secret')
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   182
    #     response = self.client.put(url, {
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   183
    #         'tc_start': timezone.now(),
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   184
    #         'tc_end': timezone.now(),
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   185
    #         'plain': "example note 1 modified",
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   186
    #         'html': "<i>example note modified</i>",
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   187
    #         'raw': "<i>example note modified</i>",
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   188
    #         'margin_note': "margin note",
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   189
    #         'categorization': "[]"
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   190
    #     }, format='json')
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   191
e17899ced2b8 add a test for updating nodes
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   192
    #     self.assertEqual(response.status_code, status.HTTP_200_OK)
119
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   193
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   194
    def test_root_note_list(self):
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   195
        url = reverse('notes:note-list')
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   196
        self.client.login(username='test_user1', password='top_secret')
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   197
        response = self.client.get(url, format='json')
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   198
        self.assertEqual(response.status_code, status.HTTP_200_OK)
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   199
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   200
        json_resp = response.json()
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   201
        self.assertIn('results', json_resp)
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   202
        self.assertEqual(2, json_resp['count'])
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   203
        self.assertEqual(2, len(json_resp['results']))
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   204
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   205
        for note_json in json_resp['results']:
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   206
            self.assertEqual(str(self.session1.ext_id), note_json.get('session'))
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   207
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   208
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   209
    def test_root_note_list_modified(self):
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   210
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   211
        nexthour = \
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   212
            datetime.datetime.utcnow().replace(tzinfo=timezone.utc) +\
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   213
            datetime.timedelta(hours=1)
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   214
        Note.objects.filter(pk=self.note2.id).update(updated=nexthour)
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   215
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   216
        url = reverse('notes:note-list')
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   217
        self.client.login(username='test_user1', password='top_secret')
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   218
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   219
        response = self.client.get(
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   220
            url,
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   221
            {'modified_since': (nexthour - datetime.timedelta(minutes=30)).timestamp()},
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   222
            format='json'
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   223
        )
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   224
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   225
        self.assertEqual(response.status_code, status.HTTP_200_OK)
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   226
        json_resp = response.json()
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   227
        self.assertIn('results', json_resp)
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   228
        self.assertEqual(1, json_resp['count'])
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   229
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   230
        self.assertEqual(len(json_resp['results']), 1, "must have one note")
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   231
        self.assertEqual(str(self.session1.ext_id), json_resp['results'][0].get('session'))
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   232
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   233
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   234
    def test_root_note_detail(self):
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   235
        url = reverse('notes:note-detail', kwargs={'ext_id': self.note2.ext_id})
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   236
        self.client.login(username='test_user1', password='top_secret')
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   237
        response = self.client.get(url, format='json')
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   238
        self.assertEqual(response.status_code, status.HTTP_200_OK)
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   239
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   240
        json_resp = response.json()
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   241
        self.assertEqual(str(self.session1.ext_id), json_resp.get('session'))
8ff8e2aee0f9 add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   242
        self.assertEqual('example note 1.1', json_resp.get('plain'))