Skip to content

Commit

Permalink
Merge pull request #1773 from uktrade/LTD-Use-factories-in-tests
Browse files Browse the repository at this point in the history
LTD: Update tests to use factories
  • Loading branch information
saruniitr authored Feb 5, 2024
2 parents e55fe49 + c059ae6 commit 023169a
Show file tree
Hide file tree
Showing 54 changed files with 571 additions and 708 deletions.
3 changes: 2 additions & 1 deletion api/applications/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class StandardApplicationFactory(factory.django.DjangoModelFactory):
name = "Application Test Name"
export_type = ApplicationExportType.PERMANENT
case_type_id = CaseTypeEnum.SIEL.id
have_you_been_informed = (ApplicationExportLicenceOfficialType.YES,)
have_you_been_informed = ApplicationExportLicenceOfficialType.YES
reference_number_on_information_form = ""
activity = "Trade"
usage = "Trade"
Expand All @@ -73,6 +73,7 @@ class StandardApplicationFactory(factory.django.DjangoModelFactory):
intended_end_use = "this is our intended end use"
is_shipped_waybill_or_lading = True
non_waybill_or_lading_route_details = None
is_mod_security_approved = False
submitted_by = factory.SubFactory(ExporterUserFactory)

class Meta:
Expand Down
18 changes: 9 additions & 9 deletions api/applications/tests/test_adding_goods.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from api.audit_trail.models import Audit
from api.cases.enums import CaseTypeEnum
from api.goods.enums import ItemType
from lite_content.lite_api import strings
from api.staticdata.missing_document_reasons.enums import GoodMissingDocumentReasons
from api.goods.tests.factories import GoodFactory
from api.staticdata.units.enums import Units
from lite_content.lite_api import strings
from test_helpers.clients import DataTestClient
from test_helpers.decorators import none_param_tester

Expand All @@ -19,7 +19,7 @@ class AddingGoodsOnApplicationTests(DataTestClient):
def setUp(self):
super().setUp()
self.draft = self.create_draft_standard_application(self.organisation)
self.good = self.create_good("A good", self.organisation)
self.good = GoodFactory(name="A good", organisation=self.organisation)

