Skip to content

Commit

Permalink
fix unstable selenium test
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasertl committed Jun 29, 2024
1 parent f7f69c2 commit ba63333
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
5 changes: 2 additions & 3 deletions ca/django_ca/static/django_ca/admin/js/sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,14 @@ document.addEventListener('DOMContentLoaded', function() {
const csr_data = await csr_response.json();
const subject = csr_data["subject"];

// Set the data-fetched property, so that Selenium tests can wait for completion.
input.dataset.fetched = "true"

// No need to do anything if the CSR has an empty subject
if (subject.length === 0) {
csr_subject_input_chapter.querySelector(".no-csr").style.display = "none";
csr_subject_input_chapter.querySelector(".has-content").style.display = "none";
csr_subject_input_chapter.querySelector(".no-content").style.display = "block";

// Set the data-fetched property, so that Selenium tests can wait for completion.
input.dataset.fetched = "true"
return;
}

Expand Down
39 changes: 22 additions & 17 deletions ca/django_ca/tests/admin/test_add_cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,9 @@ def test_csr_integration(self) -> None:
]

# Elements of the CSR chapter
csr_chapter = self.key_value_field.find_element(By.CSS_SELECTOR, ".subject-input-chapter.csr")
csr_subject_input_chapter = self.key_value_field.find_element(
By.CSS_SELECTOR, ".subject-input-chapter.csr"
)
no_csr = self.key_value_field.find_element(By.CSS_SELECTOR, ".subject-input-chapter.csr .no-csr")
has_content = self.key_value_field.find_element(
By.CSS_SELECTOR, ".subject-input-chapter.csr .has-content"
Expand All @@ -1044,39 +1046,42 @@ def test_csr_integration(self) -> None:
)

# Check that the right parts of the CSR chapter is displayed
self.assertIs(no_csr.is_displayed(), True) # this is displayed as we haven't pasted a CSR
self.assertIs(has_content.is_displayed(), False)
self.assertIs(no_content.is_displayed(), False)
assert no_csr.is_displayed() is True # this is displayed as we haven't pasted a CSR
assert has_content.is_displayed() is False
assert no_content.is_displayed() is False

csr: x509.CertificateSigningRequest = CERT_DATA["all-extensions"]["csr"]["parsed"]
csr_field = self.find("textarea#id_csr")
csr_field.send_keys(csr.public_bytes(Encoding.PEM).decode("ascii"))

# Make sure that the displayed subject has not changed
self.assertNotModified()
self.assertEqual(self.value, initial_subject)
assert self.value == initial_subject

# check the JSON value from the chapter
self.assertEqual(
json.loads(csr_chapter.get_attribute("data-value")), # type: ignore[arg-type]
csr_subject,
# Wait for the CSR results to be fetched
WebDriverWait(self.selenium, 3, poll_frequency=0.1).until(
lambda driver: driver.find_element(By.ID, "id_csr").get_attribute("data-fetched") == "true",
"data-fetched for CSR was not set.",
)

# check the JSON value from the chapter
assert json.loads(csr_subject_input_chapter.get_attribute("data-value")) == csr_subject

# check that the right chapter is displayed
self.assertIs(no_csr.is_displayed(), False)
self.assertIs(has_content.is_displayed(), True)
self.assertIs(no_content.is_displayed(), False)
assert no_csr.is_displayed() is False
assert has_content.is_displayed() is True
assert no_content.is_displayed() is False

# Check the li element inside
lis = has_content.find_elements(By.TAG_NAME, "li")
self.assertEqual(len(lis), len(csr_subject))
self.assertEqual(lis[0].text, "countryName (C): AT") # just testing the first one
assert len(lis) == len(csr_subject)
assert lis[0].text == "countryName (C): AT" # just testing the first one

# Click the copy button and validate that the subject is set
csr_chapter.find_element(By.CSS_SELECTOR, ".copy-button").click()
csr_subject_input_chapter.find_element(By.CSS_SELECTOR, ".copy-button").click()
self.assertModified()
self.assertEqual(self.value, csr_subject)
self.assertEqual(self.displayed_value, csr_subject)
assert self.value == csr_subject
assert self.displayed_value == csr_subject

@override_tmpcadir()
def test_paste_csr_no_subject(self) -> None:
Expand Down

0 comments on commit ba63333

Please sign in to comment.