Пример #1
0
    def test_delete(self):
        """Can delete badge"""
        user = self._get_user()
        badge = Badge(creator=user, title="Test III",
                      description="Another test")
        badge.save()
        slug = badge.slug

        badge.award_to(user)

        self.client.login(username="******", password="******")

        r = self.client.get(reverse('badger.views.detail',
            args=(badge.slug,)), follow=True)
        doc = pq(r.content)

        eq_('badge_detail', doc.find('body').attr('id'))
        delete_url = doc.find('a.delete_badge').attr('href')
        ok_(delete_url is not None)

        r = self.client.get(delete_url)
        doc = pq(r.content)
        eq_('badge_delete', doc.find('body').attr('id'))
        eq_("1", doc.find('.awards_count').text())

        r = self.client.post(delete_url, {}, follow=True)
        doc = pq(r.content)

        try:
            badge = Badge.objects.get(slug=slug)
            ok_(False)
        except Badge.DoesNotExist:
            ok_(True)
Пример #2
0
    def test_edit(self):
        """Can edit badge detail"""
        user = self._get_user()
        badge = Badge(creator=user, title="Test II", description="Another test")
        badge.save()

        self.client.login(username="******", password="******")

        r = self.client.get(reverse("badger.views.detail", args=(badge.slug,)), follow=True)
        doc = pq(r.content)

        eq_("badge_detail", doc.find("body").attr("id"))
        edit_url = doc.find("a.edit_badge").attr("href")
        ok_(edit_url is not None)

        r = self.client.get(edit_url)
        doc = pq(r.content)
        eq_("badge_edit", doc.find("body").attr("id"))

        badge_title = "Edited title"
        badge_desc = "Edited description"

        r = self.client.post(edit_url, dict(title=badge_title, description=badge_desc), follow=True)
        doc = pq(r.content)

        eq_("badge_detail", doc.find("body").attr("id"))
        ok_(badge_title in doc.find(".badge .title").text())
        eq_(badge_desc, doc.find(".badge .description").text())

        slug = doc.find(".badge").attr("data-slug")

        badge = Badge.objects.get(slug=slug)
        eq_(badge_title, badge.title)
        eq_(badge_desc, badge.description)
Пример #3
0
    def test_badge_detail(self):
        """Can view badge detail"""
        user = self._get_user()
        badge = Badge(creator=user,
                      title="Test II",
                      description="Another test")
        badge.save()

        r = self.client.get(reverse('badger.views.detail',
                                    args=(badge.slug, )),
                            follow=True)
        doc = pq(r.content)

        eq_('badge_detail', doc.find('body').attr('id'))
        eq_(1, doc.find('.badge .title:contains("%s")' % badge.title).length)
        eq_(badge.description, doc.find('.badge .description').text())

        # Now, take a look at the JSON format
        url = reverse('badger.detail_json', args=(badge.slug, ))
        r = self.client.get(url, follow=True)

        data = json.loads(r.content)
        eq_(badge.title, data['name'])
        eq_(badge.description, data['description'])
        eq_('http://testserver%s' % badge.get_absolute_url(), data['criteria'])
Пример #4
0
    def test_delete_award(self):
        """Can delete award"""
        user = self._get_user()
        badge = Badge(creator=user, title="Test III",
                      description="Another test")
        badge.save()

        award = badge.award_to(user)

        self.client.login(username="******", password="******")

        r = self.client.get(reverse('badger.views.award_detail',
            args=(badge.slug, award.id)), follow=True)
        doc = pq(r.content)

        eq_('award_detail', doc.find('body').attr('id'))
        delete_url = doc.find('a.delete_award').attr('href')
        ok_(delete_url is not None)

        r = self.client.post(delete_url, {}, follow=True)

        try:
            award = Award.objects.get(pk=award.pk)
            ok_(False)
        except Award.DoesNotExist:
            ok_(True)
    def setUp(self):
        self.client = Client()

        self.testuser = self._get_user()

        self.badge = Badge(creator=self.testuser,
                           title="Test II",
                           description="Another test",
                           unique=True)
        self.badge.save()

        self.awards_url = reverse('badger.views.awards_list',
                                  args=(self.badge.slug, ))

        key = Key()
        key.user = self.testuser
        self.password = key.generate_secret()
        self.username = key.key
        key.save()
        self.key = key

        auth_ct = '%s:%s' % (self.username, self.password)
        self.basic_auth = 'Basic %s' % b64encode(auth_ct)
        self.headers = {'HTTP_AUTHORIZATION': self.basic_auth}

        Award.objects.all().delete()
