##// END OF EJS Templates
404 page now returns the 404 status for real. Fixed some tests related to...
neko259 -
r890:dec155c1 default
parent child Browse files
Show More
@@ -1,56 +1,52 b''
1 from django.test import TestCase, Client
1 from django.test import TestCase, Client
2 from boards.models import Tag, Post
2 from boards.models import Tag, Post
3
3
4 TEST_TEXT = 'test'
4 TEST_TEXT = 'test'
5
5
6 NEW_THREAD_PAGE = '/'
6 NEW_THREAD_PAGE = '/'
7 THREAD_PAGE_ONE = '/thread/1/'
7 THREAD_PAGE_ONE = '/thread/1/'
8 THREAD_PAGE = '/thread/'
8 THREAD_PAGE = '/thread/'
9 TAG_PAGE = '/tag/'
9 TAG_PAGE = '/tag/'
10 HTTP_CODE_REDIRECT = 302
10 HTTP_CODE_REDIRECT = 301
11 HTTP_CODE_OK = 200
11 HTTP_CODE_OK = 200
12 HTTP_CODE_NOT_FOUND = 404
12 HTTP_CODE_NOT_FOUND = 404
13
13
14 PAGE_404 = 'boards/404.html'
15
16
14
17 class PagesTest(TestCase):
15 class PagesTest(TestCase):
18
16
19 def test_404(self):
17 def test_404(self):
20 """Test receiving error 404 when opening a non-existent page"""
18 """Test receiving error 404 when opening a non-existent page"""
21
19
22 tag_name = 'test_tag'
20 tag_name = 'test_tag'
23 tag = Tag.objects.create(name=tag_name)
21 tag = Tag.objects.create(name=tag_name)
24 client = Client()
22 client = Client()
25
23
26 Post.objects.create_post('title', TEST_TEXT, tags=[tag])
24 Post.objects.create_post('title', TEST_TEXT, tags=[tag])
27
25
28 existing_post_id = Post.objects.all()[0].id
26 existing_post_id = Post.objects.all()[0].id
29 response_existing = client.get(THREAD_PAGE + str(existing_post_id) +
27 response_existing = client.get(THREAD_PAGE + str(existing_post_id) +
30 '/')
28 '/')
31 self.assertEqual(HTTP_CODE_OK, response_existing.status_code,
29 self.assertEqual(HTTP_CODE_OK, response_existing.status_code,
32 'Cannot open existing thread')
30 'Cannot open existing thread')
33
31
34 response_not_existing = client.get(THREAD_PAGE + str(
32 response_not_existing = client.get(THREAD_PAGE + str(
35 existing_post_id + 1) + '/')
33 existing_post_id + 1) + '/')
36 self.assertEqual(PAGE_404, response_not_existing.templates[0].name,
34 self.assertEqual(HTTP_CODE_NOT_FOUND, response_not_existing.status_code,
37 'Not existing thread is opened')
35 'Not existing thread is opened')
38
36
39 response_existing = client.get(TAG_PAGE + tag_name + '/')
37 response_existing = client.get(TAG_PAGE + tag_name + '/')
40 self.assertEqual(HTTP_CODE_OK,
38 self.assertEqual(HTTP_CODE_OK,
41 response_existing.status_code,
39 response_existing.status_code,
42 'Cannot open existing tag')
40 'Cannot open existing tag')
43
41
44 response_not_existing = client.get(TAG_PAGE + 'not_tag' + '/')
42 response_not_existing = client.get(TAG_PAGE + 'not_tag' + '/')
45 self.assertEqual(PAGE_404,
43 self.assertEqual(HTTP_CODE_NOT_FOUND, response_not_existing.status_code,
46 response_not_existing.templates[0].name,
47 'Not existing tag is opened')
44 'Not existing tag is opened')
48
45
49 reply_id = Post.objects.create_post('', TEST_TEXT,
46 reply_id = Post.objects.create_post('', TEST_TEXT,
50 thread=Post.objects.all()[0]
47 thread=Post.objects.all()[0]
51 .get_thread())
48 .get_thread())
52 response_not_existing = client.get(THREAD_PAGE + str(
49 response_not_existing = client.get(THREAD_PAGE + str(
53 reply_id) + '/')
50 reply_id) + '/')
54 self.assertEqual(PAGE_404,
51 self.assertEqual(HTTP_CODE_REDIRECT, response_not_existing.status_code,
55 response_not_existing.templates[0].name,
56 'Reply is opened as a thread')
52 'Reply is opened as a thread')
@@ -1,38 +1,46 b''
1 import logging
1 import logging
2 from django.core.urlresolvers import reverse, NoReverseMatch
2 from django.core.urlresolvers import reverse, NoReverseMatch
3 from django.test import TestCase, Client
3 from django.test import TestCase, Client
4 from boards import urls
4 from boards import urls
5
5
6
6
7 logger = logging.getLogger(__name__)
7 logger = logging.getLogger(__name__)
8
8
9 HTTP_CODE_OK = 200
9 HTTP_CODE_OK = 200
10
10
11 EXCLUDED_VIEWS = {
12 'banned',
13 }
14
11
15
12 class ViewTest(TestCase):
16 class ViewTest(TestCase):
13
17
14 def test_all_views(self):
18 def test_all_views(self):
15 """
19 """
16 Try opening all views defined in ulrs.py that don't need additional
20 Try opening all views defined in ulrs.py that don't need additional
17 parameters
21 parameters
18 """
22 """
19
23
20 client = Client()
24 client = Client()
21 for url in urls.urlpatterns:
25 for url in urls.urlpatterns:
22 try:
26 try:
23 view_name = url.name
27 view_name = url.name
28 if view_name in EXCLUDED_VIEWS:
29 logger.debug('View {} is excluded.'.format(view_name))
30 continue
31
24 logger.debug('Testing view %s' % view_name)
32 logger.debug('Testing view %s' % view_name)
25
33
26 try:
34 try:
27 response = client.get(reverse(view_name))
35 response = client.get(reverse(view_name))
28
36
29 self.assertEqual(HTTP_CODE_OK, response.status_code,
37 self.assertEqual(HTTP_CODE_OK, response.status_code,
30 '%s view not opened' % view_name)
38 'View not opened: {}'.format(view_name))
31 except NoReverseMatch:
39 except NoReverseMatch:
32 # This view just needs additional arguments
40 # This view just needs additional arguments
33 pass
41 pass
34 except Exception as e:
42 except Exception as e:
35 self.fail('Got exception %s at %s view' % (e, view_name))
43 self.fail('Got exception %s at %s view' % (e, view_name))
36 except AttributeError:
44 except AttributeError:
37 # This is normal, some views do not have names
45 # This is normal, some views do not have names
38 pass
46 pass
@@ -1,15 +1,18 b''
1 from django.shortcuts import render
1 from django.shortcuts import render
2
2
3 from boards.views.base import BaseBoardView
3 from boards.views.base import BaseBoardView
4
4
5
5
6 class NotFoundView(BaseBoardView):
6 class NotFoundView(BaseBoardView):
7 """
7 """
8 Page 404 (not found)
8 Page 404 (not found)
9 """
9 """
10
10
11 def get(self, request):
11 def get(self, request):
12 context = self.get_context_data(request=request)
12 context = self.get_context_data(request=request)
13
13
14 # TODO Use dict here
14 # TODO Use dict here
15 return render(request, 'boards/404.html', context_instance=context)
15 response = render(request, 'boards/404.html', context_instance=context)
16 response.status_code = 404
17
18 return response
General Comments 0
You need to be logged in to leave comments. Login now