src/notes/tests/api/session.py
author ymh <ymh.work@gmail.com>
Wed, 19 Jul 2017 15:57:13 +0200
changeset 119 8ff8e2aee0f9
parent 74 043477fd5c5c
child 128 34a75bd8d0b9
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:
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
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
    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
   111
        url = reverse('notes:session-list')
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
        response = self.client.post(url, {
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
            'title': "a new session",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
            'description': "description of the session",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
            'protocol': "[]"
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
        }, format='json')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
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
   118
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
    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
   122
        url = reverse('notes:session-list')
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
        self.client.login(username='test_user1', password='top_secret')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
        response = self.client.post(url, {
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
            'title': "a new session",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
            'description': "description of the session",
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
            'protocol': "[]"
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
        }, format='json')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
        json = response.json()
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
        self.assertIn('ext_id', json)
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
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
   135
    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
   136
        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
   137
        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
   138
        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
   139
        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
   140
            '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
   141
            '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
   142
            '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
   143
            '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
   144
        }, 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
   145
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
   146
        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
   147
        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
   148
        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
   149
        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
   150
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
   151
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
    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
   153
        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
   154
        self.client.login(username='test_user1', password='top_secret')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
        response = self.client.get(url, format='json')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
        self.assertEqual(response.status_code, status.HTTP_200_OK)
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
    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
   159
        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
   160
        self.client.login(username='test_user1', password='top_secret')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
        response = self.client.get(url, format='json')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
        self.assertEqual(response.status_code, status.HTTP_200_OK)
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
    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
   165
        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
   166
        self.client.login(username='test_user1', password='top_secret')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
        response = self.client.get(url, format='json')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
    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
   171
        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
   172
        self.client.login(username='test_user1', password='top_secret')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
        response = self.client.get(url, format='json')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
        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
   175
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
   176
    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
   177
        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
   178
        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
   179
        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
   180
            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
   181
            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
   182
        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
   183
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
   184
        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
   185
            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
   186
            {'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
   187
            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
   188
        )
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
   189
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
   190
        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
   191
        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
   192
        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
   193
        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
   194
        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
   195
        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
   196
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
        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
   198
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
   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: 74
diff changeset
   200
    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
   201
        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
   202
        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
   203
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
        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
   205
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
        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
   207
        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
   208
        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
   209
        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
   210
        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
   211
        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
   212
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
        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
   214
            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
   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
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
    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
   218
        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
   219
        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
   220
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
        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
   222
            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
   223
            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
   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
        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
   226
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
        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
   228
        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
   229
        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
   230
        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
   231
        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
   232
        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
   233
        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
   234
            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
   235