Clean up state of E2E tests

This changeset takes care of leaving E2E tests in a clean state so that
we can revisit the work later.  The efforts to add authentication
support did not pan out, so we will go a different route in the near
future.

Signed-off-by: Carlo Costino <carlo.costino@gsa.gov>
This commit is contained in:
Carlo Costino
2023-08-18 16:20:57 -04:00
parent f47deb1f99
commit d79e15c2a1
10 changed files with 422 additions and 175 deletions

View File

@@ -1,6 +1,7 @@
import copy
import json
import os
import re
from contextlib import contextmanager
from datetime import date, datetime, timedelta
from unittest.mock import Mock, PropertyMock
@@ -3347,13 +3348,94 @@ 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_auth_context(browser):
def end_to_end_context(browser):
# Create a context with HTTP Authentication credentials for Playwright E2E
# tests.
context = browser.new_context(http_credentials={
'username': os.environ.get('NOTIFY_STAGING_HTTP_AUTH_USER'),
'password': os.environ.get('NOTIFY_STAGING_HTTP_AUTH_PASSWORD'),
})
# tests, if the environment variables exist.
if os.getenv('NOTIFY_E2E_TEST_HTTP_AUTH_USER'):
context = browser.new_context(http_credentials={
'username': os.getenv('NOTIFY_E2E_TEST_HTTP_AUTH_USER'),
'password': os.getenv('NOTIFY_E2E_TEST_HTTP_AUTH_PASSWORD'),
})
else:
context = browser.new_context()
yield 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)
yield context