tests.py 3.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from django.contrib.auth.models import AnonymousUser, User
from django.contrib.sessions.middleware import SessionMiddleware
from django.test import TestCase, RequestFactory
from django.urls import reverse

from announcements.views import close_announcement


class AnnouncementCloseTestCase(TestCase):

    def setUp(self):
        self.factory = RequestFactory()
        self.user = User.objects.create_user(
            username='thom',
            email='test@example.com',
            password='top secret',
        )
        self.middleware = SessionMiddleware()

    def test_get_request(self):
        for user in [self.user, AnonymousUser()]:
            request = self.factory.get(
                reverse('announcements:close-announcement'))
            self.middleware.process_request(request)
            with self.subTest(user=user):
                request.user = user
                response = close_announcement(request)
                self.assertEqual(response.status_code, 405)

    def test_post_no_id(self):
        request = self.factory.post(
            reverse('announcements:close-announcement'))
        self.middleware.process_request(request)
        request.user = AnonymousUser()
        response = close_announcement(request)
        self.assertEqual(response.status_code, 400)

    def test_post_id_string(self):
        request = self.factory.post(
            reverse('announcements:close-announcement'),
            {'id': 'bla'})
        self.middleware.process_request(request)
        request.user = AnonymousUser()
        response = close_announcement(request)
        self.assertEqual(response.status_code, 400)

    def test_valid_request_anonymous(self):
        request = self.factory.post(
            reverse('announcements:close-announcement'),
            {'id': 3})
        self.middleware.process_request(request)
        request.user = AnonymousUser()
        response = close_announcement(request)

        self.assertEqual(response.status_code, 204)
        self.assertIn('closed_announcements', request.session)
        self.assertIn(3, request.session['closed_announcements'])
        self.assertTrue(request.session.modified)
        self.assertEqual(response.content, b'')

    def test_valid_request_logged_in(self):
        request = self.factory.post(
            reverse('announcements:close-announcement'),
            {'id': 3})
        self.middleware.process_request(request)
        request.user = self.user
        response = close_announcement(request)

        self.assertEqual(response.status_code, 204)
        self.assertIn('closed_announcements', request.session)
        self.assertIn(3, request.session['closed_announcements'])
        self.assertEqual(response.content, b'')
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

    def test_valid_alread_canceled(self):
        request = self.factory.post(
            reverse('announcements:close-announcement'),
            {'id': 3})
        self.middleware.process_request(request)
        request.session['closed_announcements'] = [3]
        request.user = AnonymousUser()
        response = close_announcement(request)

        self.assertEqual(response.status_code, 204)
        self.assertIn('closed_announcements', request.session)
        self.assertIn(3, request.session['closed_announcements'])
        self.assertEqual(len(request.session['closed_announcements']), 1)
        self.assertTrue(request.session.modified)
        self.assertEqual(response.content, b'')