Beispiel #1
0
class TestPinningMiddleware(amo.tests.TestCase):
    def setUp(self):
        self.pin = APIPinningMiddleware()
        self.req = RequestFactory().get("/")
        self.req.API = True
        self.key = "api-pinning:42"

    def attach_user(self, anon=True):
        self.req.amo_user = mock.Mock(id=42, is_anonymous=lambda: anon)

    def test_pinned_request_method(self):
        self.attach_user(anon=False)

        for method in ["DELETE", "PATCH", "POST", "PUT"]:
            self.req.method = method
            self.pin.process_request(self.req)
            ok_(this_thread_is_pinned())

        for method in ["GET", "HEAD", "OPTIONS", "POOP"]:
            self.req.method = method
            self.pin.process_request(self.req)
            ok_(not this_thread_is_pinned())

    def test_pinned_cached(self):
        cache.set(self.key, 1, 5)
        self.attach_user(anon=False)
        self.pin.process_request(self.req)
        ok_(this_thread_is_pinned())
        cache.delete(self.key)

    def test_not_pinned(self):
        self.attach_user(anon=True)
        self.pin.process_request(self.req)
        ok_(not this_thread_is_pinned())

    def test_process_response_anon(self):
        self.attach_user(anon=True)
        self.req.method = "POST"
        self.pin.process_response(self.req, HttpResponse())
        ok_(not cache.get(self.key))

    def test_process_response(self):
        self.attach_user(anon=False)
        for method in ["DELETE", "PATCH", "POST", "PUT"]:
            self.req.method = method
            self.pin.process_response(self.req, HttpResponse())
            ok_(cache.get(self.key))
            cache.delete(self.key)

        for method in ["GET", "HEAD", "OPTIONS", "POOP"]:
            self.req.method = method
            self.pin.process_response(self.req, HttpResponse())
            ok_(not cache.get(self.key))
Beispiel #2
0
class TestPinningMiddleware(amo.tests.TestCase):
    def setUp(self):
        self.pin = APIPinningMiddleware()
        self.req = RequestFactory().get('/')
        self.req.API = True
        self.key = 'api-pinning:42'

    def attach_user(self, anon=True):
        self.req.amo_user = mock.Mock(id=42, is_anonymous=lambda: anon)

    def test_pinned_request_method(self):
        self.attach_user(anon=False)

        for method in ['DELETE', 'PATCH', 'POST', 'PUT']:
            self.req.method = method
            self.pin.process_request(self.req)
            ok_(this_thread_is_pinned())

        for method in ['GET', 'HEAD', 'OPTIONS', 'POOP']:
            self.req.method = method
            self.pin.process_request(self.req)
            ok_(not this_thread_is_pinned())

    def test_pinned_cached(self):
        cache.set(self.key, 1, 5)
        self.attach_user(anon=False)
        self.pin.process_request(self.req)
        ok_(this_thread_is_pinned())
        cache.delete(self.key)

    def test_not_pinned(self):
        self.attach_user(anon=True)
        self.pin.process_request(self.req)
        ok_(not this_thread_is_pinned())

    def test_process_response_anon(self):
        self.attach_user(anon=True)
        self.req.method = 'POST'
        self.pin.process_response(self.req, HttpResponse())
        ok_(not cache.get(self.key))

    def test_process_response(self):
        self.attach_user(anon=False)
        for method in ['DELETE', 'PATCH', 'POST', 'PUT']:
            self.req.method = method
            self.pin.process_response(self.req, HttpResponse())
            ok_(cache.get(self.key))
            cache.delete(self.key)

        for method in ['GET', 'HEAD', 'OPTIONS', 'POOP']:
            self.req.method = method
            self.pin.process_response(self.req, HttpResponse())
            ok_(not cache.get(self.key))

    def pinned_header(self):
        self.attach_user(anon=True)
        return self.pin.process_response(self.req,
                                         HttpResponse())['API-Pinned']

    @mock.patch('mkt.api.middleware.this_thread_is_pinned')
    def test_pinned_header_true(self, mock_pinned):
        mock_pinned.return_value = True
        eq_(self.pinned_header(), 'True')

    @mock.patch('mkt.api.middleware.this_thread_is_pinned')
    def test_pinned_header_false(self, mock_pinned):
        mock_pinned.return_value = False
        eq_(self.pinned_header(), 'False')
