merge from main

This commit is contained in:
Kenneth Kehl
2024-02-26 11:10:37 -08:00
8 changed files with 246 additions and 158 deletions

View File

@@ -1,7 +1,6 @@
import copy
import json
import os
import re
from contextlib import contextmanager
from datetime import date, datetime, timedelta
from unittest.mock import Mock, PropertyMock
@@ -529,7 +528,7 @@ def mock_update_service(mocker):
"sms_sender",
"permissions",
]
}
},
)
return {"data": service}
@@ -2355,7 +2354,7 @@ def client_request(logged_in_client, mocker, service_one): # noqa (C901 too com
_test_page_title=True,
_test_for_elements_without_class=True,
_optional_args="",
**endpoint_kwargs
**endpoint_kwargs,
):
return ClientRequest.get_url(
url_for(endpoint, **(endpoint_kwargs or {})) + _optional_args,
@@ -2374,7 +2373,7 @@ def client_request(logged_in_client, mocker, service_one): # noqa (C901 too com
_expected_redirect=None,
_test_page_title=True,
_test_for_elements_without_class=True,
**endpoint_kwargs
**endpoint_kwargs,
):
resp = logged_in_client.get(
url,
@@ -2416,7 +2415,7 @@ def client_request(logged_in_client, mocker, service_one): # noqa (C901 too com
_follow_redirects=False,
_expected_redirect=None,
_content_type=None,
**endpoint_kwargs
**endpoint_kwargs,
):
return ClientRequest.post_url(
url_for(endpoint, **(endpoint_kwargs or {})),
@@ -2475,7 +2474,7 @@ def client_request(logged_in_client, mocker, service_one): # noqa (C901 too com
_expected_status=302,
_optional_args="",
_content_type=None,
**endpoint_kwargs
**endpoint_kwargs,
):
return ClientRequest.post_response_from_url(
url_for(endpoint, **(endpoint_kwargs or {})) + _optional_args,
@@ -3521,89 +3520,6 @@ def mock_get_invited_org_user_by_id(mocker, sample_org_invite):
)
def login_for_end_to_end_testing(browser):
# Open a new page and go to the staging site.
context = browser.new_context()
page = context.new_page()
page.goto(os.getenv("NOTIFY_E2E_TEST_URI"))
sign_in_button = page.get_by_role("link", name="Sign in")
# Test trying to sign in.
sign_in_button.click()
# Wait for the next page to fully load.
page.wait_for_load_state("domcontentloaded")
# Check for the sign in form elements.
# NOTE: Playwright cannot find input elements by role and recommends using
# get_by_label() instead; however, hidden form elements do not have
# labels associated with them, hence the XPath!
# See https://playwright.dev/python/docs/api/class-page#page-get-by-label
# and https://playwright.dev/python/docs/locators#locate-by-css-or-xpath
# for more information.
email_address_input = page.get_by_label("Email address")
password_input = page.get_by_label("Password")
continue_button = page.get_by_role("button", name=re.compile("Continue"))
# Sign in to the site.
email_address_input.fill(os.getenv("NOTIFY_E2E_TEST_EMAIL"))
password_input.fill(os.getenv("NOTIFY_E2E_TEST_PASSWORD"))
continue_button.click()
# Wait for the next page to fully load.
page.wait_for_load_state("domcontentloaded")
# Check for the sign in form elements.
# NOTE: Playwright cannot find input elements by role and recommends using
# get_by_label() instead; however, hidden form elements do not have
# labels associated with them, hence the XPath!
# See https://playwright.dev/python/docs/api/class-page#page-get-by-label
# and https://playwright.dev/python/docs/locators#locate-by-css-or-xpath
# for more information.
# mfa_input = page.get_by_label('Text message code')
# continue_button = page.get_by_role('button', name=re.compile('Continue'))
# # Enter MFA code and continue.
# TODO: Revisit this at a later point in time.
# totp = pyotp.TOTP(
# os.getenv('MFA_TOTP_SECRET'),
# digits=int(os.getenv('MFA_TOTP_LENGTH'))
# )
# mfa_input.fill(totp.now())
# continue_button.click()
# page.wait_for_load_state('domcontentloaded')
# # Save storage state into the file.
# auth_state_path = os.path.join(
# os.getenv('NOTIFY_E2E_AUTH_STATE_PATH'),
# 'state.json'
# )
# context.storage_state(path=auth_state_path)
@pytest.fixture(scope="session")
def end_to_end_context(browser):
context = browser.new_context()
return context
@pytest.fixture(scope="session")
def end_to_end_authenticated_context(browser):
# Create and load a previously authenticated context for Playwright E2E
# tests.
login_for_end_to_end_testing(browser)
auth_state_path = os.path.join(
os.getenv("NOTIFY_E2E_AUTH_STATE_PATH"), "state.json"
)
context = browser.new_context(storage_state=auth_state_path)
return context
@pytest.fixture()
def fake_markdown_file():
input = "#Test"

