class BadgerFeedsTest(BadgerTestCase):
    def setUp(self):
        self.testuser = self._get_user()
        self.client = LocalizingClient()
        Award.objects.all().delete()

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

    def test_award_feeds(self):
        """Can view award detail"""
        user = self._get_user()
        user2 = self._get_user(username='******')

        b1, created = Badge.objects.get_or_create(creator=user,
                                                  title="Code Badge #1")
        award = b1.award_to(user2)

        # The award should show up in each of these feeds.
        feed_urls = (
            reverse('badger.feeds.awards_recent', args=('atom', )),
            reverse('badger.feeds.awards_by_badge', args=(
                'atom',
                b1.slug,
            )),
            reverse('badger.feeds.awards_by_user',
                    args=(
                        'atom',
                        user2.username,
                    )),
        )

        # Check each of the feeds
        for feed_url in feed_urls:
            r = self.client.get(feed_url, follow=True)

            # The feed should be parsed without issues by feedparser
            feed = feedparser.parse(r.content)
            eq_(0, feed.bozo)

            # Look through entries for the badge title
            found_it = False
            for entry in feed.entries:
                if b1.title in entry.title and user2.username in entry.title:
                    found_it = True

            ok_(found_it)

    def _get_user(self,
                  username="******",
                  email="*****@*****.**",
                  password="******"):
        (user,
         created) = User.objects.get_or_create(username=username,
                                               defaults=dict(email=email))
        if created:
            user.set_password(password)
            user.save()
        return user
class BadgerFeedsTest(BadgerTestCase):

    def setUp(self):
        self.testuser = self._get_user()
        self.client = LocalizingClient()
        Award.objects.all().delete()

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

    def test_award_feeds(self):
        """Can view award detail"""
        user = self._get_user()
        user2 = self._get_user(username='******')

        b1, created = Badge.objects.get_or_create(creator=user, title="Code Badge #1")
        award = b1.award_to(user2)

        # The award should show up in each of these feeds.
        feed_urls = (
            reverse('badger.feeds.awards_recent', 
                    args=('atom', )),
            reverse('badger.feeds.awards_by_badge', 
                    args=('atom', b1.slug, )),
            reverse('badger.feeds.awards_by_user',
                    args=('atom', user2.username,)),
        )

        # Check each of the feeds
        for feed_url in feed_urls:
            r = self.client.get(feed_url, follow=True)

            # The feed should be parsed without issues by feedparser
            feed = feedparser.parse(r.content)
            eq_(0, feed.bozo)

            # Look through entries for the badge title
            found_it = False
            for entry in feed.entries:
                if b1.title in entry.title and user2.username in entry.title:
                    found_it = True

            ok_(found_it)

    def _get_user(self, username="******", email="*****@*****.**",
            password="******"):
        (user, created) = User.objects.get_or_create(username=username,
                defaults=dict(email=email))
        if created:
            user.set_password(password)
            user.save()
        return user
Exemple #3
0
 def setUp(self):
     self.testuser = self._get_user()
     self.client = LocalizingClient()
     Award.objects.all().delete()
Exemple #4
0
class BadgerViewsTest(BadgerTestCase):

    def setUp(self):
        self.testuser = self._get_user()
        self.client = LocalizingClient()
        Award.objects.all().delete()

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

    @attr('json')
    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'])

    @attr('json')
    def test_award_detail(self):
        """Can view award detail"""
        user = self._get_user()
        user2 = self._get_user(username='******')

        b1, created = Badge.objects.get_or_create(creator=user, title="Code Badge #1")
        award = b1.award_to(user2)

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

        eq_('award_detail', doc.find('body').attr('id'))
        eq_(1, doc.find('.awarded_to .username:contains("%s")' % user2.username).length)
        eq_(1, doc.find('.badge .title:contains("%s")' % b1.title).length)

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

        data = simplejson.loads(r.content)
        eq_(award.user.email, data['recipient'])
        eq_('http://testserver%s' % award.get_absolute_url(), 
            data['evidence'])
        eq_(award.badge.title, data['badge']['name'])
        eq_(award.badge.description, data['badge']['description'])
        eq_('http://testserver%s' % award.badge.get_absolute_url(), 
            data['badge']['criteria'])

    def test_awards_by_user(self):
        """Can view awards by user"""
        user = self._get_user()
        user2 = self._get_user(username='******')

        b1, created = Badge.objects.get_or_create(creator=user, title="Code Badge #1")
        b2, created = Badge.objects.get_or_create(creator=user, title="Code Badge #2")
        b3, created = Badge.objects.get_or_create(creator=user, title="Code Badge #3")

        b1.award_to(user2)
        award_badge(b2.slug, user2)
        Award.objects.create(badge=b3, user=user2)

        url = reverse('badger.views.awards_by_user', args=(user2.username,))
        r = self.client.get(url, follow=True)
        doc = pq(r.content)

        eq_('badge_awards_by_user', doc.find('body').attr('id'))
        eq_(3, doc.find('.award').length)
        for b in (b1, b2, b3):
            eq_(1, doc.find('.award .badge .title:contains("%s")' % b.title)
                      .length)

    def test_awards_by_badge(self):
        """Can view awards by badge"""
        user = self._get_user()
        b1 = Badge.objects.create(creator=user, title="Code Badge #1")

        u1 = self._get_user(username='******')
        u2 = self._get_user(username='******')
        u3 = self._get_user(username='******')

        for u in (u1, u2, u3):
            b1.award_to(u)

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

        eq_('badge_awards_by_badge', doc.find('body').attr('id'))
        eq_(3, doc.find('.award').length)
        for u in (u1, u2, u3):
            eq_(1, doc.find('.award .username:contains("%s")' % u.username)
                      .length)

    @attr('issue_award')
    def test_issue_award(self):
        """Badge creator can issue award to another user"""
        
        user1 = self._get_user(username="******")
        user2 = self._get_user(username="******")

        b1 = Badge.objects.create(creator=user1, title="Badge to awarded")

        url = reverse('badger.views.award_badge', args=(b1.slug,))

        # Non-creator should be denied attempt to award badge
        self.client.login(username="******", password="******")
        r = self.client.get(url, follow=True)
        eq_(403, r.status_code)

        # But, the creator should be allowed
        self.client.login(username="******", password="******")
        r = self.client.get(url, follow=True)
        eq_(200, r.status_code)

        doc = pq(r.content)
        form = doc('form#award_badge')
        eq_(1, form.length)
        eq_(1, form.find('*[name=user]').length)
        eq_(1, form.find('input.submit,button.submit').length)

        r = self.client.post(url, dict(
            user=user2.id,
        ), follow=True)
        doc = pq(r.content)

        logging.debug(r.content)

        ok_(b1.is_awarded_to(user2))


    def _get_user(self, username="******", email="*****@*****.**",
            password="******"):
        (user, created) = User.objects.get_or_create(username=username,
                defaults=dict(email=email))
        if created:
            user.set_password(password)
            user.save()
        return user
 def setUp(self):
     self.testuser = self._get_user()
     self.client = LocalizingClient()
     Award.objects.all().delete()
