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
def test_it_can_detect_if_current_without_ends_date(yesterday): address = Address(countries.get('gb'), 'City') affiliation = Affiliation('1', address=address, organisation='Org', starts=yesterday) assert affiliation.is_current() is True
def test_it_can_detect_if_not_current_with_future_starts_date_and_no_ends_date( tomorrow): address = Address(countries.get('gb'), 'City') affiliation = Affiliation('1', address=address, organisation='Org', starts=tomorrow) assert affiliation.is_current() is False
def test_it_can_detect_if_not_current_with_no_starts_date_and_past_ends_date( yesterday): address = Address(countries.get('gb'), 'City') affiliation = Affiliation('1', address=address, organisation='Org', ends=yesterday) assert affiliation.is_current() is False
def test_it_may_have_an_end_date(tomorrow, yesterday): has = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', yesterday, ends=tomorrow) has_not = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', yesterday) assert has.ends == tomorrow assert has_not.ends is None
def test_it_may_be_restricted(): has = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017), restricted=True) has_not = Affiliation('2', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017)) assert has.restricted is True assert has_not.restricted is False
def test_it_may_have_a_department(): has = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017), department='Department') has_not = Affiliation('2', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017)) assert has.department == 'Department' assert has_not.department is None
def handle_noargs(self, **options): tribe = Tribe(name =u'Biblnet', slug =u'biblnet', creator = superuser, description = u'Everybodys favorite tribe', private = False ) tribe.save() tm = TribeMember.objects.create(tribe=tribe, user=superuser, moderator=True) tm.save() for trie in TRIBES: tribe = Tribe(name = trie['name'], slug = trie['slug'], creator = trie['creator'], description = trie['description'], private = trie['private'] ) tribe.save() tm = TribeMember.objects.create(tribe=tribe, user=trie['creator'], moderator=True) tm.save() tribe_cal = Calendar.objects.get_or_create_calendar_for_object(tribe, name = "%s" % tribe,) tribe_cal.slug = tribe.slug tribe_cal.save() icon = "images/affiliateicons/%s" % trie[u'icon'] af = Affiliation(affiliation=trie['name'], slug=trie['slug'], icon=icon, tribe=tribe) af.save() print "Affiliations and Tribes created" for occ in OCCUPATION: icon = "images/occupationicons/%s" % occ[u'icon'] Occupation(occupation = occ[u'occupation'], icon=icon, slug=occ[u'slug']).save() print "Occupations created" from schedule.models import Rule try: rule = Rule.objects.get(name="Daily") except Rule.DoesNotExist: rule = Rule(frequency = "YEARLY", name = "Yearly", description = "will recur once every Year") rule.save() print "YEARLY recurrence created" rule = Rule(frequency = "MONTHLY", name = "Monthly", description = "will recur once every Month") rule.save() print "Monthly recurrence created" rule = Rule(frequency = "WEEKLY", name = "Weekly", description = "will recur once every Week") rule.save() print "Weekly recurrence created" rule = Rule(frequency = "DAILY", name = "Daily", description = "will recur once every Day") rule.save() print "Daily recurrence created" print "Rules installed."
def test_it_normalizes_affiliation(yesterday, restricted): address = Address(countries.get('gb'), 'City', 'Region') affiliation = Affiliation('1', address=address, organisation='Org', department='Dep', starts=yesterday, restricted=restricted) assert normalize(affiliation) == { 'access': 'restricted' if restricted else '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" } } }, }
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)
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
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']
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
def handle_noargs(self, **options): for trie in TRIBES: tribe = Tribe(name = trie['name'], slug = trie['slug'], creator = trie['creator'], description = trie['description'], private = trie['private'] ) tribe.save() tm = TribeMember.objects.create(tribe=tribe, user=trie['creator'], moderator=True) tm.save() tribe_cal = Calendar.objects.get_or_create_calendar_for_object(tribe, name = "%s" % tribe,) tribe_cal.slug = tribe.slug tribe_cal.save() af = Affiliation(affiliation=trie['name'], slug=trie['slug'], icon=trie['icon'], tribe=tribe) af.save() for occ in OCCUPATION: Occupation(occupation = occ[u'occupation'], icon=occ[u'icon'], slug=occ[u'slug']).save()
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_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
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
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'}
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')
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
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" } } }, }] }
def post(self, request): attrs = json.loads(request.body) aff = Affiliation() return self.update_attrs(request, aff, attrs)
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_has_an_organisation(): affiliation = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017)) assert affiliation.organisation == 'Organisation'
def test_it_has_a_start_date(yesterday): affiliation = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', yesterday) assert affiliation.starts == yesterday
def test_it_can_be_printed(): affiliation = Affiliation('1', Address(countries.get('gb'), 'City'), 'Organisation', Date(2017)) assert repr(affiliation) == "<Affiliation '1'>"