From cae9fa96be30d5878d042cc740499d7282d07573 Mon Sep 17 00:00:00 2001 From: Krzysztof Klimonda Date: Sun, 3 Mar 2013 10:09:33 -0800 Subject: [PATCH] Write some more tests for all code --- pinry/core/tests/__init__.py | 3 ++- pinry/core/tests/views.py | 37 +++++++++++++++++++++++++++++++++++++ pinry/users/tests.py | 31 ++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 pinry/core/tests/views.py diff --git a/pinry/core/tests/__init__.py b/pinry/core/tests/__init__.py index f79a1a4..42ac7b9 100644 --- a/pinry/core/tests/__init__.py +++ b/pinry/core/tests/__init__.py @@ -1,2 +1,3 @@ from .api import * -from .forms import * \ No newline at end of file +from .forms import * +from .views import * \ No newline at end of file diff --git a/pinry/core/tests/views.py b/pinry/core/tests/views.py new file mode 100644 index 0000000..d5d110c --- /dev/null +++ b/pinry/core/tests/views.py @@ -0,0 +1,37 @@ +from django.conf import settings +from django.core.urlresolvers import reverse +from django.template import TemplateDoesNotExist +from django.test import TestCase + +from ...core.models import Image + + +__all__ = ['CreateImageTest'] + + +class CreateImageTest(TestCase): + fixtures = ['test_resources.json'] + + def setUp(self): + self.client.login(username='jdoe', password='password') + + + def test_get_browser(self): + response = self.client.get(reverse('core:create-image')) + self.assertRedirects(response, reverse('core:recent-pins')) + + def test_get_xml_http_request(self): + with self.assertRaises(TemplateDoesNotExist): + self.client.get(reverse('core:create-image'), HTTP_X_REQUESTED_WITH='XMLHttpRequest') + + def test_post(self): + with open(settings.SITE_ROOT + 'screenshot.png', mode='rb') as image: + response = self.client.post(reverse('core:create-image'), {'qqfile': image}) + image = Image.objects.latest('pk') + self.assertJSONEqual(response.content, {'success': {'id': image.pk}}) + + def test_post_error(self): + response = self.client.post(reverse('core:create-image'), {'qqfile': None}) + self.assertJSONEqual(response.content, { + 'error': {'image': ['This field is required.']} + }) diff --git a/pinry/users/tests.py b/pinry/users/tests.py index a91f604..50c70a3 100644 --- a/pinry/users/tests.py +++ b/pinry/users/tests.py @@ -1,4 +1,6 @@ +from django.core.urlresolvers import reverse from django.test import TestCase +from django.test.utils import override_settings import mock @@ -44,4 +46,31 @@ class CombinedAuthBackendTest(TestCase): user = User.objects.get(pk=1) other_user = User.objects.create_user('test', 'test@example.com', 'test') pin = Pin.objects.create(submitter=user, image=image) - self.assertFalse(self.backend.has_perm(other_user, 'add_pin', pin)) \ No newline at end of file + self.assertFalse(self.backend.has_perm(other_user, 'add_pin', pin)) + + +class CreateUserTest(TestCase): + def test_create_post(self): + data = { + 'username': 'jdoe', + 'email': 'jdoe@example.com', + 'password': 'password' + } + response = self.client.post(reverse('users:register'), data=data) + self.assertRedirects(response, reverse('core:recent-pins')) + self.assertIn('_auth_user_id', self.client.session) + + @override_settings(ALLOW_NEW_REGISTRATIONS=False) + def test_create_post_not_allowed(self): + response = self.client.get(reverse('users:register')) + self.assertRedirects(response, reverse('core:recent-pins')) + + +class LogoutViewTest(TestCase): + def setUp(self): + User.objects.create_user(username='jdoe', password='password') + self.client.login(username='jdoe', password='password') + + def test_logout_view(self): + response = self.client.get(reverse('users:logout')) + self.assertRedirects(response, reverse('core:recent-pins')) \ No newline at end of file