Exemple #1
0
    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,
        ]
Exemple #2
0
    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,
        ]
Exemple #3
0
    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,
        ]
Exemple #4
0
    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
Exemple #5
0
    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,
        ]
Exemple #6
0
    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_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
Exemple #8
0
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 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,
    }