Beispiel #1
0
 def setUp(self):
     self.middleware = APIFilterMiddleware()
     self.factory = RequestFactory()
Beispiel #2
0
class TestFilterMiddleware(amo.tests.TestCase):

    def setUp(self):
        self.middleware = APIFilterMiddleware()
        self.factory = RequestFactory()

    def _header(self, url='/', api=True, region=mkt.regions.US, lang='en-US',
                gaia=True, tablet=True, mobile=True, pro='8a7d546c.32.1',
                response_cls=HttpResponse):
        self.request = self.factory.get(url, {'pro': pro})
        self.request.API = api
        self.request.REGION = region
        self.request.LANG = lang or ''
        self.request.GAIA = gaia
        self.request.TABLET = tablet
        self.request.MOBILE = mobile
        res = self.middleware.process_response(self.request, response_cls())
        if api and response_cls.status_code < 500:
            header = res.get('API-Filter')
            assert 'vary' in res._headers
            eq_(res._headers['vary'][1], 'API-Filter')
            self._test_order(header)
            return parse_qs(header)
        else:
            assert 'vary' not in res._headers
            return None

    def _test_order(self, header):
        order = [item.split('=')[0] for item in header.split('&')]
        eq_(order, sorted(order))

    @mock.patch('mkt.api.middleware.get_carrier')
    def test_success(self, gc):
        carrier = 'telerizon'
        gc.return_value = carrier
        header = self._header()
        self.assertIsInstance(header, dict)
        assert mkt.regions.US.slug in header['region']
        assert 'en-US' in header['lang']
        assert '8a7d546c.32.1' in header['pro']
        assert carrier in header['carrier']
        self.assertSetEqual(['gaia', 'mobile', 'tablet'], header['device'])

    def test_api_false(self):
        header = self._header(api=False)
        eq_(header, None)

    def test_no_devices(self):
        header = self._header(gaia=False, tablet=False, mobile=False)
        assert 'device' not in header

    def test_one_device(self):
        header = self._header(gaia=True, tablet=False, mobile=False)
        self.assertSetEqual(['gaia'], header['device'])

    @mock.patch('mkt.api.middleware.get_carrier')
    def test_no_carrier(self, gc):
        gc.return_value = None
        header = self._header()
        assert 'carrier' not in header

    def test_region(self):
        region = mkt.regions.BR
        header = self._header(region=region)
        assert region.slug in header['region']

    def test_no_region(self):
        with self.assertRaises(AttributeError):
            self._header(region=None)

    def test_lang(self):
        lang = 'pt-BR'
        header = self._header(lang=lang)
        assert lang in header['lang']

    def test_no_lang(self):
        header = self._header(lang=None)
        assert 'lang' not in header

    def test_500(self):
        self._header(response_cls=HttpResponseServerError)
Beispiel #3
0
class TestFilterMiddleware(amo.tests.TestCase):
    def setUp(self):
        self.middleware = APIFilterMiddleware()
        self.factory = RequestFactory()

    def _header(self,
                url='/',
                api=True,
                region=mkt.regions.US,
                lang='en-US',
                gaia=True,
                tablet=True,
                mobile=True,
                pro='8a7d546c.32.1',
                response_cls=HttpResponse):
        self.request = self.factory.get(url, {'pro': pro})
        self.request.API = api
        self.request.REGION = region
        self.request.LANG = lang or ''
        self.request.GAIA = gaia
        self.request.TABLET = tablet
        self.request.MOBILE = mobile
        res = self.middleware.process_response(self.request, response_cls())
        if api and response_cls.status_code < 500:
            header = res.get('API-Filter')
            assert 'vary' in res._headers
            eq_(res._headers['vary'][1], 'API-Filter')
            self._test_order(header)
            return parse_qs(header)
        else:
            assert 'vary' not in res._headers
            return None

    def _test_order(self, header):
        order = [item.split('=')[0] for item in header.split('&')]
        eq_(order, sorted(order))

    @mock.patch('mkt.api.middleware.get_carrier')
    def test_success(self, gc):
        carrier = 'telerizon'
        gc.return_value = carrier
        header = self._header()
        self.assertIsInstance(header, dict)
        assert mkt.regions.US.slug in header['region']
        assert 'en-US' in header['lang']
        assert '8a7d546c.32.1' in header['pro']
        assert carrier in header['carrier']
        self.assertSetEqual(['gaia', 'mobile', 'tablet'], header['device'])

    def test_api_false(self):
        header = self._header(api=False)
        eq_(header, None)

    def test_no_devices(self):
        header = self._header(gaia=False, tablet=False, mobile=False)
        assert 'device' not in header

    def test_one_device(self):
        header = self._header(gaia=True, tablet=False, mobile=False)
        self.assertSetEqual(['gaia'], header['device'])

    @mock.patch('mkt.api.middleware.get_carrier')
    def test_no_carrier(self, gc):
        gc.return_value = None
        header = self._header()
        assert 'carrier' not in header

    def test_region(self):
        region = mkt.regions.BR
        header = self._header(region=region)
        assert region.slug in header['region']

    def test_no_region(self):
        with self.assertRaises(AttributeError):
            self._header(region=None)

    def test_lang(self):
        lang = 'pt-BR'
        header = self._header(lang=lang)
        assert lang in header['lang']

    def test_no_lang(self):
        header = self._header(lang=None)
        assert 'lang' not in header

    def test_500(self):
        self._header(response_cls=HttpResponseServerError)
