def test_it_does_not_updates_the_name_if_its_missing(name): profile = Profile('12345678', Name('Old Name')) orcid_record = {'person': {'name': name}} update_profile_from_orcid_record(profile, orcid_record) assert profile.name == Name('Old Name')
def test_it_updates_email_addresses(): profile = Profile('12345678', Name('Name')) profile.add_email_address('*****@*****.**', True, True) orcid_record = { 'person': { 'emails': { 'email': [ { 'email': '*****@*****.**', 'primary': True, 'verified': True, 'visibility': 'PUBLIC' }, ] }, } } update_profile_from_orcid_record(profile, orcid_record) assert len(profile.email_addresses) == 1 assert profile.email_addresses[0].email == '*****@*****.**' assert profile.email_addresses[0].restricted is False assert profile.email_addresses[0].position == 0
def _update_profile(profile: Profile, orcid_record: dict) -> None: try: update_profile_from_orcid_record(profile, orcid_record) except (AttributeError, LookupError, TypeError, ValueError) as exception: # We appear to be misunderstanding the ORCID data structure, but let's not block the # authentication flow. LOGGER.exception(exception)
def test_it_removes_email_addresses(): profile = Profile('12345678', Name('Name')) profile.add_email_address('*****@*****.**') orcid_record = {'person': {}} update_profile_from_orcid_record(profile, orcid_record) assert len(profile.email_addresses) == 0
def test_it_removes_affiliations(): profile = Profile('12345678', Name('Name')) profile.add_affiliation( Affiliation('1', Address(countries.get('gb'), 'City 1'), 'Organisation 1', Date(2017))) orcid_record = {} update_profile_from_orcid_record(profile, orcid_record) assert len(profile.affiliations) == 0
def test_it_updates_the_name_if_there_is_no_family_name(given_names: str): profile = Profile('12345678', Name('Old Name')) orcid_record = { 'person': { 'name': { 'given-names': { 'value': given_names } } } } update_profile_from_orcid_record(profile, orcid_record) assert profile.name.preferred == given_names assert profile.name.index == given_names
def test_it_updates_the_name(given_names: str, family_name: str): profile = Profile('12345678', Name('Old Name')) orcid_record = { 'person': { 'name': { 'family-name': { 'value': family_name }, 'given-names': { 'value': given_names } } } } update_profile_from_orcid_record(profile, orcid_record) assert profile.name.preferred == '{} {}'.format(given_names, family_name) assert profile.name.index == '{}, {}'.format(family_name, given_names)
def test_it_ignores_unverified_email_addresses(): profile = Profile('12345678', Name('Name')) orcid_record = { 'person': { 'emails': { 'email': [ { 'email': '*****@*****.**', 'primary': True, 'verified': False, 'visibility': 'PUBLIC' }, ] }, } } update_profile_from_orcid_record(profile, orcid_record) assert len(profile.email_addresses) == 0
def _update(payload: str) -> Response: LOGGER.info('POST request received from /orcid-webhook/%s', uri_signer.loads_unsafe(payload)[-1]) try: orcid = uri_signer.loads(payload) except BadSignature as exception: LOGGER.error( 'BadSignature: payload signature does not match: /orcid-webhook/%s', uri_signer.loads_unsafe(payload)[-1]) raise NotFound from exception try: profile = profiles.get_by_orcid(orcid) except ProfileNotFound as exception: raise NotFound(str(exception)) from exception try: access_token = orcid_tokens.get(profile.orcid).access_token except OrcidTokenNotFound: LOGGER.info( 'OrcidTokenNotFound: Access Token not found for %s. ' 'Reverting to public access token', profile.orcid) access_token = orcid_config.get('read_public_access_token') try: orcid_record = orcid_client.get_record(orcid, access_token) except RequestException as exception: if exception.response.status_code == 403 and not access_token == \ orcid_config.get('read_public_access_token'): orcid_tokens.remove(profile.orcid) # Let ORCID retry, it will use the public access token raise ServiceUnavailable from exception raise InternalServerError from exception update_profile_from_orcid_record(profile, orcid_record) return Response(status=204)
def test_it_updates_affiliations(): profile = Profile('12345678', Name('Name')) profile.add_affiliation( Affiliation('1', Address(countries.get('gb'), 'City 1'), 'Organisation 1', Date(2017))) orcid_record = { 'activities-summary': { 'employments': { 'employment-summary': [ { 'put-code': 1, 'department-name': 'Department 2', 'organization': { 'name': 'Organisation 2', 'address': { 'city': 'City 2', 'region': 'Region 2', 'country': 'US' } }, 'start-date': { 'year': { 'value': '2016' }, 'month': { 'value': '12' }, 'day': { 'value': '31' } }, 'end-date': { 'year': { 'value': '2018' }, 'month': { 'value': '02' }, 'day': { 'value': '03' } }, 'visibility': 'LIMIT' }, ] }, } } update_profile_from_orcid_record(profile, orcid_record) assert len(profile.affiliations) == 1 assert profile.affiliations[0].department == 'Department 2' assert profile.affiliations[0].organisation == 'Organisation 2' assert profile.affiliations[0].address.city == 'City 2' assert profile.affiliations[0].address.region == 'Region 2' assert profile.affiliations[0].address.country == countries.get('US') assert profile.affiliations[0].starts == Date(2016, 12, 31) assert profile.affiliations[0].ends == Date(2018, 2, 3) assert profile.affiliations[0].restricted is True assert profile.affiliations[0].position == 0
def test_it_adds_affiliations_with_a_partial_end_dates(): profile = Profile('12345678', Name('Name')) orcid_record = { 'activities-summary': { 'employments': { 'employment-summary': [ { 'put-code': 1, 'start-date': { 'year': { 'value': '2015' }, 'month': { 'value': '1' }, 'day': { 'value': '1' } }, 'end-date': { 'year': { 'value': '2016' } }, 'organization': { 'name': 'Organisation 1', 'address': { 'city': 'City 1', 'country': 'GB' } }, 'visibility': 'PUBLIC' }, { 'put-code': 2, 'start-date': { 'year': { 'value': '2015' }, 'month': { 'value': '1' }, 'day': { 'value': '1' } }, 'end-date': { 'year': { 'value': '2017' }, 'month': { 'value': '12' } }, 'department-name': 'Department 2', 'organization': { 'name': 'Organisation 2', 'address': { 'city': 'City 2', 'region': 'Region 2', 'country': 'US' } }, 'visibility': 'LIMIT' }, ] }, } } update_profile_from_orcid_record(profile, orcid_record) assert len(profile.affiliations) == 2 assert profile.affiliations[0].id == '1' assert profile.affiliations[0].restricted is False assert profile.affiliations[0].position == 0 assert profile.affiliations[0].ends == Date(2016) assert profile.affiliations[1].id == '2' assert profile.affiliations[1].restricted is True assert profile.affiliations[1].position == 1 assert profile.affiliations[1].ends == Date(2017, 12)