Пример #6
0
    def test_delete_award(self):
        """Can delete award"""
        user = self._get_user()
        badge = Badge(creator=user, title="Test III",
                      description="Another test")
        badge.save()

        award = badge.award_to(user)

        self.client.login(username="******", password="******")

        r = self.client.get(reverse('badger.views.award_detail',
            args=(badge.slug, award.id)), follow=True)
        doc = pq(r.content)

        eq_('award_detail', doc.find('body').attr('id'))
        delete_url = doc.find('a.delete_award').attr('href')
        ok_(delete_url is not None)

        r = self.client.post(delete_url, {}, follow=True)

        try:
            award = Award.objects.get(pk=award.pk)
            ok_(False)
        except Award.DoesNotExist:
            ok_(True)
Пример #7
0
    def setUp(self):
        self.creator = self._get_user(username="******")
        self.testuser = self._get_user()
        self.mw = RecentBadgeAwardsMiddleware()

        self.badges = []
        for n in ("test1", "test2", "test3"):
            badge = Badge(title=n, creator=self.creator)
            badge.save()
            self.badges.append(badge)

        self.awards = []
        for b in self.badges:
            self.awards.append(b.award_to(self.testuser))
Пример #8
0
    def setUp(self):
        self.creator = self._get_user(username='******')
        self.testuser = self._get_user()
        self.mw = RecentBadgeAwardsMiddleware()

        self.badges = []
        for n in ('test1', 'test2', 'test3'):
            badge = Badge(title=n, creator=self.creator)
            badge.save()
            self.badges.append(badge)

        self.awards = []
        for b in self.badges:
            self.awards.append(b.award_to(self.testuser))
Пример #9
0
def badge(**kwargs):
    defaults = {
        'title': u'BADGE!',
        'description': u'A badge',
    }
    defaults.update(kwargs)

    return Badge(**defaults)
Пример #10
0
    def test_edit_preserves_creator(self):
        """Edit preserves the original creator of the badge (bugfix)"""
        orig_user = self._get_user(username="******")
        badge = Badge(creator=orig_user, title="Test 3", description="Another test")
        badge.save()

        edit_user = self._get_user(username="******")
        edit_user.is_superuser = True
        edit_user.save()

        self.client.login(username="******", password="******")
        edit_url = reverse("badger.views.edit", args=(badge.slug,))
        r = self.client.post(edit_url, dict(title="New Title"), follow=True)
        doc = pq(r.content)

        # The badge's creator should not have changed to the editing user.
        badge_after = Badge.objects.get(pk=badge.pk)
        ok_(badge_after.creator.pk != edit_user.pk)
Пример #11
0
    def test_edit(self):
        """Can edit badge detail"""
        user = self._get_user()
        badge = Badge(creator=user,
                      title="Test II",
                      description="Another test")
        badge.save()

        self.client.login(username="******", password="******")

        r = self.client.get(reverse('badger.views.detail',
                                    args=(badge.slug, )),
                            follow=True)
        doc = pq(r.content)

        eq_('badge_detail', doc.find('body').attr('id'))
        edit_url = doc.find('a.edit_badge').attr('href')
        ok_(edit_url is not None)

        r = self.client.get(edit_url)
        doc = pq(r.content)
        eq_('badge_edit', doc.find('body').attr('id'))

        badge_title = "Edited title"
        badge_desc = "Edited description"

        r = self.client.post(edit_url,
                             dict(
                                 title=badge_title,
                                 description=badge_desc,
                             ),
                             follow=True)
        doc = pq(r.content)

        eq_('badge_detail', doc.find('body').attr('id'))
        ok_(badge_title in doc.find('.badge .title').text())
        eq_(badge_desc, doc.find('.badge .description').text())

        slug = doc.find('.badge').attr('data-slug')

        badge = Badge.objects.get(slug=slug)
        eq_(badge_title, badge.title)
        eq_(badge_desc, badge.description)