Beispiel #4
0
class TestFilterMiddleware(amo.tests.TestCase):
    def setUp(self):
        self.middleware = APIFilterMiddleware()
        self.factory = RequestFactory()

    def _header(
        self,
        url="/",
        api=True,
        region=mkt.regions.US,
        lang="en-US",
        gaia=True,
        tablet=True,
        mobile=True,
        pro="8a7d546c.32.1",
        response_cls=HttpResponse,
    ):
        self.request = self.factory.get(url, {"pro": pro})
        self.request.API = api
        self.request.REGION = region
        self.request.LANG = lang or ""
        self.request.GAIA = gaia
        self.request.TABLET = tablet
        self.request.MOBILE = mobile
        res = self.middleware.process_response(self.request, response_cls())
        if api and response_cls.status_code < 500:
            header = res.get("API-Filter")
            assert "vary" in res._headers
            eq_(res._headers["vary"][1], "API-Filter")
            self._test_order(header)
            return parse_qs(header)
        else:
            assert "vary" not in res._headers
            return None

    def _test_order(self, header):
        order = [item.split("=")[0] for item in header.split("&")]
        eq_(order, sorted(order))

    @mock.patch("mkt.api.middleware.get_carrier")
    def test_success(self, gc):
        carrier = "telerizon"
        gc.return_value = carrier
        header = self._header()
        self.assertIsInstance(header, dict)
        assert mkt.regions.US.slug in header["region"]
        assert "en-US" in header["lang"]
        assert "8a7d546c.32.1" in header["pro"]
        assert carrier in header["carrier"]
        self.assertSetEqual(["gaia", "mobile", "tablet"], header["device"])

    def test_api_false(self):
        header = self._header(api=False)
        eq_(header, None)

    def test_no_devices(self):
        header = self._header(gaia=False, tablet=False, mobile=False)
        assert "device" not in header

    def test_one_device(self):
        header = self._header(gaia=True, tablet=False, mobile=False)
        self.assertSetEqual(["gaia"], header["device"])

    @mock.patch("mkt.api.middleware.get_carrier")
    def test_no_carrier(self, gc):
        gc.return_value = None
        header = self._header()
        assert "carrier" not in header

    def test_region(self):
        region = mkt.regions.BR
        header = self._header(region=region)
        assert region.slug in header["region"]

    def test_no_region(self):
        with self.assertRaises(AttributeError):
            self._header(region=None)

    def test_lang(self):
        lang = "pt-BR"
        header = self._header(lang=lang)
        assert lang in header["lang"]

    def test_no_lang(self):
        header = self._header(lang=None)
        assert "lang" not in header

    def test_500(self):
        self._header(response_cls=HttpResponseServerError)
Beispiel #5
0
 def setUp(self):
     self.middleware = APIFilterMiddleware()
     self.factory = RequestFactory()