aboutsummaryrefslogtreecommitdiff
path: root/apps/notes/tests
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2018-11-19 11:51:46 -0600
committerluxagraf <sng@luxagraf.net>2018-11-19 11:51:46 -0600
commit2ef6414abec4e606d0fd96babc849cc2bde2bb38 (patch)
treedf54a3a0e6d8f1f86a1530394f67087eb97ac34f /apps/notes/tests
parenta0b95dc2dfb84c682bb8f677e5d471f84e5028fe (diff)
Updated notes to default to private and changed body_markdown to body
Diffstat (limited to 'apps/notes/tests')
-rw-r--r--apps/notes/tests/test_models.py36
-rw-r--r--apps/notes/tests/test_views.py58
2 files changed, 69 insertions, 25 deletions
diff --git a/apps/notes/tests/test_models.py b/apps/notes/tests/test_models.py
index e6cdb6a..ddb731e 100644
--- a/apps/notes/tests/test_models.py
+++ b/apps/notes/tests/test_models.py
@@ -14,17 +14,31 @@ class FolderModelTest(TestCase):
class NoteModelTest(TestCase):
-
- def test_string_representation(self):
- user = mixer.blend(User, username='test')
- note = Note(
- created_by=user,
+ def setUp(self):
+ self.user = mixer.blend(User, username='test')
+ self.note = Note.objects.create(
+ created_by=self.user,
title="test note",
- body_markdown="the body of the note",
+ body="the body of the note",
+ url="https://luxagraf.net/",
+ tags="mine,cool site"
+ )
+ self.note.save()
+ self.note_no_title = Note.objects.create(
+ created_by=self.user,
+ body="the body of the note",
url="https://luxagraf.net/",
tags="mine,cool site"
- )
- self.assertEqual(str(note), "test note")
- self.assertEqual(str(note.body_markdown), "the body of the note")
- self.assertEqual(str(note.url), "https://luxagraf.net/")
- self.assertEqual(str(note.tags), "mine,cool site")
+ )
+ self.note_no_title.save()
+
+ def test_string_representation(self):
+ self.assertEqual(str(self.note), "test note")
+ self.assertEqual(str(self.note.body), "the body of the note")
+ self.assertEqual(str(self.note.url), "https://luxagraf.net/")
+ self.assertEqual(str(self.note.tags), "mine,cool site")
+ # titleless note gets date
+ self.assertEqual(str(self.note_no_title), str(self.note_no_title.date_created))
+
+ def test_get_absolute_url(self):
+ self.assertEqual(str(self.note.get_absolute_url()), "/notes/%s/" % (self.note.id))
diff --git a/apps/notes/tests/test_views.py b/apps/notes/tests/test_views.py
index 3f21b0b..05cb8db 100644
--- a/apps/notes/tests/test_views.py
+++ b/apps/notes/tests/test_views.py
@@ -1,9 +1,11 @@
import json
from django.test import Client
from django.test import RequestFactory, TestCase
+from django.urls import reverse
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
@@ -11,28 +13,32 @@ from notes.models import Note
from notes.views import NoteListView, NoteViewSet
-class StoriesViewTest(TestCase):
+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.user = mixer.blend(User, username='tpynchon', password="gravity")
+ 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(
created_by=self.user,
title="test note",
- body_markdown="the body of the note",
+ body="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 = self.factory.get('/notes/')
request.user = self.user
response = NoteListView.as_view()(request)
self.assertEqual(response.status_code, 200)
- response.render()
+ # bad_user
def test_api_list(self):
# Make an authenticated request to the view...
@@ -45,13 +51,37 @@ class StoriesViewTest(TestCase):
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)
+ def test_note_create(self):
+ '''
+ post some data to create a new note
+ '''
+ data = {
+ 'title': "test note post",
+ 'body': "the body of the note",
+ 'url': "https://luxagraf.net/",
+ 'tags': [],
+ }
+ self.apiclient.force_authenticate(self.user)
+ url = reverse("notes:notes-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'))[0]
- self.assertEqual(api_data['title'], 'test note')
- self.assertEqual(api_data['tags'], ['mine,cool site'])
+ api_data = json.loads(response.content.decode('utf8'))
+ self.assertEqual(api_data['title'], 'test note post')
+ self.assertEqual(api_data['body'], 'the body of the note')
+ self.assertEqual(api_data['tags'], [])
+
+ def test_note_create_bad(self):
+ # create another user
+ data = {
+ 'title': "",
+ 'body': "the body of the note",
+ 'url': "https://luxagraf.net/",
+ 'tags': [],
+ }
+ url = reverse("notes:notes-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)