Пример #12
0
    def test_disallowed_badge_award(self):
        """User should not be able to POST an award to a badge for which the
        user hasn't got permission to do so"""
        user = self._get_user(username="******",
                              email="*****@*****.**")
        badge = Badge(creator=user, title="Hey you badge",
                      description="Another test", unique=True)
        badge.save()

        awards_url = reverse('badger.views.awards_list',
                             args=(self.badge.slug,))

        data = dict(emails=['*****@*****.**',])

        resp = self.client.post(awards_url, json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION=self.basic_auth)

        ok_(403, resp.status_code)
Пример #13
0
    def test_edit_preserves_creator(self):
        """Edit preserves the original creator of the badge (bugfix)"""
        orig_user = self._get_user(username='******')
        badge = Badge(creator=orig_user,
                      title="Test 3",
                      description="Another test")
        badge.save()

        edit_user = self._get_user(username='******')
        edit_user.is_superuser = True
        edit_user.save()

        self.client.login(username="******", password="******")
        edit_url = reverse('badger.views.edit', args=(badge.slug, ))
        r = self.client.post(edit_url, dict(title='New Title', ), follow=True)
        doc = pq(r.content)

        # The badge's creator should not have changed to the editing user.
        badge_after = Badge.objects.get(pk=badge.pk)
        ok_(badge_after.creator.pk != edit_user.pk)
Пример #14
0
    def test_badge_detail(self):
        """Can view badge detail"""
        user = self._get_user()
        badge = Badge(creator=user, title="Test II", description="Another test")
        badge.save()

        r = self.client.get(reverse("badger.views.detail", args=(badge.slug,)), follow=True)
        doc = pq(r.content)

        eq_("badge_detail", doc.find("body").attr("id"))
        eq_(1, doc.find('.badge-title:contains("%s")' % badge.title).length)
        eq_(badge.description, doc.find(".badge .description").text())

        # Now, take a look at the JSON format
        url = reverse("badger.detail_json", args=(badge.slug,))
        r = self.client.get(url, follow=True)

        data = simplejson.loads(r.content)
        eq_(badge.title, data["name"])
        eq_(badge.description, data["description"])
        eq_("http://testserver%s" % badge.get_absolute_url(), data["criteria"])
Пример #15
0
    def test_delete(self):
        """Can delete badge"""
        user = self._get_user()
        badge = Badge(creator=user, title="Test III",
                      description="Another test")
        badge.save()
        slug = badge.slug

        badge.award_to(user)

        self.client.login(username="******", password="******")

        r = self.client.get(reverse('badger.views.detail',
            args=(badge.slug,)), follow=True)
        doc = pq(r.content)

        eq_('badge_detail', doc.find('body').attr('id'))
        delete_url = doc.find('a.delete_badge').attr('href')
        ok_(delete_url is not None)

        r = self.client.get(delete_url)
        doc = pq(r.content)
        eq_('badge_delete', doc.find('body').attr('id'))
        eq_("1", doc.find('.awards_count').text())

        r = self.client.post(delete_url, {}, follow=True)
        doc = pq(r.content)

        try:
            badge = Badge.objects.get(slug=slug)
            ok_(False)
        except Badge.DoesNotExist:
            ok_(True)
    def test_disallowed_badge_award(self):
        """User should not be able to POST an award to a badge for which the
        user hasn't got permission to do so"""
        user = self._get_user(username="******", email="*****@*****.**")
        badge = Badge(creator=user,
                      title="Hey you badge",
                      description="Another test",
                      unique=True)
        badge.save()

        awards_url = reverse('badger.views.awards_list',
                             args=(self.badge.slug, ))

        data = dict(emails=[
            '*****@*****.**',
        ])

        resp = self.client.post(awards_url,
                                json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION=self.basic_auth)

        ok_(403, resp.status_code)
