tests.py 17.6 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
# -*- coding:utf-8 -*
#
# Copyright 2016,2017
# - Skia <skia@libskia.so>
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License a published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#

Sli's avatar
Sli committed
25
from django.conf import settings
26
from django.test import TestCase
Sli's avatar
Sli committed
27
from django.utils import timezone
Skia's avatar
Skia committed
28
29
from django.core.urlresolvers import reverse
from django.core.management import call_command
30

Skia's avatar
Skia committed
31
from core.models import User
Sli's avatar
Sli committed
32
33
from club.models import Club, Membership
from club.forms import MailingForm
Skia's avatar
Skia committed
34

35
# Create your tests here.
Skia's avatar
Skia committed
36

Krophil's avatar
Krophil committed
37

Skia's avatar
Skia committed
38
39
40
class ClubTest(TestCase):
    def setUp(self):
        call_command("populate")
Skia's avatar
Skia committed
41
42
        self.skia = User.objects.filter(username="skia").first()
        self.rbatsbak = User.objects.filter(username="rbatsbak").first()
Skia's avatar
Skia committed
43
        self.guy = User.objects.filter(username="guy").first()
Skia's avatar
Skia committed
44
        self.bdf = Club.objects.filter(unix_name="bdf").first()
Skia's avatar
Skia committed
45
46

    def test_create_add_user_to_club_from_root_ok(self):
Sli's avatar
Sli committed
47
48
49
        self.client.login(username="root", password="plop")
        self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
50
            {"users": self.skia.id, "start_date": "12/06/2016", "role": 3},
Sli's avatar
Sli committed
51
52
53
54
        )
        response = self.client.get(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id})
        )
Skia's avatar
Skia committed
55
        self.assertTrue(response.status_code == 200)
Sli's avatar
Sli committed
56
        self.assertTrue(
57
            "S&#39; Kia</a></td>\\n                    <td>Responsable info</td>"
Sli's avatar
Sli committed
58
59
            in str(response.content)
        )
Skia's avatar
Skia committed
60

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    def test_create_add_multiple_user_to_club_from_root_ok(self):
        self.client.login(username="root", password="plop")
        self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {
                "users": "|%d|%d|" % (self.skia.id, self.rbatsbak.id),
                "start_date": "12/06/2016",
                "role": 3,
            },
        )
        response = self.client.get(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id})
        )
        self.assertTrue(response.status_code == 200)
        content = str(response.content)
        self.assertTrue(
77
78
            "S&#39; Kia</a></td>\\n                    <td>Responsable info</td>"
            in content
79
80
        )
        self.assertTrue(
81
            "Richard Batsbak</a></td>\\n                    <td>Responsable info</td>"
82
83
84
            in content
        )

Skia's avatar
Skia committed
85
    def test_create_add_user_to_club_from_root_fail_not_subscriber(self):
Sli's avatar
Sli committed
86
87
88
        self.client.login(username="root", password="plop")
        response = self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
89
            {"users": self.guy.id, "start_date": "12/06/2016", "role": 3},
Sli's avatar
Sli committed
90
        )
Skia's avatar
Skia committed
91
        self.assertTrue(response.status_code == 200)
92
        self.assertTrue('<ul class="errorlist"><li>' in str(response.content))
Sli's avatar
Sli committed
93
94
95
96
        response = self.client.get(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id})
        )
        self.assertFalse(
97
            "Guy Carlier</a></td>\\n                    <td>Responsable info</td>"
Sli's avatar
Sli committed
98
99
            in str(response.content)
        )
Skia's avatar
Skia committed
100
101

    def test_create_add_user_to_club_from_root_fail_already_in_club(self):
Sli's avatar
Sli committed
102
103
104
        self.client.login(username="root", password="plop")
        self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
105
            {"users": self.skia.id, "start_date": "12/06/2016", "role": 3},
Sli's avatar
Sli committed
106
107
108
109
110
        )
        response = self.client.get(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id})
        )
        self.assertTrue(
111
            "S&#39; Kia</a></td>\\n                    <td>Responsable info</td>"
Sli's avatar
Sli committed
112
113
114
115
            in str(response.content)
        )
        response = self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
116
            {"users": self.skia.id, "start_date": "12/06/2016", "role": 4},
Sli's avatar
Sli committed
117
        )
Skia's avatar
Skia committed
118
        self.assertTrue(response.status_code == 200)
Sli's avatar
Sli committed
119
120
121
122
        self.assertFalse(
            "S&#39; Kia</a></td>\\n                <td>Secrétaire</td>"
            in str(response.content)
        )
Skia's avatar
Skia committed
123

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    def test_create_add_user_non_existent_to_club_from_root_fail(self):
        self.client.login(username="root", password="plop")
        response = self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {"users": [9999], "start_date": "12/06/2016", "role": 3},
        )
        self.assertTrue(response.status_code == 200)
        content = str(response.content)
        self.assertTrue('<ul class="errorlist"><li>' in content)
        self.assertFalse("<td>Responsable info</td>" in content)
        self.client.login(username="root", password="plop")
        response = self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {
                "users": "|%d|%d|" % (self.skia.id, 9999),
                "start_date": "12/06/2016",
                "role": 3,
            },
        )
        self.assertTrue(response.status_code == 200)
        content = str(response.content)
        self.assertTrue('<ul class="errorlist"><li>' in content)
        self.assertFalse("<td>Responsable info</td>" in content)

