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
369
370
371
372
373
374
375
376
377
378
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
import datetime
from django.test.client import Client
from askbot.tests.utils import AskbotTestCase
from askbot.conf import settings
from askbot import models
from askbot.models.badges import award_badges_signal
class BadgeTests(AskbotTestCase):
def setUp(self):
self.u1 = self.create_user(username = 'user1')
self.u2 = self.create_user(username = 'user2')
self.u3 = self.create_user(username = 'user3')
self.client = Client()
def assert_have_badge(self, badge_key, recipient = None, expected_count = 1):
"""note - expected_count matches total number of
badges within test, not the one that was awarded between the calls
to this assertion"""
filters = {'badge__slug': badge_key, 'user': recipient}
count = models.Award.objects.filter(**filters).count()
self.assertEquals(count, expected_count)
def assert_accepted_answer_badge_works(self,
badge_key = None,
min_score = None,
expected_count = 1,
previous_count = 0,
trigger = None
):
assert(trigger in ('accept_best_answer', 'upvote_answer'))
question = self.post_question(user = self.u1)
answer = self.post_answer(user = self.u2, question = question)
answer.score = min_score - 1
answer.save()
recipient = answer.author
if trigger == 'accept_best_answer':
self.u1.upvote(answer)
self.assert_have_badge(badge_key, recipient, previous_count)
self.u1.accept_best_answer(answer)
else:
self.u1.accept_best_answer(answer)
self.assert_have_badge(badge_key, recipient, previous_count)
self.u1.upvote(answer)
self.assert_have_badge(badge_key, recipient, expected_count)
def assert_upvoted_answer_badge_works(self,
badge_key = None,
min_score = None,
multiple = False
):
"""test answer badge where answer author is the recipient
where badge award is triggered by upvotes
* min_score - minimum # of upvotes required
* multiple - multiple award or not
* badge_key - key on askbot.models.badges.Badge object
"""
question = self.post_question(user = self.u1)
answer = self.post_answer(user = self.u2, question = question)
answer.score = min_score - 1
answer.save()
self.u1.upvote(answer)
self.assert_have_badge(badge_key, recipient = self.u2)
self.u3.upvote(answer)
self.assert_have_badge(badge_key, recipient = self.u2, expected_count = 1)
#post another question and check that there are no new badges
answer2 = self.post_answer(user = self.u2, question = question)
answer2.score = min_score - 1
answer2.save()
self.u1.upvote(answer2)
if multiple == True:
expected_count = 2
else:
expected_count = 1
self.assert_have_badge(
badge_key,
recipient = self.u2,
expected_count = expected_count
)
def assert_upvoted_question_badge_works(self,
badge_key = None,
min_score = None,
multiple = False
):
"""test question badge where question author is the recipient
where badge award is triggered by upvotes
* min_score - minimum # of upvotes required
* multiple - multiple award or not
* badge_key - key on askbot.models.badges.Badge object
"""
question = self.post_question(user = self.u1)
question.score = min_score - 1
question.save()
self.u2.upvote(question)
self.assert_have_badge(badge_key, recipient = self.u1)
self.u3.upvote(question)
self.assert_have_badge(badge_key, recipient = self.u1, expected_count = 1)
#post another question and check that there are no new badges
question2 = self.post_question(user = self.u1)
question2.score = min_score - 1
question2.save()
self.u2.upvote(question2)
if multiple == True:
expected_count = 2
else:
expected_count = 1
self.assert_have_badge(
badge_key,
recipient = self.u1,
expected_count = expected_count
)
def test_disciplined_badge(self):
question = self.post_question(user = self.u1)
question.score = settings.DISCIPLINED_BADGE_MIN_UPVOTES
question.save()
self.u1.delete_question(question)
self.assert_have_badge('disciplined', recipient = self.u1)
question2 = self.post_question(user = self.u1)
question2.score = settings.DISCIPLINED_BADGE_MIN_UPVOTES
question2.save()
self.u1.delete_question(question2)
self.assert_have_badge('disciplined', recipient = self.u1, expected_count = 2)
def test_peer_pressure_badge(self):
question = self.post_question(user = self.u1)
answer = self.post_answer(user = self.u1, question = question)
answer.score = -1*settings.PEER_PRESSURE_BADGE_MIN_DOWNVOTES
answer.save()
self.u1.delete_answer(answer)
self.assert_have_badge('peer-pressure', recipient = self.u1)
def test_teacher_badge(self):
self.assert_upvoted_answer_badge_works(
badge_key = 'teacher',
min_score = settings.TEACHER_BADGE_MIN_UPVOTES,
multiple = False
)
def test_nice_answer_badge(self):
self.assert_upvoted_answer_badge_works(
badge_key = 'nice-answer',
min_score = settings.NICE_ANSWER_BADGE_MIN_UPVOTES,
multiple = True
)
def test_nice_question_badge(self):
self.assert_upvoted_question_badge_works(
badge_key = 'nice-question',
min_score = settings.NICE_QUESTION_BADGE_MIN_UPVOTES,
multiple = True
)
def test_popular_question_badge(self):
question = self.post_question(user = self.u1)
min_views = settings.POPULAR_QUESTION_BADGE_MIN_VIEWS
question.thread.view_count = min_views - 1
question.thread.save()
#patch not_a_robot_request to return True
from askbot.utils import functions
functions.not_a_robot_request = lambda v: True
url = question.get_absolute_url()
self.client.login(method='force', user_id = self.u2.id)
self.client.get(url)
self.assert_have_badge('popular-question', recipient = self.u1)
self.client.login(method='force', user_id = self.u3.id)
self.client.get(url)
self.assert_have_badge('popular-question', recipient = self.u1, expected_count = 1)
question2 = self.post_question(user = self.u1)
question2.thread.view_count = min_views - 1
question2.thread.save()
self.client.login(method='force', user_id = self.u2.id)
self.client.get(question2.get_absolute_url())
self.assert_have_badge('popular-question', recipient = self.u1, expected_count = 2)
def test_student_badge(self):
question = self.post_question(user = self.u1)
self.u2.upvote(question)
self.assert_have_badge('student', recipient = self.u1)
self.u3.upvote(question)
self.assert_have_badge('student', recipient = self.u1, expected_count = 1)
question2 = self.post_question(user = self.u1)
self.u2.upvote(question)
self.assert_have_badge('student', recipient = self.u1, expected_count = 1)
def test_supporter_badge(self):
question = self.post_question(user = self.u1)
self.u2.upvote(question)
self.assert_have_badge('supporter', recipient = self.u2)
answer = self.post_answer(user = self.u1, question = question)
self.u3.upvote(answer)
self.assert_have_badge('supporter', recipient = self.u3)
self.u2.upvote(answer)
self.assert_have_badge('supporter', recipient = self.u2, expected_count = 1)
def test_critic_badge(self):
question = self.post_question(user = self.u1)
self.u2.downvote(question)
self.assert_have_badge('critic', recipient = self.u2)
answer = self.post_answer(user = self.u1, question = question)
self.u3.downvote(answer)
self.assert_have_badge('critic', recipient = self.u3)
self.u2.downvote(answer)
self.assert_have_badge('critic', recipient = self.u2, expected_count = 1)
def test_self_learner_badge(self):
question = self.post_question(user = self.u1)
answer = self.post_answer(user = self.u1, question = question)
min_votes = settings.SELF_LEARNER_BADGE_MIN_UPVOTES
answer.score = min_votes - 1
answer.save()
self.u2.upvote(answer)
self.assert_have_badge('self-learner', recipient = self.u1)
#copy-paste of the first question, except expect second badge
question = self.post_question(user = self.u1)
answer = self.post_answer(user = self.u1, question = question)
answer.score = min_votes - 1
answer.save()
self.u2.upvote(answer)
self.assert_have_badge('self-learner', recipient = self.u1, expected_count = 2)
question = self.post_question(user = self.u2)
answer = self.post_answer(user = self.u1, question = question)
answer.score = min_votes - 1
answer.save()
self.u2.upvote(answer)
#no badge when asker != answerer
self.assert_have_badge(
'self-learner',
recipient = self.u1,
expected_count = 2
)
def test_civic_duty_badge(self):
settings.update('CIVIC_DUTY_BADGE_MIN_VOTES', 2)
question = self.post_question(user = self.u1)
answer = self.post_answer(user = self.u2, question = question)
answer2 = self.post_answer(user = self.u1, question = question)
self.u3.upvote(question)
self.u3.downvote(answer)
self.assert_have_badge('civic-duty', recipient = self.u3)
self.u3.upvote(answer2)
self.assert_have_badge('civic-duty', recipient = self.u3, expected_count = 1)
self.u3.downvote(answer)
self.assert_have_badge('civic-duty', recipient = self.u3, expected_count = 1)
def test_scholar_badge(self):
question = self.post_question(user = self.u1)
answer = self.post_answer(user = self.u2, question = question)
self.u1.accept_best_answer(answer)
self.assert_have_badge('scholar', recipient = self.u1)
answer2 = self.post_answer(user = self.u2, question = question)
self.u1.accept_best_answer(answer2)
self.assert_have_badge(
'scholar',
recipient = self.u1,
expected_count=1
)
def assert_enlightened_badge_works(self, trigger):
self.assert_accepted_answer_badge_works(
'enlightened',
min_score = settings.ENLIGHTENED_BADGE_MIN_UPVOTES,
expected_count = 1,
trigger = trigger
)
self.assert_accepted_answer_badge_works(
'enlightened',
min_score = settings.ENLIGHTENED_BADGE_MIN_UPVOTES,
expected_count = 1,
previous_count = 1,
trigger = trigger
)
def assert_guru_badge_works(self, trigger):
self.assert_accepted_answer_badge_works(
'guru',
min_score = settings.GURU_BADGE_MIN_UPVOTES,
expected_count = 1,
trigger = trigger
)
self.assert_accepted_answer_badge_works(
'guru',
min_score = settings.GURU_BADGE_MIN_UPVOTES,
previous_count = 1,
expected_count = 2,
trigger = trigger
)
def test_enlightened_badge1(self):
self.assert_enlightened_badge_works('upvote_answer')
def test_enlightened_badge2(self):
self.assert_enlightened_badge_works('accept_best_answer')
def test_guru_badge1(self):
self.assert_guru_badge_works('upvote_answer')
def test_guru_badge1(self):
self.assert_guru_badge_works('accept_best_answer')
def test_necromancer_badge(self):
question = self.post_question(user = self.u1)
now = datetime.datetime.now()
delta = datetime.timedelta(settings.NECROMANCER_BADGE_MIN_DELAY + 1)
future = now + delta
answer = self.post_answer(
user = self.u2,
question = question,
timestamp = future
)
answer.score = settings.NECROMANCER_BADGE_MIN_UPVOTES - 1
answer.save()
self.assert_have_badge('necromancer', self.u2, expected_count = 0)
self.u1.upvote(answer)
self.assert_have_badge('necromancer', self.u2, expected_count = 1)
def test_citizen_patrol_question(self):
self.u2.set_status('m')
question = self.post_question(user = self.u1)
self.u2.flag_post(question)
self.assert_have_badge('citizen-patrol', self.u2)
question = self.post_question(user = self.u1)
self.u2.flag_post(question)
self.assert_have_badge('citizen-patrol', self.u2, 1)
def test_citizen_patrol_answer(self):
self.u2.set_status('m')
question = self.post_question(user = self.u1)
answer = self.post_answer(user = self.u1, question = question)
self.u2.flag_post(answer)
self.assert_have_badge('citizen-patrol', self.u2)
question = self.post_question(user = self.u1)
answer = self.post_answer(user = self.u1, question = question)
self.u2.flag_post(answer)
self.assert_have_badge('citizen-patrol', self.u2, 1)
def test_editor_badge_question(self):
self.u2.set_status('m')
question = self.post_question(user = self.u1)
self.u2.edit_question(
question = question,
title = 'hahaha',
body_text = 'heheeh',
revision_comment = 'ihihih'
)
self.assert_have_badge('editor', self.u2, 1)
#double check that its not multiple
question = self.post_question(user = self.u1)
self.u2.edit_question(
question = question,
title = 'hahaha',
body_text = 'heheeh',
revision_comment = 'ihihih'
)
self.assert_have_badge('editor', self.u2, 1)
def test_editor_badge_answer(self):
self.u2.set_status('m')
question = self.post_question(user = self.u1)
answer = self.post_answer(user = self.u1, question = question)
self.u2.edit_answer(answer = answer, body_text = 'hahaha')
self.assert_have_badge('editor', self.u2, 1)
#double check that its not multiple
question = self.post_question(user = self.u1)
answer = self.post_answer(user = self.u1, question = question)
self.u2.edit_answer(answer = answer, body_text = 'hahaha')
self.assert_have_badge('editor', self.u2, 1)
def test_associate_editor_badge(self):
self.u2.set_status('m')
question = self.post_question(user = self.u1)
settings.update('ASSOCIATE_EDITOR_BADGE_MIN_EDITS', 2)
self.u2.edit_question(
question = question,
title = 'hahaha',
body_text = 'sdgsdjghsldkfshd',
revision_comment = 'sdgdfgsgfs'
)
self.assert_have_badge('strunk-and-white', self.u2, 0)
self.u2.edit_question(
question = question,
title = 'hahaha',
body_text = 'sdgsdjghsldkfshd',
revision_comment = 'sdgdfgsgfs'
)
self.assert_have_badge('strunk-and-white', self.u2, 1)
self.u2.edit_question(
question = question,
title = 'hahaha',
body_text = 'sdgsdjghsldkfshd',
revision_comment = 'sdgdfgsgfs'
)
self.assert_have_badge('strunk-and-white', self.u2, 1)
def test_organizer_badge(self):
question = self.post_question(user = self.u1)
self.u1.retag_question(question = question, tags = 'blah boom')
self.assert_have_badge('organizer', self.u1, 1)
self.u1.retag_question(question = question, tags = 'blah pooh')
self.assert_have_badge('organizer', self.u1, 1)
def test_autobiographer_badge(self):
self.u1.real_name = 'blah'
self.u1.website = 'cnn.com'
self.u1.location = 'irvine'
self.u1.about = 'blah'
self.u1.save()
award_badges_signal.send(None,
event = 'update_user_profile',
actor = self.u1,
context_object = self.u1
)
self.assert_have_badge('autobiographer', self.u1, 1)
award_badges_signal.send(None,
event = 'update_user_profile',
actor = self.u1,
context_object = self.u1
)
self.assert_have_badge('autobiographer', self.u1, 1)
def test_stellar_badge1(self):
question = self.post_question(user = self.u1)
settings.update('STELLAR_QUESTION_BADGE_MIN_STARS', 2)
self.u2.toggle_favorite_question(question)
self.assert_have_badge('stellar-question', self.u1, 0)
self.u3.toggle_favorite_question(question)
self.assert_have_badge('stellar-question', self.u1, 1)
def test_stellar_badge2(self):
question = self.post_question(user = self.u1)
settings.update('STELLAR_QUESTION_BADGE_MIN_STARS', 2)
self.u2.toggle_favorite_question(question)
self.assert_have_badge('stellar-question', self.u1, 0)
self.u1.toggle_favorite_question(question)
"""no gaming"""
self.assert_have_badge('stellar-question', self.u1, 0)
def test_stellar_badge3(self):
question = self.post_question(user = self.u1)
settings.update('STELLAR_QUESTION_BADGE_MIN_STARS', 2)
self.u2.toggle_favorite_question(question)
self.assert_have_badge('stellar-question', self.u1, 0)
self.u3.toggle_favorite_question(question)
#award now
self.assert_have_badge('stellar-question', self.u1, 1)
self.u3.toggle_favorite_question(question)
#dont take back
self.assert_have_badge('stellar-question', self.u1, 1)
self.u3.toggle_favorite_question(question)
#dont reaward
self.assert_have_badge('stellar-question', self.u1, 1)
def test_commentator_badge(self):
question = self.post_question(user = self.u1)
min_comments = settings.COMMENTATOR_BADGE_MIN_COMMENTS
for i in xrange(min_comments - 1):
self.post_comment(user = self.u1, parent_post = question)
self.assert_have_badge('commentator', self.u1, 0)
self.post_comment(user = self.u1, parent_post = question)
self.assert_have_badge('commentator', self.u1, 1)
self.post_comment(user = self.u1, parent_post = question)
self.assert_have_badge('commentator', self.u1, 1)
def test_taxonomist_badge(self):
self.post_question(user = self.u1, tags = 'test')
min_use = settings.TAXONOMIST_BADGE_MIN_USE_COUNT
for i in xrange(min_use - 2):
self.post_question(user = self.u2, tags = 'test')
self.assert_have_badge('taxonomist', self.u1, 0)
self.post_question(user = self.u2, tags = 'test')
self.assert_have_badge('taxonomist', self.u1, 1)
def test_enthusiast_badge(self):
yesterday = datetime.datetime.now() - datetime.timedelta(1)
self.u1.last_seen = yesterday
prev_visit_count = settings.ENTHUSIAST_BADGE_MIN_DAYS - 1
self.u1.consecutive_days_visit_count = prev_visit_count
self.u1.save()
self.assert_have_badge('enthusiast', self.u1, 0)
self.client.login(method = 'force', user_id = self.u1.id)
self.client.get('/')
self.assert_have_badge('enthusiast', self.u1, 1)
|