Skip to content

Commit

Permalink
TP2000-450 certificate multiple objects returned error on certificate…
Browse files Browse the repository at this point in the history
… description create (pt. 2) (#644)

* update get_initial to use current

* add form and view tests, update get_context_data to use current queryset
  • Loading branch information
gabelton authored Aug 5, 2022
1 parent ca229fc commit 2b52aac
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
23 changes: 23 additions & 0 deletions certificates/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
26 changes: 26 additions & 0 deletions certificates/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion certificates/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
)
Expand Down

0 comments on commit 2b52aac

Please sign in to comment.