Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/scaffold UI test user on demand #2286

Merged
merged 43 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
9b3c5d1
initial commit
andrewleith Jul 18, 2024
7352db5
feat: create and destroy test users on demand
andrewleith Jul 22, 2024
de7d5b9
Merge branch 'main' into feat/scaffold-ui-test-user-on-demand
andrewleith Sep 13, 2024
5f5eb9f
chore(config): add some hardcoded UUIDs for cypress related items
andrewleith Sep 13, 2024
cfe8719
feat(cypress api): update create_test_user route:
andrewleith Sep 13, 2024
00dc686
feat(cypress api): update cleanup_stale_users route:
andrewleith Sep 13, 2024
6d1acba
chore: formatting
andrewleith Sep 13, 2024
7704b89
chore(format): formatting files I haven't touched :(
andrewleith Sep 13, 2024
0dd0721
chore: formatting
andrewleith Sep 13, 2024
7417e0e
chore: formatting
andrewleith Sep 13, 2024
9bb10ae
chore: formatting
andrewleith Sep 13, 2024
33e2ca7
chore: update docstrings; enhance exception handling
andrewleith Sep 16, 2024
490e666
fix(cypress api): use service id from config
andrewleith Sep 17, 2024
ae3be90
chore: add more cypress values to config
andrewleith Sep 17, 2024
17b4a2b
feat(cypress api): dont pass password around since its already a secr…
andrewleith Sep 23, 2024
425e156
feat(cypress data): migration to create cypress service, permissions,…
andrewleith Sep 23, 2024
3be6c6c
Merge branch 'main' into feat/scaffold-ui-test-user-on-demand
andrewleith Sep 27, 2024
f4c313a
feat(create_test_user): update delete logic to be more complete; para…
andrewleith Sep 28, 2024
74dd85d
chore: formatting
andrewleith Sep 28, 2024
0b1584a
Merge branch 'main' into feat/scaffold-ui-test-user-on-demand
andrewleith Oct 4, 2024
9608861
chore: remove unreachable code
andrewleith Oct 4, 2024
b3f9d5d
task: add config values for testing
andrewleith Oct 25, 2024
09d027c
feat(auth): add separate auth mechanism for ui testing
andrewleith Oct 25, 2024
dd93318
chore: add tests
andrewleith Oct 25, 2024
e4ed50a
chore: formatting
andrewleith Oct 25, 2024
b38cb7a
chore: fix tests
andrewleith Oct 25, 2024
0afb435
chore: remove unused code
andrewleith Oct 25, 2024
4e4027d
Merge branch 'main' into feat/scaffold-ui-test-user-on-demand
andrewleith Oct 25, 2024
350649c
chore: fix migration due to incoming migrations
andrewleith Oct 28, 2024
1cdb7ea
Merge branch 'main' into feat/scaffold-ui-test-user-on-demand
andrewleith Nov 8, 2024
0a31d2c
chore: re-number migrations due to merge
andrewleith Nov 8, 2024
c0f4456
task: pass new secret `CYPRESS_USER_PW_SECRET` into CI
andrewleith Nov 8, 2024
6518911
chore: add some debugging to the workflow
andrewleith Nov 8, 2024
56f8cb2
task: debug workflow
andrewleith Nov 8, 2024
911b957
fix(workflow): put the env statement in the right place
andrewleith Nov 8, 2024
bbfdb84
chore: fix workflow
andrewleith Nov 8, 2024
baeb377
chore: add __init__.py in `tests/app/cypress` folder to make tests work
andrewleith Nov 8, 2024
7dc741f
Plz [review] !
andrewleith Nov 8, 2024
1bf62de
chore: fix incorrect descriptions
andrewleith Nov 8, 2024
18458ad
fix: correct typo in function name
andrewleith Nov 8, 2024
a566d52
fix: use cds domain instead of gov.uk
andrewleith Nov 8, 2024
d3d877c
chore: fix failing test
andrewleith Nov 8, 2024
15d248a
Merge branch 'main' into feat/scaffold-ui-test-user-on-demand
andrewleith Nov 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def register_blueprint(application):
template_statistics as template_statistics_blueprint,
)
from app.user.rest import user_blueprint

from app.cypress.rest import cypress_blueprint
register_notify_blueprint(application, service_blueprint, requires_admin_auth, "/service")

register_notify_blueprint(application, user_blueprint, requires_admin_auth, "/user")
Expand Down Expand Up @@ -270,6 +270,8 @@ def register_blueprint(application):

register_notify_blueprint(application, template_category_blueprint, requires_admin_auth)

register_notify_blueprint(application, cypress_blueprint, requires_admin_auth, "/cypress")

register_notify_blueprint(application, cache_blueprint, requires_cache_clear_auth)


Expand Down
4 changes: 4 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ class Config(object):
DEFAULT_TEMPLATE_CATEGORY_MEDIUM = "f75d6706-21b7-437e-b93a-2c0ab771e28e"
DEFAULT_TEMPLATE_CATEGORY_HIGH = "c4f87d7c-a55b-4c0f-91fe-e56c65bb1871"

# UUIDs for Cypress tests
CYPRESS_SERVICE_ID = "5c8a0501-2aa8-433a-ba51-cefb8063ab93"
CYPRESS_TEST_USER_ID = "5e8fdc9b-4080-430d-962a-8065a1a17274"

# Allowed service IDs able to send HTML through their templates.
ALLOW_HTML_SERVICE_IDS: List[str] = [id.strip() for id in os.getenv("ALLOW_HTML_SERVICE_IDS", "").split(",")]

