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

Task/add cypress to ci #1955

Draft
wants to merge 60 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
5231de0
ci(cypress): add cypress workflow to CI
andrewleith Aug 8, 2023
7933461
test: add a sanity check to see if the API is running in cypress
andrewleith Aug 8, 2023
1e169f6
fix(cypress workflow): pass cypress the cypress.env.json file
andrewleith Aug 8, 2023
3c8da4d
fix: pass env to flask
andrewleith Aug 8, 2023
05a481c
fix(cypress ci): pass more env vars
andrewleith Aug 8, 2023
26c48cc
chore(ci/cypress): remove db upgrade for now
andrewleith Aug 8, 2023
63e0af9
chore(ci): try to get the db and redis setup
andrewleith Aug 8, 2023
07b855e
chore(ci): update redis port config
andrewleith Aug 8, 2023
bd1edf6
chore(ci): checkout the app :facepalm:
andrewleith Aug 8, 2023
8c25a10
chore(ci): restore libcurl jobs
andrewleith Aug 8, 2023
d5c3d43
chore(ci): updated db url
andrewleith Aug 8, 2023
7bbe305
chore(ci): add host to createdb command
andrewleith Aug 8, 2023
052331c
chore(ci): try a different hostname
andrewleith Aug 8, 2023
914e5a8
chore(ci): try localhost
andrewleith Aug 8, 2023
574ad57
chore(ci): remove createdb step
andrewleith Aug 8, 2023
a31ecae
chore(ci): temporarily remove db upgrade command
andrewleith Aug 8, 2023
76e02f1
chore(ci): flail wildly
andrewleith Aug 8, 2023
e8e8f8a
chore(ci): add flask debug
andrewleith Aug 8, 2023
634fee7
chore(ci): add missing env vars to job (not passing any actual secret…
andrewleith Aug 9, 2023
66fd0cd
fix(ci): setup .env a different way
andrewleith Aug 9, 2023
367a09d
fix(ci): use correct env file syntax
andrewleith Aug 9, 2023
75414f1
fix(ci): :pray:
andrewleith Aug 9, 2023
c987644
chore(ci): add some debugging statements
andrewleith Aug 9, 2023
7ee0092
fix(ci): add version file :/
andrewleith Aug 9, 2023
4ddc4b5
fix(ci): force flask to the bg
andrewleith Aug 9, 2023
a60e3a3
fix(ci): correct path to test
andrewleith Aug 9, 2023
60092c9
fix(ci): update db credentials
andrewleith Aug 9, 2023
d92abdb
fix(ci): update db uri
andrewleith Aug 9, 2023
06141cf
fix(ci): update db uri again
andrewleith Aug 9, 2023
9cdee56
fix(ci): update database name in URI
andrewleith Aug 9, 2023
8b355bc
feat(ci): migrate the db before running the tests
andrewleith Aug 10, 2023
93eafb2
fix(ci): use secrets from github secrets; seed db with test data
andrewleith Aug 10, 2023
ec70f39
feat(seed): 1st attempt at seeding data for cypress tests
andrewleith Aug 10, 2023
532e5a2
fix(ci): trigger seeding with poetry
andrewleith Aug 10, 2023
e22de4e
fix(ci): hardcode db uri for now
andrewleith Aug 10, 2023
2fe2b01
fix(seeding): update seed script
andrewleith Aug 10, 2023
cfdfea3
fix(ci): use temp db uri
andrewleith Aug 10, 2023
48c4679
fix(ci): insert org first
andrewleith Aug 10, 2023
e8c92e5
fix(ci): insert org first
andrewleith Aug 10, 2023
fca91ff
fix(ci): insert user
andrewleith Aug 10, 2023
3d1acaa
fix(ci): fix user id
andrewleith Aug 10, 2023
f5ad107
fix(ci): use differetn secret key and dangerous salt
andrewleith Aug 10, 2023
cc9e0b7
fix(ci): use local config
andrewleith Aug 10, 2023
df2f799
fix(seed): insert service permissions
andrewleith Aug 10, 2023
660c079
fix(ci): make service id consistent with api keys
andrewleith Aug 10, 2023
3ff62d3
fix(ci): run less tests to troubleshoot
andrewleith Aug 10, 2023
b88cf5c
fix(cypress config): update config to use [email protected]
andrewleith Aug 10, 2023
d21f469
fix(cypress config): update the correct config this time
andrewleith Aug 10, 2023
a8b7dbd
fix(seed): add test user to service team
andrewleith Aug 10, 2023
35ad21e
fix(seeding): update permissions
andrewleith Aug 10, 2023
3a3a28d
fix(ci): actually run all the SQL commands
andrewleith Aug 10, 2023
c3f0777
fix: remove team key tests
andrewleith Aug 11, 2023
362b342
fix(cypress ci): change db connection string to the same as dev
andrewleith Aug 11, 2023
512922b
fix(cypress tests): remove some tests for now
andrewleith Aug 11, 2023
15cc051
fix(seed_db): update db URI
andrewleith Aug 11, 2023
d79d18e
fix(seed): add reply to data
andrewleith Aug 14, 2023
02804e2
fix(api tests): re-enable all tests
andrewleith Aug 14, 2023
12844c7
fix(seed): hardcode URI temporarily
andrewleith Aug 14, 2023
9c92905
chore: formatting
andrewleith Aug 14, 2023
6a94114
Merge branch 'main' into task/add-cypress-to-ci
andrewleith Sep 14, 2023
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
118 changes: 118 additions & 0 deletions .github/workflows/cypress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
name: Cypress tests

on:
push:

jobs:
cypress-tests:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:11.17-bullseye
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test_notification_api
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
redis:
image: redis:6.2@sha256:9e75c88539241ad7f61bc9c39ea4913b354064b8a75ca5fc40e1cef41b645bc0
ports:
- 6379:6379

steps:
- name: Install libcurl
run: sudo apt-get update && sudo apt-get install libssl-dev libcurl4-openssl-dev
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2

- name: Set up Python 3.10
uses: actions/setup-python@57ded4d7d5e986d7296eab16560982c6dd7c923b # v4.6.0
with:
python-version: '3.10'

- name: Upgrade pip
run: python -m pip install --upgrade pip
- uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 # v3.3.1
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-

- name: Install poetry
env:
POETRY_VERSION: 1.3.2
run: pip install poetry==${POETRY_VERSION} && poetry --version

- name: Check poetry.lock aligns with pyproject.toml
run: poetry lock --check

- name: Install requirements
run: poetry install --with test

- name: Get python version
run: |
python_version=$(python -V | cut -d' ' -f2)
echo "python_version=${python_version}" >> $GITHUB_ENV

- name: Make version file
run: |
printf '__commit_sha__ = "09cfe03100443fb9071bba88d5c8775ff54a9ebc"\n__time__ = "2022-07-25:15:11:05"\n' > version.py
cp version.py "${{ github.workspace }}/app/"

- name: Copy site-packages in workspace
working-directory: ${{ github.workspace }}
shell: bash
run: |
mkdir -p "${{ github.workspace }}/env/" && cp -fR $(poetry env list | poetry env info -p)/lib/python3.10/site-packages "${{ github.workspace }}/env/"

- name: Setup .env
run: |
echo 'NOTIFICATION_QUEUE_PREFIX=api-pr-notification-canada-ca' > .env
echo 'AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }}' >> .env
echo 'AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}' >> .env
echo 'AWS_REGION=ca-central-1' >> .env
echo 'ASSET_DOMAIN=example.com' >> .env
echo 'ASSET_UPLOAD_BUCKET_NAME=not-a-real-secret' >> .env
echo 'CSV_UPLOAD_BUCKET_NAME=not-a-real-secret' >> .env
echo 'DOCUMENTS_BUCKET=not-a-real-secret' >> .env
echo 'NOTIFY_EMAIL_DOMAIN=staging.notification.cdssandbox.xyz' >> .env
echo 'ADMIN_CLIENT_SECRET=local_app' >> .env
echo 'REDIS_URL=redis://redis:6379' >> .env
echo 'REDIS_PUBLISH_URL=redis://redis:6379' >> .env
echo 'SQLALCHEMY_DATABASE_URI=postgresql://postgres:postgres@localhost:5432/test_notification_api' >> .env
echo 'SQLALCHEMY_DATABASE_READER_URI=postgresql://postgres:postgres@localhost:5432/test_notification_api' >> .env
echo 'DOCUMENT_DOWNLOAD_API_HOST=http://host.docker.internal:7000' >> .env
echo 'FF_REDIS_BATCH_SAVING=true' >> .env
echo 'FF_BATCH_INSERTION=true' >> .env
echo 'REDIS_ENABLED=true' >> .env
echo 'FF_CLOUDWATCH_METRICS_ENABLED=false' >> .env
echo 'FF_SPIKE_SMS_DAILY_LIMIT=true' >> .env
echo 'FRESH_DESK_ENABLED=false' >> .env
echo 'SECRET_KEY=local_secret_key' >> .env
echo 'DANGEROUS_SALT=local_dangerous_salt' >> .env
echo 'NOTIFY_ENVIRONMENT=test' >> .env
echo 'FLASK_APP=application.py' >> .env
echo 'FLASK_DEBUG=true' >> .env
echo 'WERKZEUG_DEBUG_PIN=off' >> .env
pwd
- name: prep db and run notify api
run: |
poetry run flask db upgrade
poetry run python tests_cypress/seed_db.py
poetry run make run &

- name: Write the cypress.env.json file
run: |
echo '${{ secrets.CYPRESS_ENV_JSON }}' > tests_cypress/cypress.env.json

- name: Run cypress tests
uses: cypress-io/github-action@d79d2d530a66e641eb4a5f227e13bc985c60b964 # v4.2.2
env:
CYPRESS_BASE_URL: http://localhost:6011
with:
record: false
build: npx cypress info
working-directory: tests_cypress
spec: cypress/e2e/api/all.cy.js
10 changes: 9 additions & 1 deletion tests_cypress/cypress/Notify/NotifyAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,15 @@ const API = {
}
});
},

