Skip to content

Commit

Permalink
Merge pull request #1996 from GSA/e2e_tests_uncomment
Browse files Browse the repository at this point in the history
Make e2e tests work fully with staging API
  • Loading branch information
terrazoon authored Oct 15, 2024
2 parents 2624bd8 + d86b1af commit 34ccfb5
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 85 deletions.
4 changes: 2 additions & 2 deletions .ds.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
"filename": ".github/workflows/checks.yml",
"hashed_secret": "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8",
"is_verified": false,
"line_number": 66,
"line_number": 68,
"is_secret": false
}
],
Expand Down Expand Up @@ -684,5 +684,5 @@
}
]
},
"generated_at": "2024-09-03T17:36:57Z"
"generated_at": "2024-10-07T20:44:40Z"
}
10 changes: 8 additions & 2 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ jobs:

end-to-end-tests:
if: ${{ github.actor != 'dependabot[bot]' }}

permissions:
checks: write
pull-requests: write
contents: write
runs-on: ubuntu-latest
environment: staging
services:
postgres:
image: postgres
Expand Down Expand Up @@ -97,21 +99,25 @@ jobs:
# insert this line:
# tail -f admin-server.log &
# above make e2e-test


run: |
make run-flask > admin-server.log 2>&1 &
tail -f admin-server.log &
make e2e-test
env:
API_HOST_NAME: https://notify-api-staging.app.cloud.gov/
DANGEROUS_SALT: ${{ secrets.DANGEROUS_SALT }}
SECRET_KEY: ${{ secrets.SECRET_KEY }}
DANGEROUS_SALT: ${{ secrets.DANGEROUS_SALT }}
ADMIN_CLIENT_SECRET: ${{ secrets.ADMIN_CLIENT_SECRET }}
ADMIN_CLIENT_USERNAME: notify-admin
NOTIFY_ENVIRONMENT: e2etest
NOTIFY_E2E_AUTH_STATE_PATH: ${{ secrets.NOTIFY_E2E_AUTH_STATE_PATH }}
NOTIFY_E2E_TEST_EMAIL: ${{ secrets.NOTIFY_E2E_TEST_EMAIL }}
NOTIFY_E2E_TEST_PASSWORD: ${{ secrets.NOTIFY_E2E_TEST_PASSWORD }}
NOTIFY_E2E_TEST_URI: http://localhost:6012/

VCAP_SERVICES: ${{ secrets.VCAP_SERVICES }}
validate-new-relic-config:
runs-on: ubuntu-latest
environment: staging
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,3 +507,13 @@ insurance.
For more information on what we're working on, the Notify tool, and how to get
involved with our team,
[see our flyer.](https://github.com/GSA/notifications-admin/blob/main/docs/notify-pilot-flyer.md)

## Updating secrets for the E2E tests

At some point, E2E tests will fail because the secrets held in VCAP_SERVICES have expired. To refresh
them, you will need to do the following:

1. Log in the normal way to access cloudfoundry command line options
2. In your terminal, run `chmod +x print_vcap.sh`
3. In your terminal, run `./print_vcap.sh`
4. Copy the value in your terminal and paste it into the VCAP_SERVICES secret in Github on the staging tier.
12 changes: 9 additions & 3 deletions app/main/views/send.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,9 +802,15 @@ def get_skip_link(step_index, template):
)
@user_has_permissions("send_messages", restrict_admin_usage=True)
def send_one_off_to_myself(service_id, template_id):
db_template = current_service.get_template_with_user_permission_or_403(
template_id, current_user
)
current_app.logger.info("Send one off to myself")
try:
db_template = current_service.get_template_with_user_permission_or_403(
template_id, current_user
)
except Exception:
current_app.logger.exception("Couldnt get template for one off")
# Use 406 just because we're limited to certain codes here and it will point us back to a problem here
abort(406)

if db_template["template_type"] not in ("sms", "email"):
abort(404)
Expand Down
41 changes: 23 additions & 18 deletions app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def is_pending(self):
@property
def is_gov_user(self):
is_gov = is_gov_user(self.email_address)
current_app.logger.info(f"User {self.id} is_gov_user: {is_gov}")
# current_app.logger.info(f"User {self.id} is_gov_user: {is_gov}")
return is_gov

