|
| 1 | +from uuid import uuid4 |
| 2 | + |
| 3 | +from django.contrib.admin.templatetags.admin_urls import admin_urlname |
| 4 | +from django.urls import reverse |
| 5 | +from rest_framework import status |
| 6 | + |
| 7 | +from datahub.company.test.factories import CompanyFactory, ContactFactory |
| 8 | +from datahub.core.admin import get_change_url |
| 9 | +from datahub.core.test_utils import AdminTestMixin, random_obj_for_model |
| 10 | +from datahub.interaction.models import CommunicationChannel, Interaction |
| 11 | +from datahub.interaction.test.factories import CompanyInteractionFactory |
| 12 | +from datahub.metadata.models import Service, Team |
| 13 | + |
| 14 | + |
| 15 | +class TestInteractionAdmin(AdminTestMixin): |
| 16 | + """ |
| 17 | + Tests for interaction admin. |
| 18 | +
|
| 19 | + TODO: these tests will be removed once the migration from contact to contacts is complete. |
| 20 | + """ |
| 21 | + |
| 22 | + def test_add(self): |
| 23 | + """Test that adding an interaction also sets the contacts field.""" |
| 24 | + company = CompanyFactory() |
| 25 | + contact = ContactFactory() |
| 26 | + communication_channel = random_obj_for_model(CommunicationChannel) |
| 27 | + |
| 28 | + url = reverse(admin_urlname(Interaction._meta, 'add')) |
| 29 | + data = { |
| 30 | + 'id': uuid4(), |
| 31 | + 'kind': Interaction.KINDS.interaction, |
| 32 | + 'communication_channel': communication_channel.pk, |
| 33 | + 'subject': 'whatever', |
| 34 | + 'date_0': '2018-01-01', |
| 35 | + 'date_1': '00:00:00', |
| 36 | + 'dit_adviser': self.user.pk, |
| 37 | + 'company': company.pk, |
| 38 | + 'contact': contact.pk, |
| 39 | + 'service': random_obj_for_model(Service).pk, |
| 40 | + 'dit_team': random_obj_for_model(Team).pk, |
| 41 | + 'was_policy_feedback_provided': False, |
| 42 | + } |
| 43 | + response = self.client.post(url, data, follow=True) |
| 44 | + |
| 45 | + assert response.status_code == status.HTTP_200_OK |
| 46 | + |
| 47 | + assert Interaction.objects.count() == 1 |
| 48 | + interaction = Interaction.objects.first() |
| 49 | + |
| 50 | + assert interaction.contact == contact |
| 51 | + assert list(interaction.contacts.all()) == [contact] |
| 52 | + |
| 53 | + def test_update_contact_to_non_null(self): |
| 54 | + """ |
| 55 | + Test that updating an interaction with a non-null contact also sets the contacts |
| 56 | + field. |
| 57 | + """ |
| 58 | + interaction = CompanyInteractionFactory(contacts=[]) |
| 59 | + new_contact = ContactFactory(company=interaction.company) |
| 60 | + |
| 61 | + url = get_change_url(interaction) |
| 62 | + data = { |
| 63 | + # Unchanged values |
| 64 | + 'id': interaction.pk, |
| 65 | + 'kind': Interaction.KINDS.interaction, |
| 66 | + 'communication_channel': interaction.communication_channel.pk, |
| 67 | + 'subject': interaction.subject, |
| 68 | + 'date_0': interaction.date.date().isoformat(), |
| 69 | + 'date_1': interaction.date.time().isoformat(), |
| 70 | + 'dit_adviser': interaction.dit_adviser.pk, |
| 71 | + 'company': interaction.company.pk, |
| 72 | + 'service': interaction.service.pk, |
| 73 | + 'dit_team': interaction.dit_team.pk, |
| 74 | + 'was_policy_feedback_provided': interaction.was_policy_feedback_provided, |
| 75 | + 'policy_feedback_notes': interaction.policy_feedback_notes, |
| 76 | + 'policy_areas': [], |
| 77 | + 'policy_issue_types': [], |
| 78 | + 'event': '', |
| 79 | + |
| 80 | + # Changed values |
| 81 | + 'contact': new_contact.pk, |
| 82 | + } |
| 83 | + response = self.client.post(url, data=data, follow=True) |
| 84 | + |
| 85 | + assert response.status_code == status.HTTP_200_OK |
| 86 | + |
| 87 | + interaction.refresh_from_db() |
| 88 | + assert interaction.contact == new_contact |
| 89 | + assert list(interaction.contacts.all()) == [new_contact] |
| 90 | + |
| 91 | + def test_update_contact_to_null(self): |
| 92 | + """Test that updating an interaction with a null contact clears the contacts field.""" |
| 93 | + interaction = CompanyInteractionFactory(contacts=[]) |
| 94 | + |
| 95 | + url = get_change_url(interaction) |
| 96 | + data = { |
| 97 | + # Unchanged values |
| 98 | + 'id': interaction.pk, |
| 99 | + 'kind': Interaction.KINDS.interaction, |
| 100 | + 'communication_channel': interaction.communication_channel.pk, |
| 101 | + 'subject': interaction.subject, |
| 102 | + 'date_0': interaction.date.date().isoformat(), |
| 103 | + 'date_1': interaction.date.time().isoformat(), |
| 104 | + 'dit_adviser': interaction.dit_adviser.pk, |
| 105 | + 'company': interaction.company.pk, |
| 106 | + 'service': interaction.service.pk, |
| 107 | + 'dit_team': interaction.dit_team.pk, |
| 108 | + 'was_policy_feedback_provided': interaction.was_policy_feedback_provided, |
| 109 | + 'policy_feedback_notes': interaction.policy_feedback_notes, |
| 110 | + 'policy_areas': [], |
| 111 | + 'policy_issue_types': [], |
| 112 | + 'event': '', |
| 113 | + |
| 114 | + # Changed values |
| 115 | + 'contact': '', |
| 116 | + } |
| 117 | + response = self.client.post(url, data=data, follow=True) |
| 118 | + |
| 119 | + assert response.status_code == status.HTTP_200_OK |
| 120 | + |
| 121 | + interaction.refresh_from_db() |
| 122 | + assert interaction.contact is None |
| 123 | + assert interaction.contacts.count() == 0 |
0 commit comments