Пример #17
0
    def test_badge_detail(self):
        """Can view badge detail"""
        user = self._get_user()
        badge = Badge(creator=user, title="Test II",
                      description="Another test")
        badge.save()

        r = self.client.get(reverse('badger.views.detail',
            args=(badge.slug,)), follow=True)
        doc = pq(r.content)

        eq_('badge_detail', doc.find('body').attr('id'))
        eq_(badge.title, doc.find('.badge .title').text())
        eq_(badge.description, doc.find('.badge .description').text())

        # Now, take a look at the JSON format
        url = reverse('badger.detail_json', args=(badge.slug, ))
        r = self.client.get(url, follow=True)

        data = simplejson.loads(r.content)
        eq_(badge.title, data['name'])
        eq_(badge.description, data['description'])
        eq_('http://testserver%s' % badge.get_absolute_url(), 
            data['criteria'])
Пример #18
0
    def setUp(self):
        self.client = Client()

        self.testuser = self._get_user()

        self.badge = Badge(creator=self.testuser, title="Test II",
                           description="Another test", unique=True)
        self.badge.save()

        self.awards_url = reverse('badger.views.awards_list',
                                  args=(self.badge.slug,))
        
        key = Key()
        key.user = self.testuser
        self.password = key.generate_secret()
        self.username = key.key
        key.save()
        self.key = key

        auth_ct = '%s:%s' % (self.username, self.password)
        self.basic_auth = 'Basic %s' % b64encode(auth_ct)
        self.headers = {'HTTP_AUTHORIZATION': self.basic_auth}

        Award.objects.all().delete()
