This repository has been archived by the owner on Apr 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
test_new_talk_voting.py
368 lines (256 loc) · 14.2 KB
/
test_new_talk_voting.py
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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
from datetime import timedelta
from unittest import mock
import pytest
from django.urls import reverse
from django.utils import timezone
from conference.models import Conference, VotoTalk, TALK_STATUS, TALK_TYPE_CHOICES, TALK_ADMIN_TYPE
from conference.talk_voting import VotingOptions, find_talks
from tests.factories import SpeakerFactory, TalkFactory, TalkSpeakerFactory, TalkVotingTicketFactory
from tests.common_tools import make_user, create_talk_for_user, get_default_conference, template_used
pytestmark = [pytest.mark.django_db]
def test_talk_voting_unavailable_before_talk_voting_start(user_client):
get_default_conference(voting_start=timezone.now() + timedelta(days=2))
url = reverse('talk_voting:talks')
response = user_client.get(url)
assert response.status_code == 200
assert template_used(response, "conference/talk_voting/voting_is_closed.html")
def test_talk_voting_unavailable_after_talk_voting_end(user_client):
get_default_conference(voting_end=timezone.now() - timedelta(days=2))
url = reverse('talk_voting:talks')
response = user_client.get(url)
assert response.status_code == 200
assert template_used(response, "conference/talk_voting/voting_is_closed.html")
def test_talk_voting_unavailable_without_a_ticket(user_client):
get_default_conference()
url = reverse('talk_voting:talks')
response = user_client.get(url)
assert response.status_code == 200
assert not user_client.user.ticket_set.exists()
assert template_used(response, "conference/talk_voting/voting_is_unavailable.html")
def test_talk_voting_available_with_ticket(user_client):
get_default_conference()
TalkVotingTicketFactory(user=user_client.user)
url = reverse('talk_voting:talks')
response = user_client.get(url)
assert response.status_code == 200
assert template_used(response, "conference/talk_voting/voting.html")
def test_talk_voting_available_with_proposal(user_client):
get_default_conference()
create_talk_for_user(user=user_client.user)
url = reverse("talk_voting:talks")
response = user_client.get(url)
assert response.status_code == 200
assert template_used(response, "conference/talk_voting/voting.html")
@mock.patch('conference.talk_voting.is_user_allowed_to_vote', return_value=True)
def test_talk_voting_lists_proposed_talks(mock_allowed_to_vote, user_client):
get_default_conference()
url = reverse("talk_voting:talks")
talk = create_talk_for_user(user=None, type=TALK_TYPE_CHOICES.t_30)
training = create_talk_for_user(user=None, type=TALK_TYPE_CHOICES.r_180)
poster = create_talk_for_user(user=None, type=TALK_TYPE_CHOICES.i_60)
helpdesk = create_talk_for_user(user=None, type=TALK_TYPE_CHOICES.h_180)
response = user_client.get(url)
assert response.status_code == 200
assert template_used(response, "conference/talk_voting/voting.html")
assert talk.title in response.content.decode()
assert training.title in response.content.decode()
assert poster.title in response.content.decode()
assert helpdesk.title in response.content.decode()
@mock.patch('conference.talk_voting.is_user_allowed_to_vote', return_value=True)
def test_if_talk_voting_doesnt_contain_duplicates_if_there_are_more_speakers(
mock_allowed_to_vote, user_client,
):
get_default_conference()
url = reverse("talk_voting:talks")
talk = create_talk_for_user(user=None)
SpeakerFactory(user=user_client.user)
TalkSpeakerFactory(talk=talk, speaker=user_client.user.speaker)
response = user_client.get(url)
assert response.status_code == 200
assert template_used(response, "conference/talk_voting/voting.html")
assert talk.speakers.count() == 2
assert response.content.decode().count(talk.title) == 1
@mock.patch('conference.talk_voting.is_user_allowed_to_vote', return_value=True)
def test_talk_voting_vote_filters(mock_allowed_to_vote, user_client):
get_default_conference()
url = reverse("talk_voting:talks")
# A talk for which the user hasn't voted
not_voted = create_talk_for_user(user=None)
# A talk for which the user has voted
voted = create_talk_for_user(user=None)
VotoTalk.objects.create(talk=voted, user=user_client.user, vote=VotingOptions.maybe)
# A talk which the user submitted
user_talk = create_talk_for_user(user=user_client.user)
# A talk which the user did not submit, but he is a speaker
speaker_talk = create_talk_for_user(user=None)
TalkSpeakerFactory(talk=speaker_talk, speaker=user_client.user.speaker)
response = user_client.get(url)
assert response.status_code == 200
assert template_used(response, "conference/talk_voting/voting.html")
assert not_voted.title in response.content.decode()
assert voted.title in response.content.decode()
assert user_talk.title in response.content.decode()
assert speaker_talk.title in response.content.decode()
response = user_client.get(url, data={'filter': 'voted'})
assert not_voted.title not in response.content.decode()
assert voted.title in response.content.decode()
assert user_talk.title not in response.content.decode()
assert speaker_talk.title not in response.content.decode()
response = user_client.get(url, data={'filter': 'not-voted'})
assert not_voted.title in response.content.decode()
assert voted.title not in response.content.decode()
assert user_talk.title not in response.content.decode()
assert speaker_talk.title not in response.content.decode()
response = user_client.get(url, data={'filter': 'mine'})
assert not_voted.title not in response.content.decode()
assert voted.title not in response.content.decode()
assert user_talk.title in response.content.decode()
assert speaker_talk.title in response.content.decode()
@mock.patch('conference.talk_voting.is_user_allowed_to_vote', return_value=True)
def test_talk_voting_type_filters(mock_allowed_to_vote, user_client):
get_default_conference()
url = reverse("talk_voting:talks")
talk = create_talk_for_user(user=None, type=TALK_TYPE_CHOICES.t_30)
training = create_talk_for_user(user=None, type=TALK_TYPE_CHOICES.r_180)
poster = create_talk_for_user(user=None, type=TALK_TYPE_CHOICES.i_60)
helpdesk = create_talk_for_user(user=None, type=TALK_TYPE_CHOICES.h_180)
response = user_client.get(url, data={'talk_type': 'talk'})
assert response.status_code == 200
assert template_used(response, "conference/talk_voting/voting.html")
assert talk.title in response.content.decode()
assert training.title not in response.content.decode()
assert poster.title not in response.content.decode()
assert helpdesk.title not in response.content.decode()
response = user_client.get(url, data={'talk_type': 'training'})
assert talk.title not in response.content.decode()
assert training.title in response.content.decode()
assert poster.title not in response.content.decode()
assert helpdesk.title not in response.content.decode()
response = user_client.get(url, data={'talk_type': 'poster'})
assert talk.title not in response.content.decode()
assert training.title not in response.content.decode()
assert poster.title in response.content.decode()
assert helpdesk.title not in response.content.decode()
response = user_client.get(url, data={'talk_type': 'helpdesk'})
assert talk.title not in response.content.decode()
assert training.title not in response.content.decode()
assert poster.title not in response.content.decode()
assert helpdesk.title in response.content.decode()
@mock.patch('conference.talk_voting.is_user_allowed_to_vote', return_value=True)
def test_talk_voting_both_filters(mock_allowed_to_vote, user_client):
get_default_conference()
url = reverse("talk_voting:talks")
not_voted_talk = create_talk_for_user(user=None, type=TALK_TYPE_CHOICES.t_30)
voted_training = create_talk_for_user(user=None, type=TALK_TYPE_CHOICES.r_180)
VotoTalk.objects.create(talk=voted_training, user=user_client.user, vote=VotingOptions.maybe)
response = user_client.get(url, data={'talk_type': 'talk', 'filter': 'voted'})
assert response.status_code == 200
assert template_used(response, "conference/talk_voting/voting.html")
assert not_voted_talk.title not in response.content.decode()
assert voted_training.title not in response.content.decode()
response = user_client.get(url, data={'talk_type': 'training', 'filter': 'voted'})
assert not_voted_talk.title not in response.content.decode()
assert voted_training.title in response.content.decode()
response = user_client.get(url, data={'talk_type': 'talk', 'filter': 'not-voted'})
assert not_voted_talk.title in response.content.decode()
assert voted_training.title not in response.content.decode()
@mock.patch('conference.talk_voting.is_user_allowed_to_vote', return_value=True)
def test_talk_voting_hides_admin_talks(mock_allowed_to_vote, user_client):
get_default_conference()
url = reverse("talk_voting:talks")
talk = create_talk_for_user(user=None, admin_type=TALK_ADMIN_TYPE[0][0])
response = user_client.get(url)
assert response.status_code == 200
assert template_used(response, "conference/talk_voting/voting.html")
assert talk.title not in response.content.decode()
@mock.patch('conference.talk_voting.is_user_allowed_to_vote', return_value=True)
def test_talk_voting_hides_accepted_talks(mock_allowed_to_vote, user_client):
get_default_conference()
url = reverse("talk_voting:talks")
talk = create_talk_for_user(user=None, status=TALK_STATUS.accepted)
response = user_client.get(url)
assert response.status_code == 200
assert template_used(response, "conference/talk_voting/voting.html")
assert talk.title not in response.content.decode()
@mock.patch('conference.talk_voting.is_user_allowed_to_vote', return_value=True)
def test_vote_submission_forbidden_outside_of_talk_voting_period(mock_allowed_to_vote, user_client):
get_default_conference(voting_start=timezone.now() + timedelta(days=1))
talk = TalkFactory()
TalkSpeakerFactory(talk=talk)
url = reverse("talk_voting:vote", kwargs={'talk_uuid': talk.uuid})
response = user_client.post(url, data={'vote': VotingOptions.maybe})
assert response.status_code == 403
def test_vote_submission_allowed_for_users_with_talk_proposal(user_client):
get_default_conference()
create_talk_for_user(user=user_client.user)
talk = TalkFactory()
speaker = SpeakerFactory(user=make_user())
TalkSpeakerFactory(talk=talk, speaker=speaker)
url = reverse("talk_voting:vote", kwargs={'talk_uuid': talk.uuid})
user_client.post(url, data={'vote': VotingOptions.maybe})
assert VotoTalk.objects.count() == 1
def test_vote_submission_allowed_for_users_with_ticket(user_client):
get_default_conference()
TalkVotingTicketFactory(user=user_client.user)
talk = TalkFactory()
TalkSpeakerFactory(talk=talk)
url = reverse("talk_voting:vote", kwargs={'talk_uuid': talk.uuid})
user_client.post(url, data={'vote': VotingOptions.maybe})
assert VotoTalk.objects.count() == 1
def test_vote_submission_not_allowed_for_talk_created_by_user(user_client):
get_default_conference()
# User is speaker but did not create the talk
talk = TalkFactory(status=TALK_STATUS.proposed)
speaker = SpeakerFactory(user=user_client.user)
TalkSpeakerFactory(talk=talk, speaker=speaker)
url = reverse("talk_voting:vote", kwargs={'talk_uuid': talk.uuid})
user_client.post(url, data={'vote': VotingOptions.maybe})
assert VotoTalk.objects.count() == 0
def test_vote_submission_not_allowed_for_talk_where_user_is_speaker(user_client):
get_default_conference()
talk = TalkFactory(status=TALK_STATUS.proposed, created_by=user_client.user)
url = reverse("talk_voting:vote", kwargs={'talk_uuid': talk.uuid})
user_client.post(url, data={'vote': VotingOptions.maybe})
assert VotoTalk.objects.count() == 0
def test_vote_submission_not_allowed_with_no_ticket_or_talk_proposal(user_client):
get_default_conference()
talk = TalkFactory(status=TALK_STATUS.proposed)
url = reverse("talk_voting:vote", kwargs={'talk_uuid': talk.uuid})
response = user_client.post(url, data={'vote': VotingOptions.maybe})
assert response.status_code == 403
assert VotoTalk.objects.count() == 0
def test_dont_vote_talks_without_speaker_details(user_client):
get_default_conference()
talk = TalkFactory(status=TALK_STATUS.proposed)
url = reverse("talk_voting:vote", kwargs={'talk_uuid': talk.uuid})
user_client.post(url, data={'vote': VotingOptions.maybe})
assert VotoTalk.objects.count() == 0
@mock.patch('conference.talk_voting.is_user_allowed_to_vote', return_value=True)
def test_vote_talks_with_speaker_details(mock_allowed_to_vote, user_client):
get_default_conference()
talk = TalkFactory(status=TALK_STATUS.proposed)
speaker = SpeakerFactory(user=make_user())
TalkSpeakerFactory(talk=talk, speaker=speaker)
url = reverse("talk_voting:vote", kwargs={'talk_uuid': talk.uuid})
user_client.post(url, data={'vote': VotingOptions.maybe})
assert VotoTalk.objects.count() == 1
@mock.patch('conference.talk_voting.is_user_allowed_to_vote', return_value=True)
def test_view_talks_without_speaker_details_not_visible(mock_allowed_to_vote, user_client):
get_default_conference()
talk = TalkFactory(status=TALK_STATUS.proposed, title="DeadBeef")
talk2 = TalkFactory(status=TALK_STATUS.proposed, title="TestProposalForTests")
speaker = SpeakerFactory(user=make_user())
TalkSpeakerFactory(talk=talk2, speaker=speaker)
url = reverse("talk_voting:talks")
response = user_client.get(url)
assert talk.title not in response.content.decode("utf8")
assert talk2.title in response.content.decode("utf8")
def test_helper_query_talks_without_speaker_details_not_visible(user_client):
get_default_conference()
talk = TalkFactory(status=TALK_STATUS.proposed, title="DeadBeef")
talk2 = TalkFactory(status=TALK_STATUS.proposed, title="TestProposalForTests")
speaker = SpeakerFactory(user=make_user())
TalkSpeakerFactory(talk=talk2, speaker=speaker)
talks = find_talks(user_client.user, Conference.objects.current(), [])
assert talk not in talks
assert talk2 in talks