src/notes/tests/api/session.py
author ymh <ymh.work@gmail.com>
Tue, 25 Jul 2017 19:11:26 +0200
changeset 128 34a75bd8d0b9
parent 119 8ff8e2aee0f9
child 131 adad5563603c
permissions -rw-r--r--
add filter on session and node list to recover specific objects
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
"""
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
Tests the core api for sessions
63be3ce389f7 improve api
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: 74
diff changeset
     4
import datetime
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
import logging
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
     6
from uuid import uuid4
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
from django.contrib.auth import get_user_model
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
from django.urls import reverse
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
from django.utils import timezone
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
from rest_framework import status
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
from rest_framework.test import APITransactionTestCase
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
from notes.models import Session, Note
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
logger = logging.getLogger(__name__)
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
class SessionApiTests(APITransactionTestCase):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    def setUp(self):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
        User = get_user_model()
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
        user1 = User.objects.create_user(
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
            username='test_user1',
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
            email='test_user@emial.com',
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
            password='top_secret'
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
        user2 = User.objects.create_user(
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
            username='test_user2',
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
            email='test_user@emial.com',
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
            password='top_secret'
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        user3 = User.objects.create_user(
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
            username='test_user3',
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
            email='test_user@emial.com',
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
            password='top_secret'
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
        self.session1 = Session.objects.create(
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
            title="a new session 1",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
            description="Description 1",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
            protocol="[]",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
            owner=user1
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        self.session2 = Session.objects.create(
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
            title="a new session 2",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
            description="Description 2",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
            protocol="[]",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
            owner=user2
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
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: 74
diff changeset
    52
        self.session3 = Session.objects.create(
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            title="a new session 3",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
            description="Description 3",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
            protocol="[]",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
            owner=user3
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
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: 74
diff changeset
    59
        self.session4 = Session.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: 74
diff changeset
    60
            title="a new session 4",
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: 74
diff changeset
    61
            description="Description 4",
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: 74
diff changeset
    62
            protocol="[]",
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: 74
diff changeset
    63
            owner=user3
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: 74
diff changeset
    64
        )
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: 74
diff changeset
    65
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
        Note.objects.create(
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
            tc_start=timezone.now(),
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
            tc_end=timezone.now(),
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
            session=self.session1,
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
            plain="example note 1",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
            html="<i>example note 1</i>",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
            raw="<i>example note 1</i>",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
            margin_note="margin note 1",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
            categorization="[]"
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        Note.objects.create(
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
            tc_start=timezone.now(),
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
            tc_end=timezone.now(),
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
            session=self.session2,
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
            plain="example note 2",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
            html="<i>example note</i>",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
            raw="<i>example note</i>",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
            margin_note="margin note",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
            categorization="[]"
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
    def test_list_session_no_user(self):
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
    90
        url = reverse('notes:session-list')
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
        response = self.client.post(url)
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
    92
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
    def test_list_session(self):
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
    96
        url = reverse('notes:session-list')
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
        self.client.login(username='test_user1', password='top_secret')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
        response = self.client.get(url)
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
        self.assertEqual(response.status_code, status.HTTP_200_OK)
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
        json = response.json()
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: 74
diff changeset
   101
        self.assertIn('results', json, "must have 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: 74
diff changeset
   102
        self.assertIn('count', json, "must have 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: 74
diff changeset
   103
        self.assertEqual(json['count'], 1, "must have one 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: 74
diff changeset
   104
        self.assertEqual(len(json['results']), 1, "must have one 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: 74
diff changeset
   105
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: 74
diff changeset
   106
        for session in json['results']:
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
            self.assertEqual(session['owner'], 'test_user1')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
128
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   110
    def test_list_session_filter(self):
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   111
        url = reverse('notes:session-list')
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   112
        self.client.login(username='test_user1', password='top_secret')
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   113
        response = self.client.get(url, {"ext_id__in": ",".join([str(self.session1.ext_id)])})
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   114
        self.assertEqual(response.status_code, status.HTTP_200_OK)
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   115
        json = response.json()
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   116
        self.assertIn('results', json, "must have results")
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   117
        self.assertIn('count', json, "must have count")
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   118
        self.assertEqual(json['count'], 1, "must have one session")
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   119
        self.assertEqual(len(json['results']), 1, "must have one session")
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   120
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   121
        for session in json['results']:
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   122
            self.assertEqual(session['owner'], 'test_user1')
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   123
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   124
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   125
    def test_list_session_filter_bad(self):
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   126
        url = reverse('notes:session-list')
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   127
        self.client.login(username='test_user1', password='top_secret')
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   128
        response = self.client.get(url, {"ext_id__in": ",".join([str(uuid4())])})
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   129
        self.assertEqual(response.status_code, status.HTTP_200_OK)
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   130
        json = response.json()
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   131
        self.assertIn('results', json, "must have results")
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   132
        self.assertIn('count', json, "must have count")
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   133
        self.assertEqual(json['count'], 0, "must have no session")
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   134
        self.assertEqual(len(json['results']), 0, "must have no session")
34a75bd8d0b9 add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
   135
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
    def test_create_session_no_user(self):
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   137
        url = reverse('notes:session-list')
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
        response = self.client.post(url, {
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
            'title': "a new session",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
            'description': "description of the session",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
            'protocol': "[]"
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
        }, format='json')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   144
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
    def test_create_session(self):
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   148
        url = reverse('notes:session-list')
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
        self.client.login(username='test_user1', password='top_secret')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
        response = self.client.post(url, {
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
            'title': "a new session",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
            'description': "description of the session",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
            'protocol': "[]"
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
        }, format='json')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
        json = response.json()
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
        self.assertIn('ext_id', json)
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   161
    def test_create_session_with_ext_id(self):
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   162
        url = reverse('notes:session-list')
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   163
        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: 31
diff changeset
   164
        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: 31
diff changeset
   165
        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: 31
diff changeset
   166
            '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: 31
diff changeset
   167
            'title': "a new session",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   168
            'description': "description of the session",
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   169
            'protocol': "[]"
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   170
        }, 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: 31
diff changeset
   171
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   172
        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: 31
diff changeset
   173
        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: 31
diff changeset
   174
        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: 31
diff changeset
   175
        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: 31
diff changeset
   176
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   177
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
    def test_detail_session(self):
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   179
        url = reverse('notes:session-detail', kwargs={'ext_id':str(self.session1.ext_id)})
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
        self.client.login(username='test_user1', password='top_secret')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
        response = self.client.get(url, format='json')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
        self.assertEqual(response.status_code, status.HTTP_200_OK)
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
    def test_list_notes(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: 74
diff changeset
   185
        url = reverse('notes:notes-session-list', kwargs={'session_ext_id':str(self.session1.ext_id)})
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
        self.client.login(username='test_user1', password='top_secret')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
        response = self.client.get(url, format='json')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
        self.assertEqual(response.status_code, status.HTTP_200_OK)
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
    def test_detail_session_bad(self):
74
043477fd5c5c add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   191
        url = reverse('notes:session-detail', kwargs={'ext_id':str(self.session2.ext_id)})
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
        self.client.login(username='test_user1', password='top_secret')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
        response = self.client.get(url, format='json')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
    def test_list_notes_bad(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: 74
diff changeset
   197
        url = reverse('notes:notes-session-list', kwargs={'session_ext_id':str(self.session2.ext_id)})
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
        self.client.login(username='test_user1', password='top_secret')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
        response = self.client.get(url, format='json')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
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: 74
diff changeset
   201
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: 74
diff changeset
   202
    def test_filter_modified_since(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: 74
diff changeset
   203
        url = reverse('notes:session-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: 74
diff changeset
   204
        self.client.login(username='test_user3', 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: 74
diff changeset
   205
        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: 74
diff changeset
   206
            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: 74
diff changeset
   207
            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: 74
diff changeset
   208
        Session.objects.filter(pk=self.session4.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: 74
diff changeset
   209
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: 74
diff changeset
   210
        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: 74
diff changeset
   211
            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: 74
diff changeset
   212
            {'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: 74
diff changeset
   213
            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: 74
diff changeset
   214
        )
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: 74
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: 74
diff changeset
   216
        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: 74
diff changeset
   217
        json = 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: 74
diff changeset
   218
        self.assertIn('results', json, "must have 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: 74
diff changeset
   219
        self.assertIn('count', json, "must have 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: 74
diff changeset
   220
        self.assertEqual(json['count'], 1, "must have one 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: 74
diff changeset
   221
        self.assertEqual(len(json['results']), 1, "must have one 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: 74
diff changeset
   222
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: 74
diff changeset
   223
        self.assertEqual(json['results'][0].get('title'), "a new session 4")
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: 74
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: 74
diff changeset
   225
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: 74
diff changeset
   226
    def test_filter_modified_since_zero(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: 74
diff changeset
   227
        url = reverse('notes:session-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: 74
diff changeset
   228
        self.client.login(username='test_user3', 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: 74
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: 74
diff changeset
   230
        response = self.client.get(url, {'modified_since': 0}, 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: 74
diff changeset
   231
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: 74
diff changeset
   232
        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: 74
diff changeset
   233
        json = 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: 74
diff changeset
   234
        self.assertIn('results', json, "must have 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: 74
diff changeset
   235
        self.assertIn('count', json, "must have 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: 74
diff changeset
   236
        self.assertEqual(json['count'], 2, "must have two sessions")
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: 74
diff changeset
   237
        self.assertEqual(len(json['results']), 2, "must have two sessions")
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: 74
diff changeset
   238
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: 74
diff changeset
   239
        for session_json in json['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: 74
diff changeset
   240
            self.assertIn(session_json.get('title'), ['a new session 3', 'a new session 4'])
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: 74
diff changeset
   241
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: 74
diff changeset
   242
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: 74
diff changeset
   243
    def test_filter_modified_seconds(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: 74
diff changeset
   244
        url = reverse('notes:session-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: 74
diff changeset
   245
        self.client.login(username='test_user3', 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: 74
diff changeset
   246
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: 74
diff changeset
   247
        prevmoment = \
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: 74
diff changeset
   248
            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: 74
diff changeset
   249
            datetime.timedelta(seconds=5)
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: 74
diff changeset
   250
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: 74
diff changeset
   251
        response = self.client.get(url, {'modified_since': prevmoment.timestamp()}, 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: 74
diff changeset
   252
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: 74
diff changeset
   253
        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: 74
diff changeset
   254
        json = 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: 74
diff changeset
   255
        self.assertIn('results', json, "must have 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: 74
diff changeset
   256
        self.assertIn('count', json, "must have 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: 74
diff changeset
   257
        self.assertEqual(json['count'], 2, "must have two sessions")
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: 74
diff changeset
   258
        self.assertEqual(len(json['results']), 2, "must have two sessions")
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: 74
diff changeset
   259
        for session_json in json['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: 74
diff changeset
   260
            self.assertIn(session_json.get('title'), ['a new session 3', 'a new session 4'])
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: 74
diff changeset
   261