Skia's avatar
Skia committed
148
    def test_create_add_user_to_club_from_skia_ok(self):
Sli's avatar
Sli committed
149
150
151
        self.client.login(username="root", password="plop")
        self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
152
            {"users": self.skia.id, "start_date": "12/06/2016", "role": 10},
Sli's avatar
Sli committed
153
154
155
156
        )
        self.client.login(username="skia", password="plop")
        self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
157
            {"users": self.rbatsbak.id, "start_date": "12/06/2016", "role": 9},
Sli's avatar
Sli committed
158
159
160
161
        )
        response = self.client.get(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id})
        )
Skia's avatar
Skia committed
162
        self.assertTrue(response.status_code == 200)
Sli's avatar
Sli committed
163
        self.assertTrue(
164
            """Richard Batsbak</a></td>\\n                    <td>Vice-Pr\\xc3\\xa9sident</td>"""
Sli's avatar
Sli committed
165
166
            in str(response.content)
        )
Skia's avatar
Skia committed
167

Skia's avatar
Skia committed
168
    def test_create_add_user_to_club_from_richard_fail(self):
Sli's avatar
Sli committed
169
170
171
        self.client.login(username="root", password="plop")
        self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
172
            {"users": self.rbatsbak.id, "start_date": "12/06/2016", "role": 3},
Sli's avatar
Sli committed
173
174
175
176
        )
        self.client.login(username="rbatsbak", password="plop")
        response = self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
177
            {"users": self.skia.id, "start_date": "12/06/2016", "role": 10},
Sli's avatar
Sli committed
178
        )
Skia's avatar
Skia committed
179
        self.assertTrue(response.status_code == 200)
Sli's avatar
Sli committed
180
181
182
183
        self.assertTrue(
            "<li>Vous n&#39;avez pas la permission de faire cela</li>"
            in str(response.content)
        )
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217

    def test_role_required_if_users_specified(self):
        self.client.login(username="root", password="plop")
        response = self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {"users": self.rbatsbak.id, "start_date": "12/06/2016"},
        )
        self.assertTrue(
            '<ul class="errorlist"><li>Vous devez choisir un r' in str(response.content)
        )

    def test_mark_old_user_to_club_from_skia_ok(self):
        self.client.login(username="root", password="plop")
        self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {
                "users": "|%d|%d|" % (self.skia.id, self.rbatsbak.id),
                "start_date": "12/06/2016",
                "role": 3,
            },
        )
        self.client.login(username="skia", password="plop")
        response = self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {"users_old": self.rbatsbak.id},
        )
        self.assertTrue(response.status_code == 302)

        response = self.client.get(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id})
        )
        self.assertTrue(response.status_code == 200)
        content = str(response.content)
        self.assertFalse(
218
            "Richard Batsbak</a></td>\\n                    <td>Responsable info</td>"
219
220
221
            in content
        )
        self.assertTrue(
222
223
            "S&#39; Kia</a></td>\\n                    <td>Responsable info</td>"
            in content
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
        )

        # Skia is board member so he should be able to mark as old even without being in the club
        response = self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {"users_old": self.skia.id},
        )
        self.client.login(username="root", password="plop")
        self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {"users": self.rbatsbak.id, "start_date": "12/06/2016", "role": 3},
        )
        self.client.login(username="skia", password="plop")
        response = self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {"users_old": self.rbatsbak.id},
        )
        self.assertFalse(
242
            "Richard Batsbak</a></td>\\n                    <td>Responsable info</td>"
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
            in str(response.content)
        )

    def test_mark_old_multiple_users_from_skia_ok(self):
        self.client.login(username="root", password="plop")
        self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {
                "users": "|%d|%d|" % (self.skia.id, self.rbatsbak.id),
                "start_date": "12/06/2016",
                "role": 3,
            },
        )
        self.client.login(username="skia", password="plop")
        response = self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {"users_old": [self.rbatsbak.id, self.skia.id]},
        )
        self.assertTrue(response.status_code == 302)

        response = self.client.get(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id})
        )
        self.assertTrue(response.status_code == 200)
        content = str(response.content)
        self.assertFalse(
269
            "Richard Batsbak</a></td>\\n                    <td>Responsable info</td>"
270
271
272
            in content
        )
        self.assertFalse(
273
274
            "S&#39; Kia</a></td>\\n                    <td>Responsable info</td>"
            in content
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
        )

    def test_mark_old_user_to_club_from_richard_ok(self):
        self.client.login(username="root", password="plop")
        self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {
                "users": "|%d|%d|" % (self.skia.id, self.rbatsbak.id),
                "start_date": "12/06/2016",
                "role": 3,
            },
        )

        # Test with equal rights
        self.client.login(username="rbatsbak", password="plop")
        response = self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {"users_old": self.skia.id},
        )
        self.assertTrue(response.status_code == 302)

        response = self.client.get(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id})
        )
        self.assertTrue(response.status_code == 200)
        content = str(response.content)
        self.assertTrue(
302
            "Richard Batsbak</a></td>\\n                    <td>Responsable info</td>"
303
304
305
            in content
        )
        self.assertFalse(
306
307
            "S&#39; Kia</a></td>\\n                    <td>Responsable info</td>"
            in content
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
        )

        # Test with lower rights
        self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {"users": self.skia.id, "start_date": "12/06/2016", "role": 0},
        )

        self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {"users_old": self.skia.id},
        )
        response = self.client.get(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id})
        )
        self.assertTrue(response.status_code == 200)
        content = str(response.content)
        self.assertTrue(
326
            "Richard Batsbak</a></td>\\n                    <td>Responsable info</td>"
327
328
329
            in content
        )
        self.assertFalse(
330
            "S&#39; Kia</a></td>\\n                    <td>Curieux</td>" in content
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
        )

    def test_mark_old_user_to_club_from_richard_fail(self):
        self.client.login(username="root", password="plop")
        self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {"users": self.skia.id, "start_date": "12/06/2016", "role": 3},
        )

        # Test with richard outside of the club
        self.client.login(username="rbatsbak", password="plop")
        response = self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {"users_old": self.skia.id},
        )
        self.assertTrue(response.status_code == 200)

        response = self.client.get(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id})
        )
        self.assertTrue(response.status_code == 200)
        self.assertTrue(
353
            "S&#39; Kia</a></td>\\n                    <td>Responsable info</td>"
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
            in str(response.content)
        )

        # Test with lower rights
        self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {"users": self.rbatsbak.id, "start_date": "12/06/2016", "role": 0},
        )

        self.client.post(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id}),
            {"users_old": self.skia.id},
        )
        response = self.client.get(
            reverse("club:club_members", kwargs={"club_id": self.bdf.id})
        )
        self.assertTrue(response.status_code == 200)
        content = str(response.content)
        self.assertTrue(
373
            "Richard Batsbak</a></td>\\n                    <td>Curieux</td>" in content
374
375
        )
        self.assertTrue(
376
377
            "S&#39; Kia</a></td>\\n                    <td>Responsable info</td>"
            in content
378
        )