class BadgerApiViewsTests(BadgerTestCase):
    def setUp(self):
        self.client = Client()

        self.testuser = self._get_user()

        self.badge = Badge(creator=self.testuser,
                           title="Test II",
                           description="Another test",
                           unique=True)
        self.badge.save()

        self.awards_url = reverse('badger.views.awards_list',
                                  args=(self.badge.slug, ))

        key = Key()
        key.user = self.testuser
        self.password = key.generate_secret()
        self.username = key.key
        key.save()
        self.key = key

        auth_ct = '%s:%s' % (self.username, self.password)
        self.basic_auth = 'Basic %s' % b64encode(auth_ct)
        self.headers = {'HTTP_AUTHORIZATION': self.basic_auth}

        Award.objects.all().delete()

    def tearDown(self):
        self.key.delete()
        Award.objects.all().delete()
        Badge.objects.all().delete()

    def test_forbidden_without_key(self):
        """POST should require a valid key, or else yield a 403 response"""
        resp = self.client.get(self.awards_url)
        ok_(200, resp.status_code)

        resp = self.client.post(self.awards_url)
        ok_(403, resp.status_code)

        resp = self.client.post(self.awards_url,
                                {'HTTP_AUTHORIZATION': 'Basic THISISINVALID'})
        ok_(403, resp.status_code)

        resp = self.client.post(self.awards_url, self.headers)
        ok_(200, resp.status_code)

        resp = self.client.get(self.awards_url, self.headers)
        ok_(200, resp.status_code)

    def test_bad_data(self):
        """Bad JSON request should result in a 400 response"""
        resp = self.client.post(self.awards_url,
                                "THISISBADDATA",
                                content_type='application/json',
                                HTTP_AUTHORIZATION=self.basic_auth)
        ok_(400, resp.status_code)

    def test_badge_award(self):
        """Can award badges from API"""
        invite_email = '*****@*****.**'
        invalid_email = 'THISISINVALID'
        description = "Is a hoopy frood."
        award_user = self._get_user(username="******",
                                    email="*****@*****.**")

        # Construct the request data...
        data = dict(
            description=description,
            emails=[
                award_user.email,
                invite_email,
                invalid_email,
            ],
        )

        # POST to the awards URL
        resp = self.client.post(self.awards_url,
                                json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION=self.basic_auth)

        ok_(self.badge.is_awarded_to(award_user))
        award = Award.objects.get(user=award_user, badge=self.badge)
        eq_(description, award.description)

        eq_('application/json', resp['Content-Type'])
        data = json.loads(resp.content)

        ok_('successes' in data)
        ok_(award_user.email in data['successes'])
        eq_('AWARDED', data['successes'][award_user.email])
        ok_(invite_email in data['successes'])
        eq_('INVITED', data['successes'][invite_email])

        ok_('errors' in data)
        ok_(invalid_email in data['errors'])
        eq_('INVALID', data['errors'][invalid_email])

    def test_no_description(self):
        """Awards can be issued with no description"""
        award_user = self._get_user(username="******",
                                    email="*****@*****.**")

        data = {"emails": [award_user.email]}

        resp = self.client.post(self.awards_url,
                                json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION=self.basic_auth)

        eq_('application/json', resp['Content-Type'])
        data = json.loads(resp.content)

        ok_(award_user.email in data['successes'])
        ok_(award_user.email not in data['errors'])
        ok_(self.badge.is_awarded_to(award_user))

    def test_already_awarded(self):
        """Can award badges from API"""
        description = "Is a hoopy frood."
        invite_email = '*****@*****.**'
        award_user = self._get_user(username="******",
                                    email="*****@*****.**")

        award = self.badge.award_to(email=award_user.email)
        deferred_award = self.badge.award_to(email=invite_email)

        # Construct the request data...
        data = dict(
            description=description,
            emails=[
                invite_email,
                award_user.email,
            ],
        )

        # POST to the awards URL
        resp = self.client.post(self.awards_url,
                                json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION=self.basic_auth)

        eq_('application/json', resp['Content-Type'])
        data = json.loads(resp.content)

        ok_('errors' in data)
        ok_(award_user.email in data['errors'])
        eq_('ALREADYAWARDED', data['errors'][award_user.email])
        ok_(invite_email in data['errors'])
        eq_('ALREADYAWARDED', data['errors'][invite_email])

    def test_disallowed_badge_award(self):
        """User should not be able to POST an award to a badge for which the
        user hasn't got permission to do so"""
        user = self._get_user(username="******", email="*****@*****.**")
        badge = Badge(creator=user,
                      title="Hey you badge",
                      description="Another test",
                      unique=True)
        badge.save()

        awards_url = reverse('badger.views.awards_list',
                             args=(self.badge.slug, ))

        data = dict(emails=[
            '*****@*****.**',
        ])

        resp = self.client.post(awards_url,
                                json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION=self.basic_auth)

        ok_(403, resp.status_code)
