Page MenuHomeSoftware Heritage

vault: Escape provided email address in the URL
ClosedPublic

Authored by vlorentz on Sep 17 2021, 3:37 PM.

Details

Summary

Resolves SWH-VAULT-33

Test Plan

Cypress does not actually type in the field and I don't understand why :(

Diff Detail

Repository
rDWAPPS Web applications
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

vlorentz edited the summary of this revision. (Show Details)

Build has FAILED

Patch application report for D6289 (id=22863)

Rebasing onto 3a5f6087f1...

Current branch diff-target is up to date.
Changes applied before test
commit 1c59fdbffd25acdd3213db7dfe4232aca107c97f
Author: Valentin Lorentz <vlorentz@softwareheritage.org>
Date:   Fri Sep 17 15:37:15 2021 +0200

    vault: Escape provided email address in the URL

Link to build: https://jenkins.softwareheritage.org/job/DWAPPS/job/tests-on-diff/1107/
See console output for more information: https://jenkins.softwareheritage.org/job/DWAPPS/job/tests-on-diff/1107/console

Cypress does not actually type in the field and I don't understand why :(

There is multiple DOM elements with the modal-dialog class in the vault-create-taks.html template
and they do not have all a input[type="email"] child element so this must confuse cypress somehow.

I managed to make the test pass the following way:

it('should create a directory cooking task with an email address', function() {

    // Browse a directory
    cy.visit(this.directoryUrl);

    // Stub responses when checking vault task status
    cy.intercept('GET', this.vaultDirectoryUrl, {body: {'exception': 'NotFoundExc'}})
      .as('checkVaultCookingTask');

    // Stub responses when requesting the vault API to simulate
    // a task has been created
    cy.intercept('POST', this.vaultDirectoryUrl + '?email=foo%2Bbar%40example.org', {
      body: this.genVaultDirCookingResponse('new')
    }).as('createVaultCookingTask');

    // Open vault cook directory modal
    cy.contains('button', 'Download')
      .click();

    cy.wait('@checkVaultCookingTask');

    // Create a vault cooking task through the GUI and fill email input
    cy.get('#vault-cook-directory-modal input[type="email"]')
      .type('foo+bar@example.org', {force: true});

    cy.get('#vault-cook-directory-modal')
      .contains('button:visible', 'Ok')
      .click();

    cy.wait('@createVaultCookingTask');

  });

While running the test, I got an error claiming the email is not valid as the validation regexp used is incomplete.
I replaced its use by the email-validator package installed with yarn and test was passing after that change.

diff --git a/assets/src/bundles/vault/vault-create-tasks.js b/assets/src/bundles/vault/vault-create-tasks.js
index 5cc98a4e..38fbf1c3 100644
--- a/assets/src/bundles/vault/vault-create-tasks.js
+++ b/assets/src/bundles/vault/vault-create-tasks.js
@@ -5,6 +5,8 @@
  * See top-level LICENSE file for more information
  */
 
+import * as EmailValidator from 'email-validator';
+
 import {handleFetchError, csrfPost, htmlAlert} from 'utils/functions';
 
 const alertStyle = {
@@ -102,14 +104,9 @@ async function addVaultCookingTask(objectType, cookingTask) {
   }
 }
 
-function validateEmail(email) {
-  const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
-  return re.test(String(email).toLowerCase());
-}
-
 export function cookDirectoryArchive(swhid) {
   const email = $('#swh-vault-directory-email').val().trim();
-  if (!email || validateEmail(email)) {
+  if (!email || EmailValidator.validate(email)) {
     const cookingTask = {
       'bundle_type': 'flat',
       'swhid': swhid,
@@ -133,7 +130,7 @@ export async function fetchDirectoryArchive(directorySwhid) {
 
 export function cookRevisionArchive(revisionId) {
   const email = $('#swh-vault-revision-email').val().trim();
-  if (!email || validateEmail(email)) {
+  if (!email || EmailValidator.validate(email)) {
     const cookingTask = {
       'bundle_type': 'git_bare',
       'swhid': revisionId,
This revision now requires changes to proceed.Sep 21 2021, 4:41 PM

fix test + use email-validator + rebase

Build has FAILED

Patch application report for D6289 (id=22947)

Rebasing onto 9e51595ad7...

Current branch diff-target is up to date.
Changes applied before test
commit 8aa625784dfe95b97ba921f43704acb4739aede4
Author: Valentin Lorentz <vlorentz@softwareheritage.org>
Date:   Fri Sep 17 15:37:15 2021 +0200

    vault: Escape provided email address in the URL + fix email validation

Link to build: https://jenkins.softwareheritage.org/job/DWAPPS/job/tests-on-diff/1111/
See console output for more information: https://jenkins.softwareheritage.org/job/DWAPPS/job/tests-on-diff/1111/console

Build is green

Patch application report for D6289 (id=22947)

Rebasing onto 9e51595ad7...

Current branch diff-target is up to date.
Changes applied before test
commit 8aa625784dfe95b97ba921f43704acb4739aede4
Author: Valentin Lorentz <vlorentz@softwareheritage.org>
Date:   Fri Sep 17 15:37:15 2021 +0200

    vault: Escape provided email address in the URL + fix email validation

See https://jenkins.softwareheritage.org/job/DWAPPS/job/tests-on-diff/1112/ for more details.

This revision is now accepted and ready to land.Sep 22 2021, 2:01 PM