Esempio n. 1
0
class TestAPIVersionMiddleware(amo.tests.TestCase):
    def setUp(self):
        self.api_version_middleware = APIVersionMiddleware()
        self.prefix_middleware = RedirectPrefixedURIMiddleware()

    def response(self, url):
        req = RequestFactory().get(url)
        self.prefix_middleware.process_request(req)
        resp = self.api_version_middleware.process_request(req)
        if resp:
            return resp
        return self.api_version_middleware.process_response(
            req, HttpResponse())

    def header(self, res, header):
        return res.get(header, None)

    def test_non_api(self):
        res1 = self.response('/foo/')
        eq_(self.header(res1, 'API-Version'), None)
        eq_(self.header(res1, 'API-Status'), None)

        res2 = self.response('/foo/')
        eq_(self.header(res2, 'API-Version'), None)
        eq_(self.header(res2, 'API-Status'), None)

    def test_version_not_specified(self):
        res = self.response('/api/')
        eq_(self.header(res, 'API-Version'), '1')
        eq_(self.header(res, 'API-Status'), 'Deprecated')

    def test_old_version(self):
        res = self.response('/api/v1/')
        eq_(self.header(res, 'API-Version'), '1')
        eq_(self.header(res, 'API-Status'), 'Deprecated')

    def test_current_version(self):
        res = self.response('/api/v2/')
        eq_(self.header(res, 'API-Version'), '2')
        eq_(self.header(res, 'API-Status'), None)

    def test_future_version(self):
        res = self.response('/api/v3/')
        eq_(self.header(res, 'API-Version'), '3')
        eq_(self.header(res, 'API-Status'), None)

    def test_no_api_version(self):
        req = RequestFactory().get('/api/v2/')
        req.API = True
        res = self.api_version_middleware.process_response(req, HttpResponse())
        eq_(self.header(res, 'API-Version'), '2')
        eq_(self.header(res, 'API-Status'), None)
Esempio n. 2
0
class TestAPIVersionMiddleware(amo.tests.TestCase):

    def setUp(self):
        self.api_version_middleware = APIVersionMiddleware()
        self.prefix_middleware = RedirectPrefixedURIMiddleware()

    def response(self, url):
        req = RequestFactory().get(url)
        self.prefix_middleware.process_request(req)
        resp = self.api_version_middleware.process_request(req)
        if resp:
            return resp
        return self.api_version_middleware.process_response(req, HttpResponse())

    def header(self, res, header):
        return res.get(header, None)

    def test_non_api(self):
        res1 = self.response('/foo/')
        eq_(self.header(res1, 'API-Version'), None)
        eq_(self.header(res1, 'API-Status'), None)

        res2 = self.response('/foo/')
        eq_(self.header(res2, 'API-Version'), None)
        eq_(self.header(res2, 'API-Status'), None)

    def test_version_not_specified(self):
        res = self.response('/api/')
        eq_(self.header(res, 'API-Version'), '1')
        eq_(self.header(res, 'API-Status'), 'Deprecated')

    def test_old_version(self):
        res = self.response('/api/v1/')
        eq_(self.header(res, 'API-Version'), '1')
        eq_(self.header(res, 'API-Status'), 'Deprecated')

    def test_current_version(self):
        res = self.response('/api/v2/')
        eq_(self.header(res, 'API-Version'), '2')
        eq_(self.header(res, 'API-Status'), None)

    def test_future_version(self):
        res = self.response('/api/v3/')
        eq_(self.header(res, 'API-Version'), '3')
        eq_(self.header(res, 'API-Status'), None)

    def test_no_api_version(self):
        req = RequestFactory().get('/api/v2/')
        req.API = True
        res = self.api_version_middleware.process_response(req, HttpResponse())
        eq_(self.header(res, 'API-Version'), '2')
        eq_(self.header(res, 'API-Status'), None)
