Example #1
0
def _update_affiliations_from_orcid_record(profile: Profile,
                                           orcid_record: dict) -> None:
    orcid_affiliations = jmespath.search(
        '"activities-summary".employments."employment-summary"[*]',
        orcid_record) or []

    found_affiliation_ids = set()
    for index, orcid_affiliation in enumerate(orcid_affiliations):
        organization = orcid_affiliation['organization']
        address = organization['address']

        affiliation = Affiliation(
            affiliation_id=str(orcid_affiliation['put-code']),
            department=orcid_affiliation.get('department-name'),
            organisation=organization['name'],
            address=Address(
                city=address['city'],
                region=address.get('region'),
                country=countries.get(address['country']),
            ),
            starts=_convert_orcid_date(
                orcid_affiliation.get('start-date') or {}),
            ends=_convert_orcid_date(orcid_affiliation.get('end-date') or {}),
            restricted=orcid_affiliation['visibility'] != VISIBILITY_PUBLIC,
        )
        profile.add_affiliation(affiliation, index)
        found_affiliation_ids.add(affiliation.id)

    for affiliation in profile.affiliations:
        if affiliation.id not in found_affiliation_ids:
            profile.remove_affiliation(affiliation.id)
Example #2
0
def test_it_can_get_all_affiliations_including_restricted(yesterday):
    address = Address(countries.get('gb'), 'City')

    affiliation = Affiliation('1',
                              address=address,
                              organisation='Org',
                              starts=yesterday)
    affiliation2 = Affiliation('2',
                               address=address,
                               organisation='Org',
                               starts=yesterday,
                               restricted=True)
    affiliation3 = Affiliation('3',
                               address=address,
                               organisation='Org',
                               starts=yesterday)

    profile = Profile('12345678', Name('foo'), '0000-0002-1825-0097')

    profile.add_affiliation(affiliation, position=1)
    profile.add_affiliation(affiliation2, position=0)
    profile.add_affiliation(affiliation3, position=2)

    affiliations = profile.get_affiliations(include_restricted=True)

    assert len(affiliations) == 3
Example #3
0
def test_it_does_not_return_null_values_in_response(
        test_client: FlaskClient, yesterday: Date,
        commit: Callable[[], None]) -> None:

    address = Address(country=countries.get('gb'), city='City',
                      region=None)  # produces null value when serialized
    affiliation = Affiliation(
        '2',
        address=address,
        organisation='Org',
        department=None,  # produces null value when serialized
        starts=yesterday,
        restricted=False)

    profile = Profile('a1b2c3d4', Name('Foo Bar'), '0000-0002-1825-0097')
    profile.add_email_address('*****@*****.**', restricted=False)
    profile.add_affiliation(affiliation)

    db.session.add(profile)
    commit()

    headers = Headers()
    headers.set('X-Consumer-Groups',
                'View-restricted-profiles, Something else')
    response = test_client.get('/profiles/a1b2c3d4', headers=headers)
    data = json.loads(response.data.decode('UTF-8'))

    assert response.status_code == 200, response.status_code
    assert response.headers.get(
        'Content-Type') == 'application/vnd.elife.profile+json;version=1'
    assert validate_json(data, schema_name='profile.v1') is True
    assert contains_none_values(data) is False
Example #4
0
def test_it_does_not_return_restricted_data_when_authenticated(
        test_client: FlaskClient, yesterday: Date,
        commit: Callable[[], None]) -> None:

    address = Address(country=countries.get('gb'),
                      city='City',
                      region='Region')
    affiliation = Affiliation('1',
                              address=address,
                              organisation='Org',
                              department='Dep',
                              starts=yesterday,
                              restricted=True)

    profile = Profile('a1b2c3d4', Name('Foo Bar'), '0000-0002-1825-0097')
    profile.add_email_address('*****@*****.**', restricted=True)
    profile.add_affiliation(affiliation)

    db.session.add(profile)
    commit()

    response = test_client.get('/profiles/a1b2c3d4')
    data = json.loads(response.data.decode('UTF-8'))

    assert response.status_code == 200
    assert response.headers.get(
        'Content-Type') == 'application/vnd.elife.profile+json;version=1'
    assert validate_json(data, schema_name='profile.v1') is True
    assert not data['affiliations']
    assert not data['emailAddresses']
Example #5
0
def test_get_profile_response_contains_affiliations(
        test_client: FlaskClient, yesterday, commit: Callable[[],
                                                              None]) -> None:
    address = Address(country=countries.get('gb'),
                      city='City',
                      region='Region')
    affiliation = Affiliation('1',
                              address=address,
                              organisation='Org',
                              department='Dep',
                              starts=yesterday)

    profile = Profile('a1b2c3d4', Name('Foo Bar'), '0000-0002-1825-0097')

    db.session.add(profile)
    profile.add_affiliation(affiliation)

    commit()

    response = test_client.get('/profiles/a1b2c3d4')
    data = json.loads(response.data.decode('UTF-8'))

    assert response.status_code == 200
    assert response.headers.get(
        'Content-Type') == 'application/vnd.elife.profile+json;version=1'
    assert validate_json(data, schema_name='profile.v1') is True
    assert len(data['affiliations']) == 1
