aboutsummaryrefslogtreecommitdiff
path: root/apps/notes/tests/test_views.py
blob: be48226387a9f20ae2f66ac4f604775b95e02b10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import json
from django.test import Client
from django.test import RequestFactory, TestCase, TransactionTestCase
from django.urls import reverse
from django.template.defaultfilters import slugify

from rest_framework.test import force_authenticate
from rest_framework.test import APIRequestFactory
from rest_framework.test import APIClient
from mixer.backend.django import mixer

from accounts.models import User
from notes.models import Note
from notes.views import NoteListView, NoteViewSet


class NoteCreateViewTests(TransactionTestCase):
    """
    Because I need pks to get URL of object I need
    a clean database so I know the one and only object
    has a pk of 1. There's probably a better way to do
    this though.
    Especially because this is a private API and subject
    to change/deletion:
    https://docs.djangoproject.com/en/2.1/topics/testing/advanced/#django.test.TransactionTestCase.reset_sequences
    """
    reset_sequences = True

    def setUp(self):
        self.client = Client()
        self.user = User.objects.create(username='testuser', password='password')

    def test_note_create_view(self):
        data = {
                'pk': 2,
                'title': "test note post",
                'body_text': "the body of the note",
                'url': "https://luxagraf.net/",
                'tags': [],
            }
        self.client.force_login(self.user)
        url = reverse("notes:create")
        response = self.client.post(url, data)
        self.assertEqual(response.status_code, 302)
        self.assertRedirects(response, '/n/%s/%s' % (slugify(data['title']), Note.objects.count()))


class NotesViewsTest(TestCase):
    def setUp(self):
        # Every test needs access to the request factory.
        self.factory = RequestFactory()
        # test API with rest framework request factory.
        self.apifactory = APIRequestFactory()
        self.client = Client()
        # and test with rest client
        self.apiclient = APIClient()
        self.user = User.objects.create(username='testuser', password='password')
        self.bad_user = User.objects.create(username='someoneelse', password='password')
        self.note = Note.objects.create(
                owner=self.user,
                title="test note",
                body_text="the body of the note",
                url="https://luxagraf.net/",
                )
        self.note.tags.add("mine,cool site")
        self.note.save()

    def test_list_view(self):
        request = self.factory.get('/n/')
        request.user = self.user
        response = NoteListView.as_view()(request)
        self.assertEqual(response.status_code, 200)
        # bad_user

    def test_api_list(self):
        # Make an authenticated request to the view...
        request = self.factory.get('/api/notes/')
        force_authenticate(request, user=self.user)
        response = NoteViewSet.as_view({'get': 'list'})(request)
        self.assertEqual(response.status_code, 200)
        response.render()
        api_data = json.loads(response.content.decode('utf8'))[0]
        self.assertEqual(api_data['title'], 'test note')
        self.assertEqual(api_data['tags'], ['mine,cool site'])

    def test_api_note_create(self):
        '''
        Post some data to create a new note
        '''
        data = {
                'title': "test note post",
                'body_text': "the body of the note",
                'url': "https://luxagraf.net/",
                'tags': [],
            }
        self.apiclient.force_authenticate(self.user)
        url = reverse("notes-api-list")
        response = self.apiclient.post(url, data, format='json')
        self.assertEqual(response.status_code, 201)
        self.assertEqual(Note.objects.count(), 2)
        response.render()
        api_data = json.loads(response.content.decode('utf8'))
        self.assertEqual(api_data['title'], 'test note post')
        self.assertEqual(api_data['body_text'], 'the body of the note')
        self.assertEqual(api_data['tags'], [])

    def test_note_create_bad(self):
        # create another user
        data = {
                'title': "",
                'body_text': "the body of the note",
                'url': "https://luxagraf.net/",
                'tags': [],
            }
        url = reverse("notes-api-list")
        self.apiclient.force_authenticate(self.user)
        response = self.apiclient.post(url, data, format='json')
        self.assertEqual(response.status_code, 400)
        self.assertEqual(Note.objects.count(), 1)