Example #1
0
    def test_read_validate_params_invalid_client_secret(self):
        client_id = "abcd"
        client_secret_actual = "foo"
        client_secret_expected = "xyz"

        client_mock = Mock(Client)
        client_mock.secret = client_secret_expected

        client_store_mock = Mock(ClientStore)
        client_store_mock.fetch_by_client_id.return_value = client_mock

        request_mock = Mock(Request)
        request_mock.post_param.side_effect = [client_id, client_secret_actual]

        handler = ResourceOwnerGrantHandler(
            Mock(AccessTokenStore), client_store_mock, Mock(Scope), Mock(SiteAdapter), Mock()
        )

        with self.assertRaises(OAuthInvalidError) as expected:
            handler.read_validate_params(request_mock)

        error = expected.exception

        request_mock.post_param.assert_has_calls([call("client_id"), call("client_secret")])
        client_store_mock.fetch_by_client_id.assert_called_with(client_id)
        self.assertEqual(error.error, "invalid_request")
        self.assertEqual(error.explanation, "Could not authenticate client")
Example #2
0
    def test_read_validate_params(self):
        client_id = "abcd"
        client_secret = "xyz"
        password = "johnpw"
        username = "johndoe"

        client_mock = Mock(Client)
        client_mock.secret = client_secret

        client_store_mock = Mock(ClientStore)
        client_store_mock.fetch_by_client_id.return_value = client_mock

        request_mock = Mock(Request)
        request_mock.post_param.side_effect = [client_id, client_secret, password, username]

        scope_handler_mock = Mock(Scope)

        handler = ResourceOwnerGrantHandler(
            Mock(AccessTokenStore), client_store_mock, scope_handler_mock, Mock(SiteAdapter), Mock()
        )
        result = handler.read_validate_params(request_mock)

        client_store_mock.fetch_by_client_id.assert_called_with(client_id)
        scope_handler_mock.parse.assert_called_with(request=request_mock, source="POST")

        self.assertEqual(handler.client_id, client_id)
        self.assertEqual(handler.username, username)
        self.assertEqual(handler.password, password)
        self.assertTrue(result)
Example #3
0
    def test_unbind(self, mock_gofer_agent):
        context = Mock()
        context.uuid = "123"
        context.url = "http://broker.com"
        context.secret = "123-secret"
        context.details = {"task_id": "4567"}
        context.get_timeout = Mock(return_value=90)
        context.reply_queue = "pulp.task"
        context.watchdog = "pulp-watchdog"

        mock_agent = Mock()
        mock_gofer_agent.return_value = mock_agent
        mock_consumer = Mock()
        mock_agent.Consumer = Mock(return_value=mock_consumer)

        bindings = []
        options = {}

        Consumer.unbind(context, bindings, options)

        mock_gofer_agent.assert_called_with(
            context.uuid,
            ctag=context.reply_queue,
            url=context.url,
            secret=context.secret,
            timeout=context.get_timeout(),
            watchdog=context.watchdog,
            any=context.details,
        )

        mock_consumer.unbind.assert_called_with(bindings, options)
Example #4
0
    def test_unbind(self, mock_gofer_agent):
        context = Mock()
        context.agent_id = "123"
        context.secret = "test-secret"
        context.url = "http://broker.com"
        context.transport = "qpid"
        context.authenticator = Mock()
        context.details = {"task_id": "4567"}
        context.reply_queue = "pulp.task"

        mock_agent = Mock()
        mock_gofer_agent.return_value = mock_agent
        mock_consumer = Mock()
        mock_agent.Consumer = Mock(return_value=mock_consumer)

        bindings = []
        options = {}

        Consumer.unbind(context, bindings, options)

        mock_gofer_agent.assert_called_with(
            context.agent_id,
            ctag=context.reply_queue,
            url=context.url,
            transport=context.transport,
            secret=context.secret,
            authenticator=context.authenticator,
            any=context.details,
        )

        mock_consumer.unbind.assert_called_with(bindings, options)
Example #5
0
 def test_request_token_fake(self):
     c = Mock()
     c.key = self.accepted_consumer.key
     c.secret = "mom"
     self.client = OAuthClient(c)
     res = self.client.get(self.url)
     eq_(res.status_code, 401)
Example #6
0
    def test_unregistered(self, mock_gofer_agent):
        context = Mock()
        context.agent_id = "123"
        context.secret = "test-secret"
        context.url = "http://broker.com"
        context.transport = "qpid"
        context.authenticator = Mock()

        mock_agent = Mock()
        mock_gofer_agent.return_value = mock_agent
        mock_consumer = Mock()
        mock_agent.Consumer = Mock(return_value=mock_consumer)

        # test capability

        Consumer.unregistered(context)

        # validation

        mock_gofer_agent.assert_called_with(
            context.agent_id,
            url=context.url,
            secret=context.secret,
            authenticator=context.authenticator,
            transport=context.transport,
            async=True,
        )

        mock_consumer.unregistered.assert_called_with()
