You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ze...@apache.org on 2021/11/15 16:20:12 UTC

[incubator-streampipes] 01/03: Add test function to switch user

This is an automated email from the ASF dual-hosted git repository.

zehnder pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampipes.git

commit 7b50c6e4b1c39bdb67724479c987e2b4ccd0fef6
Author: Philipp Zehnder <ze...@fzi.de>
AuthorDate: Thu Nov 11 17:24:53 2021 +0100

    Add test function to switch user
---
 .../login.ts => support/builder/UserBuilder.ts}    | 40 +++++++++++++-----
 ui/cypress/support/general/login.ts                | 11 +++--
 .../{utils/UserUtils.ts => general/switchUser.ts}  | 24 +++++++++--
 ui/cypress/support/index.ts                        |  2 +
 .../support/{utils/UserUtils.ts => model/User.ts}  |  9 ++--
 ui/cypress/support/utils/UserUtils.ts              | 47 ++++++++++++++++++++-
 ui/cypress/tests/installation/login.ts             |  8 ++--
 ui/cypress/tests/userManagement/userManagement.ts  | 49 +++++++++-------------
 8 files changed, 137 insertions(+), 53 deletions(-)

diff --git a/ui/cypress/tests/installation/login.ts b/ui/cypress/support/builder/UserBuilder.ts
similarity index 53%
copy from ui/cypress/tests/installation/login.ts
copy to ui/cypress/support/builder/UserBuilder.ts
index 043cb4e..a897a78 100644
--- a/ui/cypress/tests/installation/login.ts
+++ b/ui/cypress/support/builder/UserBuilder.ts
@@ -16,17 +16,37 @@
  *
  */
 
-import { UserUtils } from '../../support/utils/UserUtils';
+import { User } from '../model/User';
+import { UserRole } from '../../../src/app/_enums/user-role.enum';
 
+export class UserBuilder {
+    user: User;
 
-describe('Login and logout of StreamPipes', () => {
+    constructor(email: string) {
+        this.user = new User();
+        this.user.email = email;
+    }
 
-  it('Perform Test', () => {
-    cy.visit('#/login');
-    cy.dataCy('login-email').type(UserUtils.testUserName);
-    cy.dataCy('login-password').type(UserUtils.testUserPassword);
-    cy.dataCy('login-button').click();
-    cy.logout();
-  });
+    public static create(email: string) {
+        return new UserBuilder(email);
+    }
 
-});
+    public setName(name: string) {
+        this.user.name = name;
+        return this;
+    }
+
+    public setPassword(password: string) {
+        this.user.password = password;
+        return this;
+    }
+
+    public addRole(role: UserRole) {
+        this.user.role.push(role);
+        return this;
+    }
+
+    build() {
+        return this.user;
+    }
+}
diff --git a/ui/cypress/support/general/login.ts b/ui/cypress/support/general/login.ts
index c47ac4a..17f72f8 100644
--- a/ui/cypress/support/general/login.ts
+++ b/ui/cypress/support/general/login.ts
@@ -18,6 +18,7 @@
 
 import { UserUtils } from '../utils/UserUtils';
 import { JwtHelperService } from '@auth0/angular-jwt';