@property
Expand All @@ -210,9 +210,9 @@ def is_authenticated(self):

@property
def platform_admin(self):
current_app.logger.warn(
f"Checking User {self.id} for platform admin: {self._platform_admin}"
)
# current_app.logger.warning(
# f"Checking User {self.id} for platform admin: {self._platform_admin}"
# )
return self._platform_admin and not session.get(
"disable_platform_admin_view", False
)
Expand Down Expand Up @@ -242,42 +242,47 @@ def has_permissions(
# we shouldn't have any pages that require permissions, but don't specify a service or organization.
# use @user_is_platform_admin for platform admin only pages
# raise NotImplementedError
current_app.logger.warn(f"VIEW ARGS ARE {request.view_args}")
# current_app.logger.warning(f"VIEW ARGS ARE {request.view_args}")
pass

log_msg = f"has_permissions user: {self.id} service: {service_id}"
# platform admins should be able to do most things (except eg send messages, or create api keys)
if self.platform_admin and not restrict_admin_usage:
current_app.logger.warn(f"{log_msg} true because user is platform_admin")
current_app.logger.debug(
"has_permissions is true because user is platform_admin"
)
return True

if org_id:
value = self.belongs_to_organization(org_id)
current_app.logger.warn(f"{log_msg} org: {org_id} returning {value}")
current_app.logger.debug(
f"has_permissions returns org: {org_id} returning {value}"
)
return value

if not permissions and self.belongs_to_service(service_id):
current_app.logger.warn(f"{log_msg} True because belongs_to_service")
current_app.logger.debug("has_permissions True because belongs_to_service")
return True

if any(self.permissions_for_service(service_id) & set(permissions)):
current_app.logger.warn(f"{log_msg} permissions valid")
current_app.logger.debug(
"has_permissions returns True because permissions valid"
)
return True

from app.models.service import Service

org_value = allow_org_user and self.belongs_to_organization(
Service.from_id(service_id).organization_id
)
current_app.logger.warn(f"{log_msg} returning {org_value}")
current_app.logger.debug(f"has_permissions returning {org_value}")
return org_value

def permissions_for_service(self, service_id):
return self._permissions.get(service_id, set())

def has_permission_for_service(self, service_id, permission):
has_permission = permission in self.permissions_for_service(service_id)
current_app.logger.warn(
current_app.logger.debug(
f"has_permission_for_service user: {self.id} service: {service_id} "
f"permission: {permission} retuning {has_permission}"
)
Expand Down Expand Up @@ -558,17 +563,17 @@ def from_session(cls):
return cls.by_id(invited_user_id) if invited_user_id else None

def has_permissions(self, *permissions):
current_app.logger.warn(
f"Checking invited user {self.id} for permissions: {permissions}"
)
# current_app.logger.warning(
# f"Checking invited user {self.id} for permissions: {permissions}"
# )
if self.status == "cancelled":
return False
return set(self.permissions) > set(permissions)

def has_permission_for_service(self, service_id, permission):
current_app.logger.warn(
f"Checking invited user {self.id} for permission: {permission} on service {service_id}"
)
# current_app.logger.warn(
# f"Checking invited user {self.id} for permission: {permission} on service {service_id}"
# )
if self.status == "cancelled":
return False
return self.service == service_id and permission in self.permissions
Expand Down
5 changes: 4 additions & 1 deletion app/s3_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ def get_s3_object(
aws_secret_access_key=secret_key,
region_name=region,
)
s3 = session.resource("s3", config=AWS_CLIENT_CONFIG)
s3 = session.resource(
"s3",
config=AWS_CLIENT_CONFIG,
)
obj = s3.Object(bucket_name, filename)
# This 'proves' that use of moto in the relevant tests in test_send.py
# mocks everything related to S3. What you will see in the logs is:
Expand Down
9 changes: 9 additions & 0 deletions app/s3_client/s3_csv_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import uuid

from flask import current_app
Expand Down Expand Up @@ -33,6 +34,14 @@ def s3upload(service_id, filedata):
bucket_name, file_location, access_key, secret_key, region = get_csv_location(
service_id, upload_id
)
if bucket_name == "":
exp_bucket = current_app.config["CSV_UPLOAD_BUCKET"]["bucket"]
exp_region = current_app.config["CSV_UPLOAD_BUCKET"]["region"]
tier = os.getenv("NOTIFY_ENVIRONMENT")
raise Exception(
f"NO BUCKET NAME SHOULD BE: {exp_bucket} WITH REGION {exp_region} TIER {tier}"
)

utils_s3upload(
filedata=filedata["data"],
region=region,
Expand Down
4 changes: 2 additions & 2 deletions app/templates/partials/jobs/status.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h2>Delivery Status</h2>
{% if job.scheduled_for %}
<div class="usa-alert usa-alert--info">
<div class="usa-alert__body">
<h4 class="usa-alert__heading">Your text has been scheduled</h4>
<h2 class="usa-alert__heading">Your text has been scheduled</h2>
<p class="usa-alert__text">
{{ job.template_name }} - {{ current_service.name }} was scheduled on {{ job.scheduled_for|format_datetime_normal }} by {{ job.created_by.name }}
</p>
Expand All @@ -35,7 +35,7 @@ <h4 class="usa-alert__heading">Your text has been scheduled</h4>
{% else %}
<div class="usa-alert usa-alert--success">
<div class="usa-alert__body">
<h4 class="usa-alert__heading">Your text has been sent</h4>
<h2 class="usa-alert__heading">Your text has been sent</h2>
<p class="usa-alert__text">
{{ job.template_name }} - {{ current_service.name }} was sent on {% if job.processing_started %}
{{ job.processing_started|format_datetime_table }} {% else %}
Expand Down
5 changes: 4 additions & 1 deletion notifications_utils/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ def s3upload(
aws_secret_access_key=secret_key,
region_name=region,
)
_s3 = session.resource("s3", config=AWS_CLIENT_CONFIG)
_s3 = session.resource(
"s3",
config=AWS_CLIENT_CONFIG,
)
# This 'proves' that use of moto in the relevant tests in test_send.py
# mocks everything related to S3. What you will see in the logs is:
# Exception: CREATED AT <MagicMock name='resource().Bucket().creation_date' id='4665562448'>
Expand Down
17 changes: 17 additions & 0 deletions print_vcap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

STAGING_APP_NAME="notify-admin-staging"

# Fetch the environment variables of the staging app
env_var_value=$(cf env "$STAGING_APP_NAME" | awk '/'"VCAP_SERVICES"':/,/^}/')


# Check if the environment variable was found"
if [ -z "$env_var_value" ]; then
echo "Environment variable VCAP_SERVICES not found in the staging environment"
else
env_var_json=$(echo "$env_var_value" | sed '1s/^[^:]*: //' | tr -d '\n')
stringified_value=$(python3 -c "import json, sys; print(json.dumps(json.loads(sys.stdin.read())))" <<< "$env_var_json")
echo "VCAP_SERVICES:"
echo "$stringified_value"
fi
1 change: 1 addition & 0 deletions tests/end_to_end/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ def check_axe_report(page):
for violation in results["violations"]:
assert violation["impact"] in [
"minor",
"moderate",
], f"Accessibility violation: {violation}"
3 changes: 3 additions & 0 deletions tests/end_to_end/test_accounts_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,19 @@ def test_add_new_service_workflow(authenticated_page, end_to_end_context):

# Check to make sure that we've arrived at the next page.
page.wait_for_load_state("domcontentloaded")
check_axe_report(page)

page.click("text='Delete this service'")

# Check to make sure that we've arrived at the next page.
page.wait_for_load_state("domcontentloaded")
check_axe_report(page)

page.click("text='Yes, delete'")

# Check to make sure that we've arrived at the next page.
page.wait_for_load_state("domcontentloaded")
check_axe_report(page)

# Check to make sure that we've arrived at the next page.
# Check the page title exists and matches what we expect.
Expand Down
Loading

0 comments on commit 34ccfb5

Please sign in to comment.