Example #7
0
 def test_request_token_fake(self):
     c = Mock()
     c.key = self.access.key
     c.secret = "mom"
     res = self.auth.is_authenticated(self.call(client=OAuthClient(c)))
     eq_(res.status_code, 401)
     eq_(json.loads(res.content)["reason"], errors["headers"])
Example #8
0
 def fetch_access_token(self, *args, **kwargs):
     if self.error:
         raise self.error("")
     response = Mock(["key", "secret"])
     response.key = self.key
     response.secret = self.secret
     return response
Example #9
0
    def test_read_validate_params_token_expired(self):
        client_id = "abc"
        client_secret = "t%gH"
        code = "xyz"
        redirect_uri = "http://callback"

        auth_code_mock = Mock(AuthorizationCode)
        auth_code_mock.code = code
        auth_code_mock.redirect_uri = redirect_uri
        auth_code_mock.is_expired.return_value = True

        auth_code_store_mock = Mock(spec=AuthCodeStore)
        auth_code_store_mock.fetch_by_code.return_value = auth_code_mock

        client_mock = Mock(Client)
        client_mock.secret = client_secret
        client_mock.redirect_uris = [redirect_uri]

        client_store_mock = Mock(spec=ClientStore)
        client_store_mock.fetch_by_client_id.return_value = client_mock

        request_mock = Mock(spec=Request)
        request_mock.post_param.side_effect = [client_id, client_secret, code, redirect_uri]

        handler = AuthorizationCodeTokenHandler(
            Mock(spec=AccessTokenStore), auth_code_store_mock, client_store_mock, Mock()
        )

        with self.assertRaises(OAuthInvalidError) as expected:
            handler.read_validate_params(request_mock)

        error = expected.exception

        self.assertEqual(error.error, "invalid_grant")
        self.assertEqual(error.explanation, "Authorization code has expired")
Example #10
0
 def test_request_token_fake(self):
     """Try with a phony consumer key"""
     c = Mock()
     c.key = "yer"
     c.secret = "mom"
     r = client.get("oauth.request_token", c)
     eq_(r.content, "Invalid consumer.")
Example #11
0
    def test_read_validate_params_wrong_redirect_uri_in_client_data(self):
        client_id = "abc"
        client_secret = "t%gH"
        code = "xyz"
        redirect_uri = "http://invalid-callback"

        client_mock = Mock(Client)
        client_mock.secret = client_secret
        client_mock.has_redirect_uri.return_value = False

        client_store_mock = Mock(spec=ClientStore)
        client_store_mock.fetch_by_client_id.return_value = client_mock

        request_mock = Mock(spec=Request)
        request_mock.post_param.side_effect = [client_id, client_secret, code, redirect_uri]

        handler = AuthorizationCodeTokenHandler(
            Mock(spec=AccessTokenStore), Mock(spec=AuthCodeStore), client_store_mock, Mock()
        )

        with self.assertRaises(OAuthInvalidError) as expected:
            handler.read_validate_params(request_mock)

        error = expected.exception

        self.assertEqual(error.error, "invalid_request")
        self.assertEqual(error.explanation, "Invalid redirect_uri parameter")
Example #12
0
 def test_request_token_fake(self):
     c = Mock()
     c.key = self.access.key
     c.secret = "mom"
     self.client = OAuthClient(c)
     res = self.client.get(self.url)
     eq_(res.status_code, 401)
     eq_(json.loads(res.content)["reason"], errors["headers"])
Example #13
0
    def setUp(self):
        # mock request object
        request = Request()
        request.method = "GET"
        request.url = "http://example.com/"
        request.headers = {}
        request.params = {}
        request.data = {}
        request.params_and_data = {}
        self.request = request

        # mock consumer object
        consumer = Mock()
        consumer.key = "123"
        consumer.secret = "456"
        self.consumer = consumer

        # mock token object
        token = Mock()
        token.key = "321"
        token.secret = "456"
        self.token = token
Example #14
0
    def setUp(self):
        # mock request object
        request = Request()
        request.method = "GET"
        request.url = "http://example.com/"
        request.headers = {}
        request.params = {}
        request.data = {}
        request.params_and_data = {}
        self.request = request

        # mock response object
        response = Mock()
        response.content = "access_token=321"
        response.headers = {"content-type": "text/html; charset=UTF-8"}
        response.ok = True
        response.status_code = 200
        response.raise_for_status = lambda: None
        self.response = response

        # mock raise_for_status with an error
        def raise_for_status():
            raise Exception("Response not OK!")

        self.raise_for_status = raise_for_status

        # mock consumer object
        consumer = Mock()
        consumer.key = "123"
        consumer.secret = "456"
        self.consumer = consumer

        # mock token object
        token = Mock()
        token.key = "321"
        token.secret = "456"
        self.token = token