def test_add_a_good_to_a_draft(self):
self.create_good_document(
Expand Down Expand Up @@ -56,7 +56,7 @@ def test_add_a_good_to_a_draft(self):
def test_user_cannot_add_another_organisations_good_to_a_draft(self):
good_name = "A good"
organisation_2, _ = self.create_organisation_with_exporter_user()
good = self.create_good(good_name, organisation_2)
good = GoodFactory(name=good_name, organisation=organisation_2)
self.create_good_document(
good,
user=self.exporter_user,
Expand Down Expand Up @@ -242,7 +242,7 @@ def test_adding_good_validate_only(self, data):
self.assertEqual(response.status_code, data["response"])

def test_adding_good_without_document_or_reason_success(self):
good = self.create_good("A good", self.organisation)
good = GoodFactory(organisation=self.organisation)
good.is_document_available = False
good.save()
data = {
Expand All @@ -259,7 +259,7 @@ def test_adding_good_without_document_or_reason_success(self):
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def test_adding_good_with_reason_official_sensitive_success(self):
good = self.create_good("A good", self.organisation)
good = GoodFactory(organisation=self.organisation)
good.is_document_sensitive = True
good.save()
data = {
Expand All @@ -281,7 +281,7 @@ def test_add_a_good_to_a_draft_failure(self, quantity, unit, value, is_good_inco
Ensure all params have to be sent otherwise fail
"""
self.create_draft_standard_application(self.organisation)
self.create_good("A good", self.organisation)
GoodFactory(organisation=self.organisation)
self.create_good_document(
self.good,
user=self.exporter_user,
Expand All @@ -308,7 +308,7 @@ class AddingGoodsOnApplicationFirearmsTests(DataTestClient):
def setUp(self):
super().setUp()
self.draft = self.create_draft_standard_application(self.organisation)
self.good = self.create_good("A good", self.organisation, create_firearm_details=True)
self.good = GoodFactory(organisation=self.organisation)

@parameterized.expand(
[
Expand Down Expand Up @@ -410,7 +410,7 @@ class AddingGoodsOnApplicationExhibitionTests(DataTestClient):
def setUp(self):
super().setUp()
self.draft = self.create_mod_clearance_application(self.organisation, CaseTypeEnum.EXHIBITION)
self.good = self.create_good("A good", self.organisation)
self.good = GoodFactory(organisation=self.organisation)

def test_add_a_good_to_a_exhibition_draft_choice(self):
self.create_good_document(
Expand Down
9 changes: 5 additions & 4 deletions api/applications/tests/test_application_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from api.core.constants import GovPermissions
from api.cases.models import CaseAssignment
from api.licences.enums import LicenceStatus
from api.licences.tests.factories import StandardLicenceFactory
from api.teams.models import Team
from api.users.models import UserOrganisationRelationship, Permission
from api.staticdata.statuses.enums import CaseStatusEnum
Expand Down Expand Up @@ -102,7 +103,7 @@ def test_gov_user_set_application_to_terminal_status_removes_case_from_queues_us
)
if case_status == CaseStatusEnum.REVOKED:
self.standard_application.licences.add(
self.create_licence(self.standard_application, status=LicenceStatus.ISSUED)
StandardLicenceFactory(case=self.standard_application, status=LicenceStatus.ISSUED)
)

data = {"status": case_status}
Expand Down Expand Up @@ -134,7 +135,7 @@ def test_gov_user_set_application_to_terminal_status_removes_case_from_queues_us
]
)
def test_certain_case_statuses_changes_licence_status(self, case_status, licence_status):
licence = self.create_licence(self.standard_application, status=LicenceStatus.ISSUED)
licence = StandardLicenceFactory(case=self.standard_application, status=LicenceStatus.ISSUED)

data = {"status": case_status}
response = self.client.put(self.url, data=data, **self.gov_headers)
Expand Down Expand Up @@ -195,7 +196,7 @@ def test_exporter_set_application_status_failure(self, new_status):
def test_exporter_set_application_status_surrendered_success(self):
self.standard_application.status = get_case_status_by_status(CaseStatusEnum.FINALISED)
self.standard_application.save()
self.create_licence(self.standard_application, status=LicenceStatus.ISSUED)
StandardLicenceFactory(case=self.standard_application, status=LicenceStatus.ISSUED)
surrendered_status = get_case_status_by_status("surrendered")

data = {"status": CaseStatusEnum.SURRENDERED}
Expand Down Expand Up @@ -285,7 +286,7 @@ def test_gov_set_status_to_applicant_editing_failure(self):
def test_gov_set_status_for_all_except_applicant_editing_and_finalised_success(self, case_status):
if case_status == CaseStatusEnum.REVOKED:
self.standard_application.licences.add(
self.create_licence(self.standard_application, status=LicenceStatus.ISSUED)
StandardLicenceFactory(case=self.standard_application, status=LicenceStatus.ISSUED)
)

data = {"status": case_status}
Expand Down
2 changes: 0 additions & 2 deletions api/applications/tests/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from django.urls import reverse

from api.applications.serializers import good as serializers
from api.audit_trail.enums import AuditType
from api.audit_trail.models import Audit
from test_helpers.clients import DataTestClient
Expand All @@ -15,7 +14,6 @@ class ApplicationDocumentViewTests(DataTestClient):
def test_audit_trail_create(self, upload_bytes_func, mock_virus_scan, mock_s3_operations_get_object):
mock_virus_scan.return_value = False
application = self.create_draft_standard_application(organisation=self.organisation, user=self.exporter_user)
good = self.create_good("A good", self.organisation)

url = reverse("applications:application_documents", kwargs={"pk": application.pk})

Expand Down
87 changes: 38 additions & 49 deletions api/applications/tests/test_edit_good_on_applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

class EditGoodOnApplicationsTests(DataTestClient):
def test_edit_a_good_on_applicaton(self):
self.create_draft_standard_application(self.organisation)
self.good_on_application.firearm_details = FirearmFactory.create()
self.good_on_application.save()
application = self.create_draft_standard_application(self.organisation)
good_on_application = application.goods.first()
good_on_application.firearm_details = FirearmFactory()
good_on_application.save()

url = reverse(
"applications:good_on_application",
kwargs={"obj_pk": self.good_on_application.id},
kwargs={"obj_pk": good_on_application.id},
)

response = self.client.put(
Expand All @@ -37,31 +38,26 @@ def test_edit_a_good_on_applicaton(self):
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.good_on_application.refresh_from_db()
self.assertEqual(
self.good_on_application.firearm_details.year_of_manufacture,
1990,
)
self.assertEqual(
self.good_on_application.firearm_details.calibre,
"1mm",
)
good_on_application.refresh_from_db()
self.assertEqual(good_on_application.firearm_details.year_of_manufacture, 1990)
self.assertEqual(good_on_application.firearm_details.calibre, "1mm")

def test_edit_a_good_on_applicaton_read_only(self):
application = self.create_draft_standard_application(self.organisation)
uneditable_status = get_case_statuses(read_only=True)[0]
application.status = get_case_status_by_status(uneditable_status)
application.save()

self.good_on_application.firearm_details = FirearmFactory.create()
self.good_on_application.save()
good_on_application = application.goods.first()
good_on_application.firearm_details = FirearmFactory()
good_on_application.save()

original_year_of_manufacture = self.good_on_application.firearm_details.year_of_manufacture
original_calibre = self.good_on_application.firearm_details.calibre
original_year_of_manufacture = good_on_application.firearm_details.year_of_manufacture
original_calibre = good_on_application.firearm_details.calibre

url = reverse(
"applications:good_on_application",
kwargs={"obj_pk": self.good_on_application.id},
kwargs={"obj_pk": good_on_application.id},
)

response = self.client.put(
Expand All @@ -77,30 +73,25 @@ def test_edit_a_good_on_applicaton_read_only(self):
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(response.json(), {"errors": [strings.Applications.Generic.READ_ONLY]})

self.good_on_application.refresh_from_db()
self.assertEqual(
self.good_on_application.firearm_details.year_of_manufacture,
original_year_of_manufacture,
)
self.assertEqual(
self.good_on_application.firearm_details.calibre,
original_calibre,
)
good_on_application.refresh_from_db()
self.assertEqual(good_on_application.firearm_details.year_of_manufacture, original_year_of_manufacture)
self.assertEqual(good_on_application.firearm_details.calibre, original_calibre)

def test_edit_a_good_on_applicaton_invalid_organisation(self):
another_organisation, _ = self.create_organisation_with_exporter_user()
application = self.create_draft_standard_application(another_organisation)
application.save()

self.good_on_application.firearm_details = FirearmFactory.create()
self.good_on_application.save()
good_on_application = application.goods.first()
good_on_application.firearm_details = FirearmFactory()
good_on_application.save()

original_year_of_manufacture = self.good_on_application.firearm_details.year_of_manufacture
original_calibre = self.good_on_application.firearm_details.calibre
original_year_of_manufacture = good_on_application.firearm_details.year_of_manufacture
original_calibre = good_on_application.firearm_details.calibre

url = reverse(
"applications:good_on_application",
kwargs={"obj_pk": self.good_on_application.id},
kwargs={"obj_pk": good_on_application.id},
)

response = self.client.put(
Expand All @@ -116,27 +107,23 @@ def test_edit_a_good_on_applicaton_invalid_organisation(self):
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(response.json(), {"errors": [strings.Applications.Generic.INVALID_ORGANISATION]})

self.good_on_application.refresh_from_db()
good_on_application.refresh_from_db()
self.assertEqual(
self.good_on_application.firearm_details.year_of_manufacture,
good_on_application.firearm_details.year_of_manufacture,
original_year_of_manufacture,
)
self.assertEqual(
self.good_on_application.firearm_details.calibre,
good_on_application.firearm_details.calibre,
original_calibre,
)


class GovUserEditGoodOnApplicationsTests(DataTestClient):
def test_edit_trigger_list_assessment_and_nca(self):
"Test updating trigger list assessment on multiple products on application"
draft = self.create_draft_standard_application(self.organisation)
draft = self.create_draft_standard_application(self.organisation, num_products=2)
application = self.submit_application(draft, self.exporter_user)
self.good_on_application2 = GoodOnApplicationFactory(
application=application,
good=GoodFactory(organisation=self.organisation, is_good_controlled=True),
)
good_on_applications = [self.good_on_application, self.good_on_application2]
good_on_applications = application.goods.all()

for good_on_application in good_on_applications:
self.assertEqual(good_on_application.nsg_list_type, "")
Expand All @@ -151,18 +138,18 @@ def test_edit_trigger_list_assessment_and_nca(self):

data = [
{
"id": self.good_on_application.id,
"id": good_on_applications[0].id,
"application": application.id,
"good": self.good_on_application.good.id,
"good": good_on_applications[0].good.id,
"nsg_list_type": NSGListType.TRIGGER_LIST,
"is_trigger_list_guidelines_applicable": True,
"is_nca_applicable": False,
"nsg_assessment_note": "Trigger list product1",
},
{
"id": self.good_on_application2.id,
"id": good_on_applications[1].id,
"application": application.id,
"good": self.good_on_application2.good.id,
"good": good_on_applications[1].good.id,
"nsg_list_type": NSGListType.TRIGGER_LIST,
"is_trigger_list_guidelines_applicable": False,
"is_nca_applicable": True,
Expand Down Expand Up @@ -195,6 +182,7 @@ def test_edit_trigger_list_assessment_and_nca(self):
def test_edit_good_on_terminal_status_application_forbidden(self, terminal_status):
draft = self.create_draft_standard_application(self.organisation)
application = self.submit_application(draft, self.exporter_user)
good_on_application = application.goods.first()
self.good_on_application2 = GoodOnApplicationFactory(
application=application,
good=GoodFactory(organisation=self.organisation, is_good_controlled=True),
Expand All @@ -211,9 +199,9 @@ def test_edit_good_on_terminal_status_application_forbidden(self, terminal_statu
url,
data=[
{
"id": self.good_on_application.id,
"id": good_on_application.id,
"application": application.id,
"good": self.good_on_application.good.id,
"good": good_on_application.good.id,
"nsg_list_type": NSGListType.TRIGGER_LIST,
"is_trigger_list_guidelines_applicable": True,
"is_nca_applicable": True,
Expand All @@ -236,6 +224,7 @@ def test_edit_good_on_terminal_status_application_forbidden(self, terminal_statu
def test_edit_good_on_application_bad_request(self):
draft = self.create_draft_standard_application(self.organisation)
application = self.submit_application(draft, self.exporter_user)
good_on_application = application.goods.first()

url = reverse(
"applications:good_on_application_update_internal",
Expand All @@ -246,9 +235,9 @@ def test_edit_good_on_application_bad_request(self):
url,
data=[
{
"id": self.good_on_application.id,
"id": good_on_application.id,
"application": application.id,
"good": self.good_on_application.good.id,
"good": good_on_application.good.id,
"nsg_list_type": "INVALID_LIST_TYPE",
"is_trigger_list_guidelines_applicable": False,
"is_nca_applicable": True,
Expand Down
14 changes: 7 additions & 7 deletions api/applications/tests/test_existing_parties.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def test_get_existing_parties(self):
response = self.client.get(self.url, **self.exporter_headers)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data["results"]), Party.objects.count())
# parties of this organisation
self.assertEqual(len(response.data["results"]), Party.objects.filter(organisation=self.organisation).count())

def test_get_existing_parties_only_returns_parties_from_own_organisation_success(self):
second_organisation, _ = self.create_organisation_with_exporter_user(name="Second organisation")
Expand Down Expand Up @@ -123,19 +124,18 @@ def test_get_existing_parties_contains_no_duplicates_without_filters_success(sel
expected_copy_id = Party.objects.filter(name="Mr Original", address="456 abc st.").get().id
second_expected_copy_id = Party.objects.filter(name="Mr Copy").get().id

# Party table data contains one duplicate, so results returned is 1 less than all parties
self.assertEqual(Party.objects.count() - 1, len(response_data))
# Party table data contains one duplicate, so results returned is 1 less than all parties of this organisation
self.assertEqual(len(response_data), Party.objects.filter(organisation=self.organisation).count() - 1)

self.assertIn(str(expected_copy_id), response_data_ids)
self.assertIn(str(second_expected_copy_id), response_data_ids)
self.assertIn(str(self.draft.end_user.party.id), response_data_ids)
self.assertNotIn(str(original_party.id), response_data_ids)

def test_get_existing_parties_contains_no_duplicates_with_filters_success(self):
params = f"?name=Mr"
params = "?name=Unique"

original_party = Party.objects.create(
name="Mr Original",
name="Unique name",
address="123 abc st.",
website="https://www.gov.py",
country=self.country,
Expand All @@ -145,7 +145,7 @@ def test_get_existing_parties_contains_no_duplicates_with_filters_success(self):
)

copied_party = Party.objects.create(
name="Mr Original",
name="Unique name",
address="123 abc st.",
website="https://www.gov.py",
country=self.country,
Expand Down
Loading

0 comments on commit 023169a

Please sign in to comment.