src/notes/tests/api/note.py
author ymh <ymh.work@gmail.com>
Thu, 22 Jun 2017 12:09:48 +0200
changeset 74 043477fd5c5c
child 85 e17899ced2b8
permissions -rw-r--r--
add api call to save notes. internally use ts for time data for notes and session
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
"""
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
     4
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
     5
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
     6
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
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
     8
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
     9
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
    10
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
    11
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
    12
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
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
    14
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
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
    16
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
class NoteApiTests(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
    18
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
    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
    20
        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
    21
        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
    22
            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
    23
            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
    24
            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
    25
        )
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
        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
    27
            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
    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
        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
    32
            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
    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
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
        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
    38
            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
    39
            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
    40
            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
    41
            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
    42
        )
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
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
        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
    45
            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
    46
            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
    47
            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
    48
            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
    49
        )
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
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
        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
    52
            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
    53
            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
    54
            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
    55
            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
    56
        )
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
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
        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
    59
            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
    60
            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
    61
            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
    62
            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
    63
            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
    64
            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
    65
            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
    66
            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
    67
        )
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
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
        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
    70
            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
    71
            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
    72
            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
    73
            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
    74
            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
    75
            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
    76
            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
    77
            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
    78
        )
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
    79
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
    80
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
    81
    def test_create_note_no_user(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
    82
        url = reverse('notes:notes-list', kwargs={'session_ext_id': self.session1.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
    83
        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
    84
            '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
    85
            '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
    86
            '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
    87
            '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
    88
            '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
    89
            '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
    90
            '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
    91
        }, 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
    92
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
        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
    94
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
    def test_create_note(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
    97
        url = reverse('notes:notes-list', kwargs={'session_ext_id': self.session1.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
    98
        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
    99
        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
   100
            '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
   101
            '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
   102
            '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
   103
            '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
   104
            '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
   105
            '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
   106
            '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
   107
        }, 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
   108
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
        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
   110
        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
   111
        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
   112
        note = Note.objects.get(ext_id=json['ext_id'], session__id=self.session1.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
   113
        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
   114
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
    def test_create_note_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:
diff changeset
   116
        url = reverse('notes:notes-list', kwargs={'session_ext_id': self.session1.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
   117
        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
   118
        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
   119
        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
   120
            '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
   121
            '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
   122
            '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
   123
            '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
   124
            '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
   125
            '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
   126
            '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
   127
            '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
   128
        }, 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
   129
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
   130
        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
   131
        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
   132
        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
   133
        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
   134
        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
   135
        self.assertTrue(note)