当前位置: 首页>>代码示例>>Python>>正文


Python Badge.is_awarded_to方法代码示例

本文整理汇总了Python中badger.models.Badge.is_awarded_to方法的典型用法代码示例。如果您正苦于以下问题:Python Badge.is_awarded_to方法的具体用法?Python Badge.is_awarded_to怎么用?Python Badge.is_awarded_to使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在badger.models.Badge的用法示例。


在下文中一共展示了Badge.is_awarded_to方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: BadgerApiViewsTests

# 需要导入模块: from badger.models import Badge [as 别名]
# 或者: from badger.models.Badge import is_awarded_to [as 别名]
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 = '[email protected]'
        invalid_email = 'THISISINVALID'
        description = "Is a hoopy frood."
        award_user = self._get_user(username="awardee1",
                                    email="[email protected]")

        # 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="awardee1",
#.........这里部分代码省略.........
开发者ID:6thmonth,项目名称:badges.mozilla.org,代码行数:103,代码来源:test_views.py


注:本文中的badger.models.Badge.is_awarded_to方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。