From 14369e61e920767a4b94f55329bf103f9e272e0a Mon Sep 17 00:00:00 2001
From: Thomas Citharel <tcit@tcit.fr>
Date: Sun, 13 Oct 2019 17:03:48 +0200
Subject: [PATCH] Fix test and handle errors better

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
---
 js/src/utils/errors.ts                   | 26 +++++++++++++++++++++++-
 js/src/views/User/Register.vue           |  1 -
 js/src/views/User/ResendConfirmation.vue |  2 +-
 js/src/views/User/SendPasswordReset.vue  |  2 +-
 js/src/vue-apollo.ts                     | 12 +++++++++--
 js/tests/e2e/specs/event.js              |  2 +-
 js/tests/e2e/specs/login.js              |  6 +++---
 js/tests/e2e/specs/register.js           |  2 +-
 8 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/js/src/utils/errors.ts b/js/src/utils/errors.ts
index 9bd5055aa..ff3f6652a 100644
--- a/js/src/utils/errors.ts
+++ b/js/src/utils/errors.ts
@@ -7,7 +7,7 @@ export const defaultError: IError = {
   value: i18n.t('An error has occurred.') as string,
 };
 
-export interface IError { match: RegExp; value: string; suggestRefresh?: boolean; }
+export interface IError { match: RegExp; value: string|null; suggestRefresh?: boolean; }
 
 export const errors: IError[] = [
   {
@@ -48,4 +48,28 @@ export const errors: IError[] = [
     value: i18n.t("The current identity doesn't have any permission on this event. You should probably change it.") as string,
     suggestRefresh: false,
   },
+  {
+    match: /^User with email not found$/,
+    value: null,
+  },
+  {
+    match: /^Username is already taken$/,
+    value: null,
+  },
+  {
+    match: /^No user with this email was found$/,
+    value: null,
+  },
+  {
+    match: /^Impossible to authenticate, either your email or password are invalid.$/,
+    value: null,
+  },
+  {
+    match: /^No user to validate with this email was found$/,
+    value: null,
+  },
+  {
+    match: /^This email is already used.$/,
+    value: null,
+  },
 ];
diff --git a/js/src/views/User/Register.vue b/js/src/views/User/Register.vue
index c231cc598..cdc45ba29 100644
--- a/js/src/views/User/Register.vue
+++ b/js/src/views/User/Register.vue
@@ -152,7 +152,6 @@ export default class Register extends Vue {
         acc[error.details] = error.message;
         return acc;
       },                                       {});
-      console.log(this.errors);
     }
   }
 }
diff --git a/js/src/views/User/ResendConfirmation.vue b/js/src/views/User/ResendConfirmation.vue
index 4ffec0a4b..24b8a80fe 100644
--- a/js/src/views/User/ResendConfirmation.vue
+++ b/js/src/views/User/ResendConfirmation.vue
@@ -10,7 +10,7 @@
             <b-input aria-required="true" required type="email" v-model="credentials.email"/>
           </b-field>
           <p class="control has-text-centered">
-            <b-button type="is-primary">
+            <b-button type="is-primary" native-type="submit">
             {{ $t('Send me the confirmation email once again') }}
             </b-button>
           </p>
diff --git a/js/src/views/User/SendPasswordReset.vue b/js/src/views/User/SendPasswordReset.vue
index 29b9c24bd..35e67a588 100644
--- a/js/src/views/User/SendPasswordReset.vue
+++ b/js/src/views/User/SendPasswordReset.vue
@@ -11,7 +11,7 @@
             <b-input aria-required="true" required type="email" v-model="credentials.email"/>
           </b-field>
           <p class="control has-text-centered">
-              <b-button type="is-primary">
+              <b-button type="is-primary" native-type="submit">
               {{ $t('Send me an email to reset my password') }}
             </b-button>
           </p>