Exemple #6
0
class BadgerViewsTest(BadgerTestCase):
    def setUp(self):
        self.testuser = self._get_user()
        self.client = LocalizingClient()
        Award.objects.all().delete()

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

    @attr('json')
    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'])

    @attr('json')
    def test_award_detail(self):
        """Can view award detail"""
        user = self._get_user()
        user2 = self._get_user(username='******')

        b1, created = Badge.objects.get_or_create(creator=user,
                                                  title="Code Badge #1")
        award = b1.award_to(user2)

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

        eq_('award_detail', doc.find('body').attr('id'))
        eq_(
            1,
            doc.find('.awarded_to .username:contains("%s")' %
                     user2.username).length)
        eq_(1, doc.find('.badge .title:contains("%s")' % b1.title).length)

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

        data = simplejson.loads(r.content)
        eq_(award.user.email, data['recipient'])
        eq_('http://testserver%s' % award.get_absolute_url(), data['evidence'])
        eq_(award.badge.title, data['badge']['name'])
        eq_(award.badge.description, data['badge']['description'])
        eq_('http://testserver%s' % award.badge.get_absolute_url(),
            data['badge']['criteria'])

    def test_awards_by_user(self):
        """Can view awards by user"""
        user = self._get_user()
        user2 = self._get_user(username='******')

        b1, created = Badge.objects.get_or_create(creator=user,
                                                  title="Code Badge #1")
        b2, created = Badge.objects.get_or_create(creator=user,
                                                  title="Code Badge #2")
        b3, created = Badge.objects.get_or_create(creator=user,
                                                  title="Code Badge #3")

        b1.award_to(user2)
        award_badge(b2.slug, user2)
        Award.objects.create(badge=b3, user=user2)

        url = reverse('badger.views.awards_by_user', args=(user2.username, ))
        r = self.client.get(url, follow=True)
        doc = pq(r.content)

        eq_('badge_awards_by_user', doc.find('body').attr('id'))
        eq_(3, doc.find('.award').length)
        for b in (b1, b2, b3):
            eq_(
                1,
                doc.find('.award .badge .title:contains("%s")' %
                         b.title).length)

    def test_awards_by_badge(self):
        """Can view awards by badge"""
        user = self._get_user()
        b1 = Badge.objects.create(creator=user, title="Code Badge #1")

        u1 = self._get_user(username='******')
        u2 = self._get_user(username='******')
        u3 = self._get_user(username='******')

        for u in (u1, u2, u3):
            b1.award_to(u)

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

        eq_('badge_awards_by_badge', doc.find('body').attr('id'))
        eq_(3, doc.find('.award').length)
        for u in (u1, u2, u3):
            eq_(
                1,
                doc.find('.award .username:contains("%s")' %
                         u.username).length)

    @attr('issue_award')
    def test_issue_award(self):
        """Badge creator can issue award to another user"""

        user1 = self._get_user(username="******")
        user2 = self._get_user(username="******")

        b1 = Badge.objects.create(creator=user1, title="Badge to awarded")

        url = reverse('badger.views.award_badge', args=(b1.slug, ))

        # Non-creator should be denied attempt to award badge
        self.client.login(username="******", password="******")
        r = self.client.get(url, follow=True)
        eq_(403, r.status_code)

        # But, the creator should be allowed
        self.client.login(username="******", password="******")
        r = self.client.get(url, follow=True)
        eq_(200, r.status_code)

        doc = pq(r.content)
        form = doc('form#award_badge')
        eq_(1, form.length)
        eq_(1, form.find('*[name=user]').length)
        eq_(1, form.find('input.submit,button.submit').length)

        r = self.client.post(url, dict(user=user2.id, ), follow=True)
        doc = pq(r.content)

        logging.debug(r.content)

        ok_(b1.is_awarded_to(user2))

    def _get_user(self,
                  username="******",
                  email="*****@*****.**",
                  password="******"):
        (user,
         created) = User.objects.get_or_create(username=username,
                                               defaults=dict(email=email))
        if created:
            user.set_password(password)
            user.save()
        return user