Пример #20
0
class BadgerApiViewsTests(BadgerTestCase):

    def setUp(self):
        self.client = Client()

        self.testuser = self._get_user()

        self.badge = Badge(creator=self.testuser, title="Test II",
                           description="Another test", unique=True)
        self.badge.save()

        self.awards_url = reverse('badger.views.awards_list',
                                  args=(self.badge.slug,))
        
        key = Key()
        key.user = self.testuser
        self.password = key.generate_secret()
        self.username = key.key
        key.save()
        self.key = key

        auth_ct = '%s:%s' % (self.username, self.password)
        self.basic_auth = 'Basic %s' % b64encode(auth_ct)
        self.headers = {'HTTP_AUTHORIZATION': self.basic_auth}

        Award.objects.all().delete()

    def tearDown(self):
        self.key.delete()
        Award.objects.all().delete()
        Badge.objects.all().delete()

    def test_forbidden_without_key(self):
        """POST should require a valid key, or else yield a 403 response"""
        resp = self.client.get(self.awards_url)
        ok_(200, resp.status_code)

        resp = self.client.post(self.awards_url)
        ok_(403, resp.status_code)

        resp = self.client.post(self.awards_url, 
            {'HTTP_AUTHORIZATION': 'Basic THISISINVALID'})
        ok_(403, resp.status_code)

        resp = self.client.post(self.awards_url, self.headers)
        ok_(200, resp.status_code)

        resp = self.client.get(self.awards_url, self.headers)
        ok_(200, resp.status_code)

    def test_bad_data(self):
        """Bad JSON request should result in a 400 response"""
        resp = self.client.post(self.awards_url, "THISISBADDATA",
                         content_type='application/json',
                         HTTP_AUTHORIZATION=self.basic_auth)
        ok_(400, resp.status_code)

    def test_badge_award(self):
        """Can award badges from API"""
        invite_email = '*****@*****.**'
        invalid_email = 'THISISINVALID'
        description = "Is a hoopy frood."
        award_user = self._get_user(username="******",
                                    email="*****@*****.**")

        # Construct the request data...
        data = dict(
            description = description,
            emails = [
                award_user.email,
                invite_email,
                invalid_email,
            ],
        )

        # POST to the awards URL
        resp = self.client.post(self.awards_url, json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION=self.basic_auth)
        
        ok_(self.badge.is_awarded_to(award_user))
        award = Award.objects.get(user=award_user, badge=self.badge)
        eq_(description, award.description)

        eq_('application/json', resp['Content-Type'])
        data = json.loads(resp.content)

        ok_('successes' in data)
        ok_(award_user.email in data['successes'])
        eq_('AWARDED', data['successes'][award_user.email])
        ok_(invite_email in data['successes'])
        eq_('INVITED', data['successes'][invite_email])

        ok_('errors' in data)
        ok_(invalid_email in data['errors'])
        eq_('INVALID', data['errors'][invalid_email])

    def test_no_description(self):
        """Awards can be issued with no description"""
        award_user = self._get_user(username="******",
                                    email="*****@*****.**")

        data = {"emails": [award_user.email]}

        resp = self.client.post(self.awards_url, json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION=self.basic_auth)

        eq_('application/json', resp['Content-Type'])
        data = json.loads(resp.content)
        
        ok_(award_user.email in data['successes'])
        ok_(award_user.email not in data['errors'])
        ok_(self.badge.is_awarded_to(award_user))

    def test_already_awarded(self):
        """Can award badges from API"""
        description = "Is a hoopy frood."
        invite_email = '*****@*****.**'
        award_user = self._get_user(username="******",
                                    email="*****@*****.**")

        award = self.badge.award_to(email=award_user.email)
        deferred_award = self.badge.award_to(email=invite_email)

        # Construct the request data...
        data = dict(
            description = description,
            emails = [
                invite_email,
                award_user.email,
            ],
        )

        # POST to the awards URL
        resp = self.client.post(self.awards_url, json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION=self.basic_auth)
        
        eq_('application/json', resp['Content-Type'])
        data = json.loads(resp.content)

        print "DATA %s" % data

        ok_('errors' in data)
        ok_(award_user.email in data['errors'])
        eq_('ALREADYAWARDED', data['errors'][award_user.email])
        ok_(invite_email in data['errors'])
        eq_('ALREADYAWARDED', data['errors'][invite_email])

    def test_disallowed_badge_award(self):
        """User should not be able to POST an award to a badge for which the
        user hasn't got permission to do so"""
        user = self._get_user(username="******",
                              email="*****@*****.**")
        badge = Badge(creator=user, title="Hey you badge",
                      description="Another test", unique=True)
        badge.save()

        awards_url = reverse('badger.views.awards_list',
                             args=(self.badge.slug,))

        data = dict(emails=['*****@*****.**',])

        resp = self.client.post(awards_url, json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION=self.basic_auth)

        ok_(403, resp.status_code)
Пример #21
0
def create(user, badge_type):
    badge = Badge()
    badge.user = user
    badge.badge_type = badge_type
    badge.save()
    return badge
Пример #22
0
def badgeModule(sender, module, **kwargs):

	badge = Badge(title= module.name, slug = module.slug, description = module.description, unique = True)
	badge.save()