diff --git a/js/src/vue-apollo.ts b/js/src/vue-apollo.ts
index 00e84c262..06ffa767b 100644
--- a/js/src/vue-apollo.ts
+++ b/js/src/vue-apollo.ts
@@ -92,7 +92,11 @@ const errorLink = onError(({ graphQLErrors, networkError, forward, operation })
     const messages: Set<string> = new Set();
 
     graphQLErrors.forEach(({ message, locations, path }) => {
-      messages.add(computeErrorMessage(message));
+      const computedMessage = computeErrorMessage(message);
+      if (computedMessage) {
+        console.log('computed message', computedMessage);
+        messages.add(computedMessage);
+      }
       console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
     });
 
@@ -103,7 +107,10 @@ const errorLink = onError(({ graphQLErrors, networkError, forward, operation })
 
   if (networkError) {
     console.log(`[Network error]: ${networkError}`);
-    Snackbar.open({ message: computeErrorMessage(networkError), type: 'is-danger', position: 'is-bottom' });
+    const computedMessage = computeErrorMessage(networkError);
+    if (computedMessage) {
+      Snackbar.open({ message: computedMessage, type: 'is-danger', position: 'is-bottom' });
+    }
   }
 });
 
@@ -115,6 +122,7 @@ const computeErrorMessage = (message) => {
     return acc;
   },                                  defaultError);
 
+  if (error.value === null) return null;
   return error.suggestRefresh === false ? error.value : `${error.value}<br>${refreshSuggestion}`;
 };
 
diff --git a/js/tests/e2e/specs/event.js b/js/tests/e2e/specs/event.js
index cc1780e1a..b87d389a5 100644
--- a/js/tests/e2e/specs/event.js
+++ b/js/tests/e2e/specs/event.js
@@ -37,7 +37,7 @@ describe('Events', () => {
 
         cy.contains('.navbar-item', 'My events').click();
         cy.contains('.title', EVENT.title);
-        cy.contains('.content.column', 'You\'re organizing this event');
+        cy.contains('.content.column', 'Organized by I\'m a test user');
         cy.contains('.title-wrapper .date-component .datetime-container .month', 'Sep');
         cy.contains('.title-wrapper .date-component .datetime-container .day', '15');
     });
diff --git a/js/tests/e2e/specs/login.js b/js/tests/e2e/specs/login.js
index 7df635a87..d7f35781d 100644
--- a/js/tests/e2e/specs/login.js
+++ b/js/tests/e2e/specs/login.js
@@ -44,9 +44,9 @@ describe('Login', () => {
     cy.get('input[type=email]').type('user@email.com');
     cy.get('input[type=password]').type('some password');
     cy.get('form').submit();
-    cy.contains('.navbar-link', 'test_user');
+    cy.get('.navbar-link span.icon i').should('have.class', 'mdi-account-circle');
     cy.contains('article.message.is-info', 'Welcome back I\'m a test user');
-    cy.contains('.navbar-item.has-dropdown', 'test_user').click();
+    cy.get('.navbar-item.has-dropdown').click();
     cy.get('.navbar-item').last().contains('Log out').click();
   });
 
@@ -77,7 +77,7 @@ describe('Login', () => {
     cy.get('form').submit();
     cy.wait(1000);
 
-    cy.contains('.navbar-link', 'test_user_2');
+    cy.get('.navbar-link span.icon i').should('have.class', 'mdi-account-circle');
     cy.contains('article.message.is-info', 'Welcome back DuplicateNot');
   });
 });
diff --git a/js/tests/e2e/specs/register.js b/js/tests/e2e/specs/register.js
index e34ee3bf9..abedfd5db 100644
--- a/js/tests/e2e/specs/register.js
+++ b/js/tests/e2e/specs/register.js
@@ -65,7 +65,7 @@ describe('Registration', () => {
       expect(loc.pathname).to.eq('/');
     });
 
-    cy.contains('.navbar-link', 'tester');
+    cy.get('.navbar-link span.icon i').should('have.class', 'mdi-account-circle');
     cy.contains('article.message.is-info', 'Welcome back tester account');
   });
 });
\ No newline at end of file