Skip to content

Commit

Permalink
WIP refactor: more independent e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rdubigny committed Jul 7, 2023
1 parent a954ecc commit 3f59751
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 61 deletions.
16 changes: 10 additions & 6 deletions .github/workflows/end-to-end.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ env:
PGDATABASE: api-auth-test
PGHOST: 127.0.0.1
PGPORT: 5432
DATABASE_URL: postgres://api-auth:[email protected]:5432/api-auth-test
SENDINBLUE_API_KEY: ${{ secrets.SENDINBLUE_API_KEY }}
MONCOMPTEPRO_HOST: http://localhost:3000
DO_NOT_SEND_MAIL: "False"
DO_NOT_VALIDATE_MAIL: "True"
DO_NOT_RATE_LIMIT: "True"
DO_NOT_USE_ANNUAIRE_EMAILS: "True"
TEST_CONTACT_EMAIL: "[email protected]"
SESSION_COOKIE_SECRET: secret
DEBOUNCE_API_KEY:
SENTRY_DSN:
Expand All @@ -31,6 +31,9 @@ env:

jobs:
test:
strategy:
matrix:
e2e_test: [ join_collectivite_territoriale ]
runs-on: ubuntu-20.04
services:
redis:
Expand Down Expand Up @@ -63,21 +66,22 @@ jobs:
${{ runner.os }}-build-
${{ runner.os }}-
- run: npm ci
- run: npm run load-ci-fixtures
env:
DATABASE_URL: postgres://${{ env.PGUSER }}:${{ env.PGPASSWORD }}@127.0.0.1:${{ env.PGPORT }}/${{ env.PGDATABASE }}
- run: npm install cypress@12
- run: npm run migrate up
- run: npm run load-ci-fixtures -- scripts/fixtures/${{ matrix.e2e_test }}.sql
- run: npm run update-organization-info -- 2000
- name: Cypress run
uses: cypress-io/github-action@v5
with:
wait-on: ${{ env.MONCOMPTEPRO_HOST }}/users/start-sign-in
build: npm run build
start: npm start
install: false
spec: cypress/e2e/${{ matrix.e2e_test }}.cy.js
env:
NODE_ENV: production
DATABASE_URL: postgres://${{ env.PGUSER }}:${{ env.PGPASSWORD }}@127.0.0.1:${{ env.PGPORT }}/${{ env.PGDATABASE }}
CYPRESS_MONCOMPTEPRO_HOST: ${{ env.MONCOMPTEPRO_HOST }}
TEST_CONTACT_EMAIL: "[email protected]"
- run: npm run delete-database
# Store tests runs in case of failure
- uses: actions/upload-artifact@v3
if: failure()
Expand Down
13 changes: 10 additions & 3 deletions cypress/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cd /opt/apps/api-auth/current
export $(cat /etc/api-auth.conf | xargs)
export DATABASE_URL=postgres://api-auth:api-auth@localhost:5432/api-auth-test
export PGDATABASE=api-auth-test
npm run load-ci-fixtures
npm run migrate up
exit
```

Expand All @@ -27,10 +27,10 @@ export DO_NOT_RATE_LIMIT=True
export DO_NOT_USE_ANNUAIRE_EMAILS=True
export TEST_CONTACT_EMAIL=34c5063f-81c0-4d09-9d0b-a7502f844cdf@mailslurp.com
export DATABASE_URL=postgres://api-auth:[email protected]:5432/api-auth-test

export DO_NOT_SEND_MAIL=False
export SENDINBLUE_API_KEY="xxx"
npm run build
npm run start
npm run build && npm run start
```

On your host, install cypress:
Expand All @@ -48,6 +48,13 @@ export CYPRESS_MONCOMPTEPRO_HOST=https://app-development.moncomptepro.beta.gouv.
cypress open
```

```bash
export PGDATABASE=api-auth-test
export DATABASE_URL=postgres://api-auth:[email protected]:5432/api-auth-test
npm run delete-database
npm run load-ci-fixtures scripts/fixtures/join_collectivite_territoriale.sql
npm run update-organization-info 2000
```
reset user table

```bash
Expand Down
19 changes: 3 additions & 16 deletions cypress/e2e/join_collectivite_territoriale.cy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const host = Cypress.env('MONCOMPTEPRO_HOST') || 'http://localhost:3000';
import { MONCOMPTEPRO_HOST } from '../../src/env';

