瀏覽代碼

Write some more tests for all code

tags/v1.0.0
Krzysztof Klimonda 11 年之前
父節點
當前提交
cae9fa96be
共有 3 個文件被更改,包括 69 次插入2 次删除
  1. +2
    -1
      pinry/core/tests/__init__.py
  2. +37
    -0
      pinry/core/tests/views.py
  3. +30
    -1
      pinry/users/tests.py

+ 2
- 1
pinry/core/tests/__init__.py 查看文件

@@ -1,2 +1,3 @@
from .api import *
from .forms import *
from .forms import *
from .views import *

+ 37
- 0
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.']}
})

+ 30
- 1
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))
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'))

Loading…
取消
儲存