def test_refresh_request_with_token_format_opaque(self): requests = FakeRequests() session = MockSession() with patch('oauth2_client.credentials_manager.requests', new=requests), \ patch('cloudfoundry_client.client.requests', new=requests): requests.Session.return_value = session requests.get.return_value = MockResponse( '%s/v2/info' % TARGET_ENDPOINT, status_code=OK, text=json.dumps( dict(api_version='2.1', authorization_endpoint=TARGET_ENDPOINT))) requests.post.return_value = MockResponse( '%s/oauth/token' % TARGET_ENDPOINT, status_code=OK, text=json.dumps( dict(access_token='access-token', refresh_token='refresh-token'))) client = CloudFoundryClient(TARGET_ENDPOINT, token_format='opaque') client.init_with_token('refresh-token') self.assertEqual('Bearer access-token', session.headers.get('Authorization')) requests.post.assert_called_with( requests.post.return_value.url, data=dict(grant_type='refresh_token', scope='', refresh_token='refresh-token', token_format='opaque'), headers=dict(Accept='application/json', Authorization='Basic Y2Y6'), proxies=dict(http='', https=''), verify=True)
def test_open_cfignore_file(self): with patch("%s.open" % built_in_entry, mock_open(read_data="*.log")) as mock_file, \ patch('os.path.isfile', create=True) as mock_isfile: mock_isfile.__return_value__ = True application_path = '/some/path' CfIgnore(application_path) mock_file.assert_called_with( os.path.join(application_path, '.cfignore'), 'r')
def test_ignore_wildcard_resources(self): with patch("%s.open" % built_in_entry, mock_open(read_data="*.log")), \ patch('os.path.isfile', create=True) as mock_isfile: mock_isfile.__return_value__ = True cf_ignore = CfIgnore('/some/path') self.assertTrue(cf_ignore.is_entry_ignored("toto.log")) self.assertTrue( cf_ignore.is_entry_ignored("/some/other/path/toto.log"))
def test_main_push(self): class FakeOperation(object): def __init__(self): self.push = MagicMock() client = object() push_operation = FakeOperation() with patch('cloudfoundry_client.main.main.build_client_from_configuration', new=lambda: client), \ patch('cloudfoundry_client.main.operation_commands.PushOperation', new=lambda c: push_operation): main.main() push_operation.push.assert_called_with('space_id', get_fixtures_path('fake', 'manifest_main.yml'))
def test_ignore_directory(self): with patch("%s.open" % built_in_entry, mock_open(read_data="ignored/directory/")), \ patch('os.path.isfile', create=True) as mock_isfile: mock_isfile.__return_value__ = True cf_ignore = CfIgnore('/some/path') self.assertTrue( cf_ignore.is_entry_ignored("ignored/directory/resource.file")) self.assertTrue( cf_ignore.is_entry_ignored("/ignored/directory/resource.file")) self.assertTrue( cf_ignore.is_entry_ignored( "/some/sub/directory/containing/ignored/directory/resource.file" )) # File in fact self.assertFalse(cf_ignore.is_entry_ignored('/ignored/directory'))
def test_main_list_services(self): with patch("cloudfoundry_client.main.build_client_from_configuration", new=lambda: self.client): self.client.get.return_value = mock_response( "/v2/services", OK, None, "v2", "services", "GET_response.json" ) main.main() self.client.get.assert_called_with(self.client.get.return_value.url)
def test_main_list_spaces(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response( '/v2/spaces', OK, None, 'v2', 'spaces', 'GET_response.json') main.main() self.client.get.assert_called_with( self.client.get.return_value.url)
def test_main_delete_apps(self): with patch('cloudfoundry_client.main.main.build_client_from_configuration', new=lambda: self.client): self.client.delete.return_value = mock_response('/v2/apps/906775ea-622e-4bc7-af5d-9aab3b652f81', NO_CONTENT, None) main.main() self.client.delete.assert_called_with(self.client.delete.return_value.url)
def build_client(self): with patch('cloudfoundry_client.client.requests') as fake_requests: fake_info_response = mock_response('/v2/info', 200, None) fake_info_response.text = json.dumps(dict(api_version='2.X', authorization_endpoint=TARGET_ENDPOINT, logging_endpoint=TARGET_ENDPOINT)) fake_requests.get.return_value = fake_info_response self.client = CloudFoundryClient(TARGET_ENDPOINT)
def test_main_get_service_instance(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response('/v2/service_instances/df52420f-d5b9-4b86-a7d3-6d7005d1ce96', OK, None, 'v2', 'service_instances', 'GET_{id}_response.json') main.main() self.client.get.assert_called_with(self.client.get.return_value.url)
def test_main_get_buildpack(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response('/v2/buildpacks/6e72c33b-dff0-4020-8603-bcd8a4eb05e4', OK, None, 'v2', 'buildpacks', 'GET_{id}_response.json') main.main() self.client.get.assert_called_with(self.client.get.return_value.url)
def test_main_get_organization(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response('/v2/organizations/fe79371b-39b8-4f0d-8331-cff423a06aca', OK, None, 'v2', 'organizations', 'GET_{id}_response.json') main.main() self.client.get.assert_called_with(self.client.get.return_value.url)
def test_main_get_stack(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response('/v2/stacks/6e72c33b-dff0-4020-8603-bcd8a4eb05e4', OK, None, 'v2', 'stacks', 'GET_{id}_response.json') main.main() self.client.get.assert_called_with(self.client.get.return_value.url)
def test_main_get_spaces(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response('/v2/spaces/2d745a4b-67e3-4398-986e-2adbcf8f7ec9', OK, None, 'v2', 'spaces', 'GET_{id}_response.json') main.main() self.client.get.assert_called_with(self.client.get.return_value.url)
def test_main_get_service_plan(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response('/v2/service_plans/5d8f3b0f-6b5b-487f-8fed-4c2d9b812a72', OK, None, 'v2', 'service_plans', 'GET_{id}_response.json') main.main() self.client.get.assert_called_with(self.client.get.return_value.url)
def test_main_get_service_broker(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response('/v2/service_brokers/ade9730c-4ee5-4290-ad37-0b15cecd2ca6', OK, None, 'v2', 'service_brokers', 'GET_{id}_response.json') main.main() self.client.get.assert_called_with(self.client.get.return_value.url)
def test_main_get_service_key(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response('/v2/service_keys/67755c27-28ed-4087-9688-c07d92f3bcc9', OK, None, 'v2', 'service_keys', 'GET_{id}_response.json') main.main() self.client.get.assert_called_with(self.client.get.return_value.url)
def test_main_get_service_binding(self): with patch('cloudfoundry_client.main.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response('/v2/service_bindings/eaabd042-8f5c-44a2-9580-1e114c36bdcb', OK, None, 'v2', 'service_bindings', 'GET_{id}_response.json') main.main() self.client.get.assert_called_with(self.client.get.return_value.url)
def test_main_get_spaces(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response( '/v2/spaces/2d745a4b-67e3-4398-986e-2adbcf8f7ec9', OK, None, 'v2', 'spaces', 'GET_{id}_response.json') main.main() self.client.get.assert_called_with( self.client.get.return_value.url)
def test_main_get_service_key(self): with patch('cloudfoundry_client.main.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response('/v2/service_keys/67755c27-28ed-4087-9688-c07d92f3bcc9', OK, None, 'v2', 'service_keys', 'GET_{id}_response.json') main.main() self.client.get.assert_called_with(self.client.get.return_value.url)
def test_main_get_service(self): with patch('cloudfoundry_client.main.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response('/v2/services/2c883dbb-a726-4ecf-a0b7-d65588897e7f', OK, None, 'v2', 'services', 'GET_{id}_response.json') main.main() self.client.get.assert_called_with(self.client.get.return_value.url)
def test_main_get_route(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response( '/v2/routes/75c16cfe-9b8a-4faf-bb65-02c713c7956f', OK, None, 'v2', 'routes', 'GET_{id}_response.json') main.main() self.client.get.assert_called_with( self.client.get.return_value.url)
def test_main_get_service_binding(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response('/v2/service_bindings/eaabd042-8f5c-44a2-9580-1e114c36bdcb', OK, None, 'v2', 'service_bindings', 'GET_{id}_response.json') main.main() self.client.get.assert_called_with(self.client.get.return_value.url)
def test_main_list_service_bindings(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response('/v2/service_bindings', OK, None, 'v2', 'service_bindings', 'GET_response.json') main.main() self.client.get.assert_called_with(self.client.get.return_value.url)
def test_main_get_app(self): with patch('cloudfoundry_client.main.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response('/v2/apps/906775ea-622e-4bc7-af5d-9aab3b652f81', OK, None, 'v2', 'apps', 'GET_{id}_response.json') main.main() self.client.get.assert_called_with(self.client.get.return_value.url)
def test_main_get_service_plan(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response( '/v2/service_plans/5d8f3b0f-6b5b-487f-8fed-4c2d9b812a72', OK, None, 'v2', 'service_plans', 'GET_{id}_response.json') main.main() self.client.get.assert_called_with( self.client.get.return_value.url)
def test_main_delete_service_key(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.delete.return_value = mock_response( '/v2/service_keys/67755c27-28ed-4087-9688-c07d92f3bcc9', NO_CONTENT, None) main.main() self.client.delete.assert_called_with(self.client.delete.return_value.url) main.main()
def test_list_tasks(self): with patch( 'cloudfoundry_client.main.main.build_client_from_configuration', new=lambda: self.client): self.client.get.return_value = mock_response( '/v3/tasks?names=task_name', OK, None, 'v3', 'tasks', 'GET_response.json') main.main() self.client.get.assert_called_with( self.client.get.return_value.url)
def test_cancel_task(self): with patch( 'cloudfoundry_client.main.main.build_client_from_configuration', new=lambda: self.client): self.client.post.return_value = mock_response( '/v3/tasks/task_id/actions/cancel', CREATED, None, 'v3', 'tasks', 'POST_{id}_actions_cancel_response.json') main.main() self.client.post.assert_called_with( self.client.post.return_value.url, json=None)
def test_main_delete_service_key(self): with patch('cloudfoundry_client.main.main.build_client_from_configuration', new=lambda: self.client): self.client.delete.return_value = mock_response( '/v2/service_keys/67755c27-28ed-4087-9688-c07d92f3bcc9', NO_CONTENT, None) main.main() self.client.delete.assert_called_with(self.client.delete.return_value.url) main.main()
def test_main_create_service_key(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.post.return_value = mock_response( '/v2/service_keys', CREATED, None, 'v2', 'service_keys', 'POST_response.json') main.main() self.client.post.assert_called_with( self.client.post.return_value.url, json=dict(service_instance_guid='service_instance_id', name='name-127'))
def test_create_task(self): with patch( 'cloudfoundry_client.main.main.build_client_from_configuration', new=lambda: self.client): self.client.post.return_value = mock_response( '/v3/apps/app_id/tasks', CREATED, None, 'v3', 'tasks', 'POST_response.json') main.main() self.client.post.assert_called_with( self.client.post.return_value.url, json=dict(command='rake db:migrate', name='example'))
def test_main_get_service(self): with patch("cloudfoundry_client.main.build_client_from_configuration", new=lambda: self.client): self.client.get.return_value = mock_response( "/v2/services/2c883dbb-a726-4ecf-a0b7-d65588897e7f", OK, None, "v2", "services", "GET_{id}_response.json", ) main.main() self.client.get.assert_called_with(self.client.get.return_value.url)
def test_main_create_service_key(self): with patch('cloudfoundry_client.main.build_client_from_configuration', new=lambda: self.client): self.client.post.return_value = mock_response( '/v2/service_keys', CREATED, None, 'v2', 'service_keys', 'POST_response.json') main.main() self.client.post.assert_called_with(self.client.post.return_value.url, json=dict(service_instance_guid='service_instance_id', name='name-127') )
def test_grant_password_request_with_login_hint(self): requests = FakeRequests() session = MockSession() with patch('oauth2_client.credentials_manager.requests', new=requests), \ patch('cloudfoundry_client.client.requests', new=requests): requests.Session.return_value = session requests.get.return_value = MockResponse( '%s/v2/info' % TARGET_ENDPOINT, status_code=OK, text=json.dumps( dict(api_version='2.1', authorization_endpoint=TARGET_ENDPOINT))) requests.post.return_value = MockResponse( '%s/oauth/token' % TARGET_ENDPOINT, status_code=OK, text=json.dumps( dict(access_token='access-token', refresh_token='refresh-token'))) client = CloudFoundryClient(TARGET_ENDPOINT, login_hint=quote( json.dumps(dict(origin='uaa'), separators=(',', ':')))) client.init_with_user_credentials('somebody', 'p@s$w0rd') self.assertEqual('Bearer access-token', session.headers.get('Authorization')) requests.post.assert_called_with( requests.post.return_value.url, data=dict(grant_type='password', username='******', scope='', password='******', login_hint='%7B%22origin%22%3A%22uaa%22%7D'), headers=dict(Accept='application/json', Authorization='Basic Y2Y6'), proxies=dict(http='', https=''), verify=True)