Skip to content

Commit

Permalink
[MDS-5337] Setup Cypress for MineSpace (#2591)
Browse files Browse the repository at this point in the history
* implement cypress e2e for minespace.

* update test

* update realm-export

* update readme markdown files

* update docker-compose.yaml file.

* fix bug in workflow for minespace.

* fix bug on build

* add to documentation
  • Loading branch information
henryoforeh-dev authored Jul 18, 2023
1 parent e622078 commit 7a39b24
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module.exports = {
parser: "@typescript-eslint/parser",
ignorePatterns: [
"services/minespace-web/webpack.config.ts",
"services/minespace-web/cypress.config.ts",
"services/minespace-web/cypress/**",
"services/core-web/webpack.config.ts",
"services/core-web/cypress.config.ts",
"services/core-web/cypress/**",
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/minespace.unit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,28 @@ jobs:

- name: Run minespace tests
run: cd services/minespace-web/ && yarn && yarn test

- name: build-minespace-web
run: |
cd services/minespace-web/
NODE_OPTIONS=--max-old-space-size=8096 yarn build
- name: start-minespace-web
run: cd services/minespace-web/ && yarn run http-server-spa build /index.html 3020 &

- name: Run cypress tests
run: cd services/minespace-web/ && yarn cypress run
env:
CYPRESS_TEST_USER: ${{ secrets.CYPRESS_TEST_USER }}
CYPRESS_TEST_PASSWORD: ${{ secrets.CYPRESS_TEST_PASSWORD }}
CYPRESS_MINESPACE_WEB_TEST_URL: ${{ secrets.CYPRESS_MINESPACE_WEB_TEST_URL }}
CYPRESS_BACKEND: ${{ secrets.CYPRESS_BACKEND }}
CYPRESS_API_URL: ${{ secrets.CYPRESS_API_URL }}
CYPRESS_KEYCLOAK_URL: ${{ secrets.CYPRESS_KEYCLOAK_URL }}
CYPRESS_ENVIRONMENT: ${{ secrets.CYPRESS_ENVIRONMENT }}
CYPRESS_DOC_MAN_URL: ${{ secrets.CYPRESS_DOC_MAN_URL }}
CYPRESS_FILE_SYSTEM_PROVIDER_URL: ${{ secrets.CYPRESS_FILE_SYSTEM_PROVIDER_URL }}
CYPRESS_MATOMO_URL: ${{ secrets.CYPRESS_MATOMO_URL }}
CYPRESS_KEYCLOAK_CLIENT_ID: ${{ secrets.CYPRESS_KEYCLOAK_CLIENT_ID }}
CYPRESS_KEYCLOAK_IDP_HINT: ${{ secrets.CYPRESS_KEYCLOAK_IDP_HINT }}
CYPRESS_KEYCLOAK_RESOURCE: ${{ secrets.CYPRESS_KEYCLOAK_RESOURCE }}
34 changes: 33 additions & 1 deletion cypress/realm-export.json
Original file line number Diff line number Diff line change
Expand Up @@ -620,14 +620,30 @@
"access.tokenResponse.claim": "false"
}
},
{
"id": "5702c34f-1b8f-41e4-bd22-f5211416265b",
"name": "BCEID Username",
"protocol": "openid-connect",
"protocolMapper": "oidc-hardcoded-claim-mapper",
"consentRequired": false,
"config": {
"claim.value": "cypress",
"userinfo.token.claim": "true",
"id.token.claim": "true",
"access.token.claim": "true",
"claim.name": "bceid_username",
"jsonType.label": "String",
"access.tokenResponse.claim": "false"
}
},
{
"id": "9c6f483e-e41b-48ec-873d-44805c786adf",
"name": "Client Roles",
"protocol": "openid-connect",
"protocolMapper": "oidc-hardcoded-claim-mapper",
"consentRequired": false,
"config": {
"claim.value": "[ \"core_edit_requirements\", \"c_mds_major_mines_administrative\", \"core_edit_variances\", \"digdag_admin\", \"core_edit_investigations\", \"docman_manager\", \"core_edit_reports\", \"core_edit_emli_contacts\", \"cs_mds_geospatial\", \"core_view_all\", \"c_mds_users\", \"cs_mds_tsf\", \"core_close_permits\", \"c_mds_permit_mines\", \"c_mds_administrative_mines\", \"c_mds_overlord\", \"cs_mds_abandoned_mines\", \"core_edit_securities\", \"core_edit_project_decision_packages\", \"core_edit_mines\", \"core_geospatial\", \"core_edit_historical_amendments\", \"c_mds_health_safety\", \"core_executive_view\", \"idir\", \"core_environmental_reports\", \"c_idir\", \"cs_mds_investigations\", \"cs_mds_permit_template_conditions\", \"core_edit_major_mine_applications\", \"c_mds_super_admin\", \"core_contact_admin\", \"core_edit_permits\", \"cs_mds_securities\", \"core_edit_template_conditions\", \"core_edit_do\", \"core_edit_tsf\", \"nris_view_all\", \"core_edit_project_summaries\", \"core_edit_incidents\", \"core_edit_all\", \"core_view_admin_route\", \"core_edit_explosives_permits\", \"core_edit_parties\", \"core_edit_submissions\", \"core_abandoned_mines\", \"cs_mds_executive\", \"core_edit_information_requirements_table\", \"core_admin\" ]",
"claim.value": "[ \"core_edit_requirements\", \"c_mds_major_mines_administrative\", \"core_edit_variances\", \"digdag_admin\", \"core_edit_investigations\", \"docman_manager\", \"core_edit_reports\", \"core_edit_emli_contacts\", \"cs_mds_geospatial\", \"core_view_all\", \"c_mds_users\", \"cs_mds_tsf\", \"core_close_permits\", \"c_mds_permit_mines\", \"c_mds_administrative_mines\", \"c_mds_overlord\", \"cs_mds_abandoned_mines\", \"core_edit_securities\", \"core_edit_project_decision_packages\", \"core_edit_mines\", \"core_geospatial\", \"core_edit_historical_amendments\", \"c_mds_health_safety\", \"core_executive_view\", \"idir\", \"core_environmental_reports\", \"c_idir\", \"cs_mds_investigations\", \"cs_mds_permit_template_conditions\", \"core_edit_major_mine_applications\", \"c_mds_super_admin\", \"core_contact_admin\", \"core_edit_permits\", \"cs_mds_securities\", \"core_edit_template_conditions\", \"core_edit_do\", \"core_edit_tsf\", \"nris_view_all\", \"core_edit_project_summaries\", \"core_edit_incidents\", \"core_edit_all\", \"core_view_admin_route\", \"core_edit_explosives_permits\", \"core_edit_parties\", \"core_edit_submissions\", \"core_abandoned_mines\", \"cs_mds_executive\", \"core_edit_information_requirements_table\", \"core_admin\" , \"mds_minespace_proponents\", \"c_mds_minespace_proponent\" ]",
"userinfo.token.claim": "true",
"id.token.claim": "true",
"access.token.claim": "true",
Expand All @@ -636,6 +652,22 @@
"access.tokenResponse.claim": "false"
}
},
{
"id": "15b20023-0f46-4669-9902-28b891948363",
"name": "BCEID User GUID",
"protocol": "openid-connect",
"protocolMapper": "oidc-hardcoded-claim-mapper",
"consentRequired": false,
"config": {
"claim.value": "CE2FAF7EE63C482097267AAF1DDB6FAC",
"userinfo.token.claim": "true",
"id.token.claim": "true",
"access.token.claim": "true",
"claim.name": "bceid_user_guid",
"jsonType.label": "String",
"access.tokenResponse.claim": "false"
}
},
{
"id": "3fede029-f297-4dd2-ae43-3e3d23a9c4c8",
"name": "IDIR Username",
Expand Down
8 changes: 4 additions & 4 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ services:

####################### Keycloak for Cypress #######################
keycloak:
image: jboss/keycloak
build:
context: ./cypress/.docker/keycloak
dockerfile: Dockerfile.test
container_name: cypress-keycloak
environment:
DB_VENDOR: h2
Expand All @@ -16,9 +18,7 @@ services:
KEYCLOAK_IMPORT: /tmp/realm-export.json -Dkeycloak.profile.feature.upload_scripts=enabled
volumes:
- ./cypress/realm-export.json:/tmp/realm-export.json
build:
context: ./cypress/.docker/keycloak
dockerfile: Dockerfile.test
- ./cypress/create-user.sh:/tmp/create-user.sh
ports:
- 8080:8080
####################### Open Telemetry #######################
Expand Down
7 changes: 6 additions & 1 deletion services/core-web/.env-example
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ CYPRESS_BACKEND=mds-python-backend
CYPRESS_API_URL=http://localhost:5000
CYPRESS_KEYCLOAK_URL=http://localhost:8080/auth
CYPRESS_ENVIRONMENT=local
CYPRESS_DOC_MAN_URL=https://mds-dev.apps.silver.devops.gov.bc.ca/document-manager
CYPRESS_DOC_MAN_URL=https://mds-dev.apps.silver.devops.gov.bc.ca/document-manager
CYPRESS_MATOMO_URL=https://matomo-4c2ba9-test.apps.silver.devops.gov.bc.ca/
CYPRESS_KEYCLOAK_CLIENT_ID=mines-digital-services-mds-public-client-4414
CYPRESS_KEYCLOAK_RESOURCE=mines-digital-services-mds-public-client-4414
CYPRESS_KEYCLOAK_IDP_HINT=idir
CYPRESS_FILE_SYSTEM_PROVIDER_URL=https://mds-dev.apps.silver.devops.gov.bc.ca/file-api/AmazonS3Provider/
26 changes: 26 additions & 0 deletions services/core-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,32 @@ yarn test:watch

Then choose option 'i' and go through the failing snapshots case by case

**Setup Cypress test**

Add the following cypress related environment variables from env-example.

CYPRESS_TEST_USER
CYPRESS_TEST_PASSWORD
CYPRESS_CORE_WEB_TEST_URL
CYPRESS_BACKEND
CYPRESS_API_URL
CYPRESS_KEYCLOAK_URL
CYPRESS_ENVIRONMENT
CYPRESS_DOC_MAN_URL
CYPRESS_MATOMO_URL
CYPRESS_KEYCLOAK_CLIENT_ID
CYPRESS_KEYCLOAK_RESOURCE
CYPRESS_KEYCLOAK_IDP_HINT
CYPRESS_FILE_SYSTEM_PROVIDER_URL

run the command `docker-compose up -d keycloak`

Navigate to `http://localhost:8080` to check if keycloak was successfully installed.

**Run Cypress test**
To run your cypress tests with a browser, type the command `yarn run cypress open`.
To run your cypress tests in headless mode, type the command `yarn cypress run`.

### Running

- [react](https://reactjs.org/) : JS library to build single page apps
Expand Down
14 changes: 14 additions & 0 deletions services/minespace-web/.env-example
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,17 @@ BASE_PATH=
ASSET_PATH=/

NODE_OPTIONS=--max_old_space_size=2048

CYPRESS_TEST_USER=cypress
CYPRESS_TEST_PASSWORD=cypress
CYPRESS_MINESPACE_WEB_TEST_URL=http://localhost:3020
CYPRESS_BACKEND=mds-python-backend
CYPRESS_API_URL=http://localhost:5000
CYPRESS_KEYCLOAK_URL=http://localhost:8080/auth
CYPRESS_ENVIRONMENT=local
CYPRESS_DOC_MAN_URL=https://mds-dev.apps.silver.devops.gov.bc.ca/document-manager
CYPRESS_MATOMO_URL=https://matomo-4c2ba9-test.apps.silver.devops.gov.bc.ca/
CYPRESS_KEYCLOAK_CLIENT_ID=mines-digital-services-mds-public-client-4414
CYPRESS_KEYCLOAK_RESOURCE=mines-digital-services-mds-public-client-4414
CYPRESS_KEYCLOAK_IDP_HINT=idir
CYPRESS_FILE_SYSTEM_PROVIDER_URL=https://mds-dev.apps.silver.devops.gov.bc.ca/file-api/AmazonS3Provider/
1 change: 1 addition & 0 deletions services/minespace-web/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/node_modules
**/coverage
**/cypress
4 changes: 4 additions & 0 deletions services/minespace-web/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
# Editor settings
.vscode/*
.vimrc

# cypress
/cypress/videos
/cypress/screenshots
25 changes: 25 additions & 0 deletions services/minespace-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@ Contributors to this codebase are expected to follow the testing standards set o

_If coverage is lower than before writing a new feature, the tests **need** to be updated, and the feature is considered **incomplete**_

**Setup Cypress test**
Add the following cypress related environment variables from env-example.

CYPRESS_TEST_USER
CYPRESS_TEST_PASSWORD
CYPRESS_MINESPACE_WEB_TEST_URL
CYPRESS_BACKEND
CYPRESS_API_URL
CYPRESS_KEYCLOAK_URL
CYPRESS_ENVIRONMENT
CYPRESS_DOC_MAN_URL
CYPRESS_MATOMO_URL
CYPRESS_KEYCLOAK_CLIENT_ID
CYPRESS_KEYCLOAK_RESOURCE
CYPRESS_KEYCLOAK_IDP_HINT
CYPRESS_FILE_SYSTEM_PROVIDER_URL

run the command `docker-compose up -d keycloak`

Navigate to `http://localhost:8080` to check if keycloak was successfully installed.

**Run Cypress test**
To run your cypress tests with a browser, type the command `yarn run cypress open`.
To run your cypress tests in headless mode, type the command `yarn cypress run`.

### Running

- [react](https://reactjs.org/) : JS library to build single page apps
Expand Down
14 changes: 14 additions & 0 deletions services/minespace-web/cypress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineConfig } from "cypress";

export default defineConfig({
chromeWebSecurity: false,
viewportWidth: 1960,
viewportHeight: 1080,
e2e: {
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
return require("./cypress/plugins/index.js")(on, config);
},
},
});
12 changes: 12 additions & 0 deletions services/minespace-web/cypress/e2e/index.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
describe("Mines Page", () => {
const url = Cypress.env("CYPRESS_MINESPACE_WEB_TEST_URL");
beforeEach(() => {
cy.login();
});

it("should navigate to the mines page successfully", () => {
cy.visit(`${url}/mines`);
// Assert that landing on the home page is successful
cy.url({ timeout: 10000 }).should("include", "/mines");
});
});
25 changes: 25 additions & 0 deletions services/minespace-web/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// / <reference types="cypress" />
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

/**
* @type {Cypress.PluginConfig}
*/
// eslint-disable-next-line no-unused-vars
const dotenv = require("dotenv");
dotenv.config();

module.exports = (on, config) => {
config.env = process.env;
return config;
};
32 changes: 32 additions & 0 deletions services/minespace-web/cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Cypress.Commands.add("login", () => {
const url = Cypress.env("CYPRESS_MINESPACE_WEB_TEST_URL");
const environmentUrl = `${url}/env`;
const commonVariables = "bceid";

const response = {
backend: Cypress.env("CYPRESS_BACKEND"),
apiUrl: Cypress.env("CYPRESS_API_URL"),
docManUrl: Cypress.env("CYPRESS_DOC_MAN_URL"),
matomoUrl: Cypress.env("CYPRESS_MATOMO_URL"),
filesystemProviderUrl: Cypress.env("CYPRESS_FILE_SYSTEM_PROVIDER_URL"),
keycloak_clientId: Cypress.env("CYPRESS_KEYCLOAK_CLIENT_ID"),
keycloak_resource: Cypress.env("CYPRESS_KEYCLOAK_RESOURCE"),
keycloak_url: Cypress.env("CYPRESS_KEYCLOAK_URL"),
keycloak_idir_idpHint: commonVariables,
keycloak_bceid_idpHint: commonVariables,
keycloak_vcauthn_idpHint: commonVariables,
siteminder_url: Cypress.env("CYPRESS_KEYCLOAK_URL"),
environment: Cypress.env("CYPRESS_ENVIRONMENT"),
vcauthn_pres_req_conf_id: "minespace-access-0.1-dev",
};

cy.intercept("GET", environmentUrl, (req) => {
req.reply(response);
});

cy.visit(url);
cy.contains("Log in with BCeID").click();
cy.get("#username").type(Cypress.env("CYPRESS_TEST_USER"));
cy.get("#password").type(Cypress.env("CYPRESS_TEST_PASSWORD"));
cy.get("#kc-login").click();
});
29 changes: 29 additions & 0 deletions services/minespace-web/cypress/support/e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// ***********************************************************
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import "./commands";

// Alternatively you can use CommonJS syntax:
// require('./commands')

// eslint-disable-next-line consistent-return
Cypress.on("uncaught:exception", (err) => {
if (err.message && /ResizeObserver loop limit exceeded/.test(err.message)) {
// returning false here prevents Cypress from
// failing the test
return false;
}
});
2 changes: 2 additions & 0 deletions services/minespace-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"cross-env": "5.2.1",
"css-loader": "3.4.2",
"cssnano": "4.1.10",
"cypress": "12.13.0",
"eslint": "6.8.0",
"eslint-config-airbnb": "17.1.1",
"eslint-config-prettier": "6.9.0",
Expand All @@ -111,6 +112,7 @@
"file-loader": "4.3.0",
"hard-source-webpack-plugin": "0.13.1",
"html-webpack-plugin": "3.2.0",
"http-server-spa": "1.3.0",
"image-webpack-loader": "8.1.0",
"jest": "24.8.0",
"less": "3.5.0",
Expand Down
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,7 @@ __metadata:
cross-env: 5.2.1
css-loader: 3.4.2
cssnano: 4.1.10
cypress: 12.13.0
dotenv: 8.2.0
enzyme: 3.11.0
enzyme-adapter-react-16: 1.14.0
Expand All @@ -2275,6 +2276,7 @@ __metadata:
hard-source-webpack-plugin: 0.13.1
hoist-non-react-statics: 3.3.1
html-webpack-plugin: 3.2.0
http-server-spa: 1.3.0
image-webpack-loader: 8.1.0
jest: 24.8.0
jest-canvas-mock: 2.1.0
Expand Down

0 comments on commit 7a39b24

Please sign in to comment.