コード例 #1
0
    def test_configure(self):

        target = Path('http://example.com/a/b', A=10, B=20)

        self.assertEquals(
            target.configure(B=30, C=40).config.DATA, {
                'A': 10,
                'B': 30,
                'C': 40
            })
        self.assertEquals(target.config.DATA, {'A': 10, 'B': 20})
コード例 #2
0
    def test_service_request_with_999_status_code(self):

        target = Path('http://example/com/a/b')

        mock_method = mock.MagicMock()
        mock_method.return_value.json.return_value = {'result': {'x': 'y'}}
        mock_method.return_value.status_code = 999
        mock_body = mock.MagicMock()
        mock_params = mock.MagicMock()

        with self.assertRaises(error.HttpError):
            target._Path__service_request(mock_method, mock_body, mock_params)
コード例 #3
0
    def test_POST(self):

        target = Path('http://example.com/a/b')

        with mock.patch.object(target, '_Path__service_request') as mock_service_request:

            body = { 'x': 'y' }

            response = target.POST(body, A = 10, B = 20)

            mock_service_request.assert_called_once_with(requests.post, body, params = { 'A': 10, 'B': 20 })
            self.assertEquals(response, mock_service_request.return_value)
コード例 #4
0
    def test_service_request_with_role_arn(self, mock_AWS4Auth):
        mock_session = mock.Mock()
        # See https://docs.python.org/3/library/unittest.mock.html#unittest.mock.PropertyMock about how to mock properties
        region_mock = mock.PropertyMock(return_value="TEST-REGION")
        type(mock_session).region_name = region_mock

        mock_role_arn = mock.MagicMock()

        print("Mock_session {} region_name={}".format(
            mock_session, mock_session.region_name))
        mock_session.client.return_value.assume_role.return_value = {
            'Credentials': {
                'AccessKeyId': mock.MagicMock(),
                'SecretAccessKey': mock.MagicMock(),
                'SessionToken': mock.MagicMock()
            }
        }
        target = Path('http://example/com/a/b',
                      session=mock_session,
                      role_arn=mock_role_arn)

        mock_method = mock.MagicMock()
        mock_method.return_value.json.return_value = {'result': {'x': 'y'}}
        mock_method.return_value.status_code = 200
        mock_body = mock.MagicMock()
        mock_params = mock.MagicMock()

        response = target._Path__service_request(mock_method, mock_body,
                                                 mock_params)

        mock_session.client.assert_called_once_with(
            'sts', endpoint_url=AWSSTSUtils("TEST-REGION").endpoint_url)
        mock_session.client.return_value.assume_role.assert_called_once_with(
            RoleArn=mock_role_arn, RoleSessionName='cgf_service_client')

        mock_AWS4Auth.assert_called_once_with(
            mock_session.client.return_value.assume_role.
            return_value['Credentials']['AccessKeyId'],
            mock_session.client.return_value.assume_role.
            return_value['Credentials']['SecretAccessKey'],
            mock_session.region_name,
            'execute-api',
            session_token=mock_session.client.return_value.assume_role.
            return_value['Credentials']['SessionToken'])

        mock_method.assert_called_once_with('http://example/com/a/b',
                                            auth=mock_AWS4Auth.return_value,
                                            json=mock_body,
                                            params=mock_params)
        self.assertEquals(response.DATA, {'x': 'y'})
コード例 #5
0
    def test_eq(self):

        self.assertEqual(Path('http://example.com'), Path('http://example.com'))
        self.assertNotEqual(Path('http://example.com'), Path('http://example.com/a'))

        self.assertEqual(Path('http://example.com', A = 10), Path('http://example.com', A = 10))
        self.assertNotEqual(Path('http://example.com', A = 10), Path('http://example.com', A = 20))
コード例 #6
0
    def test_service_request_without_session(self):

        target = Path('http://example/com/a/b')

        mock_method = mock.MagicMock()
        mock_method.return_value.json.return_value = {'result': {'x': 'y'}}
        mock_method.return_value.status_code = 200
        mock_body = mock.MagicMock()
        mock_params = mock.MagicMock()

        response = target._Path__service_request(mock_method, mock_body, mock_params)

        mock_method.assert_called_once_with('http://example/com/a/b', auth = None, json = mock_body, params = mock_params)
        self.assertEquals(response.DATA, {'x': 'y'})
コード例 #7
0
    def test_init_stores_args(self):

        target = Path('http://example.com/base', Foo = 10, Bar = 20)

        self.assertEquals(target.url, 'http://example.com/base')
        self.assertEquals(target.config.Foo, 10)
        self.assertEquals(target.config.Bar, 20)
