aboutsummaryrefslogtreecommitdiff
path: root/apps/notes/tests/test_views.py
diff options
context:
space:
mode:
Diffstat (limited to 'apps/notes/tests/test_views.py')
-rw-r--r--apps/notes/tests/test_views.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/apps/notes/tests/test_views.py b/apps/notes/tests/test_views.py
new file mode 100644
index 0000000..3f21b0b
--- /dev/null
+++ b/apps/notes/tests/test_views.py
@@ -0,0 +1,57 @@
+import json
+from django.test import Client
+from django.test import RequestFactory, TestCase
+
+from rest_framework.test import force_authenticate
+from rest_framework.test import APIRequestFactory
+from mixer.backend.django import mixer
+
+from accounts.models import User
+from notes.models import Note
+from notes.views import NoteListView, NoteViewSet
+
+
+class StoriesViewTest(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.user = mixer.blend(User, username='tpynchon', password="gravity")
+ self.note = Note.objects.create(
+ created_by=self.user,
+ title="test note",
+ body_markdown="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('/%s/notes/' % (self.user.username))
+ request.user = self.user
+ response = NoteListView.as_view()(request)
+ self.assertEqual(response.status_code, 200)
+ response.render()
+
+ 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_(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'])