tests.py 11.3 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.
#
#

Skia's avatar
Skia committed
25
26
import re

Skia's avatar
Skia committed
27
from django.test import TestCase
Skia's avatar
Skia committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from django.core.urlresolvers import reverse
from django.core.management import call_command

from core.models import User
from counter.models import Counter


class CounterTest(TestCase):
    def setUp(self):
        call_command("populate")
        self.skia = User.objects.filter(username="skia").first()
        self.mde = Counter.objects.filter(name="MDE").first()

    def test_full_click(self):
Sli's avatar
Sli committed
42
43
44
45
        response = self.client.post(
            reverse("counter:login", kwargs={"counter_id": self.mde.id}),
            {"username": self.skia.username, "password": "plop"},
        )
Krophil's avatar
Krophil committed
46
        response = self.client.get(
Sli's avatar
Sli committed
47
48
            reverse("counter:details", kwargs={"counter_id": self.mde.id})
        )
Krophil's avatar
Krophil committed
49
50

        self.assertTrue(
Sli's avatar
Sli committed
51
52
            'class="link-button">S&#39; Kia</button>' in str(response.content)
        )
Krophil's avatar
Krophil committed
53
54

        counter_token = re.search(
Sli's avatar
Sli committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
            r'name="counter_token" value="([^"]*)"', str(response.content)
        ).group(1)

        response = self.client.post(
            reverse("counter:details", kwargs={"counter_id": self.mde.id}),
            {"code": "4000k", "counter_token": counter_token},
        )
        location = response.get("location")

        response = self.client.get(response.get("location"))
        self.assertTrue(">Richard Batsbak</" in str(response.content))

        response = self.client.post(
            location,
            {
                "action": "refill",
                "amount": "10",
                "payment_method": "CASH",
                "bank": "OTHER",
            },
        )
        response = self.client.post(location, {"action": "code", "code": "BARB"})
        response = self.client.post(location, {"action": "code", "code": "fin"})

        response_get = self.client.get(response.get("location"))
        self.assertTrue(
            "<p>Client : Richard Batsbak - Nouveau montant : 8.30"
            in str(response_get.content)
        )
Skia's avatar
Skia committed
84

Krophil's avatar
Krophil committed
85
86
87
88
89
90
91
92
93
94

class BarmanConnectionTest(TestCase):
    def setUp(self):
        call_command("populate")
        self.krophil = User.objects.get(username="krophil")
        self.skia = User.objects.get(username="skia")
        self.skia.customer.account = 800
        self.krophil.customer.save()
        self.skia.customer.save()

Krophil's avatar
Krophil committed
95
        self.counter = Counter.objects.filter(id=2).first()
Krophil's avatar
Krophil committed
96
97

    def test_barman_granted(self):
Sli's avatar
Sli committed
98
99
100
101
102
103
104
        self.client.post(
            reverse("counter:login", args=[self.counter.id]),
            {"username": "krophil", "password": "plop"},
        )
        response_get = self.client.get(
            reverse("counter:details", args=[self.counter.id])
        )
Krophil's avatar
Krophil committed
105

Sli's avatar
Sli committed
106
        self.assertTrue("<p>Entrez un code client : </p>" in str(response_get.content))
Krophil's avatar
Krophil committed
107
108

    def test_counters_list_barmen(self):
Sli's avatar
Sli committed
109
110
111
112
113
114
115
        self.client.post(
            reverse("counter:login", args=[self.counter.id]),
            {"username": "krophil", "password": "plop"},
        )
        response_get = self.client.get(
            reverse("counter:activity", args=[self.counter.id])
        )
Krophil's avatar
Krophil committed
116
117

        self.assertTrue(
Sli's avatar
Sli committed
118
119
120
            '<li><a href="/user/10/">Kro Phil&#39;</a></li>'
            in str(response_get.content)
        )
Krophil's avatar
Krophil committed
121

Krophil's avatar
Krophil committed
122
    def test_barman_denied(self):
Sli's avatar
Sli committed
123
124
125
126
        self.client.post(
            reverse("counter:login", args=[self.counter.id]),
            {"username": "skia", "password": "plop"},
        )
Krophil's avatar
Krophil committed
127
        response_get = self.client.get(
Sli's avatar
Sli committed
128
129
            reverse("counter:details", args=[self.counter.id])
        )
Krophil's avatar
Krophil committed
130

Sli's avatar
Sli committed
131
        self.assertTrue("<p>Merci de vous identifier</p>" in str(response_get.content))
Krophil's avatar
Krophil committed
132
133

    def test_counters_list_no_barmen(self):
Sli's avatar
Sli committed
134
135
136
137
        self.client.post(
            reverse("counter:login", args=[self.counter.id]),
            {"username": "krophil", "password": "plop"},
        )
Krophil's avatar
Krophil committed
138
        response_get = self.client.get(
Sli's avatar
Sli committed
139
140
            reverse("counter:activity", args=[self.counter.id])
        )
Krophil's avatar
Krophil committed
141
142

        self.assertFalse(
Sli's avatar
Sli committed
143
144
            '<li><a href="/user/1/">S&#39; Kia</a></li>' in str(response_get.content)
        )
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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
269
270
271
272
273
274
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336