Beispiel #3
0
class TestPinningMiddleware(amo.tests.TestCase):

    def setUp(self):
        self.pin = APIPinningMiddleware()
        self.req = RequestFactory().get('/')
        self.req.API = True
        self.key = 'api-pinning:42'

    def attach_user(self, anon=True):
        self.req.user = mock.Mock(id=42, is_anonymous=lambda: anon)

    def test_pinned_request_method(self):
        self.attach_user(anon=False)

        for method in ['DELETE', 'PATCH', 'POST', 'PUT']:
            self.req.method = method
            self.pin.process_request(self.req)
            ok_(this_thread_is_pinned())

        for method in ['GET', 'HEAD', 'OPTIONS', 'POOP']:
            self.req.method = method
            self.pin.process_request(self.req)
            ok_(not this_thread_is_pinned())

    def test_pinned_cached(self):
        cache.set(self.key, 1, 5)
        self.attach_user(anon=False)
        self.pin.process_request(self.req)
        ok_(this_thread_is_pinned())
        cache.delete(self.key)

    def test_not_pinned(self):
        self.attach_user(anon=True)
        self.pin.process_request(self.req)
        ok_(not this_thread_is_pinned())

    def test_process_response_anon(self):
        self.attach_user(anon=True)
        self.req.method = 'POST'
        self.pin.process_response(self.req, HttpResponse())
        ok_(not cache.get(self.key))

    def test_process_response(self):
        self.attach_user(anon=False)
        for method in ['DELETE', 'PATCH', 'POST', 'PUT']:
            self.req.method = method
            self.pin.process_response(self.req, HttpResponse())
            ok_(cache.get(self.key))
            cache.delete(self.key)

        for method in ['GET', 'HEAD', 'OPTIONS', 'POOP']:
            self.req.method = method
            self.pin.process_response(self.req, HttpResponse())
            ok_(not cache.get(self.key))

    def pinned_header(self):
        self.attach_user(anon=True)
        return self.pin.process_response(self.req, HttpResponse())['API-Pinned']

    @mock.patch('mkt.api.middleware.this_thread_is_pinned')
    def test_pinned_header_true(self, mock_pinned):
        mock_pinned.return_value = True
        eq_(self.pinned_header(), 'True')

    @mock.patch('mkt.api.middleware.this_thread_is_pinned')
    def test_pinned_header_false(self, mock_pinned):
        mock_pinned.return_value = False
        eq_(self.pinned_header(), 'False')
Beispiel #4
0
class TestPinningMiddleware(amo.tests.TestCase):
    def setUp(self):
        self.pin = APIPinningMiddleware()
        self.req = RequestFactory().get("/")
        self.req.API = True
        self.key = "api-pinning:42"

    def attach_user(self, anon=True):
        self.req.user = mock.Mock(id=42, is_anonymous=lambda: anon)

    def test_pinned_request_method(self):
        self.attach_user(anon=False)

        for method in ["DELETE", "PATCH", "POST", "PUT"]:
            self.req.method = method
            self.pin.process_request(self.req)
            ok_(this_thread_is_pinned())

        for method in ["GET", "HEAD", "OPTIONS", "POOP"]:
            self.req.method = method
            self.pin.process_request(self.req)
            ok_(not this_thread_is_pinned())

    def test_pinned_cached(self):
        cache.set(self.key, 1, 5)
        self.attach_user(anon=False)
        self.pin.process_request(self.req)
        ok_(this_thread_is_pinned())
        cache.delete(self.key)

    def test_not_pinned(self):
        self.attach_user(anon=True)
        self.pin.process_request(self.req)
        ok_(not this_thread_is_pinned())

    def test_process_response_anon(self):
        self.attach_user(anon=True)
        self.req.method = "POST"
        self.pin.process_response(self.req, HttpResponse())
        ok_(not cache.get(self.key))

    def test_process_response(self):
        self.attach_user(anon=False)
        for method in ["DELETE", "PATCH", "POST", "PUT"]:
            self.req.method = method
            self.pin.process_response(self.req, HttpResponse())
            ok_(cache.get(self.key))
            cache.delete(self.key)

        for method in ["GET", "HEAD", "OPTIONS", "POOP"]:
            self.req.method = method
            self.pin.process_response(self.req, HttpResponse())
            ok_(not cache.get(self.key))

    def pinned_header(self):
        self.attach_user(anon=True)
        return self.pin.process_response(self.req, HttpResponse())["API-Pinned"]

    @mock.patch("mkt.api.middleware.this_thread_is_pinned")
    def test_pinned_header_true(self, mock_pinned):
        mock_pinned.return_value = True
        eq_(self.pinned_header(), "True")

    @mock.patch("mkt.api.middleware.this_thread_is_pinned")
    def test_pinned_header_false(self, mock_pinned):
        mock_pinned.return_value = False
        eq_(self.pinned_header(), "False")