describe('join organizations', () => {
before(() => {
Expand All @@ -20,20 +20,7 @@ describe('join organizations', () => {
'password123'
);

cy.visit(`${host}/users/join-organization`);
cy.get('[name="siret"]').type('21340126800130');
cy.get('[type="submit"]').click();

cy.contains('Votre compte est créé');
});

it('following members can join the new organization if using the same email domain', function() {
cy.login(
'[email protected]',
'password123'
);

cy.visit(`${host}/users/join-organization`);
cy.visit(`${MONCOMPTEPRO_HOST}/users/join-organization`);
cy.get('[name="siret"]').type('21340126800130');
cy.get('[type="submit"]').click();

Expand All @@ -46,7 +33,7 @@ describe('join organizations', () => {
'password123'
);

cy.visit(`${host}/users/join-organization`);
cy.visit(`${MONCOMPTEPRO_HOST}/users/join-organization`);
cy.get('[name="siret"]').type('21740056300011');
cy.get('[type="submit"]').click();

Expand Down
6 changes: 3 additions & 3 deletions cypress/e2e/join_org_with_verified_domain.cy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const host = Cypress.env('MONCOMPTEPRO_HOST') || 'http://localhost:3000';
import { MONCOMPTEPRO_HOST } from '../../src/env';

describe('join organizations', () => {
before(() => {
Expand All @@ -22,7 +22,7 @@ describe('join organizations', () => {

it('join suggested organisation', function() {
// Visit the signup page
cy.visit(`${host}/`);
cy.visit(`${MONCOMPTEPRO_HOST}/`);

cy.get('.fr-grid-row .fr-col-12:first-child .fr-tile__link').contains(
'Commune de clamart - Mairie'
Expand Down Expand Up @@ -73,7 +73,7 @@ describe('join organizations', () => {

it('join another organisation', function() {
// Visit the join organization page
cy.visit(`${host}/users/join-organization`);
cy.visit(`${MONCOMPTEPRO_HOST}/users/join-organization`);
cy.get('[name="siret"]').type('13002526500013');
cy.get('[type="submit"]').click();

Expand Down
4 changes: 2 additions & 2 deletions cypress/e2e/signup_entreprise_unipersonnelle.cy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const host = Cypress.env('MONCOMPTEPRO_HOST') || 'http://localhost:3000';
import { MONCOMPTEPRO_HOST } from '../../src/env';

describe('Signup into new entreprise unipersonnelle', () => {
before(function() {
Expand All @@ -14,7 +14,7 @@ describe('Signup into new entreprise unipersonnelle', () => {

it('creates a user', function() {
// Visit the signup page
cy.visit(`${host}/users/start-sign-in`);
cy.visit(`${MONCOMPTEPRO_HOST}/users/start-sign-in`);

// Sign up with the previously created inbox
cy.get('[name="login"]').type(this.emailAddress);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"update-organization-info": "ts-node --files scripts/update-organizations-info.ts",
"migrate": "node-pg-migrate",
"load-fixtures": " node-pg-migrate up && psql -v ON_ERROR_STOP=1 -f scripts/fixtures.sql && npm run update-organization-info 2000",
"load-ci-fixtures": "node-pg-migrate up && psql -v ON_ERROR_STOP=1 -f scripts/ci-fixtures.sql && npm run update-organization-info 2000",
"delete-database": "psql -v ON_ERROR_STOP=1 -f scripts/delete_all.sql",
"load-ci-fixtures": "node-pg-migrate up && psql -v ON_ERROR_STOP=1 -f",
"test:lint": "prettier */*.js --list-different",
"test": "DO_NOT_VALIDATE_MAIL=False ts-mocha -p tsconfig.json test/*.ts test/*.js",
"watch-js": "tsc --watch",
Expand Down
6 changes: 6 additions & 0 deletions scripts/delete_all.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DELETE FROM users_organizations;
DELETE FROM moderations;
DELETE FROM organizations;
DELETE FROM users_oidc_clients;
DELETE FROM users;
DELETE FROM oidc_clients;
20 changes: 20 additions & 0 deletions scripts/fixtures/join_collectivite_territoriale.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
INSERT INTO users
(id, email, email_verified, email_verified_at, encrypted_password, created_at, updated_at, given_name, family_name,
phone_number, job)
VALUES
(1, '[email protected]', true, CURRENT_TIMESTAMP, '$2a$10$kzY3LINL6..50Fy9shWCcuNlRfYq0ft5lS.KCcJ5PzrhlWfKK4NIO', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Jean', 'User1', '0123456789', 'Sbire'),
(2, '[email protected]', true, CURRENT_TIMESTAMP, '$2a$10$kzY3LINL6..50Fy9shWCcuNlRfYq0ft5lS.KCcJ5PzrhlWfKK4NIO', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Jean', 'User2', '0123456789', 'Sbire');


INSERT INTO organizations
(id, siret, verified_email_domains, authorized_email_domains, created_at, updated_at)
VALUES
-- COMMUNE DE CLAMART - Mairie
(1, '21920023500014', '{}', '{}', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);

-- user are already in an organization to skip organization suggestion
INSERT INTO users_organizations
(user_id, organization_id, verification_type, authentication_by_peers_type, has_been_greeted)
VALUES
(1, 1, 'verified_email_domain', 'all_members_notified', true),
(2, 1, 'verified_email_domain', 'all_members_notified', true);
46 changes: 46 additions & 0 deletions scripts/fixtures/join_org_with_verified_domain.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
INSERT INTO users
(id, email, email_verified, email_verified_at, encrypted_password, created_at, updated_at, given_name, family_name,
phone_number, job)
VALUES
(1,
'[email protected]',
'true',
CURRENT_TIMESTAMP,
'$2a$10$5oxACsw3NngPAXALyB2G3u/C0Ej0CFUyPJhPtyyHP737Xn3lW1Mv.', -- password is '[email protected]'
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
'Jean',
'User',
'0123456789',
'International knowledge practice leader'),
-- password for the following users is 'password123'
(2, '[email protected]', 'true', CURRENT_TIMESTAMP, '$2a$10$kzY3LINL6..50Fy9shWCcuNlRfYq0ft5lS.KCcJ5PzrhlWfKK4NIO', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Jean', 'User2', '0123456789', 'Sbire'),
(3, '[email protected]', 'true', CURRENT_TIMESTAMP, '$2a$10$kzY3LINL6..50Fy9shWCcuNlRfYq0ft5lS.KCcJ5PzrhlWfKK4NIO', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Jean', 'User3', '0123456789', 'Sbire'),
(4, '[email protected]', 'true', CURRENT_TIMESTAMP, '$2a$10$kzY3LINL6..50Fy9shWCcuNlRfYq0ft5lS.KCcJ5PzrhlWfKK4NIO', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Jean', 'User4', '0123456789', 'Sbire'),
(5, '[email protected]', 'true', CURRENT_TIMESTAMP, '$2a$10$kzY3LINL6..50Fy9shWCcuNlRfYq0ft5lS.KCcJ5PzrhlWfKK4NIO', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Jean', 'User5', '0123456789', 'Sbire'),
(6, '[email protected]', 'true', CURRENT_TIMESTAMP, '$2a$10$kzY3LINL6..50Fy9shWCcuNlRfYq0ft5lS.KCcJ5PzrhlWfKK4NIO', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Jean', 'User6', '0123456789', 'Sbire'),
(7, '[email protected]', 'true', CURRENT_TIMESTAMP, '$2a$10$kzY3LINL6..50Fy9shWCcuNlRfYq0ft5lS.KCcJ5PzrhlWfKK4NIO', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Jean', 'User7', '0123456789', 'Sbire');

INSERT INTO organizations
(id, siret, verified_email_domains, authorized_email_domains, created_at, updated_at)
VALUES
-- COMMUNE DE CLAMART - Mairie
(1, '21920023500014', '{"mailslurp.com"}', '{"mailslurp.com"}', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
-- COMMUNE DE CLAMART - Service assainissement
(2, '21920023500394', '{}', '{"mailslurp.com"}', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
-- DIRECTION INTERMINISTERIELLE DU NUMERIQUE (DINUM)
(3, '13002526500013', '{"beta.gouv.fr"}', '{"beta.gouv.fr"}', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);

INSERT INTO users_organizations
(user_id, organization_id, verification_type, authentication_by_peers_type, has_been_greeted)
VALUES
(3, 1, 'verified_email_domain', 'all_members_notified', true),
(4, 1, 'verified_email_domain', 'all_members_notified', true),
(5, 1, 'verified_email_domain', 'all_members_notified', true),
(6, 1, 'verified_email_domain', 'all_members_notified', true),
(7, 1, 'verified_email_domain', 'all_members_notified', true),
(3, 2, 'verified_email_domain', 'all_members_notified', true),
(4, 2, 'verified_email_domain', 'all_members_notified', true),
(5, 2, 'verified_email_domain', 'all_members_notified', true),
(6, 2, 'verified_email_domain', 'all_members_notified', true),
(7, 2, 'verified_email_domain', 'all_members_notified', true);
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
-- This script:
-- - override data at specified id without deleting database
-- - is idempotent.

INSERT INTO users
(id, email, email_verified, email_verified_at, encrypted_password, created_at, updated_at, given_name, family_name,
phone_number, job)
Expand Down Expand Up @@ -32,14 +28,6 @@ ON CONFLICT (id)
EXCLUDED.updated_at, EXCLUDED.given_name, EXCLUDED.family_name,
EXCLUDED.phone_number, EXCLUDED.job);

SELECT setval(
'users_id_seq',
GREATEST(
(SELECT MAX(id) FROM users),
(SELECT last_value FROM users_id_seq)
)
);

INSERT INTO organizations
(id, siret, verified_email_domains, authorized_email_domains, created_at, updated_at)
VALUES
Expand All @@ -55,14 +43,6 @@ ON CONFLICT (id)
SET (siret, authorized_email_domains, created_at, updated_at)
= (EXCLUDED.siret, EXCLUDED.authorized_email_domains, EXCLUDED.created_at, EXCLUDED.updated_at);

SELECT setval(
'organizations_id_seq',
GREATEST(
(SELECT MAX(id) FROM organizations),
(SELECT last_value FROM organizations_id_seq)
)
);

INSERT INTO users_organizations
(user_id, organization_id, verification_type, authentication_by_peers_type, has_been_greeted)
VALUES
Expand All @@ -80,11 +60,3 @@ ON CONFLICT (user_id, organization_id)
DO UPDATE
SET (verification_type, authentication_by_peers_type, has_been_greeted)
= (EXCLUDED.verification_type, EXCLUDED.authentication_by_peers_type, EXCLUDED.has_been_greeted);

SELECT setval(
'oidc_clients_id_seq',
GREATEST(
(SELECT MAX(id) FROM oidc_clients),
(SELECT last_value FROM oidc_clients_id_seq)
)
);
2 changes: 2 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const {
} = process.env;

export const DO_NOT_VALIDATE_MAIL = process.env.DO_NOT_VALIDATE_MAIL === 'True';
export const CONSIDER_ALL_EMAIL_DOMAINS_AS_FREE =
process.env.CONSIDER_ALL_EMAIL_DOMAINS_AS_FREE === 'True';
export const DO_NOT_SEND_MAIL = process.env.DO_NOT_SEND_MAIL === 'True';
export const SECURE_COOKIES = (process.env.SECURE_COOKIES || 'true') === 'true';
export const DO_NOT_USE_ANNUAIRE_EMAILS =
Expand Down
15 changes: 13 additions & 2 deletions src/services/uses-a-free-email-provider.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
// heavily inspired from https://stackoverflow.com/questions/71232973/check-email-domain-type-personal-email-or-company-email#answer-72640757
import { isFree } from 'is-disposable-email-domain';
import { parse_host } from 'tld-extract';
import { DO_NOT_VALIDATE_MAIL } from '../env';
import {
CONSIDER_ALL_EMAIL_DOMAINS_AS_FREE,
DO_NOT_VALIDATE_MAIL,
} from '../env';

export const isAFreeEmailProvider = (domain: string) => {
return !DO_NOT_VALIDATE_MAIL && isFree(domain);
if (CONSIDER_ALL_EMAIL_DOMAINS_AS_FREE) {
return true;
}

if (DO_NOT_VALIDATE_MAIL) {
return false;
}

return isFree(domain);
};

export const getEmailDomain = (email: string) => {
Expand Down

0 comments on commit 3f59751

Please sign in to comment.