class StudentCardTest(TestCase):
    """
    Tests for adding and deleting Stundent Cards
    Test that an user can be found with it's student card
    """

    def setUp(self):
        call_command("populate")
        self.krophil = User.objects.get(username="krophil")
        self.sli = User.objects.get(username="sli")

        self.counter = Counter.objects.filter(id=2).first()

        # Auto login on counter
        self.client.post(
            reverse("counter:login", args=[self.counter.id]),
            {"username": "krophil", "password": "plop"},
        )

    def test_search_user_with_student_card(self):
        response = self.client.post(
            reverse("counter:details", args=[self.counter.id]),
            {"code": "9A89B82018B0A0"},
        )

        self.assertEqual(
            response.url,
            reverse(
                "counter:click",
                kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
            ),
        )

    def test_add_student_card_from_counter(self):
        response = self.client.post(
            reverse(
                "counter:click",
                kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
            ),
            {"student_card_uid": "8B90734A802A8F", "action": "add_student_card"},
        )
        self.assertContains(response, text="8B90734A802A8F")

    def test_add_student_card_from_counter_fail(self):
        response = self.client.post(
            reverse(
                "counter:click",
                kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
            ),
            {"student_card_uid": "8B90734A802A8", "action": "add_student_card"},
        )
        self.assertContains(
            response, text="Ce n'est pas un UID de carte étudiante valide"
        )

        response = self.client.post(
            reverse(
                "counter:click",
                kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
            ),
            {"student_card_uid": "8B90734A802A8FA", "action": "add_student_card"},
        )
        self.assertContains(
            response, text="Ce n'est pas un UID de carte étudiante valide"
        )

    def test_delete_student_card_with_owner(self):
        self.client.login(username="sli", password="plop")
        self.client.post(
            reverse(
                "counter:delete_student_card",
                kwargs={
                    "customer_id": self.sli.customer.pk,
                    "card_id": self.sli.customer.student_cards.first().id,
                },
            )
        )
        self.assertFalse(self.sli.customer.student_cards.exists())

    def test_delete_student_card_with_board_member(self):
        self.client.login(username="skia", password="plop")
        self.client.post(
            reverse(
                "counter:delete_student_card",
                kwargs={
                    "customer_id": self.sli.customer.pk,
                    "card_id": self.sli.customer.student_cards.first().id,
                },
            )
        )
        self.assertFalse(self.sli.customer.student_cards.exists())

    def test_delete_student_card_with_root(self):
        self.client.login(username="root", password="plop")
        self.client.post(
            reverse(
                "counter:delete_student_card",
                kwargs={
                    "customer_id": self.sli.customer.pk,
                    "card_id": self.sli.customer.student_cards.first().id,
                },
            )
        )
        self.assertFalse(self.sli.customer.student_cards.exists())

    def test_delete_student_card_fail(self):
        self.client.login(username="krophil", password="plop")
        response = self.client.post(
            reverse(
                "counter:delete_student_card",
                kwargs={
                    "customer_id": self.sli.customer.pk,
                    "card_id": self.sli.customer.student_cards.first().id,
                },
            )
        )
        self.assertEqual(response.status_code, 403)
        self.assertTrue(self.sli.customer.student_cards.exists())

    def test_add_student_card_from_user_preferences(self):
        # Test with owner of the card
        self.client.login(username="sli", password="plop")
        self.client.post(
            reverse(
                "counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk}
            ),
            {"uid": "8B90734A802A8F"},
        )

        response = self.client.get(
            reverse("core:user_prefs", kwargs={"user_id": self.sli.id})
        )
        self.assertContains(response, text="8B90734A802A8F")

        # Test with board member
        self.client.login(username="skia", password="plop")
        self.client.post(
            reverse(
                "counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk}
            ),
            {"uid": "8B90734A802A8A"},
        )

        response = self.client.get(
            reverse("core:user_prefs", kwargs={"user_id": self.sli.id})
        )
        self.assertContains(response, text="8B90734A802A8A")

        # Test with root
        self.client.login(username="root", password="plop")
        self.client.post(
            reverse(
                "counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk}
            ),
            {"uid": "8B90734A802A8B"},
        )

        response = self.client.get(
            reverse("core:user_prefs", kwargs={"user_id": self.sli.id})
        )
        self.assertContains(response, text="8B90734A802A8B")

    def test_add_student_card_from_user_preferences_fail(self):
        self.client.login(username="sli", password="plop")
        response = self.client.post(
            reverse(
                "counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk}
            ),
            {"uid": "8B90734A802A8"},
        )

        self.assertContains(response, text="Cet UID est invalide")

        response = self.client.post(
            reverse(
                "counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk}
            ),
            {"uid": "8B90734A802A8FA"},
        )
        self.assertContains(response, text="Cet UID est invalide")

        # Test with unauthorized user
        self.client.login(username="krophil", password="plop")
        response = self.client.post(
            reverse(
                "counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk}
            ),
            {"uid": "8B90734A802A8F"},
        )
        self.assertEqual(response.status_code, 403)