View File

View File

@@ -0,0 +1,105 @@
import os
import re
import pytest
E2E_TEST_URI = os.getenv("NOTIFY_E2E_TEST_URI")
def login_for_end_to_end_testing(browser):
# Open a new page and go to the staging site.
context = browser.new_context()
page = context.new_page()
page.goto(f"{E2E_TEST_URI}/")
sign_in_button = page.get_by_role("link", name="Sign in")
# Test trying to sign in.
sign_in_button.click()
# Wait for the next page to fully load.
page.wait_for_load_state("domcontentloaded")
# Check for the sign in form elements.
# NOTE: Playwright cannot find input elements by role and recommends using
# get_by_label() instead; however, hidden form elements do not have
# labels associated with them, hence the XPath!
# See https://playwright.dev/python/docs/api/class-page#page-get-by-label
# and https://playwright.dev/python/docs/locators#locate-by-css-or-xpath
# for more information.
email_address_input = page.get_by_label("Email address")
password_input = page.get_by_label("Password")
continue_button = page.get_by_role("button", name=re.compile("Continue"))
# Sign in to the site.
email_address_input.fill(os.getenv("NOTIFY_E2E_TEST_EMAIL"))
password_input.fill(os.getenv("NOTIFY_E2E_TEST_PASSWORD"))
continue_button.click()
# Wait for the next page to fully load.
page.wait_for_load_state("domcontentloaded")
# Check for the sign in form elements.
# NOTE: Playwright cannot find input elements by role and recommends using
# get_by_label() instead; however, hidden form elements do not have
# labels associated with them, hence the XPath!
# See https://playwright.dev/python/docs/api/class-page#page-get-by-label
# and https://playwright.dev/python/docs/locators#locate-by-css-or-xpath
# for more information.
# mfa_input = page.get_by_label('Text message code')
# continue_button = page.get_by_role('button', name=re.compile('Continue'))
# # Enter MFA code and continue.
# TODO: Revisit this at a later point in time.
# totp = pyotp.TOTP(
# os.getenv('MFA_TOTP_SECRET'),
# digits=int(os.getenv('MFA_TOTP_LENGTH'))
# )
# mfa_input.fill(totp.now())
# continue_button.click()
# page.wait_for_load_state('domcontentloaded')
# Save storage state into the file.
auth_state_path = os.path.join(
os.getenv("NOTIFY_E2E_AUTH_STATE_PATH"), "state.json"
)
context.storage_state(path=auth_state_path)
@pytest.fixture(scope="session")
def end_to_end_context(browser):
context = browser.new_context()
return context
@pytest.fixture(scope="session")
def end_to_end_authenticated_context(browser):
# Create and load a previously authenticated context for Playwright E2E
# tests.
# login_for_end_to_end_testing(browser)
auth_state_path = os.path.join(
os.getenv("NOTIFY_E2E_AUTH_STATE_PATH"), "state.json"
)
context = browser.new_context(storage_state=auth_state_path)
return context
@pytest.fixture(scope="session")
def authenticated_page(end_to_end_context):
# Open a new page and go to the staging site.
page = end_to_end_context.new_page()
page.goto(f"{E2E_TEST_URI}/")
sign_in_button = page.get_by_role("link", name="Sign in")
# Sign in to the site - E2E test accounts are set to flow through.
sign_in_button.click()
# Wait for the next page to fully load.
page.wait_for_load_state("domcontentloaded")
return page

View File