Example #15
0
    def test_send(self, mock_gofer_agent):
        context = Mock()
        context.uuid = "123"
        context.url = "http://broker.com"
        context.secret = "123-secret"

        mock_agent = Mock()
        mock_gofer_agent.return_value = mock_agent
        mock_profile = Mock()
        mock_agent.Profile = Mock(return_value=mock_profile)

        Profile.send(context)

        mock_gofer_agent.assert_called_with(context.uuid, url=context.url, secret=context.secret)
        mock_profile.send.assert_called_with()
Example #16
0
    def test_send(self, mock_gofer_agent):
        context = Mock()
        context.uuid = "123"
        context.url = "http://broker.com"
        context.secret = "123-secret"

        task_id = "5678"
        criteria = {"match": {"task_id": task_id}}

        mock_agent = Mock()
        mock_gofer_agent.return_value = mock_agent
        mock_admin = Mock()
        mock_agent.Admin = Mock(return_value=mock_admin)

        agent = PulpAgent()
        agent.cancel(context, task_id)

        mock_gofer_agent.assert_called_with(context.uuid, url=context.url, secret=context.secret, async=True)
        mock_admin.cancel.assert_called_with(criteria=criteria)
Example #17
0
    def test_unregistered(self, mock_gofer_agent):
        context = Mock()
        context.uuid = "123"
        context.url = "http://broker.com"
        context.secret = "123-secret"

        mock_agent = Mock()
        mock_gofer_agent.return_value = mock_agent
        mock_consumer = Mock()
        mock_agent.Consumer = Mock(return_value=mock_consumer)

        # test capability

        Consumer.unregistered(context)

        # validation

        mock_gofer_agent.assert_called_with(context.uuid, url=context.url, secret=context.secret, async=True)

        mock_consumer.unregistered.assert_called_with()
Example #18
0
    def test_read_validate_params(self):
        client_id = "abc"
        client_secret = "t%gH"
        code = "defg"
        redirect_uri = "http://callback"
        scopes = ["scope"]

        auth_code = Mock(AuthorizationCode)
        auth_code.code = code
        auth_code.is_expired.return_value = False
        auth_code.redirect_uri = redirect_uri
        auth_code.scopes = scopes

        auth_code_store_mock = Mock(spec=AuthCodeStore)
        auth_code_store_mock.fetch_by_code.return_value = auth_code

        client_mock = Mock(Client)
        client_mock.secret = client_secret
        client_mock.redirect_uris = [redirect_uri]

        client_store_mock = Mock(spec=ClientStore)
        client_store_mock.fetch_by_client_id.return_value = client_mock

        request_mock = Mock(spec=Request)
        request_mock.post_param.side_effect = [client_id, client_secret, code, redirect_uri]

        handler = AuthorizationCodeTokenHandler(
            Mock(spec=AccessTokenStore), auth_code_store_mock, client_store_mock, Mock()
        )
        result = handler.read_validate_params(request_mock)

        request_mock.post_param.assert_has_calls(
            [call("client_id"), call("client_secret"), call("code"), call("redirect_uri")]
        )
        auth_code_store_mock.fetch_by_code.assert_called_with(code)
        self.assertEqual(handler.client_id, client_id)
        self.assertEqual(handler.client_secret, client_secret)
        self.assertEqual(handler.code, code)
        self.assertEqual(handler.redirect_uri, redirect_uri)
        self.assertEqual(handler.scopes, scopes)
        self.assertTrue(result)
Example #19
0
    def test_send(self, mock_gofer_agent):
        context = Mock()
        context.agent_id = "123"
        context.secret = "test-secret"
        context.url = "http://broker.com"
        context.transport = "qpid"
        context.authenticator = Mock()

        mock_agent = Mock()
        mock_gofer_agent.return_value = mock_agent
        mock_profile = Mock()
        mock_agent.Profile = Mock(return_value=mock_profile)

        Profile.send(context)

        mock_gofer_agent.assert_called_with(
            context.agent_id,
            url=context.url,
            transport=context.transport,
            secret=context.secret,
            authenticator=context.authenticator,
        )

        mock_profile.send.assert_called_with()
 def setUp(self):
     consumer = Mock(spec=oauth.OAuthConsumer)
     consumer.secret = "abc"
     token = Mock(spec=oauth.OAuthToken)
     token.secret = "abc"
     self.stream = Stream(consumer, token)
Example #21
0
 def test_request_token_fake(self):
     c = Mock()
     c.key = self.access.key
     c.secret = "mom"
     ok_(not self.auth.authenticate(Request(self.call(client=OAuthClient(c)))))
     ok_(not this_thread_is_pinned())
Example #22
0
 def test_request_token_fake(self):
     c = Mock()
     c.key = self.access.key
     c.secret = "mom"
     ok_(not self.auth.authenticate(self.call(client=OAuthClient(c))))