GetAPIStatus: () => {
return cy.request({
url: '/',
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
}
}

export default { API, Utilities, Admin };
15 changes: 15 additions & 0 deletions tests_cypress/cypress/e2e/api/sanity_check.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference types="cypress" />

import config from '../../../config';
import Notify from "../../Notify/NotifyAPI";

describe(`Sanity check [${config.CONFIG_NAME}]`, () => {
it("Can connect to API", () => {
Notify.API.GetAPIStatus().as('apiStatus');

cy.get('@apiStatus').then(resp => {
expect(resp.status).to.eq(200);
});
});
});

47 changes: 47 additions & 0 deletions tests_cypress/seed_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed

SQLALCHEMY_DATABASE_URI = "postgresql://postgres:postgres@localhost:5432/test_notification_api" #os.getenv("SQLALCHEMY_DATABASE_URI")
print(SQLALCHEMY_DATABASE_URI)

import sqlalchemy as sa

# set connection URI here ↓
engine = sa.create_engine(SQLALCHEMY_DATABASE_URI)

with engine.connect() as conn:
with conn.begin():
insert_user = """
Fixed Show fixed Hide fixed
INSERT INTO "public"."users"("id","name","email_address","created_at","updated_at","_password","mobile_number","password_changed_at","logged_in_at","failed_login_count","state","platform_admin","current_session_id","auth_type","blocked","additional_information","password_expired")
VALUES
(E'3f478896-6d3f-4ef3-aa5a-530fea1206bb',E'Notify UI Tests',E'[email protected]',E'2023-05-24 13:41:48.022737',E'2023-08-01 13:20:08.017647',E'$2b$10$jwj45gXRLUIteNBwhRgEV.4P7uHGH11O2PhNiDK6SY6oRPSdKhg2u',E'6139863526',E'2023-05-24 13:41:48.017676',E'2023-08-01 13:20:08.014524',0,E'active',FALSE,E'8f6e79df-782d-4620-b86c-adf1ea933934',E'email_auth',FALSE,E'{}',FALSE);
"""

insert_service = """
INSERT INTO "public"."services"("id","name","created_at","updated_at","active","message_limit","restricted","email_from","created_by_id","version","research_mode","organisation_type","prefix_sms","crown","rate_limit","contact_link","consent_to_research","volume_email","volume_letter","volume_sms","count_as_live","go_live_at","go_live_user_id","organisation_id","sending_domain","default_branding_is_french","sms_daily_limit","organisation_notes")
VALUES
(E'4049c2d0-0cab-455c-8f4c-f356dff51810',E'Cypress',E'2023-05-16 15:02:43.663183',E'2023-05-24 14:01:51.335072',TRUE,10000,FALSE,E'bounce',E'6af522d0-2915-4e52-83a3-3690455a5fe6',5,FALSE,E'central',TRUE,TRUE,1000,NULL,NULL,NULL,NULL,NULL,FALSE,E'2023-05-16 15:08:46.692247',NULL,E'93413f91-227f-4704-b229-b8210d1ecc0a',NULL,FALSE,1000,NULL);
"""

insert_templates = """
INSERT INTO "public"."templates"("id","name","template_type","created_at","updated_at","content","service_id","subject","created_by_id","version","archived","process_type","service_letter_contact_id","hidden","postage")
VALUES
(E'136e951e-05c8-4db4-bc50-fe122d72fcaa',E'SMOKE_TEST_EMAIL',E'email',E'2023-05-24 13:31:40.907915',NULL,E'# This is a smoke test from Cypress\n\nSMOKE_TEST_EMAIL',E'4049c2d0-0cab-455c-8f4c-f356dff51810',E'SMOKE_TEST_EMAIL',E'0840b12a-8139-4627-b27a-58872913ae14',1,FALSE,E'bulk',NULL,FALSE,NULL),
(E'258d8617-da88-4faa-ad28-46cc69f5a458',E'VARIABLES_EMAIL_TEMPLATE_ID',E'email',E'2023-07-10 16:38:43.913184',NULL,E'Hi ((name))\n\n((has_stuff??You have stuff!))',E'4049c2d0-0cab-455c-8f4c-f356dff51810',E'TESTING',E'0840b12a-8139-4627-b27a-58872913ae14',1,FALSE,E'bulk',NULL,FALSE,NULL),
(E'2d52d997-42d3-4ac0-a597-7afc94d4339a',E'SMOKE_TEST_EMAIL_LINK',E'email',E'2023-05-24 13:32:49.286647',NULL,E'# This is a smoke test from Cypress\n\nSMOKE_TEST_EMAIL_LINK\n\n((link_to_file))',E'4049c2d0-0cab-455c-8f4c-f356dff51810',E'SMOKE_TEST_EMAIL_LINK',E'0840b12a-8139-4627-b27a-58872913ae14',1,FALSE,E'bulk',NULL,FALSE,NULL),
(E'48207d93-144d-4ebb-92c5-99ff1f1baead',E'SMOKE_TEST_EMAIL_BULK',E'email',E'2023-05-24 13:32:24.912515',NULL,E'# This is a smoke test from Cypress\n\nSMOKE_TEST_EMAIL_BULK',E'4049c2d0-0cab-455c-8f4c-f356dff51810',E'SMOKE_TEST_EMAIL_BULK',E'0840b12a-8139-4627-b27a-58872913ae14',1,FALSE,E'bulk',NULL,FALSE,NULL),
(E'58db03d6-a9d8-4482-8621-26f473f3980a',E'SMOKE_TEST_EMAIL_ATTACH',E'email',E'2023-05-24 13:32:03.754124',NULL,E'# This is a smoke test from Cypress\n\nSMOKE_TEST_EMAIL_ATTACH',E'4049c2d0-0cab-455c-8f4c-f356dff51810',E'SMOKE_TEST_EMAIL_ATTACH',E'0840b12a-8139-4627-b27a-58872913ae14',1,FALSE,E'bulk',NULL,FALSE,NULL),
(E'5945e2f0-3e37-4813-9a60-e0665e02e9c8',E'SMOKE_TEST_SMS',E'sms',E'2023-05-24 13:33:20.936811',NULL,E'SMOKE_TEST_SMS',E'4049c2d0-0cab-455c-8f4c-f356dff51810',NULL,E'0840b12a-8139-4627-b27a-58872913ae14',1,FALSE,E'bulk',NULL,FALSE,NULL),
(E'b4692883-4182-4a23-b1b9-7b9df66a66e8',E'SIMPLE_EMAIL_TEMPLATE_ID',E'email',E'2023-07-10 16:33:10.288618',NULL,E'TESTING',E'4049c2d0-0cab-455c-8f4c-f356dff51810',E'TESTING',E'0840b12a-8139-4627-b27a-58872913ae14',1,FALSE,E'bulk',NULL,FALSE,NULL);
"""

insert_api_keys = """
INSERT INTO "public"."api_keys"("id","name","secret","service_id","expiry_date","created_at","created_by_id","updated_at","version","key_type")
VALUES
(E'076fc5ba-8578-44d6-8b69-122ac3a7b206',E'CYPRESS_TEST_KEY',E'IjY3YTViZmU4LWMxNjktNDQ0NS04OGVjLWRkNWExZDA3NjFmYSI.wLt-AQOYG3vgab_qP1p0Djw_r-8',E'4049c2d0-0cab-455c-8f4c-f356dff51810',NULL,E'2023-07-10 16:44:13.025984',E'6af522d0-2915-4e52-83a3-3690455a5fe6',NULL,1,E'test'),
(E'61a3f8e1-516d-446c-bc3a-c2dac33c9474',E'CYPRESS',E'IjhmYmE3MzUwLTIwN2ItNDRhYi1iNmYwLTVkZjk3ZjAzNjZlMyI.xiQ94d2oAedkohXNhx83B6cWr_0',E'4049c2d0-0cab-455c-8f4c-f356dff51810',NULL,E'2023-05-24 13:48:23.93561',E'6af522d0-2915-4e52-83a3-3690455a5fe6',NULL,1,E'normal'),
(E'74a06881-7742-4d4a-85ff-b91ae71e1bcd',E'CYPRESS_TEAM_KEY',E'IjYxZjhhYzU3LTNlZGEtNDE3MS1iMDcyLTJjNTY0OWEzODI4ZSI.p6K5JNnYp6Pcn3OEQg1kDM1UHAY',E'4049c2d0-0cab-455c-8f4c-f356dff51810',NULL,E'2023-07-10 16:43:19.425995',E'6af522d0-2915-4e52-83a3-3690455a5fe6',NULL,1,E'team');
"""# Optional: start a transaction
# conn.execute(sa.text(insert_user))
conn.execute(sa.text(insert_service))
conn.execute(sa.text(insert_templates))
conn.execute(sa.text(insert_api_keys))