class TestAuthentication(test_utils.TestCase):

    def setUp(self):
        self.authentication = OAuthAuthentication('api')
        self.factory = RequestFactory()
        self.consumer = Consumer(*keys.items()[0])

    def test_not_required(self):
        req = self.factory.get('/')
        with self.settings(REQUIRE_OAUTH=False):
            ok_(self.authentication.is_authenticated(req))

    def test_required(self):
        req = self.factory.get('/')
        with self.settings(REQUIRE_OAUTH=True):
            ok_(not self.authentication.is_authenticated(req))

    def test_signed(self):
        res = sign_request('GET', keys_dict, settings.SITE_URL, None)
        req = self.factory.get('/', HTTP_AUTHORIZATION=res)
        with self.settings(REQUIRE_OAUTH=True):
            ok_(self.authentication.is_authenticated(req))
            eq_(req.OAUTH_KEY, 'foo')

    def test_signed_incorrectly(self):
        keys_ = keys_dict.copy()
        keys_['secret'] = 'baz'
        res = sign_request('GET', keys_, settings.SITE_URL, None)
        req = self.factory.get('/foo/', HTTP_AUTHORIZATION=res)
        with self.settings(REQUIRE_OAUTH=True):
            ok_(not self.authentication.is_authenticated(req))
            eq_(req.OAUTH_KEY, None)
class TestAuthentication(test_utils.TestCase):
    def setUp(self):
        self.authentication = OAuthAuthentication('api')
        self.factory = RequestFactory()
        self.consumer = Consumer(*keys.items()[0])

    def test_not_required(self):
        req = self.factory.get('/')
        with self.settings(REQUIRE_OAUTH=False, SKIP_OAUTH=[]):
            ok_(self.authentication.is_authenticated(req))

    def test_required(self):
        req = self.factory.get('/')
        with self.settings(REQUIRE_OAUTH=True, SKIP_OAUTH=[]):
            ok_(not self.authentication.is_authenticated(req))

    def test_skip(self):
        req = self.factory.get('/skip-oauth/')
        with self.settings(REQUIRE_OAUTH=True, SKIP_OAUTH=['/skip-oauth/']):
            ok_(self.authentication.is_authenticated(req))

    def test_not_skip(self):
        req = self.factory.get('/require-oauth/')
        with self.settings(REQUIRE_OAUTH=True, SKIP_OAUTH=['/skip-oauth/']):
            ok_(not self.authentication.is_authenticated(req))

    def setup_authorization(self, keys):
        headers = {}
        sign_request(None,
                     method='GET',
                     extra=keys,
                     headers=headers,
                     url=settings.SITE_URL)
        return headers['Authorization']

    def test_signed(self):
        authorization = self.setup_authorization(keys_dict)
        req = self.factory.get('/', HTTP_AUTHORIZATION=authorization)
        with self.settings(REQUIRE_OAUTH=True, SKIP_OAUTH=[]):
            ok_(self.authentication.is_authenticated(req))
            eq_(req.OAUTH_KEY, 'foo')

    def test_signed_incorrectly(self):
        keys_ = keys_dict.copy()
        keys_['secret'] = 'baz'
        authorization = self.setup_authorization(keys_)
        req = self.factory.get('/foo/', HTTP_AUTHORIZATION=authorization)
        with self.settings(REQUIRE_OAUTH=True, SKIP_OAUTH=[]):
            ok_(not self.authentication.is_authenticated(req))
            eq_(req.OAUTH_KEY, None)
