diff options
Diffstat (limited to 'apps/notes/tests/test_views.py')
-rw-r--r-- | apps/notes/tests/test_views.py | 48 |
1 files changed, 33 insertions, 15 deletions
diff --git a/apps/notes/tests/test_views.py b/apps/notes/tests/test_views.py index 4f9a4ce..be48226 100644 --- a/apps/notes/tests/test_views.py +++ b/apps/notes/tests/test_views.py @@ -1,6 +1,6 @@ import json from django.test import Client -from django.test import RequestFactory, TestCase +from django.test import RequestFactory, TestCase, TransactionTestCase from django.urls import reverse from django.template.defaultfilters import slugify @@ -14,6 +14,37 @@ 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. @@ -35,25 +66,12 @@ class NotesViewsTest(TestCase): self.note.save() def test_list_view(self): - request = self.factory.get('/notes/') + request = self.factory.get('/n/') request.user = self.user response = NoteListView.as_view()(request) self.assertEqual(response.status_code, 200) # bad_user - def test_note_create_view(self): - data = { - '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:note-create") - response = self.client.post(url, data) - self.assertEqual(response.status_code, 302) - self.assertRedirects(response, '/notes/%s/%s' % (self.user.username, slugify(data['title']))) - def test_api_list(self): # Make an authenticated request to the view... request = self.factory.get('/api/notes/') |