def test_sync_profile(app, mocker): """Test sync_profile.""" mocker.patch("orcid_api.MemberAPIV20Api.update_employment", return_value=Mock(status=201, headers={'Location': '12344/XYZ/54321'})) mocker.patch("orcid_api.MemberAPIV20Api.update_education", return_value=Mock(status=201, headers={'Location': '12344/XYZ/12345'})) org = Organisation.create( name="THE ORGANISATION:test_sync_profile", tuakiri_name="THE ORGANISATION:test_sync_profile", confirmed=True, orcid_client_id="APP-5ZVH4JRQ0C27RVH5", orcid_secret="Client Secret", city="CITY", country="COUNTRY", disambiguated_id="ID", disambiguation_source="SOURCE") u = User.create(email="*****@*****.**", name="TEST USER", roles=Role.RESEARCHER, orcid="12344", confirmed=True, organisation=org) UserOrg.create(user=u, org=org) access_token = "ACCESS-TOKEN" t = Task.create(org=org, task_type=TaskType.SYNC) api = MemberAPI(org=org) mocker.patch("orcid_hub.orcid_client.MemberAPI.get_record", lambda *args: None) api.sync_profile(task=t, user=u, access_token=access_token) OrcidToken.create(user=u, org=org, scope="/read-limited,/activities/update") mocker.patch("orcid_hub.orcid_client.MemberAPI.get_record", lambda *args: None) api.sync_profile(task=t, user=u, access_token=access_token) assert Log.select().count() > 0 mocker.patch("orcid_hub.orcid_client.MemberAPI.get_record", return_value=get_profile()) api.sync_profile(task=t, user=u, access_token=access_token) last_log = Log.select().order_by(Log.id.desc()).first() assert "Successfully update" in last_log.message
def test_member_api(app, mocker): """Test MemberAPI extension and wrapper of ORCID API.""" configuration.access_token = None mocker.patch.multiple("orcid_hub.app.logger", error=DEFAULT, exception=DEFAULT, info=DEFAULT) org = Organisation.get(name="THE ORGANISATION") user = User.create( orcid="1001-0001-0001-0001", name="TEST USER 123", email="*****@*****.**", organisation=org, confirmed=True) UserOrg.create(user=user, org=org, affiliation=Affiliation.EDU) MemberAPI(user=user) assert configuration.access_token is None or configuration.access_token == '' MemberAPI(user=user, org=org) assert configuration.access_token is None or configuration.access_token == '' MemberAPI(user=user, org=org, access_token="ACCESS000") assert configuration.access_token == 'ACCESS000' OrcidToken.create( access_token="ACCESS123", user=user, org=org, scopes="/read-limited,/activities/update", expires_in='121') api = MemberAPI(user=user, org=org) assert configuration.access_token == "ACCESS123" # Test API call auditing: with patch.object( api.api_client.rest_client.pool_manager, "request", return_value=Mock(data=b"""{"mock": "data"}""", status=200)) as request_mock: api.get_record() request_mock.assert_called_once_with( "GET", "https://api.sandbox.orcid.org/v2.0/1001-0001-0001-0001", preload_content=False, timeout=None, fields=None, headers={ "Accept": "application/json", "Content-Type": "application/json", "User-Agent": "Swagger-Codegen/1.0.0/python", "Authorization": "Bearer ACCESS123" }) api_call = OrcidApiCall.select().first() assert api_call.response == '{"mock": "data"}' assert api_call.url == "https://api.sandbox.orcid.org/v2.0/1001-0001-0001-0001" with patch.object(OrcidApiCall, "create", side_effect=Exception("FAILURE")) as create: api.get_record() create.assert_called_once() with patch.object( api.api_client.rest_client.pool_manager, "request", return_value=Mock(data=b'', status=200)) as request_mock: # api.get_record() OrcidApiCall.delete().execute() api.view_person("1234-XXXX-XXXX-XXXX") api_call = OrcidApiCall.select().first() assert api_call.response is None assert api_call.url == "https://api.sandbox.orcid.org/v2.0/1234-XXXX-XXXX-XXXX/person" # API: with patch.object( api_client.ApiClient, "call_api", side_effect=ApiException( reason="FAILURE", status=401)) as call_api: with patch.object(OrcidToken, "delete") as delete: api.get_record() app.logger.error.assert_called_with("ApiException Occurred: (401)\nReason: FAILURE\n") call_api.assert_called_once() delete.assert_called_once() with patch.object( api_client.ApiClient, "call_api", side_effect=ApiException(reason="FAILURE 999", status=999)) as call_api: api.get_record() app.logger.error.assert_called_with("ApiException Occurred: (999)\nReason: FAILURE 999\n") with patch.object( api_client.ApiClient, "call_api", side_effect=ApiException( reason="FAILURE", status=401)) as call_api: api.get_record() app.logger.error.assert_called_with("ApiException Occurred: (401)\nReason: FAILURE\n") call_api.assert_called_once() call_api.reset_mock() api.get_record() app.logger.exception.assert_called_with("Exception occurred while retrieving ORCID Token") call_api.assert_called_once() with patch.object( api_client.ApiClient, "call_api", return_value=( Mock(data=b"""{"mock": "data"}"""), 200, [], )) as call_api: api.get_record() call_api.assert_called_with( f"/v2.0/{user.orcid}", "GET", _preload_content=False, auth_settings=["orcid_auth"], header_params={"Accept": "application/json"}, response_type=None)
def test_is_emp_or_edu_record_present(app, mocker): """Test 'is_emp_or_edu_record_present' method.""" mocker.patch.multiple("orcid_hub.app.logger", error=DEFAULT, exception=DEFAULT, info=DEFAULT) org = Organisation.get(name="THE ORGANISATION") user = User.create( orcid="1001-0001-0001-0001", name="TEST USER 123", email="*****@*****.**", organisation=org, confirmed=True) UserOrg.create(user=user, org=org, affiliation=Affiliation.EDU) api = MemberAPI(user=user, org=org) test_responses = [ None, """{"mock": "data"}""", """{ "employment-summary": [{"source": {"source-client-id": {"path": "CLIENT000"}}, "put-code": 123}], "education-summary": [{"source": {"source-client-id": {"path": "CLIENT000"}}, "put-code": 456}] }""", """{"employment-summary": [], "education-summary": []}""" ] for data in test_responses: with patch.object( api_client.ApiClient, "call_api", return_value=Mock(data=data)) as call_api: api.is_emp_or_edu_record_present(Affiliation.EDU) call_api.assert_called_with( "/v2.0/{orcid}/educations", "GET", {"orcid": "1001-0001-0001-0001"}, {}, {"Accept": "application/json"}, _preload_content=False, _request_timeout=None, _return_http_data_only=True, auth_settings=["orcid_auth"], body=None, callback=None, collection_formats={}, files={}, post_params=[], response_type="Educations") api.is_emp_or_edu_record_present(Affiliation.EMP) call_api.assert_called_with( "/v2.0/{orcid}/employments", "GET", {"orcid": "1001-0001-0001-0001"}, {}, {"Accept": "application/json"}, _preload_content=False, _request_timeout=None, _return_http_data_only=True, auth_settings=["orcid_auth"], body=None, callback=None, collection_formats={}, files={}, post_params=[], response_type="Employments") with patch.object( api_client.ApiClient, "call_api", side_effect=ApiException( reason="FAILURE", status=401)) as call_api: api.is_emp_or_edu_record_present(Affiliation.EDU) app.logger.error.assert_called_with( "For TEST USER 123 ([email protected]) while checking for employment " "and education records, Encountered Exception: (401)\nReason: FAILURE\n") with patch.object( api_client.ApiClient, "call_api", side_effect=Exception("EXCEPTION")) as call_api: api.is_emp_or_edu_record_present(Affiliation.EDU) app.logger.exception.assert_called_with( "Failed to verify presence of employment or education record.")