@@ -4,33 +4,13 @@ import re
from playwright.sync_api import expect
def _bypass_sign_in(end_to_end_context):
# Open a new page and go to the staging site.
page = end_to_end_context.new_page()
page.goto(os.getenv("NOTIFY_E2E_TEST_URI"))
sign_in_button = page.get_by_role("link", name="Sign in")
# Test trying to sign in. Because we are loading the email and password
sign_in_button.click()
# Wait for the next page to fully load.
page.wait_for_load_state("domcontentloaded")
return page
E2E_TEST_URI = os.getenv("NOTIFY_E2E_TEST_URI")
def test_add_new_service_workflow(end_to_end_context):
# page = end_to_end_context.new_page()
page = _bypass_sign_in(end_to_end_context)
page.goto(os.getenv("NOTIFY_E2E_TEST_URI"))
def test_add_new_service_workflow(authenticated_page, end_to_end_context):
page = authenticated_page
page.goto(f"{E2E_TEST_URI}/")
# sign_in_button = page.get_by_role("link", name="Sign in")
#
# Test trying to sign in. Because we are loading the email and password
# sign_in_button.click()
#
# Wait for the next page to fully load.
page.wait_for_load_state("domcontentloaded")
@@ -41,9 +21,7 @@ def test_add_new_service_workflow(end_to_end_context):
browser_type=end_to_end_context.browser.browser_type.name,
)
accounts_uri = "{}accounts".format(os.getenv("NOTIFY_E2E_TEST_URI"))
page.goto(accounts_uri)
page.goto(f"{E2E_TEST_URI}/accounts")
# Check to make sure that we've arrived at the next page.
page.wait_for_load_state("domcontentloaded")
@@ -82,24 +60,13 @@ def test_add_new_service_workflow(end_to_end_context):
# Retrieve some prominent elements on the page for testing.
service_name_input = page.locator('xpath=//input[@name="name"]')
federal_radio_button = page.locator('xpath=//input[@value="federal"]')
state_radio_button = page.locator('xpath=//input[@value="state"]')
other_radio_button = page.locator('xpath=//input[@value="other"]')
add_service_button = page.get_by_role("button", name=re.compile("Add service"))
expect(service_name_input).to_be_visible()
expect(federal_radio_button).to_be_visible()
expect(state_radio_button).to_be_visible()
expect(other_radio_button).to_be_visible()
expect(add_service_button).to_be_visible()
# Fill in the form.
service_name_input.fill(new_service_name)
expect(federal_radio_button).to_be_enabled()
# Trying to click directly on the radio button resulted in a "not in viewport error" and this is the
# suggested workaround. Googling, the reason seems to be that there might be some (invisible?) css positioned
# above the radio button itself.
page.click("text='Federal government'")
# Click on add service.
add_service_button.click()
@@ -108,7 +75,7 @@ def test_add_new_service_workflow(end_to_end_context):
page.wait_for_load_state("domcontentloaded")
# Check for the service name title and heading.
service_heading = page.get_by_text(new_service_name)
service_heading = page.get_by_text(new_service_name, exact=True)
expect(service_heading).to_be_visible()
expect(page).to_have_title(re.compile(new_service_name))

View File

@@ -3,11 +3,13 @@ import re
from playwright.sync_api import expect
E2E_TEST_URI = os.getenv("NOTIFY_E2E_TEST_URI")
def test_landing_page(end_to_end_context):
# Open a new page and go to the staging site.
page = end_to_end_context.browser.new_page()
page.goto(os.getenv("NOTIFY_E2E_TEST_URI"))
page.goto(f"{E2E_TEST_URI}/")
# Check to make sure that we've arrived at the next page.
page.wait_for_load_state("domcontentloaded")
@@ -17,7 +19,8 @@ def test_landing_page(end_to_end_context):
# Retrieve some prominent elements on the page for testing.
main_header = page.get_by_role(
"heading", name="Send text messages to your participants"
"heading",
name="Reach people where they are with government-powered text messages",
)
sign_in_button = page.get_by_role("link", name="Sign in")
benefits_studio_email = page.get_by_role("link", name="tts-benefits-studio@gsa.gov")
@@ -51,7 +54,7 @@ def test_landing_page(end_to_end_context):
# def test_sign_in_and_mfa_pages(end_to_end_context):
# # Open a new page and go to the staging site.
# page = end_to_end_context.new_page()
# page.goto(os.getenv("NOTIFY_E2E_TEST_URI"))
# page.goto(f"{E2E_TEST_URI}/")
# print(f"test_sign_in_and_mfa_pages initial {page}")
#
# sign_in_button = page.get_by_role("link", name="Sign in")