From 1a114bdfa548f0f97ebc017904486c56395d8ce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Girardot?= Date: Fri, 31 Jul 2020 17:12:46 +0200 Subject: [PATCH] Tests : Ajoute tests Cypress sur les mails Change-Id: I82bf94aa05aa6d546c89eb2d010f1d5cf6cbc927 --- .gitlab-ci.yml | 8 ++- Jenkinsfile.review | 37 +++++++---- .../E2E/cypress/fixtures/user-admin.json | 3 +- .../E2E/cypress/integration/4-search.spec.js | 16 +++-- .../integration/6-create-instructors.spec.js | 5 ++ .../tests/E2E/cypress/support/commands.js | 61 +++++++++++++++++++ docker-compose.yml | 7 ++- docker/lamp/Dockerfile | 7 +++ 8 files changed, 120 insertions(+), 24 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 75ff37b..a4d6395 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -48,6 +48,8 @@ build-test: cd $ADU_CI_COMPOSER_PROJECT_DIR cp .env.dist .env sed -i "s#DATABASE_URL=\"mysqli://notaires:password@127.0.0.1:3306/notaires\"#DATABASE_URL=\"mysqli://root:password@mariadb:3306/notaires\"#" .env + sed -i "s#MAILER_DSN=smtp://localhost:25#MAILER_DSN=smtp://mail:1025#" .env + sed -i "s#MAILER_SENDER=#MAILER_SENDER=sender@notaire.fr#" .env sed -i "s#mysqli://notaires:password@database:3306/notaires#mysqli://root:password@mariadb:3306/notaires#" phpunit.xml.dist composer install $ADU_CI_COMPOSER_CLI_OPTIONS yarn install --frozen-lockfile @@ -103,12 +105,14 @@ e2e-test: - build-test services: - mariadb:10.1 - - docker:19-dind + - name: mailhog/mailhog:latest + alias: mail variables: MYSQL_DATABASE: notaires MYSQL_ROOT_PASSWORD: password ADU_CI_COMPOSER_PROJECT_DIR: "$CI_PROJECT_DIR/appli_sf" CYPRESS_BASE_URL: "http://localhost:8000" + CYPRESS_mailhogUrl: "http://mail:8025" artifacts: expire_in: 1 day when: on_failure @@ -120,7 +124,7 @@ e2e-test: cd $ADU_CI_COMPOSER_PROJECT_DIR bin/console doctrine:schema:update --force bin/console cache:clear - bin/console user:add --username=admin --password=P@ssword12345 --email=test@deptnot.test --name=Administrateur --role=ROLE_ADMIN --no-interaction + bin/console user:add --username=admin --password=P@ssword12345 --email=admin@notaires.test --name=Administrateur --role=ROLE_ADMIN --no-interaction symfony server:start -d cd tests/E2E yarn diff --git a/Jenkinsfile.review b/Jenkinsfile.review index 2e6cb1f..be3623f 100644 --- a/Jenkinsfile.review +++ b/Jenkinsfile.review @@ -1,5 +1,10 @@ #!/usr/bin/env groovy +def hostIp(container) { + sh "docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${container.id} > hostIp" + readFile('hostIp').trim() +} + pipeline { agent any stages { @@ -9,7 +14,7 @@ pipeline { filename 'Dockerfile' dir './docker/lamp' additionalBuildArgs '--build-arg PHP_VERSION=7.2' - args '-u root --privileged -v "/var/lib/jenkins/composer/auth.json:/home/.composer/auth.json"' + args '-u root --privileged -e TZ="Europe/Paris" -v "/var/lib/jenkins/composer/auth.json:/home/.composer/auth.json" -v "/var/run/docker.sock:/var/run/docker.sock"' } } stages { @@ -27,6 +32,7 @@ pipeline { sh 'sed -i "s/127.0.0.1.*/127.0.0.1 localhost database/" ~/hosts.new' sh 'cp -f ~/hosts.new /etc/hosts' sh 'cp .env.dist .env' + sh 'sed -i "s#MAILER_SENDER=#MAILER_SENDER=sender@notaire.fr#" .env' sh "composer config cache-dir '${env.WORKSPACE}/.cache/.composer'" sh 'composer install --no-progress --no-suggest --no-interaction --no-scripts' sh 'yarn && yarn run build' @@ -90,18 +96,23 @@ pipeline { CYPRESS_BASE_URL = "http://localhost" } steps { - dir('appli_sf') { - sh 'bin/console doctrine:schema:drop --force' - sh 'bin/console doctrine:schema:update --force' - sh 'bin/console cache:clear' - sh 'bin/console user:add --username=admin --password=P@ssword12345 --email=test@deptnot.test --name=Administrateur --role=ROLE_ADMIN --no-interaction' - dir('tests/E2E') { - sh 'yarn' - sh 'cypress run --spec "cypress/integration/1-login.spec.js,cypress/integration/2-params.spec.js,cypress/integration/3-search-empty-db.spec.js"' - } - sh 'bin/console import:individus imports/individus.csv --no-interaction --quiet' - dir('tests/E2E') { - sh 'cypress run --spec "cypress/integration/4-search.spec.js,cypress/integration/5-create-users.spec.js,cypress/integration/6-create-instructors.spec.js,cypress/integration/7-stats.spec.js"' + script { + docker.image('mailhog/mailhog:latest').withRun('-p 8025:8025 -p 1025:1025') { mail -> + dir('appli_sf') { + sh "sed -i \"s#MAILER_DSN=smtp://localhost:25#MAILER_DSN=smtp://${hostIp(mail)}:1025#\" .env" + sh 'bin/console doctrine:schema:drop --force' + sh 'bin/console doctrine:schema:update --force' + sh 'bin/console cache:clear' + sh 'bin/console user:add --username=admin --password=P@ssword12345 --email=admin@notaires.test --name=Administrateur --role=ROLE_ADMIN --no-interaction' + dir('tests/E2E') { + sh 'yarn' + sh 'cypress run --spec "cypress/integration/1-login.spec.js,cypress/integration/2-params.spec.js,cypress/integration/3-search-empty-db.spec.js"' + } + sh 'bin/console import:individus imports/individus.csv --no-interaction --quiet' + dir('tests/E2E') { + sh "CYPRESS_mailhogUrl=\"http://${hostIp(mail)}:8025\" cypress run --spec \"cypress/integration/4-search.spec.js,cypress/integration/5-create-users.spec.js,cypress/integration/6-create-instructors.spec.js,cypress/integration/7-stats.spec.js\"" + } + } } } } diff --git a/appli_sf/tests/E2E/cypress/fixtures/user-admin.json b/appli_sf/tests/E2E/cypress/fixtures/user-admin.json index a8612c5..4bf5112 100644 --- a/appli_sf/tests/E2E/cypress/fixtures/user-admin.json +++ b/appli_sf/tests/E2E/cypress/fixtures/user-admin.json @@ -1,4 +1,5 @@ { "username": "admin", - "password": "P@ssword12345" + "password": "P@ssword12345", + "email": "admin@notaires.test" } diff --git a/appli_sf/tests/E2E/cypress/integration/4-search.spec.js b/appli_sf/tests/E2E/cypress/integration/4-search.spec.js index fba0a6b..e59897d 100644 --- a/appli_sf/tests/E2E/cypress/integration/4-search.spec.js +++ b/appli_sf/tests/E2E/cypress/integration/4-search.spec.js @@ -11,13 +11,14 @@ describe('Recherche', () => { this.userAdmin = user }) Cypress.Cookies.preserveOnce('PHPSESSID') + cy.deleteAllMails() }) it('Login', function () { cy.login(this.userAdmin.username, this.userAdmin.password) }) - it('Personne connue, aide récupérable', () => { + it('Personne connue, aide récupérable', function () { cy.visit('/search') cy.fillSearch({ deathDate: '10/02/2020', @@ -29,9 +30,10 @@ describe('Recherche', () => { }) cy.contains('Cette personne est connue de nos services') + cy.checkMailContent(this.userAdmin.email, "connue").should('be.true') }) - it('Personne connue, aide non récupérable', () => { + it('Personne connue, aide non récupérable', function () { cy.visit('/search') cy.fillSearch({ deathDate: '10/02/2020', @@ -43,9 +45,10 @@ describe('Recherche', () => { }) cy.contains('Cette personne est connue de nos services') + cy.checkMailContent(this.userAdmin.email, "connue").should('be.true') }) - it('Personne inconnue', () => { + it('Personne inconnue', function () { cy.visit('/search') cy.fillSearch({ deathDate: '10/02/2020', @@ -57,9 +60,10 @@ describe('Recherche', () => { }) cy.contains('Cette personne est inconnue de nos services') + cy.checkMailContent(this.userAdmin.email, "inconnue").should('be.true') }) - it('Personne inconnue, demande en cours d\'instruction', () => { + it('Personne inconnue, demande en cours d\'instruction', function () { cy.visit('/search') cy.fillSearch({ deathDate: '10/02/2020', @@ -71,9 +75,10 @@ describe('Recherche', () => { }) cy.contains('Cette personne est inconnue de nos services') + cy.checkMailContent(this.userAdmin.email, "il existe une demande en cours").should('be.true') }) - it('Resultat ambigu', () => { + it('Resultat ambigu', function () { cy.visit('/search') cy.fillSearch({ deathDate: '10/02/2020', @@ -85,6 +90,7 @@ describe('Recherche', () => { }) cy.contains('Les éléments que vous avez saisis ne permettent pas d\'établir avec certitude l\'identité') + cy.checkMailContent(this.userAdmin.email, "avec certitude").should('be.true') }) it('Déconnexion', () => { diff --git a/appli_sf/tests/E2E/cypress/integration/6-create-instructors.spec.js b/appli_sf/tests/E2E/cypress/integration/6-create-instructors.spec.js index 01e89e9..aa01b34 100644 --- a/appli_sf/tests/E2E/cypress/integration/6-create-instructors.spec.js +++ b/appli_sf/tests/E2E/cypress/integration/6-create-instructors.spec.js @@ -11,6 +11,7 @@ describe('Recherche', () => { this.userAdmin = user }) Cypress.Cookies.preserveOnce('PHPSESSID') + cy.deleteAllMails() }) it('Login', function () { @@ -48,6 +49,7 @@ describe('Recherche', () => { }) cy.get('body').should('not.contain', 'Nous n\'avons pas trouvé d\'instructeur associé à votre requête') + cy.checkMailReceiver('instructeur1@notaire.fr').should('be.true') }) it('Création d\'un second instructeur', () => { @@ -81,6 +83,7 @@ describe('Recherche', () => { }) cy.get('body').should('not.contain', 'Nous n\'avons pas trouvé d\'instructeur associé à votre requête') + cy.checkMailReceiver('instructeur2@notaire.fr').should('be.true') }) it('Création d\'un instructeur multi-réponses et initiales communes avec un autre instructeur', () => { @@ -115,6 +118,8 @@ describe('Recherche', () => { }) cy.get('body').should('not.contain', 'Nous n\'avons pas trouvé d\'instructeur associé à votre requête') + cy.checkMailReceiver('instructeur1@notaire.fr').should('be.true') + cy.checkMailReceiver('instructeur3@notaire.fr').should('be.false') }) it('Déconnexion', () => { diff --git a/appli_sf/tests/E2E/cypress/support/commands.js b/appli_sf/tests/E2E/cypress/support/commands.js index 52ab21b..bf8f6bc 100644 --- a/appli_sf/tests/E2E/cypress/support/commands.js +++ b/appli_sf/tests/E2E/cypress/support/commands.js @@ -108,3 +108,64 @@ Cypress.Commands.add('fillInstructor', (values) => { cy.get('form[name=instructor]').submit() }) + +// Vérifie qu'un mail a bien été envoyé à un destinataire donné (le mail doit être envoyé qu'une fois) +Cypress.Commands.add('checkMailReceiver', email => { + cy.request({ + method: 'GET', + url: Cypress.env('mailhogUrl') + '/api/v2/search', + headers: { + 'content-type': 'application/json', + }, + qs: { + kind: 'to', + query: decodeURIComponent(email) + }, + json: true, + }).then((response) => { + const items = response.body.items + + return items.length === 1; + }) +}); + +Cypress.Commands.add('deleteAllMails', email => { + cy.request({ + method: 'DELETE', + url: Cypress.env('mailhogUrl') + '/api/v1/messages', + headers: { + 'content-type': 'application/json', + }, + json: true, + }).then((response) => { + assert.equal(response.status, 200) + }) +}); + +Cypress.Commands.add('checkMailContent', (email, content) => { + cy.request({ + method: 'GET', + url: Cypress.env('mailhogUrl') + '/api/v2/search', + headers: { + 'content-type': 'application/json', + }, + qs: { + kind: 'to', + query: decodeURIComponent(email) + }, + json: true, + }).then((response) => { + const items = response.body.items + + if (items.length === 1) { + let mail = items[0] + let regExpContent = new RegExp(content, "g"); + + assert.match(mail.Content.Body, regExpContent) + + return true; + } + + return false; + }) +}); diff --git a/docker-compose.yml b/docker-compose.yml index 2f02593..753da73 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -81,9 +81,10 @@ services: - database - mail environment: - TZ: ${TIMEZONE} - CYPRESS_baseUrl: http://${HTTP_HOSTNAME} - DISPLAY: unix$DISPLAY + TZ: ${TIMEZONE} + DISPLAY: unix$DISPLAY + CYPRESS_baseUrl: http://${HTTP_HOSTNAME} + CYPRESS_mailhogUrl: http://mail:${MAILER_CLIENT_PORT:-8025} working_dir: /e2e volumes: - ./appli_sf/tests/E2E/:/e2e diff --git a/docker/lamp/Dockerfile b/docker/lamp/Dockerfile index 7dd46a7..485912d 100644 --- a/docker/lamp/Dockerfile +++ b/docker/lamp/Dockerfile @@ -89,6 +89,13 @@ ENV PHP_VERSION 7.2 # Install Cypress RUN yarn global add --pure-lockfile cypress@4.11.0; +# Docker +RUN curl -fsSLO https://get.docker.com/builds/Linux/x86_64/docker-17.04.0-ce.tgz && \ + tar xzvf docker-17.04.0-ce.tgz && \ + mv docker/docker /usr/local/bin && \ + rm -r docker docker-17.04.0-ce.tgz && \ + export DOCKER_HOST=unix:///var/run/docker.sock + WORKDIR /app CMD ["/run.sh"] -- GitLab