# HG changeset patch # User neko259 # Date 2014-01-20 10:36:59 # Node ID 9c27ae2e81a80d569395650bdf373ff79f912e38 # Parent 5f217adc9602c8e86c0d7ea8e1b5416facfdaad5 Added test for all views get requests. Fixed banned view failing this test diff --git a/boards/tests.py b/boards/tests.py --- a/boards/tests.py +++ b/boards/tests.py @@ -1,9 +1,13 @@ # coding=utf-8 +import time +import logging + from django.test import TestCase from django.test.client import Client -import time +from django.core.urlresolvers import reverse, NoReverseMatch from boards.models import Post, Tag +from boards import urls from neboard import settings PAGE_404 = 'boards/404.html' @@ -18,6 +22,8 @@ HTTP_CODE_REDIRECT = 302 HTTP_CODE_OK = 200 HTTP_CODE_NOT_FOUND = 404 +logger = logging.getLogger(__name__) + class PostTests(TestCase): @@ -223,11 +229,29 @@ class FormTest(TestCase): class ViewTest(TestCase): - def test_index(self): + + def test_all_views(self): + ''' + Try opening all views defined in ulrs.py that don't need additional + parameters + ''' + client = Client() + for url in urls.urlpatterns: + try: + view_name = url.name + logger.debug('Testing view %s' % view_name) - response = client.get('/') - self.assertEqual(HTTP_CODE_OK, response.status_code, 'Index page not ' - 'opened') - self.assertEqual('boards/posting_general.html', response.templates[0] - .name, 'Index page should open posting_general template') + try: + response = client.get(reverse(view_name)) + + self.assertEqual(HTTP_CODE_OK, response.status_code, + '%s view not opened' % view_name) + except NoReverseMatch: + # This view just needs additional arguments + pass + except Exception, e: + self.fail('Got exception %s at %s view' % (e, view_name)) + except AttributeError: + # This is normal, some views do not have names + pass diff --git a/boards/urls.py b/boards/urls.py --- a/boards/urls.py +++ b/boards/urls.py @@ -49,7 +49,7 @@ urlpatterns = patterns('', url(r'^delete/(?P\w+)/$', views.delete, name='delete'), url(r'^ban/(?P\w+)/$', views.ban, name='ban'), - url(r'^banned/$', views.banned.BannedView.as_view, name='banned'), + url(r'^banned/$', views.banned.BannedView.as_view(), name='banned'), url(r'^staticpage/(?P\w+)/$', views.static_page, name='staticpage'), # RSS feeds diff --git a/boards/views/banned.py b/boards/views/banned.py --- a/boards/views/banned.py +++ b/boards/views/banned.py @@ -9,7 +9,7 @@ class BannedView(BaseBoardView): def get(self, request): """Show the page that notifies that user is banned""" - context = self.get_context_data(request) + context = self.get_context_data(request=request) ban = get_object_or_404(Ban, ip=utils.get_client_ip(request)) context['ban_reason'] = ban.reason