Esempio n. 3
0
class TestTransactionMiddleware(amo.tests.TestCase):
    def setUp(self):
        self.prefix = RedirectPrefixedURIMiddleware()
        self.transaction = APITransactionMiddleware()

    def test_api(self):
        req = RequestFactory().get('/api/foo/')
        self.prefix.process_request(req)
        ok_(req.API)

    def test_not_api(self):
        req = RequestFactory().get('/not-api/foo/')
        self.prefix.process_request(req)
        ok_(not req.API)

    @mock.patch('django.db.transaction.enter_transaction_management')
    def test_transactions(self, enter):
        req = RequestFactory().get('/api/foo/')
        self.prefix.process_request(req)
        self.transaction.process_request(req)
        ok_(enter.called)

    @mock.patch('django.db.transaction.enter_transaction_management')
    def test_not_transactions(self, enter):
        req = RequestFactory().get('/not-api/foo/')
        self.prefix.process_request(req)
        self.transaction.process_request(req)
        ok_(not enter.called)
Esempio n. 4
0
class TestTransactionMiddleware(amo.tests.TestCase):

    def setUp(self):
        self.prefix = RedirectPrefixedURIMiddleware()
        self.transaction = APITransactionMiddleware()

    def test_api(self):
        req = RequestFactory().get('/api/foo/')
        self.prefix.process_request(req)
        ok_(req.API)

    def test_not_api(self):
        req = RequestFactory().get('/not-api/foo/')
        self.prefix.process_request(req)
        ok_(not req.API)

    @mock.patch('django.db.transaction.enter_transaction_management')
    def test_transactions(self, enter):
        req = RequestFactory().get('/api/foo/')
        self.prefix.process_request(req)
        self.transaction.process_request(req)
        ok_(enter.called)

    @mock.patch('django.db.transaction.enter_transaction_management')
    def test_not_transactions(self, enter):
        req = RequestFactory().get('/not-api/foo/')
        self.prefix.process_request(req)
        self.transaction.process_request(req)
        ok_(not enter.called)
Esempio n. 5
0
    def test_multiple_shared_works(self):
        request = RequestFactory().post(
            '/api',
            HTTP_AUTHORIZATION='mkt-shared-secret '
            '[email protected],56b6f1a3dd735d962c56'
            'ce7d8f46e02ec1d4748d2c00c407d75f0969d08bb'
            '9c68c31b3371aa8130317815c89e5072e31bb94b4'
            '121c5c165f3515838d4d6c60c4,165d631d3c3045'
            '458b4516242dad7ae')
        drf_request = Request(request)

        # Start with an AnonymousUser on the request, because that's a classic
        # situation: we already went through a middleware, it didn't find a
        # session cookie, if set request.user = AnonymousUser(), and now we
        # are going through the authentication code in the API.
        request.user = AnonymousUser()

        # Call middleware as they would normally be called.
        RedirectPrefixedURIMiddleware().process_request(request)
        RestSharedSecretMiddleware().process_request(request)
        RestOAuthMiddleware().process_request(request)

        drf_request.authenticators = (
            authentication.RestSharedSecretAuthentication(),
            authentication.RestOAuthAuthentication())

        eq_(drf_request.user, self.profile.user)
        eq_(drf_request._request.user, self.profile.user)
        eq_(drf_request.user.is_authenticated(), True)
        eq_(drf_request._request.user.is_authenticated(), True)
        eq_(drf_request.amo_user.pk, self.profile.pk)
        eq_(drf_request._request.amo_user.pk, self.profile.pk)
Esempio n. 6
0
 def setUp(self):
     self.prefix = RedirectPrefixedURIMiddleware()
     self.transaction = APITransactionMiddleware()
Esempio n. 7
0
 def setUp(self):
     self.prefix = RedirectPrefixedURIMiddleware()
     self.transaction = APITransactionMiddleware()
Esempio n. 8
0
 def setUp(self):
     self.api_version_middleware = APIVersionMiddleware()
     self.prefix_middleware = RedirectPrefixedURIMiddleware()
Esempio n. 9
0
 def setUp(self):
     self.api_version_middleware = APIVersionMiddleware()
     self.prefix_middleware = RedirectPrefixedURIMiddleware()