Expand Down
Empty file added app/cypress/__init__.py
Empty file.
145 changes: 145 additions & 0 deletions app/cypress/rest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import hashlib
import logging
Fixed Show fixed Hide fixed
import uuid

from datetime import datetime, timedelta
from flask import Blueprint, current_app, jsonify, request

from app import db
from app.dao.services_dao import dao_add_user_to_service
from app.dao.templates_dao import dao_update_template
from app.dao.users_dao import save_model_user
from app.errors import register_errors
from app.models import LoginEvent, Permission, Service, ServiceUser, Template, TemplateHistory, TemplateRedacted, User, VerifyCode

"""
This module will be used by the cypress tests to create users on the fly whenever a test suite is run.

Additionally, this module will also be used to clean up test users periodically to keep the data footprint small.
"""

cypress_blueprint = Blueprint("cypress", __name__)
register_errors(cypress_blueprint)

@cypress_blueprint.route("/create_user/<email_name>", methods=["POST"])
def create_test_user(email_name):
data = request.get_json()
password = data.get('password')

if current_app.config["NOTIFY_ENVIRONMENT"] == "production":
return jsonify(message="Forbidden"), 403

# Create the user
data = {
"id": uuid.uuid4(),
"name": "Notify UI testing account",
"email_address": f"notify-ui-tests+{email_name}@cds-snc.ca",
"password": hashlib.sha256((password + current_app.config["DANGEROUS_SALT"]).encode("utf-8")).hexdigest(), #"e01221bcb56f1fb931c0ca310e2a13e23390b267b4cd80dc36366b6c9ef8eb5d", # TODO: move this to a secret!
Fixed Show fixed Hide fixed
"mobile_number": "9025555555",
"state": "active",
"blocked": False,
}

user = User(**data)
save_model_user(user)

# add user to cypress service w/ full permissions
service = Service.query.filter_by(id="5c8a0501-2aa8-433a-ba51-cefb8063ab93").first()
permissions = []
for p in ["manage_users", "manage_templates", "manage_settings", "send_texts", "send_emails", "send_letters", "manage_api_keys", "view_activity"]:
permissions.append(Permission(permission=p))

dao_add_user_to_service(service, user, permissions=permissions)

return jsonify(user.serialize()), 201


def _destroy_test_user(email_name):
CYPRESS_TEST_USER_ID = current_app.config["CYPRESS_TEST_USER_ID"]

user = User.query.filter_by(email_address=f"notify-ui-tests+{email_name}@cds-snc.ca").first()

if not user:
return

try:
# update the created_by field for each template to use id CYPRESS_TEST_USER_ID
templates = Template.query.filter_by(created_by=user).all()
for template in templates:
template.created_by_id = CYPRESS_TEST_USER_ID
dao_update_template(template)

# update the created_by field for each template to use id CYPRESS_TEST_USER_ID
history_templates = TemplateHistory.query.filter_by(created_by=user).all()
for templateh in history_templates:
templateh.created_by_id = CYPRESS_TEST_USER_ID
db.session.add(templateh)

# update the created_by field for each template_redacted to use id CYPRESS_TEST_USER_ID
redacted_templates = TemplateRedacted.query.filter_by(updated_by=user).all()
for templater in redacted_templates:
templater.updated_by_id = CYPRESS_TEST_USER_ID
db.session.add(templater)

# Update services create by this user to use CYPRESS_TEST_USER_ID
services = Service.query.filter_by(created_by=user).all()
for service in services:
service.created_by_id = CYPRESS_TEST_USER_ID
db.session.add(service)

# remove all the login events for this user
LoginEvent.query.filter_by(user=user).delete()

# remove all permissions for this user
Permission.query.filter_by(user=user).delete()

# remove user_to_service entries
ServiceUser.query.filter_by(user_id=user.id).delete()

# remove verify codes
VerifyCode.query.filter_by(user=user).delete()

# remove the user
User.query.filter_by(email_address=f"notify-ui-tests+{email_name}@cds-snc.ca").delete()

print("removal success: " + email_name)
except Exception as e:
print(f"Error cleaning up test user: {e}")
db.session.rollback()


"""
Endpoint for cleaning up stale users. This endpoint will only be used internally by the Cypress tests.

This endpoint is responsible for removing stale testing users from the database.
Stale users are identified as users whose email addresses match the pattern "%notify-ui-tests+%@cds-snc.ca%" and whose creation time is older than three hours ago.

Returns:
A JSON response with a success message if the cleanup is successful, or an error message if an exception occurs during the cleanup process.
"""
@cypress_blueprint.route("/cleanup", methods=["GET"])
def cleanup_stale_users():
if current_app.config["NOTIFY_ENVIRONMENT"] == "production":
return jsonify(message="Forbidden"), 403

three_hours_ago = datetime.utcnow() - timedelta(hours=3)
users = User.query.filter(User.email_address.like(f"%notify-ui-tests+%@cds-snc.ca%"),
User.created_at < three_hours_ago).all()

# get the list of email_address property from users
users_emails = [user for user in users]

print('users to clean: ' + str(users_emails))

# loop through users and call destroy_user on each one
for user in users:
user_email = user.email_address.split("+")[1].split("@")[0]
print("Trying to remove:" + user_email)

try:
_destroy_test_user(user_email)
except Exception as e:
return jsonify(message="Error cleaning up"), 500

db.session.commit()
return jsonify(message="Zeds dead, baby"), 201
Loading