tests.py 7.6 KB
Newer Older
1
"""Tests for things provided by this module"""
2
3
import doctest

Thom Wiggers's avatar
Thom Wiggers committed
4
from django.contrib.auth import get_user_model
5
from django.contrib.auth.models import Permission
Thom Wiggers's avatar
Thom Wiggers committed
6
7
from django.test import TestCase, override_settings

8
from activemembers.models import Committee, MemberGroupMembership, Society
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
9
from members.models import Profile
10
from thaliawebsite.templatetags import bleach_tags
11
from thaliawebsite import sitemaps
12
13


14
def load_tests(_loader, tests, _ignore):
15
16
17
18
19
    """
    Load all tests in this module
    """
    # Adds the doctests in bleach_tags
    tests.addTests(doctest.DocTestSuite(bleach_tags))
20
    tests.addTests(doctest.DocTestSuite(sitemaps))
21
22
    return tests

Thom Wiggers's avatar
Thom Wiggers committed
23
24
25

class WikiLoginTestCase(TestCase):
    """Tests event registrations"""
26
    fixtures = ['member_groups.json']
Thom Wiggers's avatar
Thom Wiggers committed
27

28
29
30
    @classmethod
    def setUpTestData(cls):
        cls.user = get_user_model().objects.create_user(
Thom Wiggers's avatar
Thom Wiggers committed
31
32
33
34
35
36
            username='testuser',
            first_name='first',
            last_name='last_name',
            email='foo@bar.com',
            password='top secret')

37
38
    def test_login_get_request_denied(self):
        """GET shouldn't work for the wiki API"""
Thom Wiggers's avatar
Thom Wiggers committed
39
40
41
42
43
        response = self.client.get('/api/wikilogin')
        self.assertEqual(response.status_code, 405)

    @override_settings(WIKI_API_KEY='wrongkey')
    def test_login_wrong_apikey(self):
44
        """API key should be verified"""
Thom Wiggers's avatar
Thom Wiggers committed
45
46
47
48
49
50
51
52
53
        response = self.client.post('/api/wikilogin',
                                    {'apikey': 'rightkey',
                                     'username': 'testuser',
                                     'password': 'top secret'})
        self.assertEqual(response.status_code, 403)
        self.assertEqual(response.json()['status'], 'error')

    @override_settings(WIKI_API_KEY='key')
    def test_login(self):
54
        """Test a correct log in attempt"""
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
        response = self.client.post('/api/wikilogin',
                                    {'apikey': 'key',
                                     'user': 'testuser',
                                     'password': 'top secret'})

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), {'admin': False,
                                           'committees': [],
                                           'msg': 'Logged in',
                                           'mail': 'foo@bar.com',
                                           'name': 'first last_name',
                                           'status': 'ok'})

    @override_settings(WIKI_API_KEY='key')
    def test_login_with_profile(self):
70
        """A user that has a profile should be able to log in"""
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
71
72
73
74
75
        Profile.objects.create(
            user=self.user,
            student_number='s1234567'
        )

Thom Wiggers's avatar
Thom Wiggers committed
76
77
78
79
80
81
82
83
84
85
86
87
88
        response = self.client.post('/api/wikilogin',
                                    {'apikey': 'key',
                                     'user': 'testuser',
                                     'password': 'top secret'})

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), {'admin': False,
                                           'committees': [],
                                           'msg': 'Logged in',
                                           'mail': 'foo@bar.com',
                                           'name': 'first last_name',
                                           'status': 'ok'})

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    @override_settings(WIKI_API_KEY='key')
    def test_login_with_committee_group_membership(self):
        """A user that has a profile should be able to log in"""
        committee = Committee.objects.get(pk=1)
        MemberGroupMembership.objects.create(
            member=self.user,
            group=committee
        )

        response = self.client.post('/api/wikilogin',
                                    {'apikey': 'key',
                                     'user': 'testuser',
                                     'password': 'top secret'})

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), {'admin': False,
                                           'committees': ['testcie1:ns'],
                                           'msg': 'Logged in',
                                           'mail': 'foo@bar.com',
                                           'name': 'first last_name',
                                           'status': 'ok'})

    @override_settings(WIKI_API_KEY='key')
    def test_login_with_society_group_membership(self):
        """A user that has a profile should be able to log in"""
        society = Society.objects.get(pk=4)
        MemberGroupMembership.objects.create(
            member=self.user,
            group=society
        )

        response = self.client.post('/api/wikilogin',
                                    {'apikey': 'key',
                                     'user': 'testuser',
                                     'password': 'top secret'})

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), {'admin': False,
                                           'committees': [],
                                           'msg': 'Logged in',
                                           'mail': 'foo@bar.com',
                                           'name': 'first last_name',
                                           'status': 'ok'})

133
134
    @override_settings(WIKI_API_KEY='key')
    def test_board_permission(self):
135
        """The board should get access to the board wiki"""
136
137
138
139
140
141
142
143
144
145
146
147
148
149
        self.user.user_permissions.add(
            Permission.objects.get(codename='board_wiki'))
        response = self.client.post('/api/wikilogin',
                                    {'apikey': 'key',
                                     'user': 'testuser',
                                     'password': 'top secret'})
        self.assertEqual(response.json(), {'admin': False,
                                           'committees': ['bestuur'],
                                           'msg': 'Logged in',
                                           'mail': 'foo@bar.com',
                                           'name': 'first last_name',
                                           'status': 'ok'})
        self.assertEqual(response.status_code, 200)

Thom Wiggers's avatar
Thom Wiggers committed
150
151
    @override_settings(WIKI_API_KEY='key')
    def test_wrongargs(self):
152
        """Check that the arguments are correct"""
Thom Wiggers's avatar
Thom Wiggers committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
        response = self.client.post('/api/wikilogin',
                                    {'apikey': 'key',
                                     'username': 'testuser',
                                     'password': 'top secret'})
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json()['status'], 'error')

        response = self.client.post('/api/wikilogin',
                                    {'apikey': 'key',
                                     'user': 'testuser'})
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json()['status'], 'error')

    @override_settings(WIKI_API_KEY='key')
    def test_login_wrong_password(self):
168
        """Check that the password is actually checked"""
Thom Wiggers's avatar
Thom Wiggers committed
169
170
171
172
173
174
        response = self.client.post('/api/wikilogin',
                                    {'apikey': 'key',
                                     'user': 'testuser',
                                     'password': 'wrong secret'})
        self.assertEqual(response.status_code, 403)
        self.assertEqual(response.json()['status'], 'error')
175
176
177
178
179
180
181
182


class SitemapTest(TestCase):
    fixtures = ['members.json', 'member_groups.json']

    def test_sitemap_success(self):
        response = self.client.get('/sitemap.xml')
        self.assertEqual(response.status_code, 200)