-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtest_shift_views.py
More file actions
297 lines (264 loc) · 11 KB
/
test_shift_views.py
File metadata and controls
297 lines (264 loc) · 11 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
133
134
135
136
137
138
139
140
141
142
143
144
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
"""Test cases for the shift views of the teams application."""
from __future__ import annotations
from bs4 import BeautifulSoup
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from django.utils import timezone
from camps.models import Permission as CampPermission
from teams.models import TeamShift
from utils.tests import BornhackTestBase
class TeamShiftViewTest(BornhackTestBase):
"""Test Team Shift View"""
@classmethod
def setUpTestData(cls) -> None:
"""Setup test data."""
# first add users and other basics
super().setUpTestData()
permission_content_type = ContentType.objects.get_for_model(CampPermission)
cls.users[4].user_permissions.add(
Permission.objects.get(
content_type=permission_content_type,
codename="noc_team_lead",
),
)
def test_team_shift_view_permissions(self) -> None:
"""Test the team shift view permissions."""
self.client.force_login(self.users[0]) # Non noc team member
# Test access control to the views
url = reverse("teams:shift_create", kwargs={
"team_slug": self.teams["noc"].slug,
"camp_slug": self.camp.slug,
})
response = self.client.get(path=url)
assert response.status_code == 302
def test_team_user_shift_view(self) -> None:
"""Test the user shift view."""
self.client.force_login(self.users[4]) # Noc teamlead
url = reverse("teams:user_shifts", kwargs={
"camp_slug": self.camp.slug,
})
response = self.client.get(path=url)
assert response.status_code == 200
def test_team_shift_views(self) -> None:
"""Test the team shift views."""
self.client.force_login(self.users[4]) # Noc teamlead
# Test creating a shift
url = reverse("teams:shift_create", kwargs={
"team_slug": self.teams["noc"].slug,
"camp_slug": self.camp.slug,
})
response = self.client.get(url)
assert response.status_code == 200
response = self.client.post(
path=url,
data={
"from_datetime": self.camp.buildup.lower.date(),
"to_datetime": self.camp.buildup.lower + timezone.timedelta(hours=1),
"people_required": 1,
},
follow=True,
)
assert response.status_code == 200
content = response.content.decode()
soup = BeautifulSoup(content, "html.parser")
rows = soup.select("table#main_table > tbody > tr")
self.assertEqual(len(rows), 1, "team shift list does not return 1 entries after create")
# Test same start and end.
response = self.client.post(
path=url,
data={
"from_datetime": self.camp.buildup.lower,
"to_datetime": self.camp.buildup.lower,
"people_required": 1,
},
follow=True,
)
assert response.status_code == 200
content = response.content.decode()
soup = BeautifulSoup(content, "html.parser")
rows = soup.select("ul.list-unstyled.text-danger > li")
matches = [s for s in rows if "Start can not be the same as end." in str(s)]
self.assertEqual(len(matches), 1, "team shift Start can not be equal to end")
# Test same end before start.
response = self.client.post(
path=url,
data={
"from_datetime": self.camp.buildup.lower + timezone.timedelta(hours=1),
"to_datetime": self.camp.buildup.lower,
"people_required": 1,
},
follow=True,
)
assert response.status_code == 200
content = response.content.decode()
soup = BeautifulSoup(content, "html.parser")
rows = soup.select("ul.list-unstyled.text-danger > li")
matches = [s for s in rows if "Start can not be after end." in str(s)]
self.assertEqual(len(matches), 1, "team shift Start can not be before to end")
# Test Creating multiple shifts
url = reverse("teams:shift_create_multiple", kwargs={
"team_slug": self.teams["noc"].slug,
"camp_slug": self.camp.slug,
})
response = self.client.get(url)
assert response.status_code == 200
response = self.client.post(
path=url,
data={
"from_datetime": self.camp.camp.lower.date(),
"shift_length": 60,
"number_of_shifts": 10,
"people_required": 5,
},
follow=True,
)
assert response.status_code == 200
content = response.content.decode()
soup = BeautifulSoup(content, "html.parser")
rows = soup.select("table#main_table > tbody > tr")
self.assertEqual(len(rows), 11, "team shift list does not return 11 entries after create multiple")
# Lookup the id of one of the shifts.
shift_row = soup.select_one("table#main_table > tbody > tr:nth-of-type(1) td:nth-of-type(5)")
shift_link = shift_row.find("a")
shift_id = int(shift_link["href"].split("/")[5])
# Test the update view
url = reverse("teams:shift_update", kwargs={
"team_slug": self.teams["noc"].slug,
"camp_slug": self.camp.slug,
"pk": shift_id,
})
from_datetime = self.camp.buildup.lower
to_datetime = from_datetime + timezone.timedelta(hours=2)
response = self.client.get(url)
assert response.status_code == 200
response = self.client.post(
path=url,
data={
"from_datetime": from_datetime,
"to_datetime": to_datetime,
"people_required": 2,
},
follow=True,
)
assert response.status_code == 200
content = response.content.decode()
soup = BeautifulSoup(content, "html.parser")
row = soup.select_one("table#main_table > tbody > tr:nth-of-type(1) td:nth-of-type(3)").get_text(strip=True)
self.assertEqual(row, "2", "team shift people required count does not return 2 entries after update")
# Test the delete view
url = reverse("teams:shift_delete", kwargs={
"team_slug": self.teams["noc"].slug,
"camp_slug": self.camp.slug,
"pk": shift_id,
})
response = self.client.get(url)
assert response.status_code == 200
response = self.client.post(path=url, follow=True)
assert response.status_code == 200
content = response.content.decode()
soup = BeautifulSoup(content, "html.parser")
rows = soup.select("table#main_table > tbody > tr")
self.assertEqual(len(rows), 10, "team shift list does not return 10 entries after delete")
def test_team_shift_actions(self) -> None:
"""Test the team shift actions."""
self.client.force_login(self.users[4]) # Noc teamlead
team_shift_1 = TeamShift(
team=self.teams["noc"],
shift_range=(
self.camp.buildup.lower,
self.camp.buildup.lower + timezone.timedelta(hours=1),
),
people_required=1,
)
team_shift_1.save()
team_shift_2 = TeamShift(
team=self.teams["noc"],
shift_range=(
self.camp.buildup.lower,
self.camp.buildup.lower + timezone.timedelta(hours=1),
),
people_required=1,
)
team_shift_2.save()
# Test taking a shift
url = reverse("teams:shift_member_take", kwargs={
"team_slug": team_shift_1.team.slug,
"camp_slug": self.camp.slug,
"pk": team_shift_1.pk,
})
response = self.client.get(
path=url,
follow=True,
)
assert response.status_code == 200
content = response.content.decode()
soup = BeautifulSoup(content, "html.parser")
rows = soup.select_one("table#main_table > tbody > tr:nth-of-type(1) td:nth-of-type(5)")
matches = [s for s in rows if "Unassign me" in str(s)]
self.assertEqual(len(matches), 1, "team shift assign failed")
# Test taking a double shift
url = reverse("teams:shift_member_take", kwargs={
"team_slug": team_shift_1.team.slug,
"camp_slug": self.camp.slug,
"pk": team_shift_2.pk,
})
response = self.client.get(
path=url,
follow=True,
)
assert response.status_code == 200
content = response.content.decode()
soup = BeautifulSoup(content, "html.parser")
rows = soup.select("div.alert.alert-danger")
matches = [s for s in rows if "overlapping" in str(s)]
self.assertEqual(len(matches), 1, "team shift double assign failed to fail")
# Test selling a shift
url = reverse("teams:shift_member_sell", kwargs={
"team_slug": team_shift_1.team.slug,
"camp_slug": self.camp.slug,
"pk": team_shift_1.pk,
})
response = self.client.get(
path=url,
follow=True,
)
assert response.status_code == 200
content = response.content.decode()
soup = BeautifulSoup(content, "html.parser")
rows = soup.select_one("table#main_table > tbody > tr:nth-of-type(1) td:nth-of-type(4)")
matches = [s for s in rows if "for sale!" in str(s)]
self.assertEqual(len(matches), 1, "team shift sell failed")
# Test taking a sellable shift with user1
self.client.force_login(self.users[1]) # Noc team member
url = reverse("teams:shift_member_take", kwargs={
"team_slug": team_shift_1.team.slug,
"camp_slug": self.camp.slug,
"pk": team_shift_1.pk,
})
response = self.client.get(
path=url,
follow=True,
)
assert response.status_code == 200
content = response.content.decode()
soup = BeautifulSoup(content, "html.parser")
rows = soup.select_one("table#main_table > tbody > tr:nth-of-type(1) td:nth-of-type(5)")
matches = [s for s in rows if "Unassign me" in str(s)]
self.assertEqual(len(matches), 1, "team shift assign failed")
# Test dropping a shift
url = reverse("teams:shift_member_drop", kwargs={
"team_slug": team_shift_1.team.slug,
"camp_slug": self.camp.slug,
"pk": team_shift_1.pk,
})
response = self.client.get(
path=url,
follow=True,
)
assert response.status_code == 200
content = response.content.decode()
soup = BeautifulSoup(content, "html.parser")
rows = soup.select_one("table#main_table > tbody > tr:nth-of-type(1) td:nth-of-type(5)")
matches = [s for s in rows if "Assign me" in str(s)]
self.assertEqual(len(matches), 1, "team shift unassign failed")