def test_grant_deferred_award(self): """Deferred award for a badge can be granted to an email address.""" deferred_email = "*****@*****.**" user1 = self._get_user(username="******", email="*****@*****.**") b1 = Badge.objects.create(creator=user1, title="Badge to defer") da = DeferredAward(badge=b1, creator=user1, email='*****@*****.**') da.save() url = da.get_claim_url() self.client.login(username="******", password="******") r = self.client.get(url, follow=True) eq_(200, r.status_code) doc = pq(r.content) form = doc('form#grant_award') eq_(1, form.length) eq_(1, form.find('*[name=email]').length) eq_(1, form.find('input.submit,button.submit').length) r = self.client.post(url, dict( is_grant=1, email=deferred_email, ), follow=False) user2 = self._get_user(username="******", email=deferred_email) self.client.login(username="******", password="******") r = self.client.get(reverse('badger.views.detail', args=(b1.slug,)), follow=True) ok_(b1.is_awarded_to(user2))
def test_deferred_award_immediate_claim(self): """Ensure that a deferred award can be immediately claimed rather than viewing detail""" deferred_email = "*****@*****.**" user1 = self._get_user(username="******", email="*****@*****.**") b1 = Badge.objects.create(creator=user1, title="Badge to defer") da = DeferredAward(badge=b1, creator=user1) da.save() url = da.get_claim_url() # Just viewing the claim URL shouldn't require login. r = self.client.get(url, follow=False) eq_(200, r.status_code) # But, attempting to claim the award should require login r = self.client.post(reverse('badger.views.claim_deferred_award'), dict( code=da.claim_code, ), follow=False) eq_(302, r.status_code) ok_('login' in r['Location']) # So, try logging in and fetch the immediate-claim URL user2 = self._get_user(username="******", email=deferred_email) self.client.login(username="******", password="******") r = self.client.post(reverse('badger.views.claim_deferred_award'), dict( code=da.claim_code, ), follow=False) eq_(302, r.status_code) ok_('awards' in r['Location']) ok_(b1.is_awarded_to(user2))
def test_claim_code_shows_awards_after_claim(self): """Claim code URL should lead to award detail or list after claim""" user1 = self._get_user(username="******", email="*****@*****.**") user2 = self._get_user(username="******", email="*****@*****.**") b1 = Badge.objects.create(creator=user1, unique=False, title="Badge for claim viewing") da = DeferredAward(badge=b1, creator=user1) da.save() url = da.get_claim_url() # Before claim, code URL leads to claim page. r = self.client.get(url, follow=False) eq_(200, r.status_code) doc = pq(r.content) form = doc('form#claim_award') # After claim, code URL leads to a single award detail page. award = da.claim(user2) r = self.client.get(url, follow=False) eq_(302, r.status_code) award_url = reverse('badger.views.award_detail', args=(award.badge.slug, award.pk)) ok_(award_url in r['Location'])
def test_claim_code_shows_awards_after_claim(self): """Claim code URL should lead to award detail or list after claim""" user1 = self._get_user(username="******", email="*****@*****.**") user2 = self._get_user(username="******", email="*****@*****.**") b1 = Badge.objects.create(creator=user1, unique=False, title="Badge for claim viewing") da = DeferredAward(badge=b1, creator=user1, reusable=True) da.save() url = reverse('badger.views.claim_deferred_award', args=(da.claim_code,)) # First claim leads to a single award detail page. award = da.claim(user2) r = self.client.get(url, follow=False) eq_(302, r.status_code) award_url = reverse('badger.views.award_detail', args=(award.badge.slug, award.pk)) ok_(award_url in r['Location']) # Second claim leads to a list of awards. award = da.claim(user2) r = self.client.get(url, follow=False) eq_(302, r.status_code) list_url = reverse('badger.views.awards_list', args=(award.badge.slug,)) ok_(list_url in r['Location'])
def test_claim_by_code(self): """Can claim a deferred award by claim code""" user = self._get_user() awardee = self._get_user(username='******', email='*****@*****.**') badge1 = self._get_badge(title="Test A", creator=user) ok_(not badge1.is_awarded_to(awardee)) da = DeferredAward(badge=badge1) da.save() code = da.claim_code eq_(1, DeferredAward.objects.filter(claim_code=code).count()) DeferredAward.objects.claim_by_code(awardee, code) eq_(0, DeferredAward.objects.filter(claim_code=code).count()) ok_(badge1.is_awarded_to(awardee))
def test_reusable_claim(self): """Can repeatedly claim a reusable deferred award""" user = self._get_user() awardee = self._get_user(username='******', email='*****@*****.**') badge1 = self._get_badge(title="Test A", creator=user, unique=False) ok_(not badge1.is_awarded_to(awardee)) da = DeferredAward(badge=badge1, reusable=True) da.save() code = da.claim_code for i in range(0, 5): eq_(1, DeferredAward.objects.filter(claim_code=code).count()) DeferredAward.objects.claim_by_code(awardee, code) ok_(badge1.is_awarded_to(awardee)) eq_(5, Award.objects.filter(badge=badge1, user=awardee).count())
def test_disallowed_claim(self): """Deferred award created by someone not allowed to award a badge cannot be claimed""" user = self._get_user() random_guy = self._get_user(username='******', is_superuser=False) awardee = self._get_user(username='******', email='*****@*****.**') badge1 = self._get_badge(title="Test A", creator=user) ok_(not badge1.is_awarded_to(awardee)) da = DeferredAward(badge=badge1, creator=random_guy) da.save() code = da.claim_code eq_(1, DeferredAward.objects.filter(claim_code=code).count()) result = DeferredAward.objects.claim_by_code(awardee, code) eq_(0, DeferredAward.objects.filter(claim_code=code).count()) ok_(not badge1.is_awarded_to(awardee))
def test_claim_code_shows_awards_after_claim(self): """Claim code URL should lead to award detail or list after claim""" user1 = self._get_user(username="******", email="*****@*****.**") user2 = self._get_user(username="******", email="*****@*****.**") b1 = Badge.objects.create(creator=user1, unique=False, title="Badge for claim viewing") da = DeferredAward(badge=b1, creator=user1, reusable=True) da.save() url = reverse('badger.views.claim_deferred_award', args=(da.claim_code, )) # First claim leads to a single award detail page. award = da.claim(user2) r = self.client.get(url, follow=False) eq_(302, r.status_code) award_url = reverse('badger.views.award_detail', args=(award.badge.slug, award.pk)) ok_(award_url in r['Location']) # Second claim leads to a list of awards. award = da.claim(user2) r = self.client.get(url, follow=False) eq_(302, r.status_code) list_url = reverse('badger.views.awards_list', args=(award.badge.slug, )) ok_(list_url in r['Location'])
def test_claim_by_code(self): """Can claim a deferred award by claim code""" user = self._get_user() awardee = self._get_user(username='******', email='*****@*****.**') badge1 = self._get_badge(title="Test A", creator=user) ok_(not badge1.is_awarded_to(awardee)) da = DeferredAward(badge=badge1) da.save() code = da.claim_code eq_(1, DeferredAward.objects.filter(claim_code=code).count()) DeferredAward.objects.claim_by_code(awardee, code) eq_(0, DeferredAward.objects.filter(claim_code=code).count()) ok_(badge1.is_awarded_to(awardee)) # Ensure the award was marked with the claim code. award = Award.objects.get(claim_code=code) eq_(award.badge.pk, badge1.pk)
def test_granted_claim(self): """Reusable deferred award can be granted to someone by email""" # Assemble the characters involved... creator = self._get_user() random_guy = self._get_user(username='******', email='*****@*****.**', is_superuser=False) staff_person = self._get_user(username='******', email='*****@*****.**', is_staff=True) grantee_email = '*****@*****.**' grantee = self._get_user(username='******', email=grantee_email) # Create a consumable award claim badge1 = self._get_badge(title="Test A", creator=creator) original_email = '*****@*****.**' da = DeferredAward(badge=badge1, creator=creator, email=original_email) claim_code = da.claim_code da.save() # Grant the deferred award, ensure the existing one is modified. new_da = da.grant_to(email=grantee_email, granter=staff_person) ok_(claim_code != new_da.claim_code) ok_(da.email != original_email) eq_(da.pk, new_da.pk) eq_(new_da.email, grantee_email) # Claim the deferred award, assert that the appropriate deferred award # was destroyed eq_(1, DeferredAward.objects.filter(pk=da.pk).count()) eq_(1, DeferredAward.objects.filter(pk=new_da.pk).count()) DeferredAward.objects.claim_by_email(grantee) eq_(0, DeferredAward.objects.filter(pk=da.pk).count()) eq_(0, DeferredAward.objects.filter(pk=new_da.pk).count()) # Finally, assert the award condition ok_(badge1.is_awarded_to(grantee)) # Create a reusable award claim badge2 = self._get_badge(title="Test B", creator=creator) da = DeferredAward(badge=badge2, creator=creator, reusable=True) claim_code = da.claim_code da.save() # Grant the deferred award, ensure a new deferred award is generated. new_da = da.grant_to(email=grantee_email, granter=staff_person) ok_(claim_code != new_da.claim_code) ok_(da.pk != new_da.pk) eq_(new_da.email, grantee_email) # Claim the deferred award, assert that the appropriate deferred award # was destroyed eq_(1, DeferredAward.objects.filter(pk=da.pk).count()) eq_(1, DeferredAward.objects.filter(pk=new_da.pk).count()) DeferredAward.objects.claim_by_email(grantee) eq_(1, DeferredAward.objects.filter(pk=da.pk).count()) eq_(0, DeferredAward.objects.filter(pk=new_da.pk).count()) # Finally, assert the award condition ok_(badge2.is_awarded_to(grantee)) # Create one more award claim badge3 = self._get_badge(title="Test C", creator=creator) da = DeferredAward(badge=badge3, creator=creator) claim_code = da.claim_code da.save() # Grant the deferred award, ensure a new deferred award is generated. try: new_da = da.grant_to(email=grantee_email, granter=random_guy) is_ok = False except Exception, e: ok_(type(e) is DeferredAwardGrantNotAllowedException) is_ok = True