def test_notify_on_order_cancelled(self): """Test that a notification is triggered when an order is cancelled.""" order = OrderFactory(assignees=[]) OrderAssigneeFactory.create_batch(1, order=order, is_lead=True) OrderSubscriberFactory.create_batch(2, order=order) notify.client.reset_mock() order.cancel(by=AdviserFactory(), reason=CancellationReason.objects.first()) # 1 = customer, 3 = assignees/subscribers assert len( notify.client.send_email_notification.call_args_list) == (3 + 1) templates_called = [ data[1]['template_id'] for data in notify.client.send_email_notification.call_args_list ] assert templates_called == [ Template.order_cancelled_for_customer.value, Template.order_cancelled_for_adviser.value, Template.order_cancelled_for_adviser.value, Template.order_cancelled_for_adviser.value, ]
def test_notify_on_order_paid(self): """Test that a notification is triggered when an order is marked as paid.""" order = OrderWithAcceptedQuoteFactory(assignees=[]) OrderAssigneeFactory.create_batch(1, order=order) OrderSubscriberFactory.create_batch(2, order=order) notify.client.reset_mock() order.mark_as_paid( by=AdviserFactory(), payments_data=[ { 'amount': order.total_cost, 'received_on': dateutil_parse('2017-01-02').date(), }, ], ) # 1 = customer, 3 = assignees/subscribers assert len( notify.client.send_email_notification.call_args_list) == (3 + 1) templates_called = [ data[1]['template_id'] for data in notify.client.send_email_notification.call_args_list ] assert templates_called == [ Template.order_paid_for_customer.value, Template.order_paid_for_adviser.value, Template.order_paid_for_adviser.value, Template.order_paid_for_adviser.value, ]
def test_notify_on_quote_cancelled(self, mocked_notify_client): """Test that a notification is triggered when a quote is cancelled.""" order = OrderWithOpenQuoteFactory(assignees=[]) OrderAssigneeFactory.create_batch(1, order=order) OrderSubscriberFactory.create_batch(2, order=order) mocked_notify_client.reset_mock() order.reopen(by=AdviserFactory()) # 1 = customer, 3 = assignees/subscribers assert len( mocked_notify_client.send_email_notification.call_args_list) == ( 3 + 1) templates_called = [ data[1]['template_id'] for data in mocked_notify_client.send_email_notification.call_args_list ] assert templates_called == [ Template.quote_cancelled_for_customer.value, Template.quote_cancelled_for_adviser.value, Template.quote_cancelled_for_adviser.value, Template.quote_cancelled_for_adviser.value, ]
def setup_data(setup_es): """Sets up data for the tests.""" with freeze_time('2017-01-01 13:00:00'): company = CompanyFactory(name='Mercury trading', alias='Uranus supplies') contact = ContactFactory(company=company, first_name='John', last_name='Doe') order = OrderFactory( reference='abcd', primary_market_id=constants.Country.japan.value.id, uk_region_id=constants.UKRegion.channel_islands.value.id, assignees=[], status=OrderStatus.draft, company=company, contact=contact, discount_value=0, delivery_date=dateutil_parse('2018-01-01').date(), vat_verified=False, ) OrderSubscriberFactory( order=order, adviser=AdviserFactory(dit_team_id=constants.Team.healthcare_uk.value.id), ) OrderAssigneeFactory( order=order, adviser=AdviserFactory(dit_team_id=constants.Team.tees_valley_lep.value.id), estimated_time=60, ) with freeze_time('2017-02-01 13:00:00'): company = CompanyFactory(name='Venus Ltd', alias='Earth outsourcing') contact = ContactFactory(company=company, first_name='Jenny', last_name='Cakeman') order = OrderWithAcceptedQuoteFactory( reference='efgh', primary_market_id=constants.Country.france.value.id, uk_region_id=constants.UKRegion.east_midlands.value.id, assignees=[], status=OrderStatus.quote_awaiting_acceptance, company=company, contact=contact, discount_value=0, delivery_date=dateutil_parse('2018-02-01').date(), vat_verified=False, ) OrderSubscriberFactory( order=order, adviser=AdviserFactory(dit_team_id=constants.Team.td_events_healthcare.value.id), ) OrderAssigneeFactory( order=order, adviser=AdviserFactory(dit_team_id=constants.Team.food_from_britain.value.id), estimated_time=120, ) setup_es.indices.refresh()
def test_non_empty(self): """ Test that calling GET returns the list of advisers subscribed to the order. """ advisers = AdviserFactory.create_batch(3) order = OrderFactory() for adviser in advisers[:2]: OrderSubscriberFactory(order=order, adviser=adviser) url = reverse( 'api-v3:omis:order:subscriber-list', kwargs={'order_pk': order.id}, ) response = self.api_client.get(url) assert response.status_code == status.HTTP_200_OK assert response.json() == [{ 'id': str(adviser.id), 'first_name': adviser.first_name, 'last_name': adviser.last_name, 'name': adviser.name, 'dit_team': { 'id': str(adviser.dit_team.id), 'name': adviser.dit_team.name, 'uk_region': { 'id': str(adviser.dit_team.uk_region.pk), 'name': adviser.dit_team.uk_region.name, }, }, } for adviser in advisers[:2]]
def test_change_existing_list(self, allowed_status): """ Test that calling PUT with a different list of advisers completely changes the subscriber list: - advisers not in the list will be removed - new advisers will be added - existing advisers will be kept """ previous_advisers = AdviserFactory.create_batch(2) order = OrderFactory(status=allowed_status) subscriptions = [ OrderSubscriberFactory(order=order, adviser=adviser) for adviser in previous_advisers ] final_advisers = [ AdviserFactory(), # new previous_advisers[1], # existing ] url = reverse( 'api-v3:omis:order:subscriber-list', kwargs={'order_pk': order.id}, ) response = self.api_client.put( url, [{'id': adviser.id} for adviser in final_advisers], ) assert response.status_code == status.HTTP_200_OK assert {adv['id'] for adv in response.json()} == {str(adv.id) for adv in final_advisers} # check that the id of the existing subscription didn't change assert order.subscribers.filter(id=subscriptions[1].id).exists()
def test_advisers_notified(self): """ Test that calling `quote_cancelled` sends an email to all advisers notifying them that the quote has been cancelled. """ order = OrderFactory(assignees=[]) assignees = OrderAssigneeFactory.create_batch(2, order=order) subscribers = OrderSubscriberFactory.create_batch(2, order=order) canceller = AdviserFactory() notify.client.reset_mock() notify.quote_cancelled(order, by=canceller) assert notify.client.send_email_notification.called # 1 = customer, 4 = assignees/subscribers assert len( notify.client.send_email_notification.call_args_list) == (4 + 1) calls_by_email = { data['email_address']: { 'template_id': data['template_id'], 'personalisation': data['personalisation'], } for _, data in notify.client.send_email_notification.call_args_list } for item in itertools.chain(assignees, subscribers): call = calls_by_email[item.adviser.get_current_email()] assert call[ 'template_id'] == Template.quote_cancelled_for_adviser.value assert call['personalisation'][ 'recipient name'] == item.adviser.name assert call['personalisation'][ 'embedded link'] == order.get_datahub_frontend_url() assert call['personalisation']['canceller'] == canceller.name
def test_notify_on_order_completed(self): """Test that a notification is triggered when an order is marked as completed.""" order = OrderPaidFactory(assignees=[]) OrderAssigneeCompleteFactory.create_batch(1, order=order, is_lead=True) OrderSubscriberFactory.create_batch(2, order=order) notify.client.reset_mock() order.complete(by=None) # 3 = assignees/subscribers assert len(notify.client.send_email_notification.call_args_list) == 3 templates_called = [ data[1]['template_id'] for data in notify.client.send_email_notification.call_args_list ] assert templates_called == [ Template.order_completed_for_adviser.value, Template.order_completed_for_adviser.value, Template.order_completed_for_adviser.value, ]
def test_notify_on_order_subscriber_added(self): """ Test that a notification is sent to the adviser when they get subscribed to an order. """ order = OrderFactory(assignees=[]) notify.client.reset_mock() subscriber = OrderSubscriberFactory(order=order) assert notify.client.send_email_notification.called call_args = notify.client.send_email_notification.call_args_list[0][1] assert call_args['email_address'] == subscriber.adviser.contact_email assert call_args[ 'template_id'] == Template.you_have_been_added_for_adviser.value
def test_notify_on_quote_accepted(self): """Test that a notification is triggered when a quote is accepted.""" order = OrderWithOpenQuoteFactory(assignees=[]) OrderAssigneeFactory.create_batch(1, order=order, is_lead=True) OrderSubscriberFactory.create_batch(2, order=order) notify.client.reset_mock() order.accept_quote(by=None) # 1 = customer, 3 = assignees/subscribers assert len( notify.client.send_email_notification.call_args_list) == (3 + 1) templates_called = [ data[1]['template_id'] for data in notify.client.send_email_notification.call_args_list ] assert templates_called == [ Template.quote_accepted_for_customer.value, Template.quote_accepted_for_adviser.value, Template.quote_accepted_for_adviser.value, Template.quote_accepted_for_adviser.value, ]
def test_notify_on_order_subscriber_deleted(self, mocked_notify_client): """ Test that a notification is sent to the adviser when they get removed from an order. """ order = OrderFactory(assignees=[]) subscriber = OrderSubscriberFactory(order=order) mocked_notify_client.reset_mock() order.subscribers.all().delete() assert mocked_notify_client.send_email_notification.called call_args = mocked_notify_client.send_email_notification.call_args_list[ 0][1] assert call_args['email_address'] == subscriber.adviser.contact_email assert call_args[ 'template_id'] == Template.you_have_been_removed_for_adviser.value
def test_remove_all(self, allowed_status): """ Test that calling PUT with an empty list, removes all the subscribers. """ advisers = AdviserFactory.create_batch(2) order = OrderFactory(status=allowed_status) for adviser in advisers: OrderSubscriberFactory(order=order, adviser=adviser) url = reverse( 'api-v3:omis:order:subscriber-list', kwargs={'order_pk': order.id}, ) response = self.api_client.put(url, []) assert response.status_code == status.HTTP_200_OK assert response.json() == []
def test_adding_subscribers_syncs_order_to_es(setup_es): """ Test that when a subscriber is added to an order, the linked order gets synced to ES. """ order = OrderFactory() subscribers = OrderSubscriberFactory.create_batch(2, order=order) setup_es.indices.refresh() result = setup_es.get( index=OrderSearchApp.es_model.get_write_index(), doc_type=OrderSearchApp.name, id=order.pk, ) indexed = {str(subscriber['id']) for subscriber in result['_source']['subscribers']} expected = {str(subscriber.adviser.pk) for subscriber in subscribers} assert indexed == expected assert len(indexed) == 2
def test_adding_subscribers_syncs_order_to_opensearch(opensearch_with_signals): """ Test that when a subscriber is added to an order, the linked order gets synced to OpenSearch. """ order = OrderFactory() subscribers = OrderSubscriberFactory.create_batch(2, order=order) opensearch_with_signals.indices.refresh() result = opensearch_with_signals.get( index=OrderSearchApp.search_model.get_write_index(), id=order.pk, ) indexed = { str(subscriber['id']) for subscriber in result['_source']['subscribers'] } expected = {str(subscriber.adviser.pk) for subscriber in subscribers} assert indexed == expected assert len(indexed) == 2
def setup_data(es_with_collector): """Sets up data for the tests.""" with freeze_time('2017-01-01 13:00:00'): company = CompanyFactory( name='Mercury trading', trading_names=[], ) contact = ContactFactory(company=company, first_name='John', last_name='Doe') order = OrderFactory( reference='abcd', primary_market_id=constants.Country.japan.value.id, uk_region_id=constants.UKRegion.channel_islands.value.id, assignees=[], status=OrderStatus.DRAFT, company=company, contact=contact, discount_value=0, delivery_date=dateutil_parse('2018-01-01').date(), completed_on=dateutil_parse('2018-05-01T13:00:00Z'), vat_verified=False, ) OrderSubscriberFactory( order=order, adviser=AdviserFactory(dit_team_id=constants.Team.healthcare_uk.value.id), ) OrderAssigneeFactory( order=order, adviser=AdviserFactory(dit_team_id=constants.Team.tees_valley_lep.value.id), estimated_time=60, ) with freeze_time('2017-02-01 13:00:00'): company = CompanyFactory( name='Venus Ltd', trading_names=['Maine Coon', 'Egyptian Mau', '3a'], ) contact = ContactFactory(company=company, first_name='Jenny', last_name='Cakeman') order = OrderWithAcceptedQuoteFactory( reference='efgh', primary_market_id=constants.Country.france.value.id, uk_region_id=constants.UKRegion.east_midlands.value.id, assignees=[], status=OrderStatus.QUOTE_AWAITING_ACCEPTANCE, company=company, contact=contact, discount_value=0, delivery_date=dateutil_parse('2018-02-01').date(), completed_on=dateutil_parse('2018-06-01T13:00:00Z'), vat_verified=False, ) OrderSubscriberFactory( order=order, adviser=AdviserFactory(dit_team_id=constants.Team.td_events_healthcare.value.id), ) OrderAssigneeFactory( order=order, adviser=AdviserFactory(dit_team_id=constants.Team.food_from_britain.value.id), estimated_time=120, ) es_with_collector.flush_and_refresh()
def test_order_to_dict(order_factory): """Test converting an order to dict.""" order = order_factory() invoice = order.invoice OrderSubscriberFactory.create_batch(2, order=order) OrderAssigneeFactory.create_batch(2, order=order) result = ESOrder.db_object_to_dict(order) assert result == { 'id': order.pk, 'company': { 'id': str(order.company.pk), 'name': order.company.name, 'trading_names': order.company.trading_names, }, 'contact': { 'id': str(order.contact.pk), 'first_name': order.contact.first_name, 'last_name': order.contact.last_name, 'name': order.contact.name, }, 'primary_market': { 'id': str(order.primary_market.pk), 'name': order.primary_market.name, }, 'sector': { 'id': str(order.sector.pk), 'name': order.sector.name, 'ancestors': [{ 'id': str(ancestor.pk), } for ancestor in order.sector.get_ancestors()], }, 'uk_region': { 'id': str(order.uk_region.pk), 'name': order.uk_region.name, }, 'service_types': [{ 'id': str(service_type.pk), 'name': service_type.name, } for service_type in order.service_types.all()], 'created_on': order.created_on, 'created_by': { 'id': str(order.created_by.pk), 'first_name': order.created_by.first_name, 'last_name': order.created_by.last_name, 'name': order.created_by.name, 'dit_team': { 'id': str(order.created_by.dit_team.id), 'name': order.created_by.dit_team.name, }, }, 'modified_on': order.modified_on, 'reference': order.reference, 'status': order.status, 'description': order.description, 'contacts_not_to_approach': order.contacts_not_to_approach, 'further_info': order.further_info, 'existing_agents': order.existing_agents, 'delivery_date': order.delivery_date, 'contact_email': order.contact_email, 'contact_phone': order.contact_phone, 'subscribers': [{ 'id': str(subscriber.adviser.pk), 'first_name': subscriber.adviser.first_name, 'last_name': subscriber.adviser.last_name, 'name': str(subscriber.adviser.name), 'dit_team': { 'id': str(subscriber.adviser.dit_team.pk), 'name': str(subscriber.adviser.dit_team.name), }, } for subscriber in order.subscribers.all()], 'assignees': [{ 'id': str(assignee.adviser.pk), 'first_name': assignee.adviser.first_name, 'last_name': assignee.adviser.last_name, 'name': str(assignee.adviser.name), 'dit_team': { 'id': str(assignee.adviser.dit_team.pk), 'name': str(assignee.adviser.dit_team.name), }, } for assignee in order.assignees.all()], 'po_number': order.po_number, 'discount_value': order.discount_value, 'vat_status': order.vat_status, 'vat_number': order.vat_number, 'vat_verified': order.vat_verified, 'net_cost': order.net_cost, 'payment_due_date': None if not invoice else invoice.payment_due_date, 'subtotal_cost': order.subtotal_cost, 'vat_cost': order.vat_cost, 'total_cost': order.total_cost, 'billing_company_name': order.billing_company_name, 'billing_contact_name': order.billing_contact_name, 'billing_email': order.billing_email, 'billing_phone': order.billing_phone, 'billing_address_1': order.billing_address_1, 'billing_address_2': order.billing_address_2, 'billing_address_town': order.billing_address_town, 'billing_address_county': order.billing_address_county, 'billing_address_postcode': order.billing_address_postcode, 'billing_address_country': { 'id': str(order.billing_address_country.pk), 'name': order.billing_address_country.name, }, 'paid_on': order.paid_on, 'completed_by': { 'id': str(order.completed_by.pk), 'first_name': order.completed_by.first_name, 'last_name': order.completed_by.last_name, 'name': order.completed_by.name, } if order.completed_by else None, 'completed_on': order.completed_on, 'cancelled_by': { 'id': str(order.cancelled_by.pk), 'first_name': order.cancelled_by.first_name, 'last_name': order.cancelled_by.last_name, 'name': order.cancelled_by.name, } if order.cancelled_by else None, 'cancelled_on': order.cancelled_on, 'cancellation_reason': { 'id': str(order.cancellation_reason.pk), 'name': order.cancellation_reason.name, } if order.cancellation_reason else None, }