コード例 #8
0
    def test_service_request_with_role_arn(self, mock_AWS4Auth):

        mock_session = mock.MagicMock()
        mock_role_arn = mock.MagicMock()

        mock_session.client.return_value.assume_role.return_value = {
            'Credentials': {
                'AccessKeyId': mock.MagicMock(),
                'SecretAccessKey': mock.MagicMock(),
                'SessionToken': mock.MagicMock()
            }
        }

        target = Path('http://example/com/a/b',
                      session=mock_session,
                      role_arn=mock_role_arn)

        mock_method = mock.MagicMock()
        mock_method.return_value.json.return_value = {'result': {'x': 'y'}}
        mock_method.return_value.status_code = 200
        mock_body = mock.MagicMock()
        mock_params = mock.MagicMock()

        response = target._Path__service_request(mock_method, mock_body,
                                                 mock_params)

        mock_session.client.assert_called_once_with('sts')
        mock_session.client.return_value.assume_role.assert_called_once_with(
            RoleArn=mock_role_arn, RoleSessionName='cgf_service_client')

        mock_AWS4Auth.assert_called_once_with(
            mock_session.client.return_value.assume_role.
            return_value['Credentials']['AccessKeyId'],
            mock_session.client.return_value.assume_role.
            return_value['Credentials']['SecretAccessKey'],
            mock_session.region_name,
            'execute-api',
            session_token=mock_session.client.return_value.assume_role.
            return_value['Credentials']['SessionToken'])

        mock_method.assert_called_once_with('http://example/com/a/b',
                                            auth=mock_AWS4Auth.return_value,
                                            json=mock_body,
                                            params=mock_params)
        self.assertEquals(response.DATA, {'x': 'y'})
コード例 #9
0
def for_url(url, **kwargs):
    '''Create a Path object that can be used to make requests using paths relative to the specified url.

    Arguments:

      url - The url.
      
      **kwargs - Used to configure the Path object.

    '''
    return Path(url, **kwargs)
コード例 #10
0
    def test_service_request_with_session(self, mock_AWS4Auth):

        mock_session = mock.MagicMock()

        target = Path('http://example/com/a/b', session = mock_session)

        mock_method = mock.MagicMock()
        mock_method.return_value.json.return_value = {'result': {'x': 'y'}}
        mock_method.return_value.status_code = 200
        mock_body = mock.MagicMock()
        mock_params = mock.MagicMock()

        response = target._Path__service_request(mock_method, mock_body, mock_params)

        mock_AWS4Auth.assert_called_once_with(
            mock_session.get_credentials.return_value.get_frozen_credentials.return_value.access_key,
            mock_session.get_credentials.return_value.get_frozen_credentials.return_value.secret_key,
            mock_session.region_name,
            'execute-api',
            session_token = mock_session.get_credentials().get_frozen_credentials().token
        )

        mock_method.assert_called_once_with('http://example/com/a/b', auth = mock_AWS4Auth.return_value, json = mock_body, params = mock_params)
        self.assertEquals(response.DATA, {'x': 'y'})
コード例 #11
0
    def test_apply(self):
        target = Path('http://example.com/a/b')

        self.assertEquals(
            target.apply('c', 'd').url, 'http://example.com/a/b/c/d')
        self.assertEquals(
            target.apply('c/d').url, 'http://example.com/a/b/c/d')
        self.assertEquals(target.apply('/root').url, 'http://example.com/root')
        self.assertEquals(
            target.apply('..', 'c', 'd').url, 'http://example.com/a/c/d')
        self.assertEquals(
            target.apply('../c/d').url, 'http://example.com/a/c/d')
        self.assertEquals(
            target.apply('x/y/../../c/d').url, 'http://example.com/a/b/c/d')
        self.assertEquals(target.apply('..', '..').url, 'http://example.com')
        self.assertEquals(target.apply('../..').url, 'http://example.com')
        self.assertEquals(
            target.apply('..', '..', 'c', 'd').url, 'http://example.com/c/d')
        self.assertEquals(
            target.apply('../../c/d').url, 'http://example.com/c/d')

        with self.assertRaises(ValueError):
            target.apply('..', '..', '..')

        with self.assertRaises(ValueError):
            target.apply('../../..')
コード例 #12
0
 def test_str(self):
     target = Path('http://example.com/base')
     self.assertEquals(str(target), 'http://example.com/base')
コード例 #13
0
    def test_init_removes_slash_if_url_ends_with_slash(self):
        target = Path('http://example.com/base/')

        self.assertEquals(target.url, 'http://example.com/base')
コード例 #14
0
    def test_navigate(self):

        target = Path('http://example.com/a/b')

        self.assertEquals(target.navigate('c').url, 'http://example.com/a/b/c')
        self.assertEquals(target.navigate('c', 'http://quoted').url, 'http://example.com/a/b/c/http%3A%2F%2Fquoted')