def test_sends_correct_credentials_when_setting_default_style( self, m: requests_mock.Mocker): m.post('/geoserver/rest/styles') m.put('/geoserver/rest/layers/all_detections') geoserver.install_style('test-style-id') self.assertEqual('Basic dGVzdC11c2VybmFtZTp0ZXN0LXBhc3N3b3Jk', m.request_history[1].headers['Authorization'])
def mock_server(requests_mock: Mocker): global messages_received global messages_sent messages_received = [] messages_sent = [] requests_mock.get(url, content=get_content) requests_mock.put(url, content=put_content)
def test_patch_project_only_labels( api_url: str, nuclio_client: mlrun.api.utils.clients.nuclio.Client, requests_mock: requests_mock_package.Mocker, ): project_name = "project-name" project_labels = { "some-label": "some-label-value", } mocked_project_body = _generate_project_body( project_name, labels={"label-key": "label-value"}, ) def verify_patch(request, context): # verifying the patch kept the old labels, patched the description, and added the new label expected_body = mocked_project_body expected_body["metadata"]["labels"].update(project_labels) assert ( deepdiff.DeepDiff(expected_body, request.json(), ignore_order=True,) == {} ) context.status_code = http.HTTPStatus.NO_CONTENT.value requests_mock.get( f"{api_url}/api/projects/{project_name}", json=mocked_project_body ) requests_mock.put(f"{api_url}/api/projects", json=verify_patch) nuclio_client.patch_project( None, project_name, {"metadata": {"labels": project_labels}}, )
def test_update_passphrase_file(self, *, rmock: requests_mock.Mocker) -> None: rmock.put("http://test/api/publish/s3%3Aaptly-repo%3Atest_xyz__1/test", text='{"Architectures":["amd64"],"Distribution":"test","Label":"",' '"Origin":"","Prefix":"test/xyz_1","SkipContents":false,' '"SourceKind":"local","Sources":[{"Component":"main","Name":"aptly-repo"}],' '"Storage":"s3:aptly-repo"}') self.assertEqual( self.papi.update( prefix="s3:aptly-repo:test/xyz_1", distribution="test", sign_batch=True, sign_gpgkey="A16BE921", sign_passphrase_file="/root/passphrase.txt", ), PublishEndpoint( storage='s3:aptly-repo', prefix='test/xyz_1', distribution='test', source_kind='local', sources=[{ 'Name': 'aptly-repo', 'Component': 'main' }], architectures=['amd64'], label='', origin='' ) )
def test_sends_correct_payload_when_setting_default_style( self, m: requests_mock.Mocker): m.post('/geoserver/rest/styles') m.put('/geoserver/rest/layers/all_detections') geoserver.install_style('test-style-id') xml = et.fromstring(m.request_history[1].text) # type: et.ElementTree self.assertEqual('detections', xml.findtext('defaultStyle/name'))
def test_generate_cube_failure(self, m: requests_mock.Mocker): m.post(f'{self.ENDPOINT_URL}oauth/token', json={ "access_token": "4ccsstkn983456jkfde", "token_type": "bearer" }) m.put(f'{self.ENDPOINT_URL}cubegens', response_list=[ make_result(0, 4), ]) m.get(f'{self.ENDPOINT_URL}cubegens/93', response_list=[ make_result(1, 4), make_result(2, 4, failed=True, output=['1.that', '2.was', '3.bad']), ]) observer = TestProgressObserver() with new_progress_observers(observer): cube_result = self.generator.generate_cube(self.REQUEST) self.assertEqual('error', cube_result.status) self.assertEqual(['1.that', '2.was', '3.bad'], cube_result.output) print(observer.calls) self.assertEqual( [ ('begin', [('Generating cube', 0.0, False)]), ('update', [('Generating cube', 0.25, False)]), ('end', [('Generating cube', 0.25, True)]) ], observer.calls)
def test_update_snapshot_default_key(self, *, rmock: requests_mock.Mocker) -> None: rmock.put("http://test/api/publish/s3%3Aaptly-repo%3Atest_xyz__1/test", text='{"Architectures":["amd64"],"Distribution":"test","Label":"",' '"Origin":"","Prefix":"test/xyz_1","SkipContents":false,' '"SourceKind":"snapshot","Sources":[{"Component":"main","Name":"aptly-repo-1"}],' '"Storage":"s3:aptly-repo"}') self.assertEqual( self.papi.update( prefix="s3:aptly-repo:test/xyz_1", distribution="test", snapshots=[{"Name": "aptly-repo-1"}], force_overwrite=True, sign_batch=True, sign_passphrase="123456", sign_keyring="/etc/gpg-managed-keyring/pubring.pub", sign_secret_keyring="/etc/gpg-managed-keyring/secring.gpg" ), PublishEndpoint( storage='s3:aptly-repo', prefix='test/xyz_1', distribution='test', source_kind='snapshot', sources=[{ 'Name': 'aptly-repo-1', 'Component': 'main', }], architectures=['amd64'], label='', origin='' ) )
def test_patch_project( api_url: str, nuclio_client: mlrun.api.utils.clients.nuclio.Client, requests_mock: requests_mock_package.Mocker, ): project_name = "project-name" project_description = "some description" mocked_project_body = _generate_project_body( project_name, labels={"label-key": "label-value"}, with_spec=True ) def verify_patch(request, context): # verifying the patch kept the labels and only patched the description expected_body = mocked_project_body expected_body["spec"]["description"] = project_description assert ( deepdiff.DeepDiff(expected_body, request.json(), ignore_order=True,) == {} ) context.status_code = http.HTTPStatus.NO_CONTENT.value requests_mock.get( f"{api_url}/api/projects/{project_name}", json=mocked_project_body ) requests_mock.put(f"{api_url}/api/projects", json=verify_patch) nuclio_client.patch_project( None, project_name, mlrun.api.schemas.ProjectPatch(description=project_description), )
def test_store_project_update( api_url: str, nuclio_client: mlrun.api.utils.clients.nuclio.Client, requests_mock: requests_mock_package.Mocker, ): project_name = "project-name" project_description = "some description" mocked_project_body = _generate_project_body( project_name, labels={"label-key": "label-value"}, with_spec=True ) def verify_store_update(request, context): assert ( deepdiff.DeepDiff( _generate_project_body( project_name, project_description, with_namespace=False ), request.json(), ignore_order=True, ) == {} ) context.status_code = http.HTTPStatus.NO_CONTENT.value # mock project response so store will update requests_mock.get( f"{api_url}/api/projects/{project_name}", json=mocked_project_body ) requests_mock.put(f"{api_url}/api/projects", json=verify_store_update) nuclio_client.store_project( None, project_name, mlrun.api.schemas.Project(name=project_name, description=project_description), )
def test_repo_edit(self, *, rmock: requests_mock.Mocker) -> None: rmock.put("http://test/api/repos/aptly-repo", text='{"Name":"aptly-repo","Comment":"comment",' '"DefaultDistribution":"stretch","DefaultComponent":"main"}') self.assertEqual( self.rapi.edit("aptly-repo", comment="comment", default_distribution="stretch", default_component="main"), Repo(name='aptly-repo', comment='comment', default_distribution='stretch', default_component='main') )
def test_calls_correct_url_when_setting_default_style( self, m: requests_mock.Mocker): m.post('/geoserver/rest/styles') m.put('/geoserver/rest/layers/all_detections') geoserver.install_style('test-style-id') self.assertEqual( 'http://vcap-geoserver.test.localdomain/geoserver/rest/layers/all_detections', m.request_history[1].url)
def test_sends_correct_payload_when_creating_sld(self, m: requests_mock.Mocker): m.post('/geoserver/rest/styles') m.put('/geoserver/rest/layers/all_detections') geoserver.install_style('test-style-id') xml = et.fromstring(m.request_history[0].text) # type: et.ElementTree self.assertEqual('#FF00FF', xml.findtext('.//sld:CssParameter', namespaces=XMLNS))
def test_upload_main(self, mock_requests: requests_mock.Mocker) -> None: """Use the main script to upload files.""" mock_requests.put('https://api.typeform.com/forms/beviMNpK') mock_requests.put('https://api.typeform.com/forms/VAj8bEvq') typeform_radar.main(['upload']) self.assertEqual(2, mock_requests.call_count)
def test_create_attachment(self, mocked: requests_mock.Mocker, a_test_collab: collaboratory.Collab) -> bool: mocked.put( f"{COLLABORATORY_URL}/rest/wikis/xwiki/spaces/Collabs/spaces/{COLLAB_NAME}" + "/pages/WebHome/attachments/test.py") a_test_collab.attach(name="test.py", content="print('hello')") assert ( mocked.last_request.url == f"{COLLABORATORY_URL}/rest/wikis/xwiki/spaces/Collabs/spaces/{COLLAB_NAME}" + "/pages/WebHome/attachments/test.py")
def test_upload_missing_api_key( self, mock_requests: requests_mock.Mocker) -> None: """Try uploading without an API key.""" mock_requests.put('https://api.typeform.com/forms/beviMNpK') mock_requests.put('https://api.typeform.com/forms/VAj8bEvq') with self.assertRaises(ValueError): typeform_radar.main(['upload']) self.assertFalse(mock_requests.called)
def test_update(self, *, rmock: requests_mock.Mocker) -> None: rmock.put( "http://test/api/snapshots/aptly-repo-1", text= '{"Name":"aptly-repo-2","CreatedAt":"2017-06-03T23:43:40.275605639Z",' '"Description":"test"}') self.assertEqual( self.sapi.update("aptly-repo-1", newname="aptly-repo-2", newdescription="test"), Snapshot(name='aptly-repo-2', description='test', created_at=iso8601.parse_date( '2017-06-03T23:43:40.275605639Z', default_timezone=pytz.UTC)))
def test_lambda_succeeds_if_unable_to_get_task_event( status_code, json, status, context, requests_mock: RequestsMocker, ): @task def task_app(event, api): return True event = RawTaskEvent(task_id='0', version=2).dict() get_mock = requests_mock.get(re.compile('/v2/tasks/0'), status_code=status_code, json=json) put_mock = requests_mock.put(re.compile(f'/v2/tasks/0/{status}')) result = task_app(event, context)[0] assert get_mock.called_once assert put_mock.called_once if status == 'fail': assert set(put_mock.request_history[0].json()) == {'fail_reason'} assert result is None if status == 'success': assert put_mock.request_history[0].json() == {'payload': True} assert result is True
def test_lambda_succeeds_if_user_app_fails( status, side_effect, context, mocker: MockerFixture, requests_mock: RequestsMocker, ): event = RawTaskEvent(task_id='0', version=2).dict() mocker.patch.object( RawTaskEvent, 'get_task_event', return_value=TaskEvent(asset_id=int(), company_id=int()), ) put_mock = requests_mock.put(re.compile(f'/v2/tasks/0/{status}')) result = task(mocker.Mock(side_effect=side_effect, return_value=True))(event, context)[0] assert put_mock.called_once if status == 'fail': assert put_mock.request_history[0].json() == { 'fail_reason': 'test_user_app_raises' } assert result is None if status == 'success': assert put_mock.request_history[0].json() == {'payload': True} assert result is True
def test_patch_project( api_url: str, iguazio_client: mlrun.api.utils.clients.iguazio.Client, requests_mock: requests_mock_package.Mocker, ): project = _generate_project() session_cookie = "1234" patched_description = "new desc" def verify_patch(request, context): patched_project = _generate_project(description=patched_description, created=project.metadata.created) _assert_project_creation(iguazio_client, request.json(), patched_project) context.status_code = http.HTTPStatus.OK.value _verify_request_headers(request.headers, session_cookie) return { "data": _build_project_response(iguazio_client, patched_project) } # mock project response on get (patch does get first) requests_mock.get( f"{api_url}/api/projects/__name__/{project.metadata.name}", json={"data": _build_project_response(iguazio_client, project)}, ) requests_mock.put( f"{api_url}/api/projects/__name__/{project.metadata.name}", json=verify_patch, ) patched_project, is_running_in_background = iguazio_client.patch_project( session_cookie, project.metadata.name, {"spec": { "description": patched_description }}, wait_for_completion=True, ) assert is_running_in_background is False exclude = {"status": {"state"}, "spec": {"description"}} assert (deepdiff.DeepDiff( project.dict(exclude=exclude), patched_project.dict(exclude=exclude), ignore_order=True, ) == {}) assert patched_project.status.state == project.spec.desired_state assert patched_project.spec.description == patched_description
def test_upload_forms(self, mock_requests: requests_mock.Mocker) -> None: """Upload the files from a folder.""" with open(os.path.join(self._tmpdir, 'abcdef.json'), 'w', encoding='utf-8') as json_file: json_file.write('{"id": "abcdef", "title": "Yipee"}') with open(os.path.join(self._tmpdir, 'README.md'), 'w', encoding='utf-8') as md_file: md_file.write('Not related') mock_requests.put('https://api.typeform.com/forms/abcdef') typeform_radar.upload_forms(json_input_folder=self._tmpdir) self.assertEqual(1, mock_requests.call_count) self.assertEqual('Yipee', mock_requests.request_history[0].json().get('title'))
def test_standard_api(client: Fabricator, m: requests_mock.Mocker): client.standard(with_param='id') client.start() # Mock request m.get(BASE_URL + '/', text='OK') m.post(BASE_URL + '/', status_code=201) m.get(BASE_URL + '/1', text='OK') m.put(BASE_URL + '/1', status_code=202) m.patch(BASE_URL + '/1', status_code=202) m.delete(BASE_URL + '/1', status_code=204) # Call the endpoints assert client.all().status_code == 200 get = client.get(id=1) assert get.status_code == 200 and get.text == 'OK' assert client.create(name='user').status_code == 201 assert client.overwrite(id=1, name='user').status_code == 202 assert client.update(id=1, name='user').status_code == 202 assert client.delete(id=1).status_code == 204
def test_store_project_update( api_url: str, iguazio_client: mlrun.api.utils.clients.iguazio.Client, requests_mock: requests_mock_package.Mocker, ): project = _generate_project() session_cookie = "1234" def verify_store_update(request, context): _assert_project_creation(iguazio_client, request.json(), project) context.status_code = http.HTTPStatus.OK.value _verify_request_headers(request.headers, session_cookie) return {"data": _build_project_response(iguazio_client, project)} empty_project = _generate_project(description="", labels={}, annotations={}) # mock project response so store will update requests_mock.get( f"{api_url}/api/projects/__name__/{project.metadata.name}", json={"data": _build_project_response(iguazio_client, empty_project)}, ) requests_mock.put( f"{api_url}/api/projects/__name__/{project.metadata.name}", json=verify_store_update, ) updated_project, is_running_in_background = iguazio_client.store_project( session_cookie, project.metadata.name, project, ) assert is_running_in_background is False exclude = {"status": {"state"}} assert (deepdiff.DeepDiff( project.dict(exclude=exclude), updated_project.dict(exclude=exclude), ignore_order=True, ) == {}) assert updated_project.status.state == project.spec.desired_state
def test_generate_cube_success(self, m: requests_mock.Mocker): m.post(f'{self.ENDPOINT_URL}oauth/token', json={ "access_token": "4ccsstkn983456jkfde", "token_type": "bearer" }) m.put(f'{self.ENDPOINT_URL}cubegens', response_list=[ make_result(0, 4), ]) m.get(f'{self.ENDPOINT_URL}cubegens/93', response_list=[ make_result(1, 4), make_result(2, 4), make_result(3, 4), make_result(4, 4, job_result={ "status": "ok", "result": { "data_id": "bibo.zarr" } }), ]) observer = TestProgressObserver() with new_progress_observers(observer): self.generator.generate_cube(self.REQUEST) self.assertEqual( [ ('begin', [('Generating cube', 0.0, False)]), ('update', [('Generating cube', 0.25, False)]), ('update', [('Generating cube', 0.5, False)]), ('update', [('Generating cube', 0.75, False)]), ('end', [('Generating cube', 0.75, True)]) ], observer.calls)
def test_task_app_succeeds(context, requests_mock: RequestsMocker): @task def task_app(event, api): return True event = RawTaskEvent(task_id='0', version=2).dict() get_mock = requests_mock.get( re.compile('/v2/tasks/0'), json=TaskEvent(asset_id=int(), company_id=int()).dict(), ) put_mock = requests_mock.put(re.compile('/v2/tasks/0/success')) result = task_app(event, context)[0] assert get_mock.called_once assert put_mock.called_once assert put_mock.request_history[0].json() == {'payload': True} assert result is True
def test_log_if_user_app_fails( context, mocker: MockerFixture, requests_mock: RequestsMocker, capsys, ): event = RawTaskEvent(task_id='0', version=2).dict() mocker.patch.object( RawTaskEvent, 'get_task_event', return_value=TaskEvent(asset_id=int(), company_id=int()), ) put_mock = requests_mock.put(re.compile('/v2/tasks/0/fail')) task(mocker.Mock(side_effect=Exception))(event, context) captured = capsys.readouterr() assert put_mock.called_once assert 'ASSET=0' in captured.out assert 'Task app failed to execute.' in captured.out
def test_throws_on_http_error_when_setting_default_style( self, m: requests_mock.Mocker): m.post('/geoserver/rest/styles') m.put('/geoserver/rest/layers/all_detections', status_code=500) with self.assertRaises(geoserver.InstallError): geoserver.install_style('test-style-id')
def test_put_server_error(requests_mock: Mocker): requests_mock.put(url, status_code=404) set_globals('llee', 'zman', 'pwd1', 'Hello world!') client = new_client() assert not send_message(client)
def cf_endpoint(requests_mock: Mocker): requests_mock.put(TEST_RESPONSE_URL) return requests_mock