コード例 #1
0
ファイル: test_api.py プロジェクト: bsmedberg/zamboni
class TestApiReviewer(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141', 'user_2519')

    def setUp(self, api_name='apps'):
        self.user = User.objects.get(pk=2519)
        self.profile = self.user.get_profile()
        self.profile.update(read_dev_agreement=datetime.now())
        self.grant_permission(self.profile, 'Apps:Review')

        self.access = Access.objects.create(key='foo', secret=generate(),
                                            user=self.user)
        self.client = OAuthClient(self.access, api_name=api_name)
        self.list_url = ('api_dispatch_list', {'resource_name': 'search'})

        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh()

    def test_status_reviewer(self):
        res = self.client.get(self.list_url + ({'status': 'public'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['app_slug'], self.webapp.app_slug)

        res = self.client.get(self.list_url + ({'status': 'rejected'},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0)
コード例 #2
0
class TestApiFlags(BaseOAuth, ESTestCase):
    fixtures = fixture("webapp_337141")
    url = list_url("search")

    def setUp(self):
        self.client = OAuthClient(None)
        self.webapp = Webapp.objects.get(pk=337141)

    def _flag(self, adult=False, child=False):
        Flag.objects.create(addon=self.webapp, adult_content=adult, child_content=child)
        self.webapp.save()
        self.refresh()

    def test_no_flags(self):
        self.webapp.save()
        self.refresh()
        res = self.client.get(self.url + ({"q": "something"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_adult(self):
        self._flag(adult=True)
        res = self.client.get(self.url + ({"q": "something", "region": list(mkt.regions.ADULT_EXCLUDED)[0].slug},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)["objects"]
        eq_(len(objs), 0, "App with adult_content not removed from search.")

    def test_child(self):
        self._flag(child=True)
        res = self.client.get(self.url + ({"q": "something", "region": list(mkt.regions.CHILD_EXCLUDED)[0].slug},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)["objects"]
        eq_(len(objs), 0, "App with child_content not removed from search.")
コード例 #3
0
ファイル: test_handlers.py プロジェクト: sandy-slin/zamboni
class TestFeaturedHomeHandler(BaseOAuth):

    def setUp(self):
        super(TestFeaturedHomeHandler, self).setUp()
        resource = 'featured/home'
        self.list_url = ('api_dispatch_list', {'resource_name': resource})
        self.get_url = None
        self.client = OAuthClient(None)

        self.cat = Category.objects.create(name='awesome',
                                           type=amo.ADDON_WEBAPP)

        # App, no category, worldwide region.
        self.app1 = Webapp.objects.create(status=amo.STATUS_PUBLIC,
                                          name='App 1')
        f1 = FeaturedApp.objects.create(app=self.app1, category=None)
        FeaturedAppRegion.objects.create(featured_app=f1,
                                         region=mkt.regions.WORLDWIDE.id)

        # App, with category, worldwide region. Mostly to ensure category
        # specific featured apps don't slip into the results.
        self.app2 = Webapp.objects.create(status=amo.STATUS_PUBLIC,
                                          name='App 2')
        AddonCategory.objects.create(category=self.cat, addon=self.app2)
        f2 = FeaturedApp.objects.create(app=self.app2, category=self.cat)
        FeaturedAppRegion.objects.create(featured_app=f2,
                                         region=mkt.regions.WORLDWIDE.id)

        # App, no category, US region.
        self.app3 = Webapp.objects.create(status=amo.STATUS_PUBLIC,
                                          name='App 3')
        f3 = FeaturedApp.objects.create(app=self.app3)
        FeaturedAppRegion.objects.create(featured_app=f3,
                                         region=mkt.regions.US.id)

    def test_verbs(self):
        self._allowed_verbs(self.list_url, ['get'])

    def test_get_featured(self):
        res = self.client.get(self.list_url)
        data = json.loads(res.content)
        eq_(res.status_code, 200)
        eq_(data['objects'][0]['app_slug'], self.app1.app_slug)

    def test_get_featured_region(self):
        # UK region should come up empty, so we backfill with worldwide.
        res = self.client.get(self.list_url, data=dict(region='uk'))
        data = json.loads(res.content)
        eq_(res.status_code, 200)
        eq_(data['objects'][0]['app_slug'], self.app1.app_slug)

        # US region should come have 1 plus worldwide.
        res = self.client.get(self.list_url, data=dict(region='us'))
        data = json.loads(res.content)
        eq_(res.status_code, 200)
        self.assertSetEqual([o['app_slug'] for o in data['objects']],
                            ['app-1', 'app-3'])
コード例 #4
0
ファイル: test_handlers.py プロジェクト: vdt/zamboni
class TestCategoryHandler(BaseOAuth):
    def setUp(self):
        super(TestCategoryHandler, self).setUp()
        self.cat = Category.objects.create(name='Webapp',
                                           type=amo.ADDON_WEBAPP)
        self.cat.name = {'fr': 'Le Webapp'}
        self.cat.save()
        self.other = Category.objects.create(name='other',
                                             type=amo.ADDON_EXTENSION)

        self.list_url = ('api_dispatch_list', {'resource_name': 'category'})
        self.get_url = ('api_dispatch_detail', {
            'resource_name': 'category',
            'pk': self.cat.pk
        })

        self.client = OAuthClient(None)

    def test_verbs(self):
        self._allowed_verbs(self.list_url, ['get'])
        self._allowed_verbs(self.get_url, ['get'])

    def test_weight(self):
        self.cat.update(weight=-1)
        res = self.client.get(self.list_url)
        data = json.loads(res.content)
        eq_(data['meta']['total_count'], 0)

    def test_get_categories(self):
        res = self.client.get(self.list_url)
        data = json.loads(res.content)
        eq_(data['meta']['total_count'], 1)
        eq_(data['objects'][0]['name'], 'Webapp')

    def test_get_category(self):
        res = self.client.get(self.get_url)
        data = json.loads(res.content)
        eq_(data['name'], 'Webapp')

    def test_get_category_localised(self):
        with self.activate(locale='fr'):
            res = self.client.get(self.get_url)
            data = json.loads(res.content)
            eq_(data['name'], 'Le Webapp')

    def test_get_other_category(self):
        res = self.client.get(('api_dispatch_detail', {
            'resource_name': 'category',
            'pk': self.other.pk
        }))
        eq_(res.status_code, 404)
コード例 #5
0
ファイル: test_handlers.py プロジェクト: bsmedberg/zamboni
class TestCategoryHandler(BaseOAuth):

    def setUp(self):
        super(TestCategoryHandler, self).setUp()
        self.cat = Category.objects.create(name='Webapp',
                                           type=amo.ADDON_WEBAPP,
                                           slug='thewebapp')
        self.cat.name = {'fr': 'Le Webapp'}
        self.cat.save()
        self.other = Category.objects.create(name='other',
                                             type=amo.ADDON_EXTENSION)

        self.list_url = ('api_dispatch_list', {'resource_name': 'category'})
        self.get_url = ('api_dispatch_detail',
                        {'resource_name': 'category', 'pk': self.cat.pk})

        self.client = OAuthClient(None)

    def test_verbs(self):
        self._allowed_verbs(self.list_url, ['get'])
        self._allowed_verbs(self.get_url, ['get'])

    def test_weight(self):
        self.cat.update(weight=-1)
        res = self.client.get(self.list_url)
        data = json.loads(res.content)
        eq_(data['meta']['total_count'], 0)

    def test_get_categories(self):
        res = self.client.get(self.list_url)
        data = json.loads(res.content)
        eq_(data['meta']['total_count'], 1)
        eq_(data['objects'][0]['name'], 'Webapp')
        eq_(data['objects'][0]['slug'], 'thewebapp')

    def test_get_category(self):
        res = self.client.get(self.get_url)
        data = json.loads(res.content)
        eq_(data['name'], 'Webapp')

    def test_get_category_localised(self):
        res = self.client.get(self.get_url, HTTP_ACCEPT_LANGUAGE='fr')
        data = json.loads(res.content)
        eq_(data['name'], 'Le Webapp')

    def test_get_other_category(self):
        res = self.client.get(('api_dispatch_detail',
                              {'resource_name': 'category',
                               'pk': self.other.pk}))
        eq_(res.status_code, 404)
コード例 #6
0
ファイル: test_api.py プロジェクト: at13/zamboni
class TestSuggestionsApi(ESTestCase):
    fixtures = fixture('webapp_337141')

    def setUp(self):
        self.url = list_url('suggest')
        self.refresh('webapp')
        self.client = OAuthClient(None)

    def test_suggestions(self):
        app1 = Webapp.objects.get(pk=337141)
        app1.save()
        app2 = app_factory(name=u"Second âpp", description=u"Second dèsc" * 25,
                           created=self.days_ago(3))
        self.refresh('webapp')

        response = self.client.get(self.url)
        parsed = json.loads(response.content)
        eq_(parsed[0], '')
        eq_(parsed[1], [unicode(app1.name), unicode(app2.name)])
        eq_(parsed[2], [unicode(app1.description),
                        unicode(truncate(app2.description))])
        eq_(parsed[3], [absolutify(app1.get_detail_url()),
                        absolutify(app2.get_detail_url())])
        eq_(parsed[4], [app1.get_icon_url(64), app2.get_icon_url(64)])

        # Cleanup to remove these from the index.
        unindex_webapps([app1.id, app2.id])
        app1.delete()
        app2.delete()
コード例 #7
0
ファイル: test_api.py プロジェクト: markh-bz/zamboni
 def test_owner_still_non_reviewer_access(self):
     user = Webapp.objects.get(pk=337141).authors.all()[0].user
     access = Access.objects.create(
         key='test_oauth_key_owner', secret=generate(), user=user)
     client = OAuthClient(access, api_name='reviewers')
     res = client.get(self.url)
     eq_(res.status_code, 401)
コード例 #8
0
 def test_owner_still_non_reviewer_access(self):
     user = Webapp.objects.get(pk=337141).authors.all()[0].user
     access = Access.objects.create(
         key='test_oauth_key_owner', secret=generate(), user=user)
     client = OAuthClient(access, api_name='reviewers')
     res = client.get(self.url)
     eq_(res.status_code, 401)
コード例 #9
0
class TestSuggestionsApi(ESTestCase):
    fixtures = fixture('webapp_337141')

    def setUp(self):
        self.url = list_url('suggest')
        self.refresh('webapp')
        self.client = OAuthClient(None)

    def test_suggestions(self):
        app1 = Webapp.objects.get(pk=337141)
        app1.save()
        app2 = app_factory(name=u"Second âpp",
                           description=u"Second dèsc" * 25,
                           created=self.days_ago(3))
        self.refresh('webapp')

        response = self.client.get(self.url)
        parsed = json.loads(response.content)
        eq_(parsed[0], '')
        eq_(parsed[1], [unicode(app1.name), unicode(app2.name)])
        eq_(parsed[2],
            [unicode(app1.description),
             unicode(truncate(app2.description))])
        eq_(parsed[3], [
            absolutify(app1.get_detail_url()),
            absolutify(app2.get_detail_url())
        ])
        eq_(parsed[4], [app1.get_icon_url(64), app2.get_icon_url(64)])

        # Cleanup to remove these from the index.
        unindex_webapps([app1.id, app2.id])
        app1.delete()
        app2.delete()
コード例 #10
0
ファイル: test_handlers.py プロジェクト: kmaglione/zamboni
 def test_admin_get(self):
     self.create_app()
     admin = UserProfile.objects.get(email="*****@*****.**")
     g = Group.objects.create(rules="*:*")
     GroupUser.objects.create(group=g, user=admin)
     ac = Access.objects.create(key="adminOauthKey", secret=generate(), user=admin.user)
     client = OAuthClient(ac, api_name="apps")
     r = client.get(self.get_url)
     eq_(r.status_code, 200)
コード例 #11
0
ファイル: test_handlers.py プロジェクト: kmaglione/zamboni
 def test_reviewer_get(self):
     self.create_app()
     editor = UserProfile.objects.get(email="*****@*****.**")
     g = Group.objects.create(rules="Apps:Review,Reviews:Edit")
     GroupUser.objects.create(group=g, user=editor)
     ac = Access.objects.create(key="adminOauthKey", secret=generate(), user=editor.user)
     client = OAuthClient(ac, api_name="apps")
     r = client.get(self.get_url)
     eq_(r.status_code, 200)
コード例 #12
0
 def test_reviewer_get(self):
     self.create_app()
     editor = UserProfile.objects.get(email='*****@*****.**')
     g = Group.objects.create(rules='Apps:Review,Reviews:Edit')
     GroupUser.objects.create(group=g, user=editor)
     ac = Access.objects.create(key='adminOauthKey', secret=generate(),
                                user=editor.user)
     client = OAuthClient(ac, api_name='apps')
     r = client.get(self.get_url)
     eq_(r.status_code, 200)
コード例 #13
0
 def test_admin_get(self):
     self.create_app()
     admin = UserProfile.objects.get(email='*****@*****.**')
     g = Group.objects.create(rules='*:*')
     GroupUser.objects.create(group=g, user=admin)
     ac = Access.objects.create(key='adminOauthKey', secret=generate(),
                                user=admin.user)
     client = OAuthClient(ac, api_name='apps')
     r = client.get(self.get_url)
     eq_(r.status_code, 200)
コード例 #14
0
ファイル: test_handlers.py プロジェクト: almet/zamboni
class TestCategoryHandler(BaseOAuth):
    def setUp(self):
        super(TestCategoryHandler, self).setUp()
        self.cat = Category.objects.create(name="Webapp", type=amo.ADDON_WEBAPP, slug="thewebapp")
        self.cat.name = {"fr": "Le Webapp"}
        self.cat.save()
        self.other = Category.objects.create(name="other", type=amo.ADDON_EXTENSION)

        self.list_url = ("api_dispatch_list", {"resource_name": "category"})
        self.get_url = ("api_dispatch_detail", {"resource_name": "category", "pk": self.cat.pk})

        self.client = OAuthClient(None)

    def test_verbs(self):
        self._allowed_verbs(self.list_url, ["get"])
        self._allowed_verbs(self.get_url, ["get"])

    def test_weight(self):
        self.cat.update(weight=-1)
        res = self.client.get(self.list_url)
        data = json.loads(res.content)
        eq_(data["meta"]["total_count"], 0)

    def test_get_categories(self):
        res = self.client.get(self.list_url)
        data = json.loads(res.content)
        eq_(data["meta"]["total_count"], 1)
        eq_(data["objects"][0]["name"], "Webapp")
        eq_(data["objects"][0]["slug"], "thewebapp")

    def test_get_category(self):
        res = self.client.get(self.get_url)
        data = json.loads(res.content)
        eq_(data["name"], "Webapp")

    def test_get_category_localised(self):
        res = self.client.get(self.get_url, HTTP_ACCEPT_LANGUAGE="fr")
        data = json.loads(res.content)
        eq_(data["name"], "Le Webapp")

    def test_get_other_category(self):
        res = self.client.get(("api_dispatch_detail", {"resource_name": "category", "pk": self.other.pk}))
        eq_(res.status_code, 404)
コード例 #15
0
ファイル: test_api.py プロジェクト: mitramichaeljade/zamboni
class TestApiFlags(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141')
    url = list_url('search')

    def setUp(self):
        self.create_switch('search-api-es')
        self.client = OAuthClient(None)
        self.webapp = Webapp.objects.get(pk=337141)

    def _flag(self, adult=False, child=False):
        Flag.objects.create(addon=self.webapp, adult_content=adult,
                            child_content=child)
        self.webapp.save()
        self.refresh('webapp')

    def test_no_flags(self):
        self.webapp.save()
        self.refresh('webapp')
        res = self.client.get(self.url + ({'q': 'something'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_adult(self):
        self._flag(adult=True)
        res = self.client.get(self.url + (
            {'q': 'something',
             'region': list(mkt.regions.ADULT_EXCLUDED)[0].slug},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0, 'App with adult_content not removed from search.')

    def test_child(self):
        self._flag(child=True)
        res = self.client.get(self.url + (
            {'q': 'something',
             'region': list(mkt.regions.CHILD_EXCLUDED)[0].slug},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0, 'App with child_content not removed from search.')
コード例 #16
0
ファイル: test_api.py プロジェクト: almet/zamboni
class TestAccount(BaseOAuth):
    fixtures = fixture('user_2519', 'user_10482', 'webapp_337141')

    def setUp(self):
        super(TestAccount, self).setUp()
        self.list_url = list_url('account')
        self.get_url = get_url('account', '2519')
        self.anon = OAuthClient(None, api_name='apps')
        self.user = UserProfile.objects.get(pk=2519)

    def test_verbs(self):
        self._allowed_verbs(self.list_url, ())
        self._allowed_verbs(self.get_url, ('get',))

    def test_not_allowed(self):
        eq_(self.anon.get(self.get_url).status_code, 401)

    def test_allowed(self):
        res = self.client.get(self.get_url)
        eq_(res.status_code, 200, res.content)
        data = json.loads(res.content)
        eq_(data['display_name'], self.user.display_name)
        eq_(data['installed'], [])

    def test_install(self):
        ins = Installed.objects.create(user=self.user, addon_id=337141)
        res = self.client.get(self.get_url)
        eq_(res.status_code, 200, res.content)
        data = json.loads(res.content)
        eq_(data['installed'],
            [get_absolute_url(get_url('app', ins.addon.pk), absolute=False)])

    def test_install_reviewer(self):
        Installed.objects.create(user=self.user, addon_id=337141,
                                 install_type=INSTALL_TYPE_REVIEWER)
        res = self.client.get(self.get_url)
        eq_(res.status_code, 200, res.content)
        data = json.loads(res.content)
        eq_(data['installed'], [])

    def test_other(self):
        eq_(self.client.get(get_url('account', '10482')).status_code, 403)

    def test_own(self):
        res = self.client.get(get_url('account', 'mine'))
        eq_(res.status_code, 200)
        data = json.loads(res.content)
        eq_(data['display_name'], self.user.display_name)
コード例 #17
0
ファイル: test_api.py プロジェクト: chenzihui/zamboni
class TestAccount(BaseOAuth):
    fixtures = fixture('user_2519', 'webapp_337141')

    def setUp(self):
        super(TestAccount, self).setUp(api_name='reviewers')
        self.list_url = list_url('reviewing')
        self.anon = OAuthClient(None, api_name='reviewers')
        self.user = UserProfile.objects.get(pk=2519)
        self.req = RequestFactory().get('/')
        self.req.amo_user = self.user

    def test_verbs(self):
        self._allowed_verbs(self.list_url, ('get'))

    def test_not_allowed(self):
        eq_(self.anon.get(self.list_url).status_code, 401)

    def test_still_not_allowed(self):
        eq_(self.client.get(self.list_url).status_code, 401)

    def add_perms(self):
        self.grant_permission(self.user, 'Apps:Review')

    def test_allowed(self):
        self.add_perms()
        res = self.client.get(self.list_url)
        eq_(res.status_code, 200, res.content)
        data = json.loads(res.content)
        eq_(data['objects'], [])

    def test_some(self):
        self.add_perms()

        # This feels rather brittle.
        cache.set('%s:review_viewing:%s' % (settings.CACHE_PREFIX, 337141),
                  2519, 50 * 2)
        AppsReviewing(self.req).add(337141)

        res = self.client.get(self.list_url)
        data = json.loads(res.content)
        eq_(data['objects'][0]['resource_uri'],
            get_absolute_url(get_url('app', '337141'), absolute=False))
コード例 #18
0
ファイル: test_api.py プロジェクト: almet/zamboni
class TestApiReviewer(BaseOAuth, ESTestCase):
    fixtures = fixture("webapp_337141", "user_2519")

    def setUp(self, api_name="apps"):
        self.user = User.objects.get(pk=2519)
        self.profile = self.user.get_profile()
        self.profile.update(read_dev_agreement=datetime.now())
        self.grant_permission(self.profile, "Apps:Review")

        self.access = Access.objects.create(key="foo", secret=generate(), user=self.user)
        self.client = OAuthClient(self.access, api_name=api_name)
        self.list_url = ("api_dispatch_list", {"resource_name": "search"})

        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name="test", type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh()

    def test_status_reviewer(self):
        res = self.client.get(self.list_url + ({"status": "public"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["app_slug"], self.webapp.app_slug)

        res = self.client.get(self.list_url + ({"status": "rejected"},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)["objects"]
        eq_(len(objs), 0)

        res = self.client.get(self.list_url + ({"status": "vindaloo"},))
        eq_(res.status_code, 400)
        error = json.loads(res.content)["error_message"]
        eq_(error.keys(), ["status"])

    def test_addon_type_reviewer(self):
        res = self.client.get(self.list_url + ({"type": "app"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["app_slug"], self.webapp.app_slug)

        res = self.client.get(self.list_url + ({"type": "persona"},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)["objects"]
        eq_(len(objs), 0)

        res = self.client.get(self.list_url + ({"type": "vindaloo"},))
        eq_(res.status_code, 400)
        error = json.loads(res.content)["error_message"]
        eq_(error.keys(), ["type"])
コード例 #19
0
ファイル: test_handlers.py プロジェクト: ominds/zamboni
class TestDeviceTypeHandler(BaseOAuth):

    def setUp(self):
        super(TestDeviceTypeHandler, self).setUp()

        self.dt = DeviceType.objects.create(name='Phone')
        self.dt.name = {'fr': 'Le phone'}
        self.dt.save()
        self.list_url = ('api_dispatch_list', {'resource_name': 'devicetype'})
        self.get_url = ('api_dispatch_detail',
                        {'resource_name': 'devicetype', 'pk': self.dt.pk})

        self.client = OAuthClient(None)

    def test_verbs(self):
        self._allowed_verbs(self.list_url, ['get'])
        self._allowed_verbs(self.get_url, ['get'])

    def test_get_devicetypes(self):
        res = self.client.get(self.list_url)
        data = json.loads(res.content)
        eq_(data['meta']['total_count'], 1)
        eq_(data['objects'][0]['name'], 'phone')
コード例 #20
0
ファイル: test_api.py プロジェクト: wraithan/zamboni
class TestApi(BaseOAuth, ESTestCase):
    fixtures = fixture("webapp_337141")

    def setUp(self):
        self.create_switch("search-api-es")
        self.client = OAuthClient(None)
        self.url = list_url("search")
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name="test", type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh("webapp")

    def test_verbs(self):
        self._allowed_verbs(self.url, ["get"])

    def test_has_cors(self):
        self.assertCORS(self.client.get(self.url), "get")

    def test_meta(self):
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        eq_(set(res.json.keys()), set(["objects", "meta"]))
        eq_(res.json["meta"]["total_count"], 1)

    def test_wrong_category(self):
        res = self.client.get(self.url + ({"cat": self.category.pk + 1},))
        eq_(res.status_code, 400)
        eq_(res["Content-Type"], "application/json")

    def test_wrong_weight(self):
        self.category.update(weight=-1)
        res = self.client.get(self.url + ({"cat": self.category.pk},))
        eq_(res.status_code, 200)
        eq_(len(res.json["objects"]), 0)

    def test_wrong_sort(self):
        res = self.client.get(self.url + ({"sort": "awesomeness"},))
        eq_(res.status_code, 400)

    def test_sort(self):
        queries = []

        def fake_get_query(*a, **kw):
            qs = _get_query(*a, **kw)
            m = Mock(wraps=qs)
            queries.append(m)
            return m

        with patch("mkt.search.api._get_query", fake_get_query):
            res = self.client.get(self.url, [("sort", "downloads"), ("sort", "rating")])
        eq_(res.status_code, 200)
        queries[0].order_by.assert_called_with("-weekly_downloads", "-bayesian_rating")

    def test_right_category(self):
        res = self.client.get(self.url + ({"cat": self.category.pk},))
        eq_(res.status_code, 200)
        eq_(res.json["objects"], [])

    def create(self):
        AddonCategory.objects.create(addon=self.webapp, category=self.category)
        self.webapp.save()
        self.refresh("webapp")

    def test_right_category_present(self):
        self.create()
        res = self.client.get(self.url + ({"cat": self.category.pk},))
        eq_(res.status_code, 200)
        objs = res.json["objects"]
        eq_(len(objs), 1)

    def test_dehydrate(self):
        with self.settings(SITE_URL=""):
            self.create()
            res = self.client.get(self.url + ({"cat": self.category.pk},))
            eq_(res.status_code, 200)
            obj = res.json["objects"][0]
            eq_(obj["absolute_url"], self.webapp.get_absolute_url())
            eq_(obj["app_type"], self.webapp.app_type)
            eq_(obj["content_ratings"], None)
            eq_(obj["current_version"]["version"], u"1.0")
            eq_(obj["current_version"]["developer_name"], self.webapp.current_version.developer_name)
            eq_(obj["description"], unicode(self.webapp.description))
            eq_(obj["icons"]["128"], self.webapp.get_icon_url(128))
            eq_(obj["id"], str(self.webapp.id))
            eq_(obj["manifest_url"], self.webapp.get_manifest_url())
            eq_(obj["payment_account"], None)
            eq_(obj["privacy_policy"], "/api/v1/apps/app/337141/privacy/")
            eq_(obj["public_stats"], self.webapp.public_stats)
            eq_(obj["ratings"], {"average": 0.0, "count": 0})
            eq_(obj["resource_uri"], "/api/v1/apps/app/337141/")
            eq_(obj["slug"], self.webapp.app_slug)
            eq_(obj["supported_locales"], ["en-US", "es", "pt-BR"])

            # These only exists if requested by a reviewer.
            ok_("latest_version_status" not in obj)
            ok_("reviewer_flags" not in obj)

    def test_upsell(self):
        upsell = app_factory()
        AddonUpsell.objects.create(free=self.webapp, premium=upsell)
        self.webapp.save()
        self.refresh("webapp")

        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["upsell"]["id"], upsell.id)
        eq_(obj["upsell"]["app_slug"], upsell.app_slug)
        eq_(obj["upsell"]["name"], upsell.name)
        eq_(obj["upsell"]["icon_url"], upsell.get_icon_url(128))
        eq_(obj["upsell"]["resource_uri"], "/api/v1/apps/app/%s/" % upsell.id)

    def test_dehydrate_regions(self):
        self.webapp.addonexcludedregion.create(region=mkt.regions.BR.id)
        self.webapp.save()
        self.refresh("webapp")

        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        regions = obj["regions"]
        ok_(mkt.regions.BR.slug not in [r["slug"] for r in regions])
        eq_(len(regions), len(mkt.regions.ALL_REGION_IDS) - 1)

    def test_q(self):
        res = self.client.get(self.url + ({"q": "something"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_name_localized(self):
        res = self.client.get(self.url + ({"q": "something", "lang": "es"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)
        eq_(obj["name"], u"Algo Algo Steamcube!")

    def test_name_localized_to_default_locale(self):
        self.webapp.update(default_locale="es")
        self.refresh("webapp")

        # Make a request in another language that we know will fail.
        res = self.client.get(self.url + ({"q": "something", "lang": "de"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)
        eq_(obj["name"], u"Algo Algo Steamcube!")

    def test_device(self):
        AddonDeviceType.objects.create(addon=self.webapp, device_type=DEVICE_CHOICES_IDS["desktop"])
        self.webapp.save()
        self.refresh("webapp")
        res = self.client.get(self.url + ({"device": "desktop"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_no_flash_on_firefoxos(self):
        AddonDeviceType.objects.create(addon=self.webapp, device_type=DEVICE_CHOICES_IDS["firefoxos"])
        f = self.webapp.get_latest_file()
        f.uses_flash = True
        f.save()
        self.webapp.save()
        self.refresh("webapp")
        res = self.client.get(self.url + ({"dev": "firefoxos"},))
        eq_(res.status_code, 200)
        eq_(len(res.json["objects"]), 0)

    def test_premium_types(self):
        res = self.client.get(self.url + ({"premium_types": "free"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_premium_types_empty(self):
        res = self.client.get(self.url + ({"premium_types": "premium"},))
        eq_(res.status_code, 200)
        objs = res.json["objects"]
        eq_(len(objs), 0)

    def test_multiple_premium_types(self):
        res = self.client.get(self.url + ({"premium_types": "free"}, {"premium_types": "premium"}))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_app_type_hosted(self):
        res = self.client.get(self.url + ({"app_type": "hosted"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_app_type_packaged(self):
        self.webapp.update(is_packaged=True)
        self.webapp.save()
        self.refresh("webapp")

        res = self.client.get(self.url + ({"app_type": "packaged"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_status_anon(self):
        res = self.client.get(self.url + ({"status": "public"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"status": "vindaloo"},))
        eq_(res.status_code, 400)
        error = res.json["error_message"]
        eq_(error.keys(), ["status"])

        res = self.client.get(self.url + ({"status": "any"},))
        eq_(res.status_code, 401)
        eq_(json.loads(res.content)["reason"], "Unauthorized to filter by status.")

        res = self.client.get(self.url + ({"status": "rejected"},))
        eq_(res.status_code, 401)
        eq_(json.loads(res.content)["reason"], "Unauthorized to filter by status.")

    def test_status_value_packaged(self):
        # When packaged and not a reviewer we exclude latest version status.
        self.webapp.update(is_packaged=True)
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["status"], amo.STATUS_PUBLIC)
        eq_("latest_version_status" in obj, False)

    def test_addon_type_anon(self):
        res = self.client.get(self.url + ({"type": "app"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"type": "vindaloo"},))
        eq_(res.status_code, 400)
        error = res.json["error_message"]
        eq_(error.keys(), ["type"])

        res = self.client.get(self.url + ({"type": "theme"},))
        eq_(res.status_code, 200)
        eq_(len(res.json["objects"]), 0)

    def test_adolescent_popularity(self):
        """
        Adolescent regions use global popularity.

          Webapp:   Global: 0, Regional: 0
          Unknown1: Global: 1, Regional: 1 + 10 * 1 = 11
          Unknown2: Global: 2, Regional: 0

        """
        user = UserProfile.objects.all()[0]
        cd = ClientData.objects.create(region=mkt.regions.BR.id)

        unknown1 = amo.tests.app_factory()
        Installed.objects.create(addon=unknown1, user=user, client_data=cd)

        unknown2 = amo.tests.app_factory()
        Installed.objects.create(addon=unknown2, user=user)
        Installed.objects.create(addon=unknown2, user=user)

        self.reindex(Webapp, "webapp")

        res = self.client.get(self.url + ({"region": "br"},))
        eq_(res.status_code, 200)

        objects = res.json["objects"]
        eq_(len(objects), 3)

        eq_(int(objects[0]["id"]), unknown2.id)
        eq_(int(objects[1]["id"]), unknown1.id)
        eq_(int(objects[2]["id"]), self.webapp.id)

        # Cleanup to remove these from the index.
        unknown1.delete()
        unknown2.delete()
コード例 #21
0
class TestApi(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141')

    def setUp(self):
        self.client = OAuthClient(None)
        self.list_url = ('api_dispatch_list', {'resource_name': 'search'})
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh()

    def test_verbs(self):
        self._allowed_verbs(self.list_url, ['get'])

    def test_has_cors(self):
        res = self.client.get(self.list_url)
        eq_(res['Access-Control-Allow-Origin'], '*')
        eq_(res['Access-Control-Allow-Methods'], 'GET, OPTIONS')

    def test_meta(self):
        res = self.client.get(self.list_url)
        eq_(res.status_code, 200)
        data = json.loads(res.content)
        eq_(set(data.keys()), set(['objects', 'meta']))
        eq_(data['meta']['total_count'], 1)

    def test_wrong_category(self):
        res = self.client.get(self.list_url + ({
            'cat': self.category.pk + 1
        }, ))
        eq_(res.status_code, 400)
        eq_(res['Content-Type'], 'application/json')

    def test_wrong_weight(self):
        self.category.update(weight=-1)
        res = self.client.get(self.list_url + ({'cat': self.category.pk}, ))
        eq_(res.status_code, 200)
        data = json.loads(res.content)
        eq_(len(data['objects']), 0)

    def test_wrong_sort(self):
        res = self.client.get(self.list_url + ({'sort': 'awesomeness'}, ))
        eq_(res.status_code, 400)

    def test_right_category(self):
        res = self.client.get(self.list_url + ({'cat': self.category.pk}, ))
        eq_(res.status_code, 200)
        eq_(json.loads(res.content)['objects'], [])

    def create(self):
        AddonCategory.objects.create(addon=self.webapp, category=self.category)
        self.webapp.save()
        self.refresh()

    def test_right_category_present(self):
        self.create()
        res = self.client.get(self.list_url + ({'cat': self.category.pk}, ))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 1)

    def test_dehydrate(self):
        with self.settings(SITE_URL=''):
            self.create()
            res = self.client.get(self.list_url + ({
                'cat': self.category.pk
            }, ))
            eq_(res.status_code, 200)
            obj = json.loads(res.content)['objects'][0]
            eq_(obj['slug'], self.webapp.app_slug)
            eq_(obj['icons']['128'], self.webapp.get_icon_url(128))
            eq_(obj['absolute_url'], self.webapp.get_absolute_url())
            eq_(obj['resource_uri'], None)

    def test_q(self):
        res = self.client.get(self.list_url + ({'q': 'something'}, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_device(self):
        AddonDeviceType.objects.create(
            addon=self.webapp, device_type=DEVICE_CHOICES_IDS['desktop'])
        self.webapp.save()
        self.refresh()
        res = self.client.get(self.list_url + ({'device': 'desktop'}, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_premium_types(self):
        res = self.client.get(self.list_url + ({'premium_types': 'free'}, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_premium_types_empty(self):
        res = self.client.get(self.list_url + ({'premium_types': 'premium'}, ))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0)

    def test_multiple_premium_types(self):
        res = self.client.get(self.list_url + ({
            'premium_types': 'free'
        }, {
            'premium_types': 'premium'
        }))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_app_type_hosted(self):
        res = self.client.get(self.list_url + ({'app_type': 'hosted'}, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_app_type_packaged(self):
        self.webapp.update(is_packaged=True)
        self.webapp.save()
        self.refresh()

        res = self.client.get(self.list_url + ({'app_type': 'packaged'}, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_status_anon(self):
        res = self.client.get(self.list_url + ({'status': 'public'}, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.list_url + ({'status': 'vindaloo'}, ))
        eq_(res.status_code, 400)
        error = json.loads(res.content)['error_message']
        eq_(error.keys(), ['status'])

        res = self.client.get(self.list_url + ({'status': 'any'}, ))
        eq_(res.status_code, 401)
        eq_(
            json.loads(res.content)['reason'],
            'Unauthorized to filter by status.')

        res = self.client.get(self.list_url + ({'status': 'rejected'}, ))
        eq_(res.status_code, 401)
        eq_(
            json.loads(res.content)['reason'],
            'Unauthorized to filter by status.')

    def test_status_value_packaged(self):
        # When packaged and not a reviewer we exclude latest version status.
        self.webapp.update(is_packaged=True)
        res = self.client.get(self.list_url)
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['status'], amo.STATUS_PUBLIC)
        eq_('latest_version_status' in obj, False)

    def test_addon_type_anon(self):
        res = self.client.get(self.list_url + ({'type': 'app'}, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.list_url + ({'type': 'vindaloo'}, ))
        eq_(res.status_code, 400)
        error = json.loads(res.content)['error_message']
        eq_(error.keys(), ['type'])

        res = self.client.get(self.list_url + ({'type': 'persona'}, ))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0)
コード例 #22
0
ファイル: test_api.py プロジェクト: canuckistani/zamboni
class TestApi(BaseOAuth, ESTestCase):
    fixtures = ['webapps/337141-steamcube']

    def setUp(self):
        self.client = OAuthClient(None)
        self.list_url = ('api_dispatch_list', {'resource_name': 'search'})
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh()

    def test_verbs(self):
        self._allowed_verbs(self.list_url, ['get'])

    def test_meta(self):
        res = self.client.get(self.list_url)
        eq_(res.status_code, 200)
        eq_(set(json.loads(res.content).keys()), set(['objects', 'meta']))

    def test_wrong_category(self):
        res = self.client.get(self.list_url + ({'cat': self.category.pk + 1},))
        eq_(res.status_code, 400)
        eq_(res['Content-Type'], 'application/json')

    def test_wrong_weight(self):
        self.category.update(weight=-1)
        res = self.client.get(self.list_url + ({'cat': self.category.pk},))
        eq_(res.status_code, 400)

    def test_wrong_sort(self):
        res = self.client.get(self.list_url + ({'sort': 'awesomeness'},))
        eq_(res.status_code, 400)

    def test_right_category(self):
        res = self.client.get(self.list_url + ({'cat': self.category.pk},))
        eq_(res.status_code, 200)
        eq_(json.loads(res.content)['objects'], [])

    def create(self):
        AddonCategory.objects.create(addon=self.webapp, category=self.category)
        self.webapp.save()
        self.refresh()

    def test_right_category_present(self):
        self.create()
        res = self.client.get(self.list_url + ({'cat': self.category.pk},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 1)

    def test_dehdryate(self):
        self.create()
        res = self.client.get(self.list_url + ({'cat': self.category.pk},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)
        eq_(obj['icon_url'], self.webapp.get_icon_url(32))
        eq_(obj['absolute_url'], self.webapp.get_absolute_url())
        eq_(obj['resource_uri'], None)

    @mock.patch('mkt.search.api._filter_search')
    def test_others_ignored(self, _filter_search):
        _filter_search.return_value = []
        res = self.client.get(self.list_url +
                              ({'q': 'foo', 'sort': 'rating'},))
        eq_(res.status_code, 200)
        args = _filter_search.call_args[0][1]
        assert 'sort' in args
        assert 'q' not in args
コード例 #23
0
ファイル: test_api.py プロジェクト: pombredanne/zamboni
class TestApiReviewer(BaseOAuth, ESTestCase):
    fixtures = fixture("webapp_337141", "user_2519")

    def setUp(self, api_name="reviewers"):
        super(TestApiReviewer, self).setUp(api_name=api_name)
        self.user = User.objects.get(pk=2519)
        self.profile = self.user.get_profile()
        self.profile.update(read_dev_agreement=datetime.now())
        self.grant_permission(self.profile, "Apps:Review")

        self.access = Access.objects.create(key="test_oauth_key", secret=generate(), user=self.user)
        self.client = OAuthClient(self.access, api_name=api_name)
        self.url = list_url("search")

        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name="test", type=amo.ADDON_WEBAPP)

        self.webapp.update(status=amo.STATUS_PENDING)
        self.refresh("webapp")

    def test_fields(self):
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        self.assertSetEqual(obj.keys(), ReviewersSearchResource._meta.fields)

    def test_anonymous_access(self):
        res = self.anon.get(self.url)
        eq_(res.status_code, 401)

    def test_non_reviewer_access(self):
        GroupUser.objects.filter(group__rules="Apps:Review", user=self.profile).delete()
        res = self.client.get(self.url)
        eq_(res.status_code, 401)

    def test_owner_still_non_reviewer_access(self):
        user = Webapp.objects.get(pk=337141).authors.all()[0].user
        access = Access.objects.create(key="test_oauth_key_owner", secret=generate(), user=user)
        client = OAuthClient(access, api_name="reviewers")
        res = client.get(self.url)
        eq_(res.status_code, 401)

    def test_status(self):
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"status": "pending"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"status": "rejected"},))
        eq_(res.status_code, 200)
        objs = res.json["objects"]
        eq_(len(objs), 0)

        self.webapp.update(status=amo.STATUS_REJECTED)
        self.refresh("webapp")

        res = self.client.get(self.url + ({"status": "rejected"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        self.webapp.update(status=amo.STATUS_PUBLIC)
        self.refresh("webapp")

        res = self.client.get(self.url + ({"status": "public"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"status": "any"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"status": "vindaloo"},))
        eq_(res.status_code, 400)
        error = res.json["error_message"]
        eq_(error.keys(), ["status"])

    def test_is_escalated(self):
        res = self.client.get(self.url + ({"is_escalated": True},))
        eq_(res.status_code, 200)
        objs = res.json["objects"]
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({"is_escalated": False},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"is_escalated": None},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_has_editors_comment(self):
        res = self.client.get(self.url + ({"has_editor_comment": True},))
        eq_(res.status_code, 200)
        objs = res.json["objects"]
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({"has_editor_comment": False},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"has_editor_comment": None},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_has_info_request(self):
        res = self.client.get(self.url + ({"has_info_request": True},))
        eq_(res.status_code, 200)
        objs = res.json["objects"]
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({"has_info_request": False},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"has_info_request": None},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_addon_type(self):
        res = self.client.get(self.url + ({"type": "app"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"type": "theme"},))
        eq_(res.status_code, 200)
        objs = res.json["objects"]
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({"type": "vindaloo"},))
        eq_(res.status_code, 400)
        error = res.json["error_message"]
        eq_(error.keys(), ["type"])

    def test_no_region_filtering(self):
        self.webapp.addonexcludedregion.create(region=mkt.regions.BR.id)
        self.webapp.save()
        self.refresh("webapp")

        res = self.client.get(self.url + ({"region": "br"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_no_feature_profile_filtering(self):
        self.create_switch("buchets")
        feature_profile = FeatureProfile().to_signature()
        qs = {"q": "something", "pro": feature_profile, "dev": "firefoxos"}

        # Enable an app feature that doesn't match one in our profile.
        self.webapp.current_version.features.update(has_pay=True)
        self.webapp.save()
        self.refresh("webapp")

        res = self.client.get(self.url + (qs,))
        eq_(res.status_code, 200)
        eq_(len(res.json["objects"]), 1)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_no_flash_filtering(self):
        f = self.webapp.get_latest_file()
        f.uses_flash = True
        f.save()
        self.webapp.save()
        self.refresh("webapp")
        res = self.client.get(self.url + ({"dev": "firefoxos"},))
        eq_(res.status_code, 200)
        eq_(len(res.json["objects"]), 1)

    def test_no_premium_filtering(self):
        self.webapp.update(premium_type=amo.ADDON_PREMIUM)
        self.refresh("webapp")
        res = self.client.get(self.url + ({"dev": "android"},))
        eq_(res.status_code, 200)
        eq_(len(res.json["objects"]), 1)
コード例 #24
0
class TestApi(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141')

    def setUp(self):
        self.create_switch('search-api-es')
        self.client = OAuthClient(None)
        self.url = list_url('search')
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh('webapp')

    def test_verbs(self):
        self._allowed_verbs(self.url, ['get'])

    def test_has_cors(self):
        self.assertCORS(self.client.get(self.url), 'get')

    def test_meta(self):
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        eq_(set(res.json.keys()), set(['objects', 'meta']))
        eq_(res.json['meta']['total_count'], 1)

    def test_wrong_category(self):
        res = self.client.get(self.url + ({'cat': self.category.pk + 1}, ))
        eq_(res.status_code, 400)
        eq_(res['Content-Type'], 'application/json')

    def test_wrong_weight(self):
        self.category.update(weight=-1)
        res = self.client.get(self.url + ({'cat': self.category.pk}, ))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 0)

    def test_wrong_sort(self):
        res = self.client.get(self.url + ({'sort': 'awesomeness'}, ))
        eq_(res.status_code, 400)

    def test_right_category(self):
        res = self.client.get(self.url + ({'cat': self.category.pk}, ))
        eq_(res.status_code, 200)
        eq_(res.json['objects'], [])

    def create(self):
        AddonCategory.objects.create(addon=self.webapp, category=self.category)
        self.webapp.save()
        self.refresh('webapp')

    def test_right_category_present(self):
        self.create()
        res = self.client.get(self.url + ({'cat': self.category.pk}, ))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 1)

    def test_dehydrate(self):
        with self.settings(SITE_URL=''):
            self.create()
            res = self.client.get(self.url + ({'cat': self.category.pk}, ))
            eq_(res.status_code, 200)
            obj = res.json['objects'][0]
            eq_(obj['absolute_url'], self.webapp.get_absolute_url())
            eq_(obj['app_type'], self.webapp.app_type)
            eq_(obj['content_ratings'], None)
            eq_(obj['current_version']['version'], u'1.0')
            eq_(obj['description'], unicode(self.webapp.description))
            eq_(obj['icons']['128'], self.webapp.get_icon_url(128))
            eq_(obj['id'], str(self.webapp.id))
            eq_(obj['manifest_url'], self.webapp.get_manifest_url())
            eq_(obj['payment_account'], None)
            eq_(obj['privacy_policy'], '/api/v1/apps/app/337141/privacy/')
            eq_(obj['public_stats'], self.webapp.public_stats)
            eq_(obj['ratings'], {'average': 0.0, 'count': 0})
            eq_(obj['resource_uri'], '/api/v1/apps/app/337141/')
            eq_(obj['slug'], self.webapp.app_slug)
            eq_(obj['summary'], u'')
            eq_(obj['supported_locales'], ['en-US', 'es', 'pt-BR'])

            # These only exists if requested by a reviewer.
            ok_('latest_version_status' not in obj)
            ok_('reviewer_flags' not in obj)

    def test_upsell(self):
        upsell = app_factory()
        AddonUpsell.objects.create(free=self.webapp, premium=upsell)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['upsell']['id'], upsell.id)
        eq_(obj['upsell']['app_slug'], upsell.app_slug)
        eq_(obj['upsell']['name'], upsell.name)
        eq_(obj['upsell']['icon_url'], upsell.get_icon_url(128))
        eq_(obj['upsell']['resource_uri'], '/api/v1/apps/app/%s/' % upsell.id)

    def test_dehydrate_regions(self):
        self.webapp.addonexcludedregion.create(region=mkt.regions.BR.id)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        regions = obj['regions']
        ok_(mkt.regions.BR.slug not in [r['slug'] for r in regions])
        eq_(len(regions), len(mkt.regions.ALL_REGION_IDS) - 1)

    def test_q(self):
        res = self.client.get(self.url + ({'q': 'something'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_name_localized(self):
        res = self.client.get(self.url + ({'q': 'something', 'lang': 'es'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)
        eq_(obj['name'], u'Algo Algo Steamcube!')

    def test_name_localized_to_default_locale(self):
        self.webapp.update(default_locale='es')
        self.refresh('webapp')

        # Make a request in another language that we know will fail.
        res = self.client.get(self.url + ({'q': 'something', 'lang': 'de'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)
        eq_(obj['name'], u'Algo Algo Steamcube!')

    def test_device(self):
        AddonDeviceType.objects.create(
            addon=self.webapp, device_type=DEVICE_CHOICES_IDS['desktop'])
        self.webapp.save()
        self.refresh('webapp')
        res = self.client.get(self.url + ({'device': 'desktop'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_no_flash_on_firefoxos(self):
        AddonDeviceType.objects.create(
            addon=self.webapp, device_type=DEVICE_CHOICES_IDS['firefoxos'])
        f = self.webapp.get_latest_file()
        f.uses_flash = True
        f.save()
        self.webapp.save()
        self.refresh('webapp')
        res = self.client.get(self.url + ({'dev': 'firefoxos'}, ))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 0)

    def test_premium_types(self):
        res = self.client.get(self.url + ({'premium_types': 'free'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_premium_types_empty(self):
        res = self.client.get(self.url + ({'premium_types': 'premium'}, ))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

    def test_multiple_premium_types(self):
        res = self.client.get(self.url + ({
            'premium_types': 'free'
        }, {
            'premium_types': 'premium'
        }))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_app_type_hosted(self):
        res = self.client.get(self.url + ({'app_type': 'hosted'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_app_type_packaged(self):
        self.webapp.update(is_packaged=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + ({'app_type': 'packaged'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_status_anon(self):
        res = self.client.get(self.url + ({'status': 'public'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'status': 'vindaloo'}, ))
        eq_(res.status_code, 400)
        error = res.json['error_message']
        eq_(error.keys(), ['status'])

        res = self.client.get(self.url + ({'status': 'any'}, ))
        eq_(res.status_code, 401)
        eq_(
            json.loads(res.content)['reason'],
            'Unauthorized to filter by status.')

        res = self.client.get(self.url + ({'status': 'rejected'}, ))
        eq_(res.status_code, 401)
        eq_(
            json.loads(res.content)['reason'],
            'Unauthorized to filter by status.')

    def test_status_value_packaged(self):
        # When packaged and not a reviewer we exclude latest version status.
        self.webapp.update(is_packaged=True)
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['status'], amo.STATUS_PUBLIC)
        eq_('latest_version_status' in obj, False)

    def test_addon_type_anon(self):
        res = self.client.get(self.url + ({'type': 'app'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'type': 'vindaloo'}, ))
        eq_(res.status_code, 400)
        error = res.json['error_message']
        eq_(error.keys(), ['type'])

        res = self.client.get(self.url + ({'type': 'theme'}, ))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 0)

    @patch.object(mkt.regions.US, 'supports_carrier_billing', False)
    def test_minimum_price_tier(self):
        price = Price.objects.create(name='5', price='0.50')
        PriceCurrency.objects.create(currency='BRL', price='1.00', tier=price)
        AddonPremium.objects.create(addon=self.webapp, price=price)
        self.webapp.save()
        self.refresh('webapp')
        res = self.client.get(self.url + ({'region': 'br'}, ))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 1)
        res2 = self.client.get(self.url + ({'region': 'us'}, ))
        eq_(res2.status_code, 200)
        eq_(len(res2.json['objects']), 0)

    def test_adolescent_popularity(self):
        """
        Adolescent regions use global popularity.

          Webapp:   Global: 0, Regional: 0
          Unknown1: Global: 1, Regional: 1 + 10 * 1 = 11
          Unknown2: Global: 2, Regional: 0

        """
        user = UserProfile.objects.all()[0]
        cd = ClientData.objects.create(region=mkt.regions.BR.id)

        unknown1 = amo.tests.app_factory()
        Installed.objects.create(addon=unknown1, user=user, client_data=cd)

        unknown2 = amo.tests.app_factory()
        Installed.objects.create(addon=unknown2, user=user)
        Installed.objects.create(addon=unknown2, user=user)

        self.reindex(Webapp, 'webapp')

        res = self.client.get(self.url + ({'region': 'br'}, ))
        eq_(res.status_code, 200)

        objects = res.json['objects']
        eq_(len(objects), 3)

        eq_(int(objects[0]['id']), unknown2.id)
        eq_(int(objects[1]['id']), unknown1.id)
        eq_(int(objects[2]['id']), self.webapp.id)

        # Cleanup to remove these from the index.
        unknown1.delete()
        unknown2.delete()
コード例 #25
0
class TestApiFeatures(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141')

    def setUp(self):
        self.create_switch('search-api-es')
        self.create_switch('buchets')
        self.client = OAuthClient(None)
        self.url = list_url('search')
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)
        # A typical desktop profile on Firefox with the following features:
        # {'apps': True,
        #  'audio': True,
        #  'battery': True,
        #  'device_storage': True,
        #  'fullscreen': True,
        #  'geolocation': True,
        #  'idle': True,
        #  'indexeddb': True,
        #  'light_events': True,
        #  'network_info': True,
        #  'orientation': True,
        #  'proximity': True,
        #  'push': True,
        #  'sms': True,
        #  'vibrate': True,
        #  'video_webm': True,
        #  'webaudio': True}
        self.profile = '8a7dd46c.32.1'
        self.qs = {'q': 'something', 'pro': self.profile, 'dev': 'firefoxos'}

    def test_no_features(self):
        # Base test to make sure we find the app.
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (self.qs, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_one_good_feature(self):
        # Enable an app feature that matches one in our profile.
        self.webapp.current_version.features.update(has_geolocation=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (self.qs, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_one_bad_feature(self):
        # Enable an app feature that doesn't match one in our profile.
        self.webapp.current_version.features.update(has_pay=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (self.qs, ))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0)

    def test_all_good_features(self):
        # Enable app features so they exactly match our device profile.
        fp = FeatureProfile.from_signature(self.profile)
        self.webapp.current_version.features.update(**dict(
            ('has_%s' % k, v) for k, v in fp.items()))
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (self.qs, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_bad_profile_on_desktop(self):
        # Enable an app feature that doesn't match one in our profile.
        qs = self.qs.copy()
        del qs['dev']  # Desktop doesn't send a device.
        self.webapp.current_version.features.update(has_pay=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (qs, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)
コード例 #26
0
class TestApiReviewer(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141', 'user_2519')

    def setUp(self, api_name='apps'):
        self.create_switch('search-api-es')
        self.user = User.objects.get(pk=2519)
        self.profile = self.user.get_profile()
        self.profile.update(read_dev_agreement=datetime.now())
        self.grant_permission(self.profile, 'Apps:Review')

        self.access = Access.objects.create(key='test_oauth_key',
                                            secret=generate(),
                                            user=self.user)
        self.client = OAuthClient(self.access, api_name=api_name)
        self.url = list_url('search')

        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh('webapp')

    def test_status_reviewer(self):
        res = self.client.get(self.url + ({'status': 'public'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'status': 'rejected'}, ))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({'status': 'any'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'status': 'vindaloo'}, ))
        eq_(res.status_code, 400)
        error = res.json['error_message']
        eq_(error.keys(), ['status'])

    def test_status_value_packaged(self):
        # When packaged we also include the latest version status.
        self.webapp.update(is_packaged=True)
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['status'], amo.STATUS_PUBLIC)
        eq_(obj['latest_version_status'], amo.STATUS_PUBLIC)

    def test_addon_type_reviewer(self):
        res = self.client.get(self.url + ({'type': 'app'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'type': 'theme'}, ))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({'type': 'vindaloo'}, ))
        eq_(res.status_code, 400)
        error = res.json['error_message']
        eq_(error.keys(), ['type'])

    def test_extra_attributes(self):
        version = self.webapp.versions.latest()
        version.has_editor_comment = True
        version.has_info_request = True
        version.save()

        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]

        # These only exist if requested by a reviewer.
        eq_(obj['latest_version_status'], amo.STATUS_PUBLIC)
        eq_(obj['reviewer_flags']['has_comment'], True)
        eq_(obj['reviewer_flags']['has_info_request'], True)
        eq_(obj['reviewer_flags']['is_escalated'], False)
コード例 #27
0
ファイル: test_api.py プロジェクト: rtilder/zamboni
class TestApiReviewer(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141', 'user_2519')

    def setUp(self, api_name='apps'):
        self.user = User.objects.get(pk=2519)
        self.profile = self.user.get_profile()
        self.profile.update(read_dev_agreement=datetime.now())
        self.grant_permission(self.profile, 'Apps:Review')

        self.access = Access.objects.create(
            key='test_oauth_key', secret=generate(), user=self.user)
        self.client = OAuthClient(self.access, api_name=api_name)
        self.url = list_url('search')

        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh()

    def test_status_reviewer(self):
        res = self.client.get(self.url + ({'status': 'public'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'status': 'rejected'},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({'status': 'any'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'status': 'vindaloo'},))
        eq_(res.status_code, 400)
        error = json.loads(res.content)['error_message']
        eq_(error.keys(), ['status'])

    def test_status_value_packaged(self):
        # When packaged we also include the latest version status.
        self.webapp.update(is_packaged=True)
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['status'], amo.STATUS_PUBLIC)
        eq_(obj['latest_version_status'], amo.STATUS_PUBLIC)

    def test_addon_type_reviewer(self):
        res = self.client.get(self.url + ({'type': 'app'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'type': 'persona'},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({'type': 'vindaloo'},))
        eq_(res.status_code, 400)
        error = json.loads(res.content)['error_message']
        eq_(error.keys(), ['type'])

    def test_extra_attributes(self):
        version = self.webapp.versions.latest()
        version.has_editor_comment = True
        version.has_info_request = True
        version.save()

        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]

        # These only exist if requested by a reviewer.
        eq_(obj['latest_version_status'], None)
        eq_(obj['reviewer_flags']['has_comment'], True)
        eq_(obj['reviewer_flags']['has_info_request'], True)
        eq_(obj['reviewer_flags']['is_escalated'], False)
コード例 #28
0
ファイル: test_api.py プロジェクト: markh-bz/zamboni
class TestApiReviewer(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141', 'user_2519')

    def setUp(self, api_name='reviewers'):
        super(TestApiReviewer, self).setUp(api_name=api_name)
        self.user = User.objects.get(pk=2519)
        self.profile = self.user.get_profile()
        self.profile.update(read_dev_agreement=datetime.now())
        self.grant_permission(self.profile, 'Apps:Review')

        self.access = Access.objects.create(
            key='test_oauth_key', secret=generate(), user=self.user)
        self.client = OAuthClient(self.access, api_name=api_name)
        self.url = list_url('search')

        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)

        self.webapp.update(status=amo.STATUS_PENDING)
        self.refresh('webapp')

    def test_anonymous_access(self):
        res = self.anon.get(self.url)
        eq_(res.status_code, 401)

    def test_non_reviewer_access(self):
        GroupUser.objects.filter(group__rules='Apps:Review',
                                 user=self.profile).delete()
        res = self.client.get(self.url)
        eq_(res.status_code, 401)

    def test_owner_still_non_reviewer_access(self):
        user = Webapp.objects.get(pk=337141).authors.all()[0].user
        access = Access.objects.create(
            key='test_oauth_key_owner', secret=generate(), user=user)
        client = OAuthClient(access, api_name='reviewers')
        res = client.get(self.url)
        eq_(res.status_code, 401)

    def test_status(self):
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'status': 'pending'},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'status': 'rejected'},))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

        self.webapp.update(status=amo.STATUS_REJECTED)
        self.refresh('webapp')

        res = self.client.get(self.url + ({'status': 'rejected'},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        self.webapp.update(status=amo.STATUS_PUBLIC)
        self.refresh('webapp')

        res = self.client.get(self.url + ({'status': 'public'},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'status': 'any'},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'status': 'vindaloo'},))
        eq_(res.status_code, 400)
        error = res.json['error_message']
        eq_(error.keys(), ['status'])

    def test_is_privileged(self):
        res = self.client.get(self.url + ({'is_privileged': True},))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({'is_privileged': False},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'is_privileged': None},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    @patch('versions.models.Version.is_privileged', True)
    def test_is_privileged_true(self):
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + ({'is_privileged': False},))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({'is_privileged': True},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'is_privileged': None},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_is_escalated(self):
        res = self.client.get(self.url + ({'is_escalated': True},))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({'is_escalated': False},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'is_escalated': None},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_has_editors_comment(self):
        res = self.client.get(self.url + ({'has_editor_comment': True},))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({'has_editor_comment': False},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'has_editor_comment': None},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_has_info_request(self):
        res = self.client.get(self.url + ({'has_info_request': True},))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({'has_info_request': False},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'has_info_request': None},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_addon_type(self):
        res = self.client.get(self.url + ({'type': 'app'},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'type': 'theme'},))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({'type': 'vindaloo'},))
        eq_(res.status_code, 400)
        error = res.json['error_message']
        eq_(error.keys(), ['type'])

    def test_no_region_filtering(self):
        self.webapp.addonexcludedregion.create(region=mkt.regions.BR.id)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + ({'region': 'br'},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_no_feature_profile_filtering(self):
        self.create_switch('buchets')
        feature_profile = FeatureProfile().to_signature()
        qs = {'q': 'something', 'pro': feature_profile, 'dev': 'firefoxos'}

        # Enable an app feature that doesn't match one in our profile.
        self.webapp.current_version.features.update(has_pay=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (qs,))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)
コード例 #29
0
ファイル: test_api.py プロジェクト: fwenzel/zamboni
class TestApi(BaseOAuth, ESTestCase):
    fixtures = ['webapps/337141-steamcube']

    def setUp(self):
        self.client = OAuthClient(None)
        self.list_url = ('api_dispatch_list', {'resource_name': 'search'})
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh()

    def test_verbs(self):
        self._allowed_verbs(self.list_url, ['get'])

    def test_meta(self):
        res = self.client.get(self.list_url)
        eq_(res.status_code, 200)
        eq_(set(json.loads(res.content).keys()), set(['objects', 'meta']))

    def test_wrong_category(self):
        res = self.client.get(self.list_url + ({'cat': self.category.pk + 1},))
        eq_(res.status_code, 400)
        eq_(res['Content-Type'], 'application/json')

    def test_wrong_weight(self):
        self.category.update(weight=-1)
        res = self.client.get(self.list_url + ({'cat': self.category.pk},))
        eq_(res.status_code, 400)

    def test_wrong_sort(self):
        res = self.client.get(self.list_url + ({'sort': 'awesomeness'},))
        eq_(res.status_code, 400)

    def test_right_category(self):
        res = self.client.get(self.list_url + ({'cat': self.category.pk},))
        eq_(res.status_code, 200)
        eq_(json.loads(res.content)['objects'], [])

    def create(self):
        AddonCategory.objects.create(addon=self.webapp, category=self.category)
        self.webapp.save()
        self.refresh()

    def test_right_category_present(self):
        self.create()
        res = self.client.get(self.list_url + ({'cat': self.category.pk},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 1)

    def test_dehydrate(self):
        self.create()
        res = self.client.get(self.list_url + ({'cat': self.category.pk},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['app_slug'], self.webapp.app_slug)
        eq_(obj['icon_url_128'], self.webapp.get_icon_url(128))
        eq_(obj['absolute_url'], self.webapp.get_absolute_url())
        eq_(obj['resource_uri'], None)

    def test_q(self):
        res = self.client.get(self.list_url + ({'q': 'something'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['app_slug'], self.webapp.app_slug)

    def test_device(self):
        AddonDeviceType.objects.create(
            addon=self.webapp, device_type=DEVICE_CHOICES_IDS['desktop'])
        self.webapp.save()
        self.refresh()
        res = self.client.get(self.list_url + ({'device': 'desktop'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['app_slug'], self.webapp.app_slug)

    def test_premium_types(self):
        res = self.client.get(self.list_url + (
            {'premium_types': 'free'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['app_slug'], self.webapp.app_slug)

    def test_premium_types_empty(self):
        res = self.client.get(self.list_url + (
            {'premium_types': 'premium'},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0)

    def test_multiple_premium_types(self):
        res = self.client.get(self.list_url + (
            {'premium_types': 'free'},
            {'premium_types': 'premium'}))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['app_slug'], self.webapp.app_slug)
コード例 #30
0
class TestApi(BaseOAuth, ESTestCase):
    fixtures = ['webapps/337141-steamcube']

    def setUp(self):
        self.client = OAuthClient(None)
        self.list_url = ('api_dispatch_list', {'resource_name': 'search'})
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh()

    def test_verbs(self):
        self._allowed_verbs(self.list_url, ['get'])

    def test_meta(self):
        res = self.client.get(self.list_url)
        eq_(res.status_code, 200)
        eq_(set(json.loads(res.content).keys()), set(['objects', 'meta']))

    def test_wrong_category(self):
        res = self.client.get(self.list_url + ({'cat': self.category.pk + 1},))
        eq_(res.status_code, 400)
        eq_(res['Content-Type'], 'application/json')

    def test_wrong_weight(self):
        self.category.update(weight=-1)
        res = self.client.get(self.list_url + ({'cat': self.category.pk},))
        eq_(res.status_code, 400)

    def test_wrong_sort(self):
        res = self.client.get(self.list_url + ({'sort': 'awesomeness'},))
        eq_(res.status_code, 400)

    def test_right_category(self):
        res = self.client.get(self.list_url + ({'cat': self.category.pk},))
        eq_(res.status_code, 200)
        eq_(json.loads(res.content)['objects'], [])

    def create(self):
        AddonCategory.objects.create(addon=self.webapp, category=self.category)
        self.webapp.save()
        self.refresh()

    def test_right_category_present(self):
        self.create()
        res = self.client.get(self.list_url + ({'cat': self.category.pk},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 1)

    def test_dehdryate(self):
        self.create()
        res = self.client.get(self.list_url + ({'cat': self.category.pk},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)
        eq_(obj['icon_url_128'], self.webapp.get_icon_url(128))
        eq_(obj['absolute_url'], self.webapp.get_absolute_url())
        eq_(obj['resource_uri'], None)

    @mock.patch('mkt.search.api._filter_search')
    def test_others_ignored(self, _filter_search):
        _filter_search.return_value = []
        res = self.client.get(self.list_url +
                              ({'q': 'foo', 'sort': 'rating'},))
        eq_(res.status_code, 200)
        args = _filter_search.call_args[0][1]
        assert 'sort' in args
        assert 'q' not in args
コード例 #31
0
ファイル: test_api.py プロジェクト: flyun/zamboni
class TestApi(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141')

    def setUp(self):
        self.client = OAuthClient(None)
        self.list_url = ('api_dispatch_list', {'resource_name': 'search'})
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh()

    def test_verbs(self):
        self._allowed_verbs(self.list_url, ['get'])

    def test_has_cors(self):
        res = self.client.get(self.list_url)
        eq_(res['Access-Control-Allow-Origin'], '*')
        eq_(res['Access-Control-Allow-Methods'], 'GET, OPTIONS')

    def test_meta(self):
        res = self.client.get(self.list_url)
        eq_(res.status_code, 200)
        data = json.loads(res.content)
        eq_(set(data.keys()), set(['objects', 'meta']))
        eq_(data['meta']['total_count'], 1)

    def test_wrong_category(self):
        res = self.client.get(self.list_url + ({'cat': self.category.pk + 1},))
        eq_(res.status_code, 400)
        eq_(res['Content-Type'], 'application/json')

    def test_wrong_weight(self):
        self.category.update(weight=-1)
        res = self.client.get(self.list_url + ({'cat': self.category.pk},))
        eq_(res.status_code, 200)
        data = json.loads(res.content)
        eq_(len(data['objects']), 0)

    def test_wrong_sort(self):
        res = self.client.get(self.list_url + ({'sort': 'awesomeness'},))
        eq_(res.status_code, 400)

    def test_right_category(self):
        res = self.client.get(self.list_url + ({'cat': self.category.pk},))
        eq_(res.status_code, 200)
        eq_(json.loads(res.content)['objects'], [])

    def create(self):
        AddonCategory.objects.create(addon=self.webapp, category=self.category)
        self.webapp.save()
        self.refresh()

    def test_right_category_present(self):
        self.create()
        res = self.client.get(self.list_url + ({'cat': self.category.pk},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 1)

    def test_dehydrate(self):
        with self.settings(SITE_URL=''):
            self.create()
            res = self.client.get(self.list_url + ({'cat': self.category.pk},))
            eq_(res.status_code, 200)
            obj = json.loads(res.content)['objects'][0]
            eq_(obj['slug'], self.webapp.app_slug)
            eq_(obj['icons']['128'], self.webapp.get_icon_url(128))
            eq_(obj['absolute_url'], self.webapp.get_absolute_url())
            eq_(obj['resource_uri'], None)

    def test_q(self):
        res = self.client.get(self.list_url + ({'q': 'something'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_device(self):
        AddonDeviceType.objects.create(
            addon=self.webapp, device_type=DEVICE_CHOICES_IDS['desktop'])
        self.webapp.save()
        self.refresh()
        res = self.client.get(self.list_url + ({'device': 'desktop'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_premium_types(self):
        res = self.client.get(self.list_url + (
            {'premium_types': 'free'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_premium_types_empty(self):
        res = self.client.get(self.list_url + (
            {'premium_types': 'premium'},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0)

    def test_multiple_premium_types(self):
        res = self.client.get(self.list_url + (
            {'premium_types': 'free'},
            {'premium_types': 'premium'}))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_app_type_hosted(self):
        res = self.client.get(self.list_url + ({'app_type': 'hosted'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_app_type_packaged(self):
        self.webapp.update(is_packaged=True)
        self.webapp.save()
        self.refresh()

        res = self.client.get(self.list_url + ({'app_type': 'packaged'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_status_anon(self):
        res = self.client.get(self.list_url + ({'status': 'public'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.list_url + ({'status': 'vindaloo'},))
        eq_(res.status_code, 400)
        error = json.loads(res.content)['error_message']
        eq_(error.keys(), ['status'])

        res = self.client.get(self.list_url + ({'status': 'any'},))
        eq_(res.status_code, 401)
        eq_(json.loads(res.content)['reason'],
            'Unauthorized to filter by status.')

        res = self.client.get(self.list_url + ({'status': 'rejected'},))
        eq_(res.status_code, 401)
        eq_(json.loads(res.content)['reason'],
            'Unauthorized to filter by status.')

    def test_status_value_packaged(self):
        # When packaged and not a reviewer we exclude latest version status.
        self.webapp.update(is_packaged=True)
        res = self.client.get(self.list_url)
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['status'], amo.STATUS_PUBLIC)
        eq_('latest_version_status' in obj, False)

    def test_addon_type_anon(self):
        res = self.client.get(self.list_url + ({'type': 'app'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.list_url + ({'type': 'vindaloo'},))
        eq_(res.status_code, 400)
        error = json.loads(res.content)['error_message']
        eq_(error.keys(), ['type'])

        res = self.client.get(self.list_url + ({'type': 'persona'},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0)
コード例 #32
0
ファイル: test_api.py プロジェクト: chenliu0831/zamboni
class TestApi(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141')

    def setUp(self):
        self.client = OAuthClient(None)
        self.url = list_url('search')
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                slug='test',
                                                type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh('webapp')

    def test_verbs(self):
        self._allowed_verbs(self.url, ['get'])

    def test_has_cors(self):
        self.assertCORS(self.client.get(self.url), 'get')

    def test_meta(self):
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        eq_(set(res.json.keys()), set(['objects', 'meta']))
        eq_(res.json['meta']['total_count'], 1)

    def test_wrong_category(self):
        res = self.client.get(self.url + ({
            'cat': self.category.slug + 'xq'
        }, ))
        eq_(res.status_code, 400)
        eq_(res['Content-Type'], 'application/json')

    def test_wrong_weight(self):
        self.category.update(weight=-1)
        res = self.client.get(self.url + ({'cat': self.category.slug}, ))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 0)

    def test_wrong_sort(self):
        res = self.client.get(self.url + ({'sort': 'awesomeness'}, ))
        eq_(res.status_code, 400)

    def test_sort(self):
        with patch('mkt.webapps.models.Webapp.from_search') as mocked_search:
            mocked_qs = MagicMock()
            mocked_search.return_value = mocked_qs
            res = self.client.get(self.url, [('sort', 'downloads'),
                                             ('sort', 'rating')])
            eq_(res.status_code, 200)
            mocked_qs.order_by.assert_called_with('-weekly_downloads',
                                                  '-bayesian_rating')

    def test_right_category(self):
        res = self.client.get(self.url + ({'cat': self.category.pk}, ))
        eq_(res.status_code, 200)
        eq_(res.json['objects'], [])

    def create(self):
        AddonCategory.objects.create(addon=self.webapp, category=self.category)
        self.webapp.save()
        self.refresh('webapp')

    def test_right_category_present(self):
        self.create()
        res = self.client.get(self.url + ({'cat': self.category.pk}, ))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 1)

    def test_user_info_with_shared_secret(self):
        user = UserProfile.objects.all()[0]

        def fakeauth(auth, req, **kw):
            req.amo_user = user
            return True

        with patch(
                'mkt.api.authentication.SharedSecretAuthentication'
                '.is_authenticated', fakeauth):
            with self.settings(SITE_URL=''):
                self.create()
            res = self.client.get(self.url + ({'cat': self.category.pk}, ))
            obj = res.json['objects'][0]
            assert 'user' in obj

    def test_dehydrate(self):
        with self.settings(SITE_URL=''):
            self.create()
            res = self.client.get(self.url + ({'cat': self.category.pk}, ))
            eq_(res.status_code, 200)
            obj = res.json['objects'][0]
            eq_(obj['absolute_url'], self.webapp.get_absolute_url())
            eq_(obj['app_type'], self.webapp.app_type)
            eq_(obj['content_ratings'], {
                'descriptors': [],
                'interactive_elements': [],
                'ratings': None
            })
            eq_(obj['current_version'], u'1.0')
            eq_(obj['description'], unicode(self.webapp.description))
            eq_(obj['icons']['128'], self.webapp.get_icon_url(128))
            eq_(obj['id'], str(self.webapp.id))
            eq_(obj['manifest_url'], self.webapp.get_manifest_url())
            eq_(obj['payment_account'], None)
            eq_(obj['privacy_policy'], '/api/apps/app/337141/privacy/')
            eq_(obj['public_stats'], self.webapp.public_stats)
            eq_(obj['ratings'], {'average': 0.0, 'count': 0})
            eq_(obj['resource_uri'], '/api/apps/app/337141/')
            eq_(obj['slug'], self.webapp.app_slug)
            eq_(obj['supported_locales'], ['en-US', 'es', 'pt-BR'])
            eq_(obj['versions'], {u'1.0': u'/api/apps/versions/1268829/'})

            # These only exists if requested by a reviewer.
            ok_('latest_version' not in obj)
            ok_('reviewer_flags' not in obj)

    def test_upsell(self):
        upsell = app_factory(premium_type=amo.ADDON_PREMIUM)
        AddonUpsell.objects.create(free=self.webapp, premium=upsell)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url, {'premium_types': 'free'})
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 1)
        obj = res.json['objects'][0]
        eq_(obj['upsell']['id'], upsell.id)
        eq_(obj['upsell']['app_slug'], upsell.app_slug)
        eq_(obj['upsell']['name'], upsell.name)
        eq_(obj['upsell']['icon_url'], upsell.get_icon_url(128))
        eq_(obj['upsell']['resource_uri'], '/api/apps/app/%s/' % upsell.id)
        eq_(obj['upsell']['region_exclusions'], [])

        unindex_webapps([upsell.id])
        upsell.delete()

    def test_dehydrate_regions(self):
        self.webapp.addonexcludedregion.create(region=mkt.regions.BR.id)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        regions = obj['regions']
        ok_(mkt.regions.BR.slug not in [r['slug'] for r in regions])
        eq_(len(regions), len(mkt.regions.ALL_REGION_IDS) - 1)

    def test_region_filtering(self):
        self.webapp.addonexcludedregion.create(region=mkt.regions.BR.id)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + ({'region': 'br'}, ))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

    def test_languages_filtering(self):
        # This webapp's supported_locales: [u'en-US', u'es', u'pt-BR']

        res = self.client.get(self.url + ({'languages': 'fr'}, ))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

        for lang in ('fr,pt-BR', 'es, pt-BR', 'es', 'pt-BR'):
            res = self.client.get(self.url + ({'languages': lang}, ))
            eq_(res.status_code, 200)
            obj = res.json['objects'][0]
            eq_(obj['slug'], self.webapp.app_slug)

    def test_offline_filtering(self):
        def check(offline, visible):
            res = self.client.get(self.url + ({'offline': offline}, ))
            eq_(res.status_code, 200)
            objs = res.json['objects']
            eq_(len(objs), int(visible))

        # Should NOT show up in offline.
        # Should show up in online.
        # Should show up everywhere if not filtered.
        check(offline='True', visible=False)
        check(offline='False', visible=True)
        check(offline='None', visible=True)

        # Mark that app is capable offline.
        self.webapp.update(is_packaged=True)
        self.refresh('webapp')

        # Should show up in offline.
        # Should NOT show up in online.
        # Should show up everywhere if not filtered.
        check(offline='True', visible=True)
        check(offline='False', visible=False)
        check(offline='None', visible=True)

    def test_q(self):
        res = self.client.get(self.url + ({'q': 'something'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_q_exact(self):
        app1 = app_factory(name='test app test11')
        app2 = app_factory(name='test app test21')
        app3 = app_factory(name='test app test31')
        self.refresh('webapp')

        res = self.client.get(self.url + ({'q': 'test app test21'}, ))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 3)
        # app2 should be first since it's an exact match and is boosted higher.
        obj = res.json['objects'][0]
        eq_(obj['slug'], app2.app_slug)

        unindex_webapps([app1.id, app2.id, app3.id])
        app1.delete()
        app2.delete()
        app3.delete()

    def test_q_is_tag(self):
        Tag(tag_text='whatsupp').save_tag(self.webapp)
        self.webapp.save()
        self.refresh('webapp')
        res = self.client.get(self.url + ({'q': 'whatsupp'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_icu_folding(self):
        self.webapp.name = {'es': 'Páginas Amarillos'}
        self.webapp.save()
        self.refresh('webapp')
        res = self.client.get(self.url + ({'q': 'paginas'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_camel_case_word_splitting(self):
        self.webapp.name = 'AirCombat'
        self.webapp.save()
        self.refresh('webapp')
        res = self.client.get(self.url + ({'q': 'air combat'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_name_localized(self):
        res = self.client.get(self.url + ({'q': 'something', 'lang': 'es'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)
        eq_(obj['name'], u'Algo Algo Steamcube!')

    def test_name_localized_to_default_locale(self):
        self.webapp.update(default_locale='es')
        self.refresh('webapp')

        # Make a request in another language that we know will fail.
        res = self.client.get(self.url + ({'q': 'something', 'lang': 'de'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)
        eq_(obj['name'], u'Algo Algo Steamcube!')

    def test_device(self):
        AddonDeviceType.objects.create(
            addon=self.webapp, device_type=DEVICE_CHOICES_IDS['desktop'])
        self.webapp.save()
        self.refresh('webapp')
        res = self.client.get(self.url + ({'device': 'desktop'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_no_flash_on_firefoxos(self):
        AddonDeviceType.objects.create(
            addon=self.webapp, device_type=DEVICE_CHOICES_IDS['firefoxos'])
        f = self.webapp.get_latest_file()
        f.uses_flash = True
        f.save()
        self.webapp.save()
        self.refresh('webapp')
        res = self.client.get(self.url + ({'dev': 'firefoxos'}, ))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 0)

    def test_premium_types(self):
        res = self.client.get(self.url + ({'premium_types': 'free'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_premium_types_empty(self):
        res = self.client.get(self.url + ({'premium_types': 'premium'}, ))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

    def test_multiple_premium_types(self):
        res = self.client.get(self.url + ({
            'premium_types': 'free'
        }, {
            'premium_types': 'premium'
        }))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_app_type_hosted(self):
        res = self.client.get(self.url + ({'app_type': 'hosted'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_app_type_packaged(self):
        self.webapp.update(is_packaged=True)
        self.refresh('webapp')

        res = self.client.get(self.url + ({'app_type': 'packaged'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_app_type_privileged(self):
        # Override the class-decorated patch.
        with patch('versions.models.Version.is_privileged', True):
            self.webapp.update(is_packaged=True)
            self.refresh('webapp')

            res = self.client.get(self.url + ({'app_type': 'packaged'}, ))
            eq_(res.status_code, 200)
            eq_(len(res.json['objects']), 0)

            res = self.client.get(self.url + ({'app_type': 'privileged'}, ))
            eq_(res.status_code, 200)
            eq_(len(res.json['objects']), 1)
            obj = res.json['objects'][0]
            eq_(obj['slug'], self.webapp.app_slug)

    def test_status_value_packaged(self):
        # When packaged and not a reviewer we exclude latest version status.
        self.webapp.update(is_packaged=True)
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['status'], amo.STATUS_PUBLIC)
        eq_('latest_version' in obj, False)

    def test_addon_type_anon(self):
        res = self.client.get(self.url + ({'type': 'app'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'type': 'vindaloo'}, ))
        eq_(res.status_code, 400)
        error = res.json['error_message']
        eq_(error.keys(), ['type'])

        res = self.client.get(self.url + ({'type': 'theme'}, ))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 0)

    def test_adolescent_popularity(self):
        """
        Adolescent regions use global popularity.

          Webapp:   Global: 0, Regional: 0
          Unknown1: Global: 1, Regional: 1 + 10 * 1 = 11
          Unknown2: Global: 2, Regional: 0

        """
        user = UserProfile.objects.all()[0]
        cd = ClientData.objects.create(region=mkt.regions.BR.id)

        unknown1 = amo.tests.app_factory()
        Installed.objects.create(addon=unknown1, user=user, client_data=cd)

        unknown2 = amo.tests.app_factory()
        Installed.objects.create(addon=unknown2, user=user)
        Installed.objects.create(addon=unknown2, user=user)

        self.reindex(Webapp, 'webapp')

        res = self.client.get(self.url + ({'region': 'br'}, ))
        eq_(res.status_code, 200)

        objects = res.json['objects']
        eq_(len(objects), 3)

        eq_(int(objects[0]['id']), unknown2.id)
        eq_(int(objects[1]['id']), unknown1.id)
        eq_(int(objects[2]['id']), self.webapp.id)

        # Cleanup to remove these from the index.
        unindex_webapps([unknown1.id, unknown2.id])
        unknown1.delete()
        unknown2.delete()

    def test_word_delimiter_preserves_original(self):
        self.webapp.description = {
            'en-US':
            'This is testing word delimiting preservation in long '
            'descriptions and here is what we want to find: WhatsApp'
        }
        self.webapp.save()
        self.reindex(Webapp, 'webapp')

        res = self.client.get(self.url + ({'q': 'whatsapp'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)
コード例 #33
0
ファイル: test_api.py プロジェクト: wraithan/zamboni
class TestApiFeatures(BaseOAuth, ESTestCase):
    fixtures = fixture("webapp_337141")

    def setUp(self):
        self.create_switch("search-api-es")
        self.create_switch("buchets")
        self.client = OAuthClient(None)
        self.url = list_url("search")
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name="test", type=amo.ADDON_WEBAPP)
        # Pick a few common device features.
        self.profile = FeatureProfile(
            apps=True, audio=True, fullscreen=True, geolocation=True, indexeddb=True, sms=True
        ).to_signature()
        self.qs = {"q": "something", "pro": self.profile, "dev": "firefoxos"}

    def test_no_features(self):
        # Base test to make sure we find the app.
        self.webapp.save()
        self.refresh("webapp")

        res = self.client.get(self.url + (self.qs,))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_one_good_feature(self):
        # Enable an app feature that matches one in our profile.
        self.webapp.current_version.features.update(has_geolocation=True)
        self.webapp.save()
        self.refresh("webapp")

        res = self.client.get(self.url + (self.qs,))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_one_bad_feature(self):
        # Enable an app feature that doesn't match one in our profile.
        self.webapp.current_version.features.update(has_pay=True)
        self.webapp.save()
        self.refresh("webapp")

        res = self.client.get(self.url + (self.qs,))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)["objects"]
        eq_(len(objs), 0)

    def test_all_good_features(self):
        # Enable app features so they exactly match our device profile.
        fp = FeatureProfile.from_signature(self.profile)
        self.webapp.current_version.features.update(**dict(("has_%s" % k, v) for k, v in fp.items()))
        self.webapp.save()
        self.refresh("webapp")

        res = self.client.get(self.url + (self.qs,))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_bad_profile_on_desktop(self):
        # Enable an app feature that doesn't match one in our profile.
        qs = self.qs.copy()
        del qs["dev"]  # Desktop doesn't send a device.
        self.webapp.current_version.features.update(has_pay=True)
        self.webapp.save()
        self.refresh("webapp")

        res = self.client.get(self.url + (qs,))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)
コード例 #34
0
ファイル: test_api.py プロジェクト: wraithan/zamboni
class TestApiReviewer(BaseOAuth, ESTestCase):
    fixtures = fixture("webapp_337141", "user_2519")

    def setUp(self, api_name="apps"):
        self.create_switch("search-api-es")
        self.user = User.objects.get(pk=2519)
        self.profile = self.user.get_profile()
        self.profile.update(read_dev_agreement=datetime.now())
        self.grant_permission(self.profile, "Apps:Review")

        self.access = Access.objects.create(key="test_oauth_key", secret=generate(), user=self.user)
        self.client = OAuthClient(self.access, api_name=api_name)
        self.url = list_url("search")

        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name="test", type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh("webapp")

    def test_status_reviewer(self):
        res = self.client.get(self.url + ({"status": "public"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"status": "rejected"},))
        eq_(res.status_code, 200)
        objs = res.json["objects"]
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({"status": "any"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"status": "vindaloo"},))
        eq_(res.status_code, 400)
        error = res.json["error_message"]
        eq_(error.keys(), ["status"])

    def test_status_value_packaged(self):
        # When packaged we also include the latest version status.
        self.webapp.update(is_packaged=True)
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["status"], amo.STATUS_PUBLIC)
        eq_(obj["latest_version_status"], amo.STATUS_PUBLIC)

    def test_addon_type_reviewer(self):
        res = self.client.get(self.url + ({"type": "app"},))
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"type": "theme"},))
        eq_(res.status_code, 200)
        objs = res.json["objects"]
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({"type": "vindaloo"},))
        eq_(res.status_code, 400)
        error = res.json["error_message"]
        eq_(error.keys(), ["type"])

    def test_extra_attributes(self):
        version = self.webapp.versions.latest()
        version.has_editor_comment = True
        version.has_info_request = True
        version.save()

        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]

        # These only exist if requested by a reviewer.
        eq_(obj["latest_version_status"], amo.STATUS_PUBLIC)
        eq_(obj["reviewer_flags"]["has_comment"], True)
        eq_(obj["reviewer_flags"]["has_info_request"], True)
        eq_(obj["reviewer_flags"]["is_escalated"], False)

    def test_extra_attributes_no_waffle(self):
        # Make sure these still exist when 'search-api-es' is off.
        # TODO: Remove this test when we remove that switch.
        Switch.objects.all().delete()
        version = self.webapp.versions.latest()
        version.has_editor_comment = True
        version.has_info_request = True
        version.save()

        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json["objects"][0]

        # These only exist if requested by a reviewer.
        eq_(obj["latest_version_status"], amo.STATUS_PUBLIC)
        eq_(obj["reviewer_flags"]["has_comment"], True)
        eq_(obj["reviewer_flags"]["has_info_request"], True)
        eq_(obj["reviewer_flags"]["is_escalated"], False)
コード例 #35
0
ファイル: test_api.py プロジェクト: tofumatt/zamboni
class TestApi(BaseOAuth, ESTestCase):
    fixtures = fixture("webapp_337141")

    def setUp(self):
        self.create_switch("search-api-es")
        self.client = OAuthClient(None)
        self.url = list_url("search")
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name="test", type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh("webapp")

    def test_verbs(self):
        self._allowed_verbs(self.url, ["get"])

    def test_has_cors(self):
        self.assertCORS(self.client.get(self.url), "get")

    def test_meta(self):
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        data = json.loads(res.content)
        eq_(set(data.keys()), set(["objects", "meta"]))
        eq_(data["meta"]["total_count"], 1)

    def test_wrong_category(self):
        res = self.client.get(self.url + ({"cat": self.category.pk + 1},))
        eq_(res.status_code, 400)
        eq_(res["Content-Type"], "application/json")

    def test_wrong_weight(self):
        self.category.update(weight=-1)
        res = self.client.get(self.url + ({"cat": self.category.pk},))
        eq_(res.status_code, 200)
        data = json.loads(res.content)
        eq_(len(data["objects"]), 0)

    def test_wrong_sort(self):
        res = self.client.get(self.url + ({"sort": "awesomeness"},))
        eq_(res.status_code, 400)

    def test_right_category(self):
        res = self.client.get(self.url + ({"cat": self.category.pk},))
        eq_(res.status_code, 200)
        eq_(json.loads(res.content)["objects"], [])

    def create(self):
        AddonCategory.objects.create(addon=self.webapp, category=self.category)
        self.webapp.save()
        self.refresh("webapp")

    def test_right_category_present(self):
        self.create()
        res = self.client.get(self.url + ({"cat": self.category.pk},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)["objects"]
        eq_(len(objs), 1)

    def test_dehydrate(self):
        with self.settings(SITE_URL=""):
            self.create()
            res = self.client.get(self.url + ({"cat": self.category.pk},))
            eq_(res.status_code, 200)
            obj = json.loads(res.content)["objects"][0]
            eq_(obj["absolute_url"], self.webapp.get_absolute_url())
            eq_(obj["app_type"], self.webapp.app_type)
            eq_(obj["content_ratings"], None)
            eq_(obj["current_version"]["version"], u"1.0")
            eq_(obj["description"], unicode(self.webapp.description))
            eq_(obj["icons"]["128"], self.webapp.get_icon_url(128))
            eq_(obj["id"], str(self.webapp.id))
            eq_(obj["manifest_url"], self.webapp.get_manifest_url())
            eq_(obj["payment_account"], None)
            eq_(obj["privacy_policy"], "/api/v1/apps/app/337141/privacy/")
            eq_(obj["public_stats"], self.webapp.public_stats)
            eq_(obj["ratings"], {"average": 0.0, "count": 0})
            eq_(obj["resource_uri"], "/api/v1/apps/app/337141/")
            eq_(obj["slug"], self.webapp.app_slug)
            eq_(obj["summary"], u"")

            # These only exists if requested by a reviewer.
            ok_("latest_version_status" not in obj)
            ok_("reviewer_flags" not in obj)

    def test_upsell(self):
        upsell = app_factory()
        AddonUpsell.objects.create(free=self.webapp, premium=upsell)
        self.webapp.save()
        self.refresh("webapp")

        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["upsell"]["id"], upsell.id)
        eq_(obj["upsell"]["app_slug"], upsell.app_slug)
        eq_(obj["upsell"]["name"], upsell.name)
        eq_(obj["upsell"]["icon_url"], upsell.get_icon_url(128))
        eq_(obj["upsell"]["resource_uri"], "/api/v1/apps/app/%s/" % upsell.id)

    def test_q(self):
        res = self.client.get(self.url + ({"q": "something"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_name_localized(self):
        res = self.client.get(self.url + ({"q": "something", "lang": "es"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)
        eq_(obj["name"], u"Algo Algo Steamcube!")

    def test_name_localized_to_default_locale(self):
        self.webapp.update(default_locale="es")
        self.refresh("webapp")

        # Make a request in another language that we know will fail.
        res = self.client.get(self.url + ({"q": "something", "lang": "de"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)
        eq_(obj["name"], u"Algo Algo Steamcube!")

    def test_device(self):
        AddonDeviceType.objects.create(addon=self.webapp, device_type=DEVICE_CHOICES_IDS["desktop"])
        self.webapp.save()
        self.refresh("webapp")
        res = self.client.get(self.url + ({"device": "desktop"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_no_flash_on_firefoxos(self):
        AddonDeviceType.objects.create(addon=self.webapp, device_type=DEVICE_CHOICES_IDS["firefoxos"])
        f = self.webapp.get_latest_file()
        f.uses_flash = True
        f.save()
        self.webapp.save()
        self.refresh("webapp")
        res = self.client.get(self.url + ({"dev": "firefoxos"},))
        eq_(res.status_code, 200)
        eq_(len(json.loads(res.content)["objects"]), 0)

    def test_premium_types(self):
        res = self.client.get(self.url + ({"premium_types": "free"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_premium_types_empty(self):
        res = self.client.get(self.url + ({"premium_types": "premium"},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)["objects"]
        eq_(len(objs), 0)

    def test_multiple_premium_types(self):
        res = self.client.get(self.url + ({"premium_types": "free"}, {"premium_types": "premium"}))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_app_type_hosted(self):
        res = self.client.get(self.url + ({"app_type": "hosted"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_app_type_packaged(self):
        self.webapp.update(is_packaged=True)
        self.webapp.save()
        self.refresh("webapp")

        res = self.client.get(self.url + ({"app_type": "packaged"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_status_anon(self):
        res = self.client.get(self.url + ({"status": "public"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"status": "vindaloo"},))
        eq_(res.status_code, 400)
        error = json.loads(res.content)["error_message"]
        eq_(error.keys(), ["status"])

        res = self.client.get(self.url + ({"status": "any"},))
        eq_(res.status_code, 401)
        eq_(json.loads(res.content)["reason"], "Unauthorized to filter by status.")

        res = self.client.get(self.url + ({"status": "rejected"},))
        eq_(res.status_code, 401)
        eq_(json.loads(res.content)["reason"], "Unauthorized to filter by status.")

    def test_status_value_packaged(self):
        # When packaged and not a reviewer we exclude latest version status.
        self.webapp.update(is_packaged=True)
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["status"], amo.STATUS_PUBLIC)
        eq_("latest_version_status" in obj, False)

    def test_addon_type_anon(self):
        res = self.client.get(self.url + ({"type": "app"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"type": "vindaloo"},))
        eq_(res.status_code, 400)
        error = json.loads(res.content)["error_message"]
        eq_(error.keys(), ["type"])

        res = self.client.get(self.url + ({"type": "theme"},))
        eq_(res.status_code, 200)
        eq_(len(res.json["objects"]), 0)

    @patch.object(mkt.regions.US, "supports_carrier_billing", False)
    def test_minimum_price_tier(self):
        price = Price.objects.create(name="5", price="0.50")
        PriceCurrency.objects.create(currency="BRL", price="1.00", tier=price)
        AddonPremium.objects.create(addon=self.webapp, price=price)
        self.webapp.save()
        self.refresh("webapp")
        res = self.client.get(self.url + ({"region": "br"},))
        eq_(res.status_code, 200)
        eq_(len(res.json["objects"]), 1)
        res2 = self.client.get(self.url + ({"region": "us"},))
        eq_(res2.status_code, 200)
        eq_(len(res2.json["objects"]), 0)
コード例 #36
0
class TestApi(BaseOAuth, ESTestCase):
    fixtures = fixture("webapp_337141")

    def setUp(self):
        self.client = OAuthClient(None)
        self.url = list_url("search")
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name="test", type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh()

    def test_verbs(self):
        self._allowed_verbs(self.url, ["get"])

    def test_has_cors(self):
        self.assertCORS(self.client.get(self.url), "get")

    def test_meta(self):
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        data = json.loads(res.content)
        eq_(set(data.keys()), set(["objects", "meta"]))
        eq_(data["meta"]["total_count"], 1)

    def test_wrong_category(self):
        res = self.client.get(self.url + ({"cat": self.category.pk + 1},))
        eq_(res.status_code, 400)
        eq_(res["Content-Type"], "application/json")

    def test_wrong_weight(self):
        self.category.update(weight=-1)
        res = self.client.get(self.url + ({"cat": self.category.pk},))
        eq_(res.status_code, 200)
        data = json.loads(res.content)
        eq_(len(data["objects"]), 0)

    def test_wrong_sort(self):
        res = self.client.get(self.url + ({"sort": "awesomeness"},))
        eq_(res.status_code, 400)

    def test_right_category(self):
        res = self.client.get(self.url + ({"cat": self.category.pk},))
        eq_(res.status_code, 200)
        eq_(json.loads(res.content)["objects"], [])

    def create(self):
        AddonCategory.objects.create(addon=self.webapp, category=self.category)
        self.webapp.save()
        self.refresh()

    def test_right_category_present(self):
        self.create()
        res = self.client.get(self.url + ({"cat": self.category.pk},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)["objects"]
        eq_(len(objs), 1)

    def test_dehydrate(self):
        with self.settings(SITE_URL=""):
            self.create()
            res = self.client.get(self.url + ({"cat": self.category.pk},))
            eq_(res.status_code, 200)
            obj = json.loads(res.content)["objects"][0]
            eq_(obj["slug"], self.webapp.app_slug)
            eq_(obj["icons"]["128"], self.webapp.get_icon_url(128))
            eq_(obj["absolute_url"], self.webapp.get_absolute_url())
            eq_(obj["resource_uri"], None)

    def test_q(self):
        res = self.client.get(self.url + ({"q": "something"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_device(self):
        AddonDeviceType.objects.create(addon=self.webapp, device_type=DEVICE_CHOICES_IDS["desktop"])
        self.webapp.save()
        self.refresh()
        res = self.client.get(self.url + ({"device": "desktop"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_premium_types(self):
        res = self.client.get(self.url + ({"premium_types": "free"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_premium_types_empty(self):
        res = self.client.get(self.url + ({"premium_types": "premium"},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)["objects"]
        eq_(len(objs), 0)

    def test_multiple_premium_types(self):
        res = self.client.get(self.url + ({"premium_types": "free"}, {"premium_types": "premium"}))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_app_type_hosted(self):
        res = self.client.get(self.url + ({"app_type": "hosted"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_app_type_packaged(self):
        self.webapp.update(is_packaged=True)
        self.webapp.save()
        self.refresh()

        res = self.client.get(self.url + ({"app_type": "packaged"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

    def test_status_anon(self):
        res = self.client.get(self.url + ({"status": "public"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"status": "vindaloo"},))
        eq_(res.status_code, 400)
        error = json.loads(res.content)["error_message"]
        eq_(error.keys(), ["status"])

        res = self.client.get(self.url + ({"status": "any"},))
        eq_(res.status_code, 401)
        eq_(json.loads(res.content)["reason"], "Unauthorized to filter by status.")

        res = self.client.get(self.url + ({"status": "rejected"},))
        eq_(res.status_code, 401)
        eq_(json.loads(res.content)["reason"], "Unauthorized to filter by status.")

    def test_status_value_packaged(self):
        # When packaged and not a reviewer we exclude latest version status.
        self.webapp.update(is_packaged=True)
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["status"], amo.STATUS_PUBLIC)
        eq_("latest_version_status" in obj, False)

    def test_addon_type_anon(self):
        res = self.client.get(self.url + ({"type": "app"},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)["objects"][0]
        eq_(obj["slug"], self.webapp.app_slug)

        res = self.client.get(self.url + ({"type": "vindaloo"},))
        eq_(res.status_code, 400)
        error = json.loads(res.content)["error_message"]
        eq_(error.keys(), ["type"])

        res = self.client.get(self.url + ({"type": "persona"},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)["objects"]
        eq_(len(objs), 0)
コード例 #37
0
ファイル: test_api.py プロジェクト: at13/zamboni
class TestApi(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141')

    def setUp(self):
        self.create_switch('soft_delete')
        self.client = OAuthClient(None)
        self.url = list_url('search')
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test', slug='test',
                                                type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh('webapp')

    def test_verbs(self):
        self._allowed_verbs(self.url, ['get'])

    def test_has_cors(self):
        self.assertCORS(self.client.get(self.url), 'get')

    def test_meta(self):
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        eq_(set(res.json.keys()), set(['objects', 'meta']))
        eq_(res.json['meta']['total_count'], 1)

    def test_wrong_category(self):
        res = self.client.get(self.url + ({'cat': self.category.slug + 'xq'},))
        eq_(res.status_code, 400)
        eq_(res['Content-Type'], 'application/json')

    def test_wrong_weight(self):
        self.category.update(weight=-1)
        res = self.client.get(self.url + ({'cat': self.category.slug},))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 0)

    def test_wrong_sort(self):
        res = self.client.get(self.url + ({'sort': 'awesomeness'},))
        eq_(res.status_code, 400)

    def test_sort(self):
        with patch('mkt.webapps.models.Webapp.from_search') as mocked_search:
            mocked_qs = MagicMock()
            mocked_search.return_value = mocked_qs
            res = self.client.get(self.url,
                                  [('sort', 'downloads'), ('sort', 'rating')])
            eq_(res.status_code, 200)
            mocked_qs.order_by.assert_called_with('-weekly_downloads',
                                                  '-bayesian_rating')

    def test_right_category(self):
        res = self.client.get(self.url + ({'cat': self.category.pk},))
        eq_(res.status_code, 200)
        eq_(res.json['objects'], [])

    def create(self):
        AddonCategory.objects.create(addon=self.webapp, category=self.category)
        self.webapp.save()
        self.refresh('webapp')

    def test_right_category_present(self):
        self.create()
        res = self.client.get(self.url + ({'cat': self.category.pk},))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 1)

    def test_user_info_with_shared_secret(self):
        user = UserProfile.objects.all()[0]

        def fakeauth(auth, req, **kw):
            req.amo_user = user
            return True

        with patch('mkt.api.authentication.SharedSecretAuthentication'
                   '.is_authenticated', fakeauth):
            with self.settings(SITE_URL=''):
                self.create()
            res = self.client.get(self.url + ({'cat': self.category.pk},))
            obj = res.json['objects'][0]
            assert 'user' in obj

    def test_dehydrate(self):
        with self.settings(SITE_URL=''):
            self.create()
            res = self.client.get(self.url + ({'cat': self.category.pk},))
            eq_(res.status_code, 200)
            obj = res.json['objects'][0]
            eq_(obj['absolute_url'], self.webapp.get_absolute_url())
            eq_(obj['app_type'], self.webapp.app_type)
            eq_(obj['content_ratings'], None)
            eq_(obj['current_version'], u'1.0')
            eq_(obj['description'], unicode(self.webapp.description))
            eq_(obj['icons']['128'], self.webapp.get_icon_url(128))
            eq_(obj['id'], str(self.webapp.id))
            eq_(obj['manifest_url'], self.webapp.get_manifest_url())
            eq_(obj['payment_account'], None)
            eq_(obj['privacy_policy'], '/api/v1/apps/app/337141/privacy/')
            eq_(obj['public_stats'], self.webapp.public_stats)
            eq_(obj['ratings'], {'average': 0.0, 'count': 0})
            eq_(obj['resource_uri'], '/api/v1/apps/app/337141/')
            eq_(obj['slug'], self.webapp.app_slug)
            eq_(obj['supported_locales'], ['en-US', 'es', 'pt-BR'])
            eq_(obj['versions'], {u'1.0': u'/api/v1/apps/versions/1268829/'})

            # These only exists if requested by a reviewer.
            ok_('latest_version' not in obj)
            ok_('reviewer_flags' not in obj)

    def test_upsell(self):
        upsell = app_factory()
        AddonUpsell.objects.create(free=self.webapp, premium=upsell)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['upsell']['id'], upsell.id)
        eq_(obj['upsell']['app_slug'], upsell.app_slug)
        eq_(obj['upsell']['name'], upsell.name)
        eq_(obj['upsell']['icon_url'], upsell.get_icon_url(128))
        eq_(obj['upsell']['resource_uri'], '/api/v1/apps/app/%s/' % upsell.id)

        unindex_webapps([upsell.id])
        upsell.delete()

    def test_dehydrate_regions(self):
        self.webapp.addonexcludedregion.create(region=mkt.regions.BR.id)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        regions = obj['regions']
        ok_(mkt.regions.BR.slug not in [r['slug'] for r in regions])
        eq_(len(regions), len(mkt.regions.ALL_REGION_IDS) - 1)

    def test_region_filtering(self):
        self.webapp.addonexcludedregion.create(region=mkt.regions.BR.id)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + ({'region': 'br'},))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

    def test_q(self):
        res = self.client.get(self.url + ({'q': 'something'},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_name_localized(self):
        res = self.client.get(self.url + ({'q': 'something',
                                           'lang': 'es'},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)
        eq_(obj['name'], u'Algo Algo Steamcube!')

    def test_name_localized_to_default_locale(self):
        self.webapp.update(default_locale='es')
        self.refresh('webapp')

        # Make a request in another language that we know will fail.
        res = self.client.get(self.url + ({'q': 'something',
                                           'lang': 'de'},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)
        eq_(obj['name'], u'Algo Algo Steamcube!')

    def test_device(self):
        AddonDeviceType.objects.create(
            addon=self.webapp, device_type=DEVICE_CHOICES_IDS['desktop'])
        self.webapp.save()
        self.refresh('webapp')
        res = self.client.get(self.url + ({'device': 'desktop'},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_no_flash_on_firefoxos(self):
        AddonDeviceType.objects.create(
            addon=self.webapp, device_type=DEVICE_CHOICES_IDS['firefoxos'])
        f = self.webapp.get_latest_file()
        f.uses_flash = True
        f.save()
        self.webapp.save()
        self.refresh('webapp')
        res = self.client.get(self.url + ({'dev': 'firefoxos'},))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 0)

    def test_premium_types(self):
        res = self.client.get(self.url + (
            {'premium_types': 'free'},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_premium_types_empty(self):
        res = self.client.get(self.url + (
            {'premium_types': 'premium'},))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

    def test_multiple_premium_types(self):
        res = self.client.get(self.url + (
            {'premium_types': 'free'},
            {'premium_types': 'premium'}))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_app_type_hosted(self):
        res = self.client.get(self.url + ({'app_type': 'hosted'},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_app_type_packaged(self):
        self.webapp.update(is_packaged=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + ({'app_type': 'packaged'},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_status_value_packaged(self):
        # When packaged and not a reviewer we exclude latest version status.
        self.webapp.update(is_packaged=True)
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['status'], amo.STATUS_PUBLIC)
        eq_('latest_version' in obj, False)

    def test_addon_type_anon(self):
        res = self.client.get(self.url + ({'type': 'app'},))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'type': 'vindaloo'},))
        eq_(res.status_code, 400)
        error = res.json['error_message']
        eq_(error.keys(), ['type'])

        res = self.client.get(self.url + ({'type': 'theme'},))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 0)

    def test_adolescent_popularity(self):
        """
        Adolescent regions use global popularity.

          Webapp:   Global: 0, Regional: 0
          Unknown1: Global: 1, Regional: 1 + 10 * 1 = 11
          Unknown2: Global: 2, Regional: 0

        """
        user = UserProfile.objects.all()[0]
        cd = ClientData.objects.create(region=mkt.regions.BR.id)

        unknown1 = amo.tests.app_factory()
        Installed.objects.create(addon=unknown1, user=user, client_data=cd)

        unknown2 = amo.tests.app_factory()
        Installed.objects.create(addon=unknown2, user=user)
        Installed.objects.create(addon=unknown2, user=user)

        self.reindex(Webapp, 'webapp')

        res = self.client.get(self.url + ({'region': 'br'},))
        eq_(res.status_code, 200)

        objects = res.json['objects']
        eq_(len(objects), 3)

        eq_(int(objects[0]['id']), unknown2.id)
        eq_(int(objects[1]['id']), unknown1.id)
        eq_(int(objects[2]['id']), self.webapp.id)

        # Cleanup to remove these from the index.
        unindex_webapps([unknown1.id, unknown2.id])
        unknown1.delete()
        unknown2.delete()
コード例 #38
0
ファイル: test_api.py プロジェクト: chenliu0831/zamboni
class TestApiReviewer(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141', 'user_2519')

    def setUp(self, api_name='reviewers'):
        super(TestApiReviewer, self).setUp(api_name=api_name)
        self.user = User.objects.get(pk=2519)
        self.profile = self.user.get_profile()
        self.profile.update(read_dev_agreement=datetime.now())
        self.grant_permission(self.profile, 'Apps:Review')

        self.access = Access.objects.create(key='test_oauth_key',
                                            secret=generate(),
                                            user=self.user)
        self.client = OAuthClient(self.access, api_name=api_name)
        self.url = list_url('search')

        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)

        self.webapp.update(status=amo.STATUS_PENDING)
        self.refresh('webapp')

    def test_fields(self):
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        self.assertSetEqual(obj.keys(), ReviewersSearchResource._meta.fields)

    def test_anonymous_access(self):
        res = self.anon.get(self.url)
        eq_(res.status_code, 401)

    def test_non_reviewer_access(self):
        GroupUser.objects.filter(group__rules='Apps:Review',
                                 user=self.profile).delete()
        res = self.client.get(self.url)
        eq_(res.status_code, 401)

    def test_owner_still_non_reviewer_access(self):
        user = Webapp.objects.get(pk=337141).authors.all()[0].user
        access = Access.objects.create(key='test_oauth_key_owner',
                                       secret=generate(),
                                       user=user)
        client = OAuthClient(access, api_name='reviewers')
        res = client.get(self.url)
        eq_(res.status_code, 401)

    def test_status(self):
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'status': 'pending'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'status': 'rejected'}, ))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

        self.webapp.update(status=amo.STATUS_REJECTED)
        self.refresh('webapp')

        res = self.client.get(self.url + ({'status': 'rejected'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        self.webapp.update(status=amo.STATUS_PUBLIC)
        self.refresh('webapp')

        res = self.client.get(self.url + ({'status': 'public'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'status': 'any'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'status': 'vindaloo'}, ))
        eq_(res.status_code, 400)
        error = res.json['error_message']
        eq_(error.keys(), ['status'])

    def test_is_escalated(self):
        res = self.client.get(self.url + ({'is_escalated': True}, ))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({'is_escalated': False}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'is_escalated': None}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_has_editors_comment(self):
        res = self.client.get(self.url + ({'has_editor_comment': True}, ))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({'has_editor_comment': False}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'has_editor_comment': None}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_has_info_request(self):
        res = self.client.get(self.url + ({'has_info_request': True}, ))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({'has_info_request': False}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'has_info_request': None}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_addon_type(self):
        res = self.client.get(self.url + ({'type': 'app'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'type': 'theme'}, ))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.url + ({'type': 'vindaloo'}, ))
        eq_(res.status_code, 400)
        error = res.json['error_message']
        eq_(error.keys(), ['type'])

    def test_no_region_filtering(self):
        self.webapp.addonexcludedregion.create(region=mkt.regions.BR.id)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + ({'region': 'br'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_no_feature_profile_filtering(self):
        feature_profile = FeatureProfile().to_signature()
        qs = {'q': 'something', 'pro': feature_profile, 'dev': 'firefoxos'}

        # Enable an app feature that doesn't match one in our profile.
        self.webapp.current_version.features.update(has_pay=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (qs, ))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 1)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_no_flash_filtering(self):
        f = self.webapp.get_latest_file()
        f.uses_flash = True
        f.save()
        self.webapp.save()
        self.refresh('webapp')
        res = self.client.get(self.url + ({'dev': 'firefoxos'}, ))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 1)

    def test_no_premium_filtering(self):
        self.webapp.update(premium_type=amo.ADDON_PREMIUM)
        self.refresh('webapp')
        res = self.client.get(self.url + ({'dev': 'android'}, ))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 1)
コード例 #39
0
ファイル: test_api.py プロジェクト: MikeLing/zamboni
class TestApiFeatures(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141')

    def setUp(self):
        self.create_switch('search-api-es')
        self.create_switch('buchets')
        self.client = OAuthClient(None)
        self.url = list_url('search')
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)
        # A typical desktop profile on Firefox with the following features:
        # {'apps': True,
        #  'audio': True,
        #  'battery': True,
        #  'device_storage': True,
        #  'fullscreen': True,
        #  'geolocation': True,
        #  'idle': True,
        #  'indexeddb': True,
        #  'light_events': True,
        #  'network_info': True,
        #  'orientation': True,
        #  'proximity': True,
        #  'push': True,
        #  'sms': True,
        #  'vibrate': True,
        #  'video_webm': True,
        #  'webaudio': True}
        self.profile = '8a7dd46c.32.1'
        self.qs = {'q': 'something', 'pro': self.profile, 'dev': 'firefoxos'}

    def test_no_features(self):
        # Base test to make sure we find the app.
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (self.qs,))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_one_good_feature(self):
        # Enable an app feature that matches one in our profile.
        self.webapp.current_version.features.update(has_geolocation=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (self.qs,))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_one_bad_feature(self):
        # Enable an app feature that doesn't match one in our profile.
        self.webapp.current_version.features.update(has_pay=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (self.qs,))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0)

    def test_all_good_features(self):
        # Enable app features so they exactly match our device profile.
        fp = FeatureProfile.from_signature(self.profile)
        self.webapp.current_version.features.update(
            **dict(('has_%s' % k, v) for k, v in fp.items()))
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (self.qs,))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_bad_profile_on_desktop(self):
        # Enable an app feature that doesn't match one in our profile.
        qs = self.qs.copy()
        del qs['dev']  # Desktop doesn't send a device.
        self.webapp.current_version.features.update(has_pay=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (qs,))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)
コード例 #40
0
class TestApiReviewer(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141', 'user_2519')

    def setUp(self, api_name='apps'):
        self.user = User.objects.get(pk=2519)
        self.profile = self.user.get_profile()
        self.profile.update(read_dev_agreement=datetime.now())
        self.grant_permission(self.profile, 'Apps:Review')

        self.access = Access.objects.create(key='foo',
                                            secret=generate(),
                                            user=self.user)
        self.client = OAuthClient(self.access, api_name=api_name)
        self.list_url = ('api_dispatch_list', {'resource_name': 'search'})

        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh()

    def test_status_reviewer(self):
        res = self.client.get(self.list_url + ({'status': 'public'}, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.list_url + ({'status': 'rejected'}, ))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.list_url + ({'status': 'any'}, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.list_url + ({'status': 'vindaloo'}, ))
        eq_(res.status_code, 400)
        error = json.loads(res.content)['error_message']
        eq_(error.keys(), ['status'])

    def test_status_value_packaged(self):
        # When packaged we also include the latest version status.
        self.webapp.update(is_packaged=True)
        res = self.client.get(self.list_url)
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['status'], amo.STATUS_PUBLIC)
        eq_(obj['latest_version_status'], amo.STATUS_PUBLIC)

    def test_addon_type_reviewer(self):
        res = self.client.get(self.list_url + ({'type': 'app'}, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.list_url + ({'type': 'persona'}, ))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.list_url + ({'type': 'vindaloo'}, ))
        eq_(res.status_code, 400)
        error = json.loads(res.content)['error_message']
        eq_(error.keys(), ['type'])
コード例 #41
0
ファイル: test_api.py プロジェクト: chenliu0831/zamboni
class TestApiFeatures(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141')

    def setUp(self):
        self.client = OAuthClient(None)
        self.url = list_url('search')
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)
        # Pick a few common device features.
        self.profile = FeatureProfile(apps=True, audio=True, fullscreen=True,
                                      geolocation=True, indexeddb=True,
                                      sms=True).to_signature()
        self.qs = {'q': 'something', 'pro': self.profile, 'dev': 'firefoxos'}

    def test_no_features(self):
        # Base test to make sure we find the app.
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (self.qs,))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_one_good_feature(self):
        # Enable an app feature that matches one in our profile.
        self.webapp.current_version.features.update(has_geolocation=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (self.qs,))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_one_bad_feature(self):
        # Enable an app feature that doesn't match one in our profile.
        self.webapp.current_version.features.update(has_pay=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (self.qs,))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0)

    def test_all_good_features(self):
        # Enable app features so they exactly match our device profile.
        fp = FeatureProfile.from_signature(self.profile)
        self.webapp.current_version.features.update(
            **dict(('has_%s' % k, v) for k, v in fp.items()))
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (self.qs,))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_bad_profile_on_desktop(self):
        # Enable an app feature that doesn't match one in our profile.
        qs = self.qs.copy()
        del qs['dev']  # Desktop doesn't send a device.
        self.webapp.current_version.features.update(has_pay=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (qs,))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)
コード例 #42
0
ファイル: test_api.py プロジェクト: chenliu0831/zamboni
class TestApiFeatures(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141')

    def setUp(self):
        self.client = OAuthClient(None)
        self.url = list_url('search')
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)
        # Pick a few common device features.
        self.profile = FeatureProfile(apps=True,
                                      audio=True,
                                      fullscreen=True,
                                      geolocation=True,
                                      indexeddb=True,
                                      sms=True).to_signature()
        self.qs = {'q': 'something', 'pro': self.profile, 'dev': 'firefoxos'}

    def test_no_features(self):
        # Base test to make sure we find the app.
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (self.qs, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_one_good_feature(self):
        # Enable an app feature that matches one in our profile.
        self.webapp.current_version.features.update(has_geolocation=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (self.qs, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_one_bad_feature(self):
        # Enable an app feature that doesn't match one in our profile.
        self.webapp.current_version.features.update(has_pay=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (self.qs, ))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0)

    def test_all_good_features(self):
        # Enable app features so they exactly match our device profile.
        fp = FeatureProfile.from_signature(self.profile)
        self.webapp.current_version.features.update(**dict(
            ('has_%s' % k, v) for k, v in fp.items()))
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (self.qs, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_bad_profile_on_desktop(self):
        # Enable an app feature that doesn't match one in our profile.
        qs = self.qs.copy()
        del qs['dev']  # Desktop doesn't send a device.
        self.webapp.current_version.features.update(has_pay=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + (qs, ))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)
コード例 #43
0
ファイル: test_api.py プロジェクト: sandy-slin/zamboni
class TestAccount(BaseOAuth):
    fixtures = fixture('user_2519', 'user_10482', 'webapp_337141')

    def setUp(self):
        super(TestAccount, self).setUp(api_name='account')
        self.list_url = list_url('settings')
        self.get_url = get_url('settings', '2519')
        self.anon = OAuthClient(None, api_name='account')
        self.user = UserProfile.objects.get(pk=2519)

    def test_verbs(self):
        self._allowed_verbs(self.list_url, ())
        self._allowed_verbs(self.get_url, ('get', 'patch', 'put'))

    def test_not_allowed(self):
        eq_(self.anon.get(self.get_url).status_code, 401)

    def test_allowed(self):
        res = self.client.get(self.get_url)
        eq_(res.status_code, 200, res.content)
        data = json.loads(res.content)
        eq_(data['display_name'], self.user.display_name)
        eq_(data['installed'], [])

    def test_install(self):
        ins = Installed.objects.create(user=self.user, addon_id=337141)
        res = self.client.get(self.get_url)
        eq_(res.status_code, 200, res.content)
        data = json.loads(res.content)
        eq_(data['installed'],
            [get_absolute_url(get_url('app', ins.addon.pk), absolute=False)])

    def test_install_reviewer(self):
        Installed.objects.create(user=self.user, addon_id=337141,
                                 install_type=INSTALL_TYPE_REVIEWER)
        res = self.client.get(self.get_url)
        eq_(res.status_code, 200, res.content)
        data = json.loads(res.content)
        eq_(data['installed'], [])

    def test_other(self):
        eq_(self.client.get(get_url('settings', '10482')).status_code, 403)

    def test_own(self):
        res = self.client.get(get_url('settings', 'mine'))
        eq_(res.status_code, 200)
        data = json.loads(res.content)
        eq_(data['display_name'], self.user.display_name)

    def test_patch(self):
        res = self.client.patch(self.get_url,
                                data=json.dumps({'display_name': 'foo'}))
        eq_(res.status_code, 202)
        user = UserProfile.objects.get(pk=self.user.pk)
        eq_(user.display_name, 'foo')

    def test_put(self):
        res = self.client.put(self.get_url,
                              data=json.dumps({'display_name': 'foo'}))
        eq_(res.status_code, 204)
        user = UserProfile.objects.get(pk=self.user.pk)
        eq_(user.display_name, 'foo')
        eq_(user.username, self.user.username)  # Did not change.

    def test_patch_extra_fields(self):
        res = self.client.patch(self.get_url,
                                data=json.dumps({'display_name': 'foo',
                                                 'username': '******'}))
        eq_(res.status_code, 202)
        user = UserProfile.objects.get(pk=self.user.pk)
        eq_(user.display_name, 'foo')  # Got changed successfully.
        eq_(user.username, self.user.username)  # Did not change.

    def test_patch_other(self):
        res = self.client.patch(get_url('settings', '10482'),
                                data=json.dumps({'display_name': 'foo'}))
        eq_(res.status_code, 403)
コード例 #44
0
ファイル: test_api.py プロジェクト: flyun/zamboni
class TestApiReviewer(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141', 'user_2519')

    def setUp(self, api_name='apps'):
        self.user = User.objects.get(pk=2519)
        self.profile = self.user.get_profile()
        self.profile.update(read_dev_agreement=datetime.now())
        self.grant_permission(self.profile, 'Apps:Review')

        self.access = Access.objects.create(key='foo', secret=generate(),
                                            user=self.user)
        self.client = OAuthClient(self.access, api_name=api_name)
        self.list_url = ('api_dispatch_list', {'resource_name': 'search'})

        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh()

    def test_status_reviewer(self):
        res = self.client.get(self.list_url + ({'status': 'public'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.list_url + ({'status': 'rejected'},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.list_url + ({'status': 'any'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.list_url + ({'status': 'vindaloo'},))
        eq_(res.status_code, 400)
        error = json.loads(res.content)['error_message']
        eq_(error.keys(), ['status'])

    def test_status_value_packaged(self):
        # When packaged we also include the latest version status.
        self.webapp.update(is_packaged=True)
        res = self.client.get(self.list_url)
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['status'], amo.STATUS_PUBLIC)
        eq_(obj['latest_version_status'], amo.STATUS_PUBLIC)

    def test_addon_type_reviewer(self):
        res = self.client.get(self.list_url + ({'type': 'app'},))
        eq_(res.status_code, 200)
        obj = json.loads(res.content)['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.list_url + ({'type': 'persona'},))
        eq_(res.status_code, 200)
        objs = json.loads(res.content)['objects']
        eq_(len(objs), 0)

        res = self.client.get(self.list_url + ({'type': 'vindaloo'},))
        eq_(res.status_code, 400)
        error = json.loads(res.content)['error_message']
        eq_(error.keys(), ['type'])
コード例 #45
0
class TestApi(BaseOAuth, ESTestCase):
    fixtures = fixture('webapp_337141')

    def setUp(self):
        self.create_switch('soft_delete')
        self.client = OAuthClient(None)
        self.url = list_url('search')
        self.webapp = Webapp.objects.get(pk=337141)
        self.category = Category.objects.create(name='test',
                                                slug='test',
                                                type=amo.ADDON_WEBAPP)
        self.webapp.save()
        self.refresh('webapp')

    def test_verbs(self):
        self._allowed_verbs(self.url, ['get'])

    def test_has_cors(self):
        self.assertCORS(self.client.get(self.url), 'get')

    def test_meta(self):
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        eq_(set(res.json.keys()), set(['objects', 'meta']))
        eq_(res.json['meta']['total_count'], 1)

    def test_wrong_category(self):
        res = self.client.get(self.url + ({
            'cat': self.category.slug + 'xq'
        }, ))
        eq_(res.status_code, 400)
        eq_(res['Content-Type'], 'application/json')

    def test_wrong_weight(self):
        self.category.update(weight=-1)
        res = self.client.get(self.url + ({'cat': self.category.slug}, ))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 0)

    def test_wrong_sort(self):
        res = self.client.get(self.url + ({'sort': 'awesomeness'}, ))
        eq_(res.status_code, 400)

    def test_sort(self):
        queries = []

        def fake_get_query(*a, **kw):
            qs = _get_query(*a, **kw)
            m = Mock(wraps=qs)
            queries.append(m)
            return m

        with patch('mkt.search.api._get_query', fake_get_query):
            res = self.client.get(self.url, [('sort', 'downloads'),
                                             ('sort', 'rating')])
        eq_(res.status_code, 200)
        queries[0].order_by.assert_called_with('-weekly_downloads',
                                               '-bayesian_rating')

    def test_right_category(self):
        res = self.client.get(self.url + ({'cat': self.category.pk}, ))
        eq_(res.status_code, 200)
        eq_(res.json['objects'], [])

    def create(self):
        AddonCategory.objects.create(addon=self.webapp, category=self.category)
        self.webapp.save()
        self.refresh('webapp')

    def test_right_category_present(self):
        self.create()
        res = self.client.get(self.url + ({'cat': self.category.pk}, ))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 1)

    def test_user_info_with_shared_secret(self):
        user = UserProfile.objects.all()[0]

        def fakeauth(auth, req, **kw):
            req.amo_user = user
            return True

        with patch(
                'mkt.api.authentication.SharedSecretAuthentication'
                '.is_authenticated', fakeauth):
            with self.settings(SITE_URL=''):
                self.create()
            res = self.client.get(self.url + ({'cat': self.category.pk}, ))
            obj = res.json['objects'][0]
            assert 'user' in obj

    def test_dehydrate(self):
        with self.settings(SITE_URL=''):
            self.create()
            res = self.client.get(self.url + ({'cat': self.category.pk}, ))
            eq_(res.status_code, 200)
            obj = res.json['objects'][0]
            eq_(obj['absolute_url'], self.webapp.get_absolute_url())
            eq_(obj['app_type'], self.webapp.app_type)
            eq_(obj['content_ratings'], None)
            eq_(obj['current_version'], u'1.0')
            eq_(obj['description'], unicode(self.webapp.description))
            eq_(obj['icons']['128'], self.webapp.get_icon_url(128))
            eq_(obj['id'], str(self.webapp.id))
            eq_(obj['manifest_url'], self.webapp.get_manifest_url())
            eq_(obj['payment_account'], None)
            eq_(obj['privacy_policy'], '/api/v1/apps/app/337141/privacy/')
            eq_(obj['public_stats'], self.webapp.public_stats)
            eq_(obj['ratings'], {'average': 0.0, 'count': 0})
            eq_(obj['resource_uri'], '/api/v1/apps/app/337141/')
            eq_(obj['slug'], self.webapp.app_slug)
            eq_(obj['supported_locales'], ['en-US', 'es', 'pt-BR'])
            eq_(obj['versions'], {u'1.0': u'/api/v1/apps/versions/1268829/'})

            # These only exists if requested by a reviewer.
            ok_('latest_version' not in obj)
            ok_('reviewer_flags' not in obj)

    def test_upsell(self):
        upsell = app_factory()
        AddonUpsell.objects.create(free=self.webapp, premium=upsell)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['upsell']['id'], upsell.id)
        eq_(obj['upsell']['app_slug'], upsell.app_slug)
        eq_(obj['upsell']['name'], upsell.name)
        eq_(obj['upsell']['icon_url'], upsell.get_icon_url(128))
        eq_(obj['upsell']['resource_uri'], '/api/v1/apps/app/%s/' % upsell.id)

        unindex_webapps([upsell.id])
        upsell.delete()

    def test_dehydrate_regions(self):
        self.webapp.addonexcludedregion.create(region=mkt.regions.BR.id)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        regions = obj['regions']
        ok_(mkt.regions.BR.slug not in [r['slug'] for r in regions])
        eq_(len(regions), len(mkt.regions.ALL_REGION_IDS) - 1)

    def test_region_filtering(self):
        self.webapp.addonexcludedregion.create(region=mkt.regions.BR.id)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + ({'region': 'br'}, ))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

    def test_q(self):
        res = self.client.get(self.url + ({'q': 'something'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_name_localized(self):
        res = self.client.get(self.url + ({'q': 'something', 'lang': 'es'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)
        eq_(obj['name'], u'Algo Algo Steamcube!')

    def test_name_localized_to_default_locale(self):
        self.webapp.update(default_locale='es')
        self.refresh('webapp')

        # Make a request in another language that we know will fail.
        res = self.client.get(self.url + ({'q': 'something', 'lang': 'de'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)
        eq_(obj['name'], u'Algo Algo Steamcube!')

    def test_device(self):
        AddonDeviceType.objects.create(
            addon=self.webapp, device_type=DEVICE_CHOICES_IDS['desktop'])
        self.webapp.save()
        self.refresh('webapp')
        res = self.client.get(self.url + ({'device': 'desktop'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_no_flash_on_firefoxos(self):
        AddonDeviceType.objects.create(
            addon=self.webapp, device_type=DEVICE_CHOICES_IDS['firefoxos'])
        f = self.webapp.get_latest_file()
        f.uses_flash = True
        f.save()
        self.webapp.save()
        self.refresh('webapp')
        res = self.client.get(self.url + ({'dev': 'firefoxos'}, ))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 0)

    def test_premium_types(self):
        res = self.client.get(self.url + ({'premium_types': 'free'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_premium_types_empty(self):
        res = self.client.get(self.url + ({'premium_types': 'premium'}, ))
        eq_(res.status_code, 200)
        objs = res.json['objects']
        eq_(len(objs), 0)

    def test_multiple_premium_types(self):
        res = self.client.get(self.url + ({
            'premium_types': 'free'
        }, {
            'premium_types': 'premium'
        }))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_app_type_hosted(self):
        res = self.client.get(self.url + ({'app_type': 'hosted'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_app_type_packaged(self):
        self.webapp.update(is_packaged=True)
        self.webapp.save()
        self.refresh('webapp')

        res = self.client.get(self.url + ({'app_type': 'packaged'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

    def test_status_value_packaged(self):
        # When packaged and not a reviewer we exclude latest version status.
        self.webapp.update(is_packaged=True)
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['status'], amo.STATUS_PUBLIC)
        eq_('latest_version' in obj, False)

    def test_addon_type_anon(self):
        res = self.client.get(self.url + ({'type': 'app'}, ))
        eq_(res.status_code, 200)
        obj = res.json['objects'][0]
        eq_(obj['slug'], self.webapp.app_slug)

        res = self.client.get(self.url + ({'type': 'vindaloo'}, ))
        eq_(res.status_code, 400)
        error = res.json['error_message']
        eq_(error.keys(), ['type'])

        res = self.client.get(self.url + ({'type': 'theme'}, ))
        eq_(res.status_code, 200)
        eq_(len(res.json['objects']), 0)

    def test_adolescent_popularity(self):
        """
        Adolescent regions use global popularity.

          Webapp:   Global: 0, Regional: 0
          Unknown1: Global: 1, Regional: 1 + 10 * 1 = 11
          Unknown2: Global: 2, Regional: 0

        """
        user = UserProfile.objects.all()[0]
        cd = ClientData.objects.create(region=mkt.regions.BR.id)

        unknown1 = amo.tests.app_factory()
        Installed.objects.create(addon=unknown1, user=user, client_data=cd)

        unknown2 = amo.tests.app_factory()
        Installed.objects.create(addon=unknown2, user=user)
        Installed.objects.create(addon=unknown2, user=user)

        self.reindex(Webapp, 'webapp')

        res = self.client.get(self.url + ({'region': 'br'}, ))
        eq_(res.status_code, 200)

        objects = res.json['objects']
        eq_(len(objects), 3)

        eq_(int(objects[0]['id']), unknown2.id)
        eq_(int(objects[1]['id']), unknown1.id)
        eq_(int(objects[2]['id']), self.webapp.id)

        # Cleanup to remove these from the index.
        unindex_webapps([unknown1.id, unknown2.id])
        unknown1.delete()
        unknown2.delete()