Skip to content

Commit

Permalink
Merge pull request #751 from nationalarchives/AYR-1236/os-e2e-tests
Browse files Browse the repository at this point in the history
Ayr 1236/os e2e tests
  • Loading branch information
anthonyhashemi authored Jan 31, 2025
2 parents 8401eca + c1dac44 commit 97448ed
Show file tree
Hide file tree
Showing 8 changed files with 667 additions and 429 deletions.
29 changes: 29 additions & 0 deletions e2e_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ def get_desktop_page_table_rows(page: Page):
"""
)

def get_desktop_page_transferring_body_table_headers(page: Page):
return page.locator(
"th:not(.govuk-table--invisible-on-desktop)"
).evaluate_all(
"""
els => els
.map(e => e.innerText.trim())
.filter(text => text === 'Found within' || text === 'Search results')
"""
)

def get_desktop_page_transferring_body_inner_table_headers(page: Page):
return page.locator(
"th:not(.govuk-table--invisible-on-desktop)"
).evaluate_all(
"""
els => els
.map(e => e.innerText.trim())
.filter(text => text !== 'Found within' && text !== 'Search results')
"""
)

def get_desktop_page_table_metadata(page: Page):
return page.locator("tbody tbody").evaluate_all(
"""rows => rows.map(row =>
Array.from(row.querySelectorAll('.govuk-table__cell')).map(cell => cell.innerText.trim())
)"""
)


@pytest.fixture
def utils():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ def test_search_to_record(aau_user_page: Page):

aau_user_page.get_by_role("textbox").first.fill("a")
aau_user_page.get_by_role("button", name="Search").click()
expect(aau_user_page).to_have_url("search_results_summary?query=a")
expect(aau_user_page).to_have_url(
"/search_results_summary?query=a&search_area=everywhere"
)

aau_user_page.get_by_role("cell").first.get_by_role("link").first.click()
expect(aau_user_page).to_have_url(
re.compile(r"\/search\/transferring_body\/.*")
)

aau_user_page.get_by_role("cell").nth(2).get_by_role("link").first.click()
aau_user_page.get_by_role("link", name="file-a2.txt").click()
expect(aau_user_page).to_have_url(re.compile(r"\/record\/.*"))
823 changes: 445 additions & 378 deletions e2e_tests/poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions e2e_tests/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pytest-playwright = "^0.5.0"
PyJWT = "^2.8.0"
itsdangerous = "^2.2.0"
python-keycloak = "^4.1.0"
playwright = "^1.49.1"


[build-system]
Expand Down
68 changes: 67 additions & 1 deletion e2e_tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,78 @@ def test_sign_in_succeeds_when_valid_credentials(
"sub",
"typ",
"azp",
"session_state",
"scope",
"sid",
"groups",
}

refresh_token = json.loads(decoded_data)["refresh_token"]
decoded_token_dict = jwt.decode(
refresh_token, options={"verify_signature": False}
)
assert set(decoded_token_dict.keys()) == {
"aud",
"exp",
"iat",
"jti",
"iss",
"sub",
"typ",
"azp",
"scope",
"sid",
}


def test_token_expiry(page: Page, create_aau_keycloak_user):
"""
Given a user is signed in,
When their refresh token is retrieved,
Then it should contain expected claims and have an expiry time matching Keycloak config.
"""
username, password = create_aau_keycloak_user

page.goto("/sign-in")
page.get_by_label("Email address").fill(username)
page.get_by_label("Password").fill(password)
page.get_by_role("button", name="Sign in").click()
expect(page).to_have_url("/browse")

cookies = page.context.cookies()
for index, cookie in enumerate(cookies):
if cookie["name"] == "session":
session_cookie_index = index
break

flask_session_cookie = cookies[session_cookie_index]
flask_session_cookie_string = flask_session_cookie["value"]
decoded_data = decode_flask_session_cookie(flask_session_cookie_string)

access_token = json.loads(decoded_data)["access_token"]
decoded_access_token = jwt.decode(
access_token, options={"verify_signature": False}
)
exp_time = decoded_access_token["exp"]
iat_time = decoded_access_token["iat"]
token_lifetime = exp_time - iat_time
expected_lifetime = 300

assert (
abs(token_lifetime - expected_lifetime) < 5
), "Access token expiry does not match expected config"

refresh_token = json.loads(decoded_data)["refresh_token"]
decoded_refresh_token = jwt.decode(
refresh_token, options={"verify_signature": False}
)
exp_time = decoded_refresh_token["exp"]
iat_time = decoded_refresh_token["iat"]
token_lifetime = exp_time - iat_time
expected_lifetime = 7200
assert (
abs(token_lifetime - expected_lifetime) < 5
), "Refresh token expiry does not match expected config"


def decode_flask_session_cookie(cookie):
"""Decode a Flask cookie."""
Expand Down
61 changes: 35 additions & 26 deletions e2e_tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def test_search_transferring_body_pagination_get_first_page(
):
"""
Given a user on the search transferring body page
When they interact with the search form and submit a query with single search term
When they interact with the search form and submit a query with a single search term
Then the table should contain the expected headers and entries and pagination object
with first page link available
with the first page link available
"""
aau_user_page.goto(f"{self.browse_route_url}")
aau_user_page.locator("#search-input").click()
Expand All @@ -40,16 +40,17 @@ def test_search_transferring_body_pagination_get_first_page(
)
== "Pagination"
)
url = f" {self.route_url}/{self.transferring_body_id}?page=1&query=a "

url = f"{self.route_url}/{self.transferring_body_id}?page=1&query=a&search_area=everywhere"
assert (
aau_user_page.locator(
"data-testid=pagination-link"
).first.get_attribute("href")
aau_user_page.locator("data-testid=pagination-link")
.first.get_attribute("href")
.strip()
== url
)
links = aau_user_page.locator("data-testid=pagination-link-title").all()
assert links[0].text_content() == "Nextpage"
assert len(links) > 0, "No pagination links found"
assert links[0].text_content().strip() == "Nextpage"

def test_search_transferring_body_pagination_get_previous_page(
self, aau_user_page: Page
Expand Down Expand Up @@ -102,25 +103,28 @@ def test_search_transferring_body_pagination_get_next_page(
)
== "Pagination"
)
url = f" {self.route_url}/{self.transferring_body_id}?page=1&query=a "
assert (
aau_user_page.locator(
"data-testid=pagination-link"
).first.get_attribute("href")
== url
expected_url = f"{self.route_url}/{self.transferring_body_id}?page=1&query=a&search_area=everywhere"
actual_href = (
aau_user_page.locator("data-testid=pagination-link")
.first.get_attribute("href")
.strip()
)
aau_user_page.get_by_label("Page 2").click()
links = aau_user_page.locator("data-testid=pagination-link-title").all()
assert links[0].text_content() == "Previouspage"
if len(links) > 1:
assert links[1].text_content() == "Nextpage"
assert actual_href == expected_url

aau_user_page.locator(".govuk-pagination__link", has_text="2").click()

actual_relative_url = aau_user_page.url.split("://", 1)[1].split(
"/", 1
)[1]
expected_next_url = f"{self.route_url}/{self.transferring_body_id}?page=2&query=a&search_area=everywhere"
assert f"/{actual_relative_url}" == expected_next_url

def test_search_transferring_body_pagination_get_ellipses_page(
self, aau_user_page: Page
):
"""
Given a user on the search transferring body page
When they interact with the search form and submit a query with single search term
When they interact with the search form and submit a query with a single search term
Then the table should contain the expected headers and entries and pagination object
with ellipses page link
"""
Expand All @@ -138,17 +142,22 @@ def test_search_transferring_body_pagination_get_ellipses_page(
== "Pagination"
)
page_links = aau_user_page.locator("data-testid=pagination-link").all()
last_page = page_links[len(page_links) - 1].text_content()

assert page_links[0].inner_text() == "1"
assert page_links[1].inner_text() == "2"
assert page_links[3].text_content() == last_page

last_page = page_links[-1].text_content()
assert page_links[-1].text_content() == last_page

ellipsis_link = aau_user_page.locator(
".govuk-pagination__item--ellipses"
).all()
assert ellipsis_link[0].inner_text() == "…"
if ellipsis_link:
assert ellipsis_link[0].inner_text().strip() == "…"
else:
print("No ellipses links were found in the pagination.")

links = aau_user_page.locator("data-testid=pagination-link-title").all()
assert links[0].text_content() == "Nextpage"
assert links[0].text_content().strip() == "Nextpage"

def test_search_transferring_body_pagination_click_previous_page_link(
self, aau_user_page: Page
Expand Down Expand Up @@ -182,7 +191,7 @@ def test_search_transferring_body_pagination_click_previous_page_link(

aau_user_page.get_by_label("Page 1", exact=True).click()

url = f"{self.route_url}/{self.transferring_body_id}?page=1&query=a"
url = f"{self.route_url}/{self.transferring_body_id}?page=1&query=a&search_area=everywhere"
expect(aau_user_page).to_have_url(url)

def test_search_transferring_body_pagination_click_next_page_link(
Expand Down Expand Up @@ -221,7 +230,7 @@ def test_search_transferring_body_pagination_click_next_page_link(

aau_user_page.wait_for_selector(".govuk-pagination")

url = f"{self.route_url}/{self.transferring_body_id}?page=3&query=a"
url = f"{self.route_url}/{self.transferring_body_id}?page=3&query=a&search_area=everywhere"
expect(aau_user_page).to_have_url(url)

def test_search_transferring_body_pagination_get_last_page(
Expand Down
4 changes: 2 additions & 2 deletions e2e_tests/test_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def test_record_download_record(
Given the user navigates to the record page with ID "100251bb-5b93-48a9-953f-ad5bd9abfbdc"
When the user clicks the "Download record" button
Then the file "TSTA 1_ZD5B3S.doc" should be downloaded
Then the file "file-a2.txt" should be downloaded
"""
record_id = "100251bb-5b93-48a9-953f-ad5bd9abfbdc"
standard_user_page_with_download.goto(f"{self.route_url}/{record_id}")
Expand All @@ -187,7 +187,7 @@ def test_record_download_record(
"Download record"
).click()
download = download_record.value
assert "TSTA 1_ZD5B3S.doc" == download.suggested_filename
assert "file-a2.txt" == download.suggested_filename

def test_record_download_record_standard_user_without_download_perms(
self, standard_user_page: Page
Expand Down
Loading

0 comments on commit 97448ed

Please sign in to comment.