class TestAuthentication(test.TestCase):

    def setUp(self):
        self.authentication = OAuthAuthentication('api')
        self.factory = RequestFactory()
        self.consumer = Consumer(*keys.items()[0])

    def test_not_required(self):
        req = self.factory.get('/')
        with self.settings(REQUIRE_OAUTH=False, SKIP_OAUTH=[]):
            ok_(self.authentication.is_authenticated(req))

    def test_required(self):
        req = self.factory.get('/')
        with self.settings(REQUIRE_OAUTH=True, SKIP_OAUTH=[]):
            ok_(not self.authentication.is_authenticated(req))

    def test_skip(self):
        req = self.factory.get('/skip-oauth/')
        with self.settings(REQUIRE_OAUTH=True, SKIP_OAUTH=['/skip-oauth/']):
            ok_(self.authentication.is_authenticated(req))

    def test_not_skip(self):
        req = self.factory.get('/require-oauth/')
        with self.settings(REQUIRE_OAUTH=True, SKIP_OAUTH=['/skip-oauth/']):
            ok_(not self.authentication.is_authenticated(req))

    def setup_authorization(self, keys):
        headers = {}
        sign_request(None, method='GET', extra=keys, headers=headers,
                     url=settings.SITE_URL)
        return headers['Authorization']

    def test_signed(self):
        authorization = self.setup_authorization(keys_dict)
        req = self.factory.get('/', HTTP_AUTHORIZATION=authorization)
        with self.settings(REQUIRE_OAUTH=True, SKIP_OAUTH=[]):
            ok_(self.authentication.is_authenticated(req))
            eq_(req.OAUTH_KEY, 'foo')

    def test_signed_incorrectly(self):
        keys_ = keys_dict.copy()
        keys_['secret'] = 'baz'
        authorization = self.setup_authorization(keys_)
        req = self.factory.get('/foo/', HTTP_AUTHORIZATION=authorization)
        with self.settings(REQUIRE_OAUTH=True, SKIP_OAUTH=[]):
            ok_(not self.authentication.is_authenticated(req))
            eq_(req.OAUTH_KEY, None)
class TestAuthentication(test_utils.TestCase):
    def setUp(self):
        self.authentication = OAuthAuthentication("api")
        self.factory = RequestFactory()
        self.consumer = Consumer(*keys.items()[0])

    def test_not_required(self):
        req = self.factory.get("/")
        with self.settings(REQUIRE_OAUTH=False, SKIP_OAUTH=[]):
            ok_(self.authentication.is_authenticated(req))

    def test_required(self):
        req = self.factory.get("/")
        with self.settings(REQUIRE_OAUTH=True, SKIP_OAUTH=[]):
            ok_(not self.authentication.is_authenticated(req))

    def test_skip(self):
        req = self.factory.get("/skip-oauth/")
        with self.settings(REQUIRE_OAUTH=True, SKIP_OAUTH=["/skip-oauth/"]):
            ok_(self.authentication.is_authenticated(req))

    def test_not_skip(self):
        req = self.factory.get("/require-oauth/")
        with self.settings(REQUIRE_OAUTH=True, SKIP_OAUTH=["/skip-oauth/"]):
            ok_(not self.authentication.is_authenticated(req))

    def setup_authorization(self, keys):
        headers = {}
        sign_request(None, method="GET", extra=keys, headers=headers, url=settings.SITE_URL)
        return headers["Authorization"]

    def test_signed(self):
        authorization = self.setup_authorization(keys_dict)
        req = self.factory.get("/", HTTP_AUTHORIZATION=authorization)
        with self.settings(REQUIRE_OAUTH=True, SKIP_OAUTH=[]):
            ok_(self.authentication.is_authenticated(req))
            eq_(req.OAUTH_KEY, "foo")

    def test_signed_incorrectly(self):
        keys_ = keys_dict.copy()
        keys_["secret"] = "baz"
        authorization = self.setup_authorization(keys_)
        req = self.factory.get("/foo/", HTTP_AUTHORIZATION=authorization)
        with self.settings(REQUIRE_OAUTH=True, SKIP_OAUTH=[]):
            ok_(not self.authentication.is_authenticated(req))
            eq_(req.OAUTH_KEY, None)
Example #5
0
 class Meta:
     always_return_data = True
     authentication = OAuthAuthentication()
     authorization = Authorization()
 def setUp(self):
     self.authentication = OAuthAuthentication('api')
     self.factory = RequestFactory()
     self.consumer = Consumer(*keys.items()[0])