diff --git a/certificates/tests/test_forms.py b/certificates/tests/test_forms.py index 015209593..89ff7c4c7 100644 --- a/certificates/tests/test_forms.py +++ b/certificates/tests/test_forms.py @@ -178,3 +178,26 @@ def test_validation_error_raised_for_duplicate_sid(session_with_workbasket): f"Certificate with sid A01 and type {certificate_type} already exists." in form.errors["sid"] ) + + +def test_certificate_description_valid_data(): + certificate = factories.CertificateFactory.create() + data = { + "described_certificate": certificate.pk, + "description": "certifiably certified", + "validity_start_0": 1, + "validity_start_1": 1, + "validity_start_2": 2022, + } + form = forms.CertificateCreateDescriptionForm(data=data) + + assert form.is_valid() + + +def test_certificate_description_invalid_data(): + form = forms.CertificateCreateDescriptionForm(data={}) + + assert not form.is_valid() + assert "This field is required." in form.errors["described_certificate"] + assert "This field is required." in form.errors["description"] + assert "Enter the day, month and year" in form.errors["validity_start"] diff --git a/certificates/tests/test_views.py b/certificates/tests/test_views.py index c784e45ac..cd941f5a1 100644 --- a/certificates/tests/test_views.py +++ b/certificates/tests/test_views.py @@ -108,3 +108,29 @@ def test_description_create_get_initial(): initial = view.get_initial() assert initial["described_certificate"] == new_version + + +def test_description_create_get_context_data(valid_user_api_client): + """Test that posting to certificate create endpoint with valid data returns + a 302 and creates new description matching certificate.""" + certificate = factories.CertificateFactory.create(description=None) + new_version = certificate.new_version(certificate.transaction.workbasket) + url = reverse( + "certificate-ui-description-create", + args=(certificate.certificate_type.sid, certificate.sid), + ) + post_data = { + "description": "certifiably certified", + "described_certificate": new_version.pk, + "validity_start_0": 1, + "validity_start_1": 1, + "validity_start_2": 2022, + } + assert not models.CertificateDescription.objects.exists() + response = valid_user_api_client.post(url, post_data) + + assert response.status_code == 302 + assert models.CertificateDescription.objects.filter( + described_certificate__sid=new_version.sid, + described_certificate__certificate_type__sid=new_version.certificate_type.sid, + ).exists() diff --git a/certificates/views.py b/certificates/views.py index 5d3f212e9..57d866bc7 100644 --- a/certificates/views.py +++ b/certificates/views.py @@ -143,7 +143,7 @@ class CertificateCreateDescriptionMixin: def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - context["described_object"] = models.Certificate.objects.get( + context["described_object"] = models.Certificate.objects.current().get( certificate_type__sid=(self.kwargs.get("certificate_type__sid")), sid=(self.kwargs.get("sid")), )