+import { User } from '../model/User';
 
 declare global {
   namespace Cypress {
@@ -31,13 +32,17 @@ declare global {
   }
 }
 
-export const login = () => {
+export const login = (user?: User) => {
+  let _user;
+  _user = !user ? UserUtils.adminUser : user;
+
   cy.request('POST', '/streampipes-backend/api/v2/auth/login', {
-    username: UserUtils.testUserName,
-    password: UserUtils.testUserPassword
+    username: _user.email,
+    password: _user.password
   }).then(res => {
     const decodedToken = new JwtHelperService({}).decodeToken(res.body.accessToken);
     window.localStorage.setItem('auth-user', JSON.stringify(decodedToken.user));
     window.localStorage.setItem('auth-token', res.body.accessToken);
+    console.log(user);
   });
 };
diff --git a/ui/cypress/support/utils/UserUtils.ts b/ui/cypress/support/general/switchUser.ts
similarity index 62%
copy from ui/cypress/support/utils/UserUtils.ts
copy to ui/cypress/support/general/switchUser.ts
index b932f69..af66852 100644
--- a/ui/cypress/support/utils/UserUtils.ts
+++ b/ui/cypress/support/general/switchUser.ts
@@ -16,7 +16,25 @@
  *
  */
 
-export class UserUtils {
-  public static testUserName = 'admin@streampipes.apache.org';
-  public static testUserPassword = 'admin';
+import { User } from '../model/User';
+
+declare global {
+  namespace Cypress {
+    interface Chainable {
+      /**
+       * Logout and login as new user
+       * @example cy.switchUser();
+       */
+      switchUser: typeof switchUser;
+    }
+  }
 }
+
+export const switchUser = (user: User) => {
+  cy.logout();
+  cy.visit('#/login');
+  cy.dataCy('login-email').type(user.email);
+  cy.dataCy('login-password').type(user.password);
+  cy.dataCy('login-button').click();
+  cy.wait(5000);
+};
diff --git a/ui/cypress/support/index.ts b/ui/cypress/support/index.ts
index a0009f0..95aad6e 100644
--- a/ui/cypress/support/index.ts
+++ b/ui/cypress/support/index.ts
@@ -19,6 +19,7 @@
 import { login } from './general/login';
 import { logout } from './general/logout';
 import { dataCy } from './general/dataCy';
+import { switchUser } from './general/switchUser';
 // tslint:disable-next-line:no-implicit-dependencies
 import 'cypress-file-upload';
 import { resetStreamPipes } from './general/resetStreamPipes';
@@ -27,6 +28,7 @@ import { initStreamPipesTest } from './general/InitStreamPipesTest';
 // General commands
 Cypress.Commands.add('login', login);
 Cypress.Commands.add('logout', logout);
+Cypress.Commands.add('switchUser', switchUser);
 Cypress.Commands.add('dataCy', dataCy);
 Cypress.Commands.add('resetStreamPipes', resetStreamPipes);
 Cypress.Commands.add('initStreamPipesTest', initStreamPipesTest);
diff --git a/ui/cypress/support/utils/UserUtils.ts b/ui/cypress/support/model/User.ts
similarity index 82%
copy from ui/cypress/support/utils/UserUtils.ts
copy to ui/cypress/support/model/User.ts
index b932f69..891ed1e 100644
--- a/ui/cypress/support/utils/UserUtils.ts
+++ b/ui/cypress/support/model/User.ts
@@ -15,8 +15,11 @@
  * limitations under the License.
  *
  */
+import { UserRole } from '../../../src/app/_enums/user-role.enum';
 
-export class UserUtils {
-  public static testUserName = 'admin@streampipes.apache.org';
-  public static testUserPassword = 'admin';
+export class User {
+    email: string;
+    name: string;
+    password: string;
+    role: UserRole[] = [];
 }
diff --git a/ui/cypress/support/utils/UserUtils.ts b/ui/cypress/support/utils/UserUtils.ts
index b932f69..8dd2754 100644
--- a/ui/cypress/support/utils/UserUtils.ts
+++ b/ui/cypress/support/utils/UserUtils.ts
@@ -16,7 +16,50 @@
  *
  */
 
+
+import { User } from '../model/User';
+import { UserBuilder } from '../builder/UserBuilder';
+import { UserRole } from '../../../src/app/_enums/user-role.enum';
+
 export class UserUtils {
-  public static testUserName = 'admin@streampipes.apache.org';
-  public static testUserPassword = 'admin';
+  public static adminUser = UserBuilder.create('admin@streampipes.apache.org')
+      .setName('admin')
+      .setPassword('admin')
+      .addRole(UserRole.ROLE_ADMIN)
+      .build();
+
+
+  // public static testUserName = 'admin@streampipes.apache.org';
+  // public static testUserPassword = 'admin';
+
+  public static goToUserConfiguration() {
+    cy.visit('#/configuration');
+    cy.get('div').contains('Security').parent().click();
+  }
+
+  public static addUser(user: User) {
+    this.goToUserConfiguration();
+
+    // user configuration
+    cy.dataCy('add-new-user').click();
+    cy.dataCy('new-user-email').type(user.email);
+    cy.dataCy('new-user-full-name').type(user.name);
+    cy.dataCy('new-user-password').type(user.password);
+    cy.dataCy('new-user-password-repeat').type(user.password);
+
+    // Set role
+    cy.dataCy('role-' + user.role[0]).children().click();
+    cy.dataCy('new-user-enabled').children().click();
+
+    // Store
+    cy.dataCy('sp-element-edit-user-save').click();
+  }
+
+  public static deleteUser(user: User) {
+    this.goToUserConfiguration();
+
+    cy.dataCy('user-delete-btn-' + user.name).click();
+  }
+
+
 }
diff --git a/ui/cypress/tests/installation/login.ts b/ui/cypress/tests/installation/login.ts
index 043cb4e..65c0a98 100644
--- a/ui/cypress/tests/installation/login.ts
+++ b/ui/cypress/tests/installation/login.ts
@@ -23,10 +23,12 @@ describe('Login and logout of StreamPipes', () => {
 
   it('Perform Test', () => {
     cy.visit('#/login');
-    cy.dataCy('login-email').type(UserUtils.testUserName);
-    cy.dataCy('login-password').type(UserUtils.testUserPassword);
+    cy.dataCy('login-email').type(UserUtils.adminUser.email);
+    cy.dataCy('login-password').type(UserUtils.adminUser.password);
     cy.dataCy('login-button').click();
-    cy.logout();
+
+    cy.dataCy('sp-user-preferences').click();
+    cy.dataCy('sp-logout').click();
   });
 
 });
diff --git a/ui/cypress/tests/userManagement/userManagement.ts b/ui/cypress/tests/userManagement/userManagement.ts
index 4186ded..8a277c5 100644
--- a/ui/cypress/tests/userManagement/userManagement.ts
+++ b/ui/cypress/tests/userManagement/userManagement.ts
@@ -17,51 +17,42 @@
  */
 
 
+import { UserBuilder } from '../../support/builder/UserBuilder';
+import { UserRole } from '../../../src/app/_enums/user-role.enum';
+import { UserUtils } from '../../support/utils/UserUtils';
+
 describe('Test User Management', () => {
   before('Setup Test', () => {
-    // cy.initStreamPipesTest();
-    cy.login();
+    cy.initStreamPipesTest();
   });
 
   it('Perform Test', () => {
     // Add new user
-    cy.visit('#/configuration');
-    cy.get('div').contains('Security').parent().click();
-
+    UserUtils.goToUserConfiguration();
 
     cy.dataCy('user-accounts-table-row', { timeout: 10000 }).should('have.length', 1);
 
     const email = 'user@streampipes.apache.org';
     const name = 'user';
-    // user configuration
-    cy.dataCy('add-new-user').click();
-    cy.dataCy('new-user-email').type(email);
-    cy.dataCy('new-user-full-name').type(name);
-    cy.dataCy('new-user-password').type(name);
-    cy.dataCy('new-user-password-repeat').type(name);
-
-    // Set role
-    cy.dataCy('role-ROLE_ADMIN').children().click();
-    cy.dataCy('new-user-enabled').children().click();
+    const user = UserBuilder.create(email)
+        .setName(name)
+        .setPassword(name)
+        .addRole(UserRole.ROLE_ADMIN)
+        .build();
 
-    // Store
-    cy.dataCy('sp-element-edit-user-save').click();
+    UserUtils.addUser(user);
 
     cy.dataCy('user-accounts-table-row', { timeout: 10000 }).should('have.length', 2);
 
-    cy.dataCy('user-delete-btn-' + name).click();
-
-    cy.dataCy('user-accounts-table-row', { timeout: 10000 }).should('have.length', 1);
-    // Validate that new user is in list
+    // Login as user
+    cy.switchUser(user);
 
-    // Logout
+    UserUtils.goToUserConfiguration();
 
-    // Login with new user
-
-    // Logout
-
-    // Login to Admin
-
-    // Delete User
+    cy.switchUser(UserUtils.adminUser);
+    UserUtils.goToUserConfiguration();
+    UserUtils.deleteUser(user);
+    // Validate that user is removed
+    cy.dataCy('user-accounts-table-row', { timeout: 10000 }).should('have.length', 1);
   });
 });