Sli's avatar
Sli committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448


class MailingFormTest(TestCase):
    """Perform validation tests for MailingForm"""

    def setUp(self):
        call_command("populate")
        self.skia = User.objects.filter(username="skia").first()
        self.rbatsbak = User.objects.filter(username="rbatsbak").first()
        self.guy = User.objects.filter(username="krophil").first()
        self.comunity = User.objects.filter(username="comunity").first()
        self.bdf = Club.objects.filter(unix_name="bdf").first()
        Membership(
            user=self.rbatsbak,
            club=self.bdf,
            start_date=timezone.now(),
            role=settings.SITH_CLUB_ROLES_ID["Board member"],
        ).save()

    def test_mailing_list_add_no_moderation(self):
        # Test with Communication admin
        self.client.login(username="comunity", password="plop")
        self.client.post(
            reverse("club:mailing", kwargs={"club_id": self.bdf.id}),
            {"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "foyer"},
        )
        response = self.client.get(
            reverse("club:mailing", kwargs={"club_id": self.bdf.id})
        )
        self.assertContains(response, text="Liste de diffusion foyer@utbm.fr")

        # Test with Root
        self.client.login(username="root", password="plop")
        self.client.post(
            reverse("club:mailing", kwargs={"club_id": self.bdf.id}),
            {"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
        )
        response = self.client.get(
            reverse("club:mailing", kwargs={"club_id": self.bdf.id})
        )
        self.assertContains(response, text="Liste de diffusion mde@utbm.fr")

    def test_mailing_list_add_moderation(self):
        self.client.login(username="rbatsbak", password="plop")
        self.client.post(
            reverse("club:mailing", kwargs={"club_id": self.bdf.id}),
            {"action": MailingForm.ACTION_NEW_MAILING, "mailing_email": "mde"},
        )
        response = self.client.get(
            reverse("club:mailing", kwargs={"club_id": self.bdf.id})
        )
        self.assertNotContains(response, text="Liste de diffusion mde@utbm.fr")
        self.assertContains(
            response, text="<p>Listes de diffusions en attente de modération</p>"
        )
        self.assertContains(response, "<li>mde@utbm.fr")

    def test_mailing_list_forbidden(self):
        # With anonymous user
        response = self.client.get(
            reverse("club:mailing", kwargs={"club_id": self.bdf.id})
        )
        self.assertContains(response, "", status_code=403)

        # With user not in club
        self.client.login(username="krophil", password="plop")
        response = self.client.get(
            reverse("club:mailing", kwargs={"club_id": self.bdf.id})
        )
        self.assertContains(response, "", status_code=403)