Example #6
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
Example #7
0
def test_it_will_send_event_for_affiliation_insert(mock_publisher: MagicMock, profile: Profile,
                                                   session: scoped_session,
                                                   commit: Callable[[], None]) -> None:
    event_publisher = send_update_events(publisher=mock_publisher)
    models_committed.connect(receiver=event_publisher)

    affiliation = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017))

    profile.add_affiliation(affiliation)
    session.add(profile)

    commit()

    assert mock_publisher.publish.call_count == 1
    assert mock_publisher.publish.call_args[0][0] == {'id': '12345678', 'type': 'profile'}
Example #8
0
def test_exception_is_handled_by_catch_exception_decorator(mock_publisher: MagicMock,
                                                           profile: Profile,
                                                           session: scoped_session,
                                                           commit: Callable[[], None]) -> None:
    mock_publisher.publish.side_effect = Exception('Some Exception')

    event_publisher = send_update_events(publisher=mock_publisher)
    models_committed.connect(receiver=event_publisher)

    affiliation = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017))

    profile.add_affiliation(affiliation)
    session.add(profile)

    commit()

    assert mock_publisher.publish.call_count == 1
Example #9
0
def test_it_can_have_affiliations():
    profile = Profile('12345678', Name('foo'), '0000-0002-1825-0097')
    affiliation = Affiliation('1', Address(countries.get('gb'), 'City'),
                              'Organisation', Date(2017))

    assert len(profile.affiliations) == 0

    profile.add_affiliation(affiliation)

    assert len(profile.affiliations) == 1

    profile.remove_affiliation(affiliation.id)

    assert len(profile.affiliations) == 0

    with pytest.raises(AffiliationNotFound):
        profile.get_affiliation('1')
Example #10
0
def test_it_normalizes_profile_with_an_affiliation(yesterday):
    address = Address(countries.get('gb'), 'City', 'Region')
    affiliation = Affiliation('1',
                              address=address,
                              organisation='Org',
                              department='Dep',
                              starts=yesterday)
    profile = Profile('12345678', Name('Foo Bar', 'Bar, Foo'))

    profile.add_affiliation(affiliation)

    assert normalize(profile) == {
        'id':
        '12345678',
        'name': {
            'preferred': 'Foo Bar',
            'index': 'Bar, Foo',
        },
        'emailAddresses': [],
        'affiliations': [{
            'access': 'public',
            'value': {
                "name": ["Dep", "Org"],
                "address": {
                    "formatted": [
                        "City", "Region",
                        "United Kingdom of Great Britain and Northern Ireland"
                    ],
                    "components": {
                        "locality": ["City"],
                        "area": ["Region"],
                        "country":
                        "United Kingdom of Great Britain and Northern Ireland"
                    }
                }
            },
        }]
    }
Example #11
0
def test_it_can_get_all_affiliations_including_non_current(
        tomorrow, yesterday):
    address = Address(countries.get('gb'), 'City')

    affiliation1 = Affiliation('1',
                               address=address,
                               organisation='Org',
                               starts=yesterday)
    affiliation2 = Affiliation('2',
                               address=address,
                               organisation='Org',
                               starts=yesterday,
                               ends=tomorrow)
    affiliation3 = Affiliation('3',
                               address=address,
                               organisation='Org',
                               starts=tomorrow)
    affiliation4 = Affiliation('4',
                               address=address,
                               organisation='Org',
                               starts=yesterday,
                               ends=yesterday)

    profile = Profile('12345678', Name('foo'), '0000-0002-1825-0097')

    # current affiliations
    profile.add_affiliation(affiliation1)
    profile.add_affiliation(affiliation2)

    # future affiliation, should be returned when requesting 'all' affiliations
    profile.add_affiliation(affiliation3)

    # past affiliation, should be returned when requesting 'all' affiliations
    profile.add_affiliation(affiliation4)

    affiliations = profile.get_affiliations(current_only=False)

    assert len(affiliations) == 4
    assert affiliations[0] == affiliation4
    assert affiliations[1] == affiliation3
    assert affiliations[2] == affiliation2
    assert affiliations[3] == affiliation1
Example #12
0
def test_it_can_get_current_affiliations(tomorrow, yesterday):
    address = Address(countries.get('gb'), 'City')

    affiliation1 = Affiliation('1',
                               address=address,
                               organisation='Org',
                               starts=yesterday)
    affiliation2 = Affiliation('2',
                               address=address,
                               organisation='Org',
                               starts=yesterday,
                               ends=tomorrow)
    affiliation3 = Affiliation('3',
                               address=address,
                               organisation='Org',
                               starts=tomorrow)
    affiliation4 = Affiliation('4',
                               address=address,
                               organisation='Org',
                               starts=yesterday,
                               ends=yesterday)

    profile = Profile('12345678', Name('foo'), '0000-0002-1825-0097')

    # current affiliations
    profile.add_affiliation(affiliation1)
    profile.add_affiliation(affiliation2)

    # future affiliation, should not be returned with current affiliations
    profile.add_affiliation(affiliation3)

    # past affiliation, should not be returned with current affiliations
    profile.add_affiliation(affiliation4)

    affiliations = profile.get_affiliations()

    assert len(affiliations) == 2
    assert affiliations[0] == affiliation2
    assert affiliations[1] == affiliation1
Example #13
0
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