def test_cursor_pagination(factory, endpoint, api_client, monkeypatch): """ Test if pagination behaves as expected """ page_size = 2 monkeypatch.setattr( 'datahub.activity_stream.pagination.ActivityCursorPagination.page_size', page_size, ) interactions = factory.create_batch(page_size + 1) response = hawk.get(api_client, get_url(endpoint)) assert response.status_code == status.HTTP_200_OK page_1_data = response.json() page_2_url = page_1_data['next'] assert len(page_1_data['orderedItems']) == page_size response = hawk.get(api_client, page_2_url) page_2_data = response.json() page_1_url = page_2_data['previous'] assert len(page_2_data['orderedItems']) == len(interactions) - page_size response = hawk.get(api_client, page_1_url) page_1_data_2 = response.json() assert page_1_data['orderedItems'] == page_1_data_2['orderedItems'] assert page_1_data_2['next'] == page_2_url
def test_investor_profiles_ordering(api_client): """ Test that the investor profiles are ordered by ('modified_on', 'pk') """ investor_profiles = [] with freeze_time() as frozen_datetime: investor_profiles += LargeCapitalInvestorProfileFactory.create_batch(2) frozen_datetime.tick(datetime.timedelta(microseconds=1)) investor_profiles += LargeCapitalInvestorProfileFactory.create_batch(8) frozen_datetime.tick(datetime.timedelta(seconds=1, microseconds=1)) response = hawk.get( api_client, get_url('api-v3:activity-stream:large-capital-investor-profiles'), ) assert response.status_code == status.HTTP_200_OK sorted_investor_profile_ids = [ f'dit:DataHubLargeCapitalInvestorProfile:{obj.pk}' for obj in sorted(investor_profiles, key=lambda obj: (obj.modified_on, obj.pk)) ] response_investor_profile_ids = [ item['object']['id'] for item in response.json()['orderedItems'] ] assert sorted_investor_profile_ids == response_investor_profile_ids
def test_company_referrals_ordering(api_client): """ Test that the company referrals are ordered by ('modified_on', 'pk') """ company_referrals = [] with freeze_time() as frozen_datetime: company_referrals += CompanyReferralFactory.create_batch(2) frozen_datetime.tick(datetime.timedelta(microseconds=1)) company_referrals += CompanyReferralFactory.create_batch(8) frozen_datetime.tick(datetime.timedelta(seconds=1, microseconds=1)) response = hawk.get( api_client, get_url('api-v3:activity-stream:company-referrals')) assert response.status_code == status.HTTP_200_OK sorted_company_referral_ids = [ f'dit:DataHubCompanyReferral:{obj.pk}' for obj in sorted(company_referrals, key=lambda obj: (obj.modified_on, obj.pk)) ] response_company_referral_ids = [ item['object']['id'] for item in response.json()['orderedItems'] ] assert sorted_company_referral_ids == response_company_referral_ids
def test_null_team(api_client): """ Test that we can handle dit_participant.team being None """ interaction = EventServiceDeliveryFactory(dit_participants=[]) InteractionDITParticipantFactory( interaction=interaction, team=None, ) response = hawk.get(api_client, get_url('api-v3:activity-stream:interactions')) assert response.status_code == status.HTTP_200_OK
def test_null_adviser(api_client): """ Test that we can handle dit_participant.adviser being None """ interaction = CompanyInteractionFactory(dit_participants=[]) InteractionDITParticipantFactory( interaction=interaction, adviser=None, team=TeamFactory(), ) response = hawk.get(api_client, get_url('api-v3:activity-stream:interactions')) assert response.status_code == status.HTTP_200_OK
def test_null_adviser(api_client): """ Test that we can handle dit_participant.adviser being None """ with freeze_time() as frozen_datetime: interaction = CompanyInteractionFactory(dit_participants=[]) InteractionDITParticipantFactory( interaction=interaction, adviser=None, team=TeamFactory(), ) frozen_datetime.tick(datetime.timedelta(seconds=1, microseconds=1)) response = hawk.get(api_client, get_url('api-v3:activity-stream:interactions')) assert response.status_code == status.HTTP_200_OK
def test_dit_participant_ordering(api_client): """ Test that dit_participants are ordered by `pk` """ interaction = CompanyInteractionFactory(dit_participants=[]) InteractionDITParticipantFactory.create_batch(5, interaction=interaction) response = hawk.get(api_client, get_url('api-v3:activity-stream:interactions')) assert response.status_code == status.HTTP_200_OK sorted_participant_ids = [ f'dit:DataHubAdviser:{participant.adviser.pk}' for participant in sorted(interaction.dit_participants.all(), key=lambda obj: obj.pk) ] items = response.json()['orderedItems'][0]['object']['attributedTo'] response_participant_ids = [ item['id'] for item in items if item['type'] == ['Person', 'dit:Adviser'] ] assert sorted_participant_ids == response_participant_ids
def test_contacts_ordering(api_client): """ Test that contacts are ordered by `pk` """ contacts = ContactFactory.create_batch(5) CompanyInteractionFactory(contacts=contacts) response = hawk.get(api_client, get_url('api-v3:activity-stream:interactions')) assert response.status_code == status.HTTP_200_OK sorted_contact_ids = [ f'dit:DataHubContact:{contact.pk}' for contact in sorted(contacts, key=lambda obj: obj.pk) ] items = response.json()['orderedItems'][0]['object']['attributedTo'] response_contact_ids = [ item['id'] for item in items if item['type'] == ['Person', 'dit:Contact'] ] assert sorted_contact_ids == response_contact_ids
def test_interaction_ordering(api_client): """ Test that the interactions are ordered by ('modified_on', 'pk') """ interactions = [] # We create 2 interactions with the same modified_on time with freeze_time(): interactions += CompanyInteractionFactory.create_batch(2) interactions += CompanyInteractionFactory.create_batch(8) response = hawk.get(api_client, get_url('api-v3:activity-stream:interactions')) assert response.status_code == status.HTTP_200_OK sorted_interaction_ids = [ f'dit:DataHubInteraction:{obj.pk}' for obj in sorted(interactions, key=lambda obj: (obj.modified_on, obj.pk)) ] response_interaction_ids = [ item['object']['id'] for item in response.json()['orderedItems'] ] assert sorted_interaction_ids == response_interaction_ids
def test_company_referral_activity_without_team_and_contact(api_client): """ Get a list of company referrals and test the returned JSON is valid as per: https://www.w3.org/TR/activitystreams-core/ """ start = datetime.datetime(year=2012, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(start) as frozen_datetime: recipient = AdviserFactory(dit_team=None) company_referral = CompanyReferralFactory(recipient=recipient, contact=None) frozen_datetime.tick(datetime.timedelta(seconds=1, microseconds=1)) response = hawk.get( api_client, get_url('api-v3:activity-stream:company-referrals')) assert response.status_code == status.HTTP_200_OK assert response.json() == { '@context': 'https://www.w3.org/ns/activitystreams', 'summary': 'Company Referral Activities', 'type': 'OrderedCollectionPage', 'next': 'http://testserver/v3/activity-stream/company-referral' + '?cursor=2012-07-12T15%3A06%3A03.000000%2B00%3A00' + f'&cursor={str(company_referral.id)}', 'orderedItems': [ { 'id': f'dit:DataHubCompanyReferral:{company_referral.id}:Announce', 'type': 'Announce', 'published': format_date_or_datetime(company_referral.modified_on), 'generator': { 'name': 'dit:dataHub', 'type': 'Application' }, 'object': { 'id': f'dit:DataHubCompanyReferral:{company_referral.id}', 'type': ['dit:CompanyReferral'], 'startTime': format_date_or_datetime(company_referral.created_on), 'dit:subject': company_referral.subject, 'dit:status': str(company_referral.status), 'attributedTo': [ { 'id': f'dit:DataHubCompany:{company_referral.company.pk}', 'dit:dunsNumber': company_referral.company.duns_number, 'dit:companiesHouseNumber': company_referral.company.company_number, 'type': ['Organization', 'dit:Company'], 'name': company_referral.company.name, }, { 'id': f'dit:DataHubAdviser:{company_referral.created_by.pk}', 'type': ['Person', 'dit:Adviser'], 'dit:emailAddress': company_referral.created_by.contact_email or company_referral.created_by.email, 'name': company_referral.created_by.name, 'dit:team': { 'id': f'dit:DataHubTeam:{company_referral.created_by.dit_team.pk}', 'type': ['Group', 'dit:Team'], 'name': company_referral.created_by.dit_team.name, }, 'dit:DataHubCompanyReferral:role': 'sender', }, { 'id': f'dit:DataHubAdviser:{company_referral.recipient.pk}', 'type': ['Person', 'dit:Adviser'], 'dit:emailAddress': company_referral.recipient.contact_email or company_referral.recipient.email, 'name': company_referral.recipient.name, 'dit:DataHubCompanyReferral:role': 'recipient', }, ], 'url': company_referral.get_absolute_url(), }, }, ], }
def test_investment_project_added_with_gva(api_client): """ This test adds the necessary fields to compute gross_value_added property and tests if its included in the response. """ project = InvestmentProjectFactory( foreign_equity_investment=10000, sector_id=constants.Sector.aerospace_assembly_aircraft.value.id, investment_type_id=constants.InvestmentType.fdi.value.id, ) response = hawk.get( api_client, get_url('api-v3:activity-stream:investment-project-added')) assert response.status_code == status.HTTP_200_OK assert response.json() == { '@context': 'https://www.w3.org/ns/activitystreams', 'summary': 'Investment Activities Added', 'type': 'OrderedCollectionPage', 'id': 'http://testserver/v3/activity-stream/investment/project-added', 'partOf': 'http://testserver/v3/activity-stream/investment/project-added', 'previous': None, 'next': None, 'orderedItems': [ { 'id': f'dit:DataHubInvestmentProject:{project.id}:Add', 'type': 'Add', 'published': format_date_or_datetime(project.created_on), 'generator': { 'name': 'dit:dataHub', 'type': 'Application' }, 'actor': { 'id': f'dit:DataHubAdviser:{project.created_by.pk}', 'type': ['Person', 'dit:Adviser'], 'dit:emailAddress': project.created_by.contact_email or project.created_by.email, 'name': project.created_by.name, }, 'object': { 'id': f'dit:DataHubInvestmentProject:{project.id}', 'type': ['dit:InvestmentProject'], 'startTime': format_date_or_datetime(project.created_on), 'name': project.name, 'dit:investmentType': { 'name': project.investment_type.name, }, 'dit:estimatedLandDate': format_date_or_datetime(project.estimated_land_date, ), 'dit:foreignEquityInvestment': 10000.0, 'dit:grossValueAdded': 581.0, 'attributedTo': [ { 'id': f'dit:DataHubCompany:{project.investor_company.pk}', 'dit:dunsNumber': project.investor_company.duns_number, 'dit:companiesHouseNumber': project.investor_company.company_number, 'type': ['Organization', 'dit:Company'], 'name': project.investor_company.name, }, *[{ 'id': f'dit:DataHubContact:{contact.pk}', 'type': ['Person', 'dit:Contact'], 'url': contact.get_absolute_url(), 'dit:emailAddress': contact.email, 'dit:jobTitle': contact.job_title, 'name': contact.name, } for contact in project.client_contacts.order_by('pk') ], ], 'url': project.get_absolute_url(), }, }, ], }
def test_investment_project_verify_win_added(api_client): """ Get a list of investment project and test the returned JSON is valid as per: https://www.w3.org/TR/activitystreams-core/ Investment Project with verified win will have fields such as totalInvestment, numberNewJobs and foreignEquityInvestment. """ project = VerifyWinInvestmentProjectFactory() response = hawk.get( api_client, get_url('api-v3:activity-stream:investment-project-added')) assert response.status_code == status.HTTP_200_OK assert response.json() == { '@context': 'https://www.w3.org/ns/activitystreams', 'summary': 'Investment Activities Added', 'type': 'OrderedCollectionPage', 'id': 'http://testserver/v3/activity-stream/investment/project-added', 'partOf': 'http://testserver/v3/activity-stream/investment/project-added', 'previous': None, 'next': None, 'orderedItems': [ { 'id': f'dit:DataHubInvestmentProject:{project.id}:Add', 'type': 'Add', 'published': format_date_or_datetime(project.created_on), 'generator': { 'name': 'dit:dataHub', 'type': 'Application' }, 'actor': { 'id': f'dit:DataHubAdviser:{project.created_by.pk}', 'type': ['Person', 'dit:Adviser'], 'dit:emailAddress': project.created_by.contact_email or project.created_by.email, 'name': project.created_by.name, }, 'object': { 'id': f'dit:DataHubInvestmentProject:{project.id}', 'type': ['dit:InvestmentProject'], 'startTime': format_date_or_datetime(project.created_on), 'name': project.name, 'dit:investmentType': { 'name': project.investment_type.name, }, 'dit:estimatedLandDate': format_date_or_datetime(project.estimated_land_date, ), 'dit:totalInvestment': project.total_investment, 'dit:foreignEquityInvestment': project.foreign_equity_investment, 'dit:numberNewJobs': project.number_new_jobs, 'attributedTo': [ { 'id': f'dit:DataHubCompany:{project.investor_company.pk}', 'dit:dunsNumber': project.investor_company.duns_number, 'dit:companiesHouseNumber': project.investor_company.company_number, 'type': ['Organization', 'dit:Company'], 'name': project.investor_company.name, }, *[{ 'id': f'dit:DataHubContact:{contact.pk}', 'type': ['Person', 'dit:Contact'], 'url': contact.get_absolute_url(), 'dit:emailAddress': contact.email, 'dit:jobTitle': contact.job_title, 'name': contact.name, } for contact in project.client_contacts.order_by('pk') ], ], 'url': project.get_absolute_url(), }, }, ], }
def test_omis_order_added_activity(api_client, order_overrides): """ Get a list of OMIS Orders added and test the JSON returned is valid as per: https://www.w3.org/TR/activitystreams-core/ """ order = OrderFactory(**order_overrides) response = hawk.get(api_client, get_url('api-v3:activity-stream:omis-order-added')) assert response.status_code == status.HTTP_200_OK expected_data = { '@context': 'https://www.w3.org/ns/activitystreams', 'summary': 'OMIS Order Added Activity', 'type': 'OrderedCollectionPage', 'id': 'http://testserver/v3/activity-stream/omis/order-added', 'partOf': 'http://testserver/v3/activity-stream/omis/order-added', 'previous': None, 'next': None, 'orderedItems': [ { 'id': f'dit:DataHubOMISOrder:{order.id}:Add', 'type': 'Add', 'published': format_date_or_datetime(order.created_on), 'generator': {'name': 'dit:dataHub', 'type': 'Application'}, 'object': { 'id': f'dit:DataHubOMISOrder:{order.id}', 'type': ['dit:OMISOrder'], 'startTime': format_date_or_datetime(order.created_on), 'name': order.reference, 'attributedTo': [ { 'id': f'dit:DataHubCompany:{order.company.pk}', 'dit:dunsNumber': order.company.duns_number, 'dit:companiesHouseNumber': order.company.company_number, 'type': ['Organization', 'dit:Company'], 'name': order.company.name, }, { 'id': f'dit:DataHubContact:{order.contact.pk}', 'type': ['Person', 'dit:Contact'], 'url': order.contact.get_absolute_url(), 'dit:emailAddress': order.contact.email, 'dit:jobTitle': order.contact.job_title, 'name': order.contact.name, }, ], 'url': order.get_absolute_url(), }, }, ], } if 'created_by_id' not in order_overrides: expected_data['orderedItems'][0]['actor'] = { 'id': f'dit:DataHubAdviser:{order.created_by.pk}', 'type': ['Person', 'dit:Adviser'], 'dit:emailAddress': order.created_by.contact_email or order.created_by.email, 'name': order.created_by.name, } if 'primary_market_id' not in order_overrides: expected_data['orderedItems'][0]['object']['dit:country'] = { 'name': order.primary_market.name, } if 'uk_region_id' not in order_overrides: expected_data['orderedItems'][0]['object']['dit:ukRegion'] = { 'name': order.uk_region.name, } assert response.json() == expected_data
def _get_response(api_client): with freeze_time() as frozen_datetime: frozen_datetime.tick(datetime.timedelta(seconds=1, microseconds=1)) response = hawk.get(api_client, get_url('api-v3:activity-stream:events')) return response
def test_interaction_investment_project_activity(api_client): """ Get a list of interactions and test the returned JSON is valid as per: https://www.w3.org/TR/activitystreams-core/ """ interaction = InvestmentProjectInteractionFactory() project = interaction.investment_project response = hawk.get(api_client, get_url('api-v3:activity-stream:interactions')) assert response.status_code == status.HTTP_200_OK assert response.json() == { '@context': 'https://www.w3.org/ns/activitystreams', 'summary': 'Interaction Activities', 'type': 'OrderedCollectionPage', 'id': 'http://testserver/v3/activity-stream/interaction', 'partOf': 'http://testserver/v3/activity-stream/interaction', 'previous': None, 'next': None, 'orderedItems': [ { 'id': f'dit:DataHubInteraction:{interaction.id}:Announce', 'type': 'Announce', 'published': format_date_or_datetime(interaction.created_on), 'generator': {'name': 'dit:dataHub', 'type': 'Application'}, 'object': { 'id': f'dit:DataHubInteraction:{interaction.id}', 'type': ['dit:Event', 'dit:Interaction'], 'startTime': format_date_or_datetime(interaction.date), 'dit:status': interaction.status, 'dit:archived': interaction.archived, 'dit:communicationChannel': {'name': interaction.communication_channel.name}, 'dit:subject': interaction.subject, 'dit:service': {'name': interaction.service.name}, 'attributedTo': [ { 'id': f'dit:DataHubCompany:{interaction.company.pk}', 'dit:dunsNumber': interaction.company.duns_number, 'dit:companiesHouseNumber': interaction.company.company_number, 'type': ['Organization', 'dit:Company'], 'name': interaction.company.name, }, *[ { 'id': f'dit:DataHubAdviser:{participant.adviser.pk}', 'type': ['Person', 'dit:Adviser'], 'dit:emailAddress': participant.adviser.contact_email or participant.adviser.email, 'name': participant.adviser.name, 'dit:team': { 'id': f'dit:DataHubTeam:{participant.team.pk}', 'type': ['Group', 'dit:Team'], 'name': participant.team.name, }, } for participant in interaction.dit_participants.order_by('pk') ], *[ { 'id': f'dit:DataHubContact:{contact.pk}', 'type': ['Person', 'dit:Contact'], 'url': contact.get_absolute_url(), 'dit:emailAddress': contact.email, 'dit:jobTitle': contact.job_title, 'name': contact.name, } for contact in interaction.contacts.order_by('pk') ], ], 'url': interaction.get_absolute_url(), 'context': [ { 'id': f'dit:DataHubInvestmentProject:{project.pk}', 'name': project.name, 'type': 'dit:InvestmentProject', 'url': project.get_absolute_url(), }, ], }, }, ], }
def test_event_activity(api_client): """ Get a list of Events and test the returned JSON is valid """ start = datetime.datetime(year=2012, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(start) as frozen_datetime: event = EventFactory() frozen_datetime.tick(datetime.timedelta(seconds=1, microseconds=1)) response = hawk.get(api_client, get_url('api-v3:activity-stream:events')) assert response.status_code == status.HTTP_200_OK assert response.json() == { '@context': 'https://www.w3.org/ns/activitystreams', 'summary': 'Event', 'type': 'OrderedCollectionPage', 'next': 'http://testserver/v3/activity-stream/event' + '?cursor=2012-07-12T15%3A06%3A03.000000%2B00%3A00' + f'&cursor={str(event.id)}', 'orderedItems': [ { 'id': f'dit:DataHubEvent:{event.id}:Announce', 'type': 'Announce', 'published': format_date_or_datetime(event.created_on), 'generator': { 'name': 'dit:dataHub', 'type': 'Application' }, 'object': { 'id': f'dit:DataHubEvent:{event.id}', 'type': [ 'dit:dataHub:Event', ], 'name': event.name, 'dit:eventType': { 'name': event.event_type.name }, 'content': event.notes, 'startTime': format_date_or_datetime(event.start_date), 'endTime': format_date_or_datetime(event.end_date), 'url': event.get_absolute_url(), 'dit:locationType': { 'name': event.location_type.name }, 'dit:address_1': event.address_1, 'dit:address_2': event.address_2, 'dit:address_town': event.address_town, 'dit:address_county': event.address_county, 'dit:address_postcode': event.address_postcode, 'dit:address_country': { 'name': event.address_country.name }, 'dit:leadTeam': { 'name': event.lead_team.name }, 'dit:organiser': { 'name': event.organiser.name }, 'dit:disabledOn': event.disabled_on, 'dit:service': { 'name': event.service.name }, 'dit:archivedDocumentsUrlPath': event.archived_documents_url_path, 'dit:ukRegion': { 'name': event.uk_region.name }, 'dit:teams': [ *[{ 'id': f'dit:DataHubTeam:{team.pk}', 'type': ['Group', 'dit:Team'], 'name': team.name, } for team in event.teams.order_by('pk')], ], 'dit:relatedProgrammes': [ *[{ 'id': f'dit:DataHubEventProgramme:{programme.pk}', 'name': programme.name, } for programme in event.related_programmes.order_by('pk')], ], 'dit:hasRelatedTradeAgreements': event.has_related_trade_agreements, 'dit:relatedTradeAgreements': [ *[{ 'id': f'dit:DataHubTradeAgreement:{trade_agreement.pk}', 'name': trade_agreement.name, } for trade_agreement in event.related_trade_agreements.order_by('pk')], ], }, }, ], }
def test_investment_project_added(api_client): """ Get a list of investment project and test the returned JSON is valid as per: https://www.w3.org/TR/activitystreams-core/ """ start = datetime.datetime(year=2012, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(start) as frozen_datetime: project = InvestmentProjectFactory() frozen_datetime.tick(datetime.timedelta(seconds=1, microseconds=1)) response = hawk.get( api_client, get_url('api-v3:activity-stream:investment-project-added')) assert response.status_code == status.HTTP_200_OK assert response.json() == { '@context': 'https://www.w3.org/ns/activitystreams', 'summary': 'Investment Activities Added', 'type': 'OrderedCollectionPage', 'next': 'http://testserver/v3/activity-stream/investment/project-added' + '?cursor=2012-07-12T15%3A06%3A03.000000%2B00%3A00' + f'&cursor={str(project.id)}', 'orderedItems': [ { 'id': f'dit:DataHubInvestmentProject:{project.id}:Add', 'type': 'Add', 'published': format_date_or_datetime(project.created_on), 'generator': { 'name': 'dit:dataHub', 'type': 'Application' }, 'actor': { 'id': f'dit:DataHubAdviser:{project.created_by.pk}', 'type': ['Person', 'dit:Adviser'], 'dit:emailAddress': project.created_by.contact_email or project.created_by.email, 'name': project.created_by.name, }, 'object': { 'id': f'dit:DataHubInvestmentProject:{project.id}', 'type': ['dit:InvestmentProject'], 'startTime': format_date_or_datetime(project.created_on), 'name': project.name, 'dit:investmentType': { 'name': project.investment_type.name, }, 'dit:estimatedLandDate': format_date_or_datetime(project.estimated_land_date, ), 'attributedTo': [ { 'id': f'dit:DataHubCompany:{project.investor_company.pk}', 'dit:dunsNumber': project.investor_company.duns_number, 'dit:companiesHouseNumber': project.investor_company.company_number, 'type': ['Organization', 'dit:Company'], 'name': project.investor_company.name, }, *[{ 'id': f'dit:DataHubContact:{contact.pk}', 'type': ['Person', 'dit:Contact'], 'url': contact.get_absolute_url(), 'dit:emailAddress': contact.email, 'dit:jobTitle': contact.job_title, 'name': contact.name, } for contact in project.client_contacts.order_by('pk') ], ], 'url': project.get_absolute_url(), }, }, ], }
def test_complete_large_capital_investor_profile_activity(api_client): """ Get a list of complete large capital investor profiles and test the returned JSON is valid as per: https://www.w3.org/TR/activitystreams-core/ """ start = datetime.datetime(year=2012, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(start) as frozen_datetime: investor_profile = CompleteLargeCapitalInvestorProfileFactory() frozen_datetime.tick(datetime.timedelta(seconds=1, microseconds=1)) response = hawk.get( api_client, get_url('api-v3:activity-stream:large-capital-investor-profiles'), ) def get_multiple_names(values): return [{'name': value.name} for value in values] assert response.status_code == status.HTTP_200_OK assert response.json() == { '@context': 'https://www.w3.org/ns/activitystreams', 'summary': 'Large Capital Investor Profile Activities', 'type': 'OrderedCollectionPage', 'next': 'http://testserver/v3/activity-stream/investment/large-capital-investor-profiles' + '?cursor=2012-07-12T15%3A06%3A03.000000%2B00%3A00' + f'&cursor={str(investor_profile.id)}', 'orderedItems': [ { 'id': f'dit:DataHubLargeCapitalInvestorProfile:{investor_profile.id}:Announce', 'type': 'Announce', 'published': format_date_or_datetime(investor_profile.modified_on), 'generator': { 'name': 'dit:dataHub', 'type': 'Application' }, 'object': { 'id': f'dit:DataHubLargeCapitalInvestorProfile:{investor_profile.id}', 'type': ['dit:LargeCapitalInvestorProfile'], 'startTime': format_date_or_datetime(investor_profile.created_on), 'attributedTo': [ { 'id': f'dit:DataHubCompany:{investor_profile.investor_company.pk}', 'dit:dunsNumber': investor_profile.investor_company.duns_number, 'dit:companiesHouseNumber': investor_profile.investor_company.company_number, 'type': ['Organization', 'dit:Company'], 'name': investor_profile.investor_company.name, }, { 'id': f'dit:DataHubAdviser:{investor_profile.created_by.pk}', 'type': ['Person', 'dit:Adviser'], 'dit:emailAddress': investor_profile.created_by.contact_email or investor_profile.created_by.email, 'name': investor_profile.created_by.name, 'dit:team': { 'id': f'dit:DataHubTeam:{investor_profile.created_by.dit_team.pk}', 'type': ['Group', 'dit:Team'], 'name': investor_profile.created_by.dit_team.name, }, 'dit:DataHubLargeCapitalInvestorProfile:role': 'creator', }, { 'id': f'dit:DataHubAdviser:{investor_profile.modified_by.pk}', 'type': ['Person', 'dit:Adviser'], 'dit:emailAddress': investor_profile.modified_by.contact_email or investor_profile.modified_by.email, 'name': investor_profile.modified_by.name, 'dit:team': { 'id': f'dit:DataHubTeam:{investor_profile.modified_by.dit_team.pk}', 'type': ['Group', 'dit:Team'], 'name': investor_profile.modified_by.dit_team.name, }, 'dit:DataHubLargeCapitalInvestorProfile:role': 'modifier', }, ], 'url': investor_profile.get_absolute_url(), 'dit:assetClassesOfInterest': get_multiple_names( investor_profile.asset_classes_of_interest.all(), ), 'dit:constructionRisks': get_multiple_names( investor_profile.construction_risks.all(), ), 'dit:countryOfOrigin': { 'name': investor_profile.country_of_origin.name }, 'dit:dealTicketSizes': get_multiple_names( investor_profile.deal_ticket_sizes.all(), ), 'dit:desiredDealRoles': get_multiple_names( investor_profile.desired_deal_roles.all(), ), 'dit:globalAssetsUnderManagement': investor_profile.global_assets_under_management, 'dit:investableCapital': investor_profile.investable_capital, 'dit:investmentTypes': get_multiple_names( investor_profile.investment_types.all(), ), 'dit:investorDescription': investor_profile.investor_description, 'dit:investorType': { 'name': investor_profile.investor_type.name }, 'dit:minimumEquityPercentage': { 'name': investor_profile.minimum_equity_percentage.name, }, 'dit:minimumReturnRate': { 'name': investor_profile.minimum_return_rate.name }, 'dit:notesOnLocations': investor_profile.notes_on_locations, 'dit:otherCountriesBeingConsidered': get_multiple_names( investor_profile.other_countries_being_considered.all( ), ), 'dit:requiredChecksConducted': { 'name': investor_profile.required_checks_conducted.name, }, 'dit:requiredChecksConductedBy': [{ 'id': 'dit:DataHubAdviser:' f'{investor_profile.required_checks_conducted_by.pk}', 'type': ['Person', 'dit:Adviser'], 'dit:emailAddress': investor_profile.required_checks_conducted_by. contact_email or investor_profile.required_checks_conducted_by.email, 'name': investor_profile.required_checks_conducted_by.name, 'dit:team': { 'id': 'dit:DataHubTeam:' f'{investor_profile.required_checks_conducted_by.dit_team.pk}', 'type': ['Group', 'dit:Team'], 'name': investor_profile.required_checks_conducted_by. dit_team.name, }, }], 'dit:requiredChecksConductedOn': investor_profile.required_checks_conducted_on.strftime( '%Y-%m-%d'), 'dit:restrictions': get_multiple_names(investor_profile.restrictions.all(), ), 'dit:timeHorizons': get_multiple_names(investor_profile.time_horizons.all(), ), 'dit:ukRegionLocations': get_multiple_names( investor_profile.uk_region_locations.all(), ), }, }, ], }
def test_cursor_pagination(factory, endpoint, api_client, monkeypatch): """ Test if pagination behaves as expected """ page_size = 2 monkeypatch.setattr( 'datahub.activity_stream.pagination.ActivityCursorPagination.page_size', page_size, ) start = datetime.datetime(year=2012, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(start) as frozen_datetime: interactions = factory.create_batch(page_size + 1) frozen_datetime.tick(datetime.timedelta(seconds=1)) response = hawk.get(api_client, get_url(endpoint)) assert response.status_code == status.HTTP_200_OK page_0_data = response.json() assert len(page_0_data['orderedItems']) == 0 frozen_datetime.tick(datetime.timedelta(microseconds=1)) response = hawk.get(api_client, get_url(endpoint)) assert response.status_code == status.HTTP_200_OK page_1_data = response.json() page_2_url = page_1_data['next'] assert len(page_1_data['orderedItems']) == page_size response = hawk.get(api_client, page_2_url) page_2_data = response.json() page_3_url = page_2_data['next'] assert len( page_2_data['orderedItems']) == len(interactions) - page_size response = hawk.get(api_client, page_3_url) page_3_data = response.json() assert len(page_3_data['orderedItems']) == 0 assert page_3_data['next'] is None interactions = factory.create_batch(1) frozen_datetime.tick(datetime.timedelta(seconds=1, microseconds=1)) response = hawk.get(api_client, page_3_url) page_3_post_update_data = response.json() assert len(page_3_post_update_data['orderedItems']) == 1 page_4_url = page_3_post_update_data['next'] response = hawk.get(api_client, page_4_url) page_4_data = response.json() assert len(page_4_data['orderedItems']) == 0 assert page_4_data['next'] is None # Assert that DRF's cursor works, to not break existing pagination just after deployment now = datetime.datetime.now().isoformat(timespec='microseconds') cursor = base64.b64encode( (f'p={urllib.parse.quote(now)}').encode()).decode() frozen_datetime.tick(datetime.timedelta(microseconds=1)) interactions = factory.create_batch(1) frozen_datetime.tick(datetime.timedelta(seconds=1, microseconds=1)) response = hawk.get(api_client, f'{get_url(endpoint)}?cursor={cursor}') page_drf_data = response.json() assert len(page_drf_data['orderedItems']) == 1
def test_large_capital_investor_profile_activity(api_client): """ Get a list of large capital investor profiles and test the returned JSON is valid as per: https://www.w3.org/TR/activitystreams-core/ """ start = datetime.datetime(year=2012, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(start) as frozen_datetime: investor_profile = LargeCapitalInvestorProfileFactory() frozen_datetime.tick(datetime.timedelta(seconds=1, microseconds=1)) response = hawk.get( api_client, get_url('api-v3:activity-stream:large-capital-investor-profiles'), ) assert response.status_code == status.HTTP_200_OK assert response.json() == { '@context': 'https://www.w3.org/ns/activitystreams', 'summary': 'Large Capital Investor Profile Activities', 'type': 'OrderedCollectionPage', 'next': 'http://testserver/v3/activity-stream/investment/large-capital-investor-profiles' + '?cursor=2012-07-12T15%3A06%3A03.000000%2B00%3A00' + f'&cursor={str(investor_profile.id)}', 'orderedItems': [ { 'id': f'dit:DataHubLargeCapitalInvestorProfile:{investor_profile.id}:Announce', 'type': 'Announce', 'published': format_date_or_datetime(investor_profile.modified_on), 'generator': { 'name': 'dit:dataHub', 'type': 'Application' }, 'object': { 'id': f'dit:DataHubLargeCapitalInvestorProfile:{investor_profile.id}', 'type': ['dit:LargeCapitalInvestorProfile'], 'startTime': format_date_or_datetime(investor_profile.created_on), 'attributedTo': [ { 'id': f'dit:DataHubCompany:{investor_profile.investor_company.pk}', 'dit:dunsNumber': investor_profile.investor_company.duns_number, 'dit:companiesHouseNumber': investor_profile.investor_company.company_number, 'type': ['Organization', 'dit:Company'], 'name': investor_profile.investor_company.name, }, ], 'dit:countryOfOrigin': { 'name': investor_profile.country_of_origin.name }, 'url': investor_profile.get_absolute_url(), }, }, ], }
def test_closed_company_referral_activity(api_client): """ Get a list of closed company referrals and test the returned JSON is valid as per: https://www.w3.org/TR/activitystreams-core/ """ company_referral = ClosedCompanyReferralFactory() response = hawk.get(api_client, get_url('api-v3:activity-stream:company-referrals')) assert response.status_code == status.HTTP_200_OK assert response.json() == { '@context': 'https://www.w3.org/ns/activitystreams', 'summary': 'Company Referral Activities', 'type': 'OrderedCollectionPage', 'id': 'http://testserver/v3/activity-stream/company-referral', 'partOf': 'http://testserver/v3/activity-stream/company-referral', 'previous': None, 'next': None, 'orderedItems': [ { 'id': f'dit:DataHubCompanyReferral:{company_referral.id}:Announce', 'type': 'Announce', 'published': format_date_or_datetime(company_referral.modified_on), 'generator': {'name': 'dit:dataHub', 'type': 'Application'}, 'object': { 'id': f'dit:DataHubCompanyReferral:{company_referral.id}', 'type': ['dit:CompanyReferral'], 'startTime': format_date_or_datetime(company_referral.created_on), 'dit:subject': company_referral.subject, 'dit:status': str(company_referral.status), 'attributedTo': [ { 'id': f'dit:DataHubCompany:{company_referral.company.pk}', 'dit:dunsNumber': company_referral.company.duns_number, 'dit:companiesHouseNumber': company_referral.company.company_number, 'type': ['Organization', 'dit:Company'], 'name': company_referral.company.name, }, { 'id': f'dit:DataHubAdviser:{company_referral.created_by.pk}', 'type': ['Person', 'dit:Adviser'], 'dit:emailAddress': company_referral.created_by.contact_email or company_referral.created_by.email, 'name': company_referral.created_by.name, 'dit:team': { 'id': f'dit:DataHubTeam:{company_referral.created_by.dit_team.pk}', 'type': ['Group', 'dit:Team'], 'name': company_referral.created_by.dit_team.name, }, 'dit:DataHubCompanyReferral:role': 'sender', }, { 'id': f'dit:DataHubAdviser:{company_referral.recipient.pk}', 'type': ['Person', 'dit:Adviser'], 'dit:emailAddress': company_referral.recipient.contact_email or company_referral.recipient.email, 'name': company_referral.recipient.name, 'dit:team': { 'id': f'dit:DataHubTeam:{company_referral.recipient.dit_team.pk}', 'type': ['Group', 'dit:Team'], 'name': company_referral.recipient.dit_team.name, }, 'dit:DataHubCompanyReferral:role': 'recipient', }, { 'id': f'dit:DataHubContact:{company_referral.contact.pk}', 'type': ['Person', 'dit:Contact'], 'url': company_referral.contact.get_absolute_url(), 'dit:emailAddress': company_referral.contact.email, 'dit:jobTitle': company_referral.contact.job_title, 'name': company_referral.contact.name, }, ], 'url': company_referral.get_absolute_url(), }, }, ], }
def test_service_delivery_event_activity(api_client): """ Get a list of interactions and test the returned JSON is valid as per: https://www.w3.org/TR/activitystreams-core/ """ start = datetime.datetime(year=2012, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(start) as frozen_datetime: interaction = EventServiceDeliveryFactory() event = interaction.event frozen_datetime.tick(datetime.timedelta(seconds=1, microseconds=1)) response = hawk.get(api_client, get_url('api-v3:activity-stream:interactions')) assert response.status_code == status.HTTP_200_OK assert response.json() == { '@context': 'https://www.w3.org/ns/activitystreams', 'summary': 'Interaction Activities', 'type': 'OrderedCollectionPage', 'next': 'http://testserver/v3/activity-stream/interaction' + '?cursor=2012-07-12T15%3A06%3A03.000000%2B00%3A00' + f'&cursor={str(interaction.id)}', 'orderedItems': [ { 'id': f'dit:DataHubInteraction:{interaction.id}:Announce', 'type': 'Announce', 'published': format_date_or_datetime(interaction.created_on), 'generator': {'name': 'dit:dataHub', 'type': 'Application'}, 'object': { 'id': f'dit:DataHubInteraction:{interaction.id}', 'type': [ 'dit:Event', 'dit:ServiceDelivery', f'dit:datahub:theme:{interaction.theme}', ], 'content': interaction.notes, 'startTime': format_date_or_datetime(interaction.date), 'dit:status': interaction.status, 'dit:archived': interaction.archived, 'dit:subject': interaction.subject, 'dit:service': {'name': interaction.service.name}, 'attributedTo': [ *[ { 'id': f'dit:DataHubCompany:{company.pk}', 'dit:dunsNumber': company.duns_number, 'dit:companiesHouseNumber': company.company_number, 'type': ['Organization', 'dit:Company'], 'name': company.name, } for company in interaction.companies.order_by('pk') ], *[ { 'id': f'dit:DataHubAdviser:{participant.adviser.pk}', 'type': ['Person', 'dit:Adviser'], 'dit:emailAddress': participant.adviser.contact_email or participant.adviser.email, 'name': participant.adviser.name, 'dit:team': { 'id': f'dit:DataHubTeam:{participant.team.pk}', 'type': ['Group', 'dit:Team'], 'name': participant.team.name, }, } for participant in interaction.dit_participants.order_by('pk') ], *[ { 'id': f'dit:DataHubContact:{contact.pk}', 'type': ['Person', 'dit:Contact'], 'url': contact.get_absolute_url(), 'dit:emailAddress': contact.email, 'dit:jobTitle': contact.job_title, 'name': contact.name, } for contact in interaction.contacts.order_by('pk') ], ], 'url': interaction.get_absolute_url(), 'context': [ { 'id': f'dit:DataHubEvent:{event.pk}', 'type': 'dit:Event', 'dit:eventType': {'name': event.event_type.name}, 'name': interaction.event.name, 'startTime': format_date_or_datetime(event.start_date), 'endTime': format_date_or_datetime(event.end_date), 'dit:team': { 'id': f'dit:DataHubTeam:{interaction.event.lead_team.pk}', 'type': ['Group', 'dit:Team'], 'name': interaction.event.lead_team.name, }, 'url': interaction.event.get_absolute_url(), }, ], }, }, ], }