From 0d9cc29b4155e062b4cc5071abf423e788fd2d41 Mon Sep 17 00:00:00 2001 From: Daniil Palagin Date: Thu, 5 Sep 2024 17:53:48 +0200 Subject: [PATCH] [#202] Fix tests --- src/components/user/UserController.jsx | 4 +-- src/utils/Utils.js | 26 ++++++++--------- tests/__tests__/actions/UserActions.spec.jsx | 2 +- .../__tests__/components/Institution.spec.jsx | 4 +-- .../components/InstitutionMembers.spec.jsx | 4 +-- tests/__tests__/components/Records.spec.jsx | 2 +- tests/__tests__/components/User.spec.jsx | 28 ++++++------------- tests/__tests__/reducers/AuthReducer.spec.jsx | 4 +-- 8 files changed, 30 insertions(+), 44 deletions(-) diff --git a/src/components/user/UserController.jsx b/src/components/user/UserController.jsx index 658b98c9..0f240df8 100644 --- a/src/components/user/UserController.jsx +++ b/src/components/user/UserController.jsx @@ -25,10 +25,10 @@ import { } from "../../actions/UserActions"; import * as UserFactory from "../../utils/EntityFactory"; import omit from "lodash/omit"; -import { getRole } from "../../utils/Utils"; import { isUsingOidcAuth, userProfileLink } from "../../utils/OidcUtils"; import { isAdmin } from "../../utils/SecurityUtils"; import PropTypes from "prop-types"; +import { generateRandomUsername } from "../../utils/Utils.js"; class UserController extends React.Component { constructor(props) { @@ -131,7 +131,7 @@ class UserController extends React.Component { }; _generateUsername = () => { - this.props.generateUsername(getRole(this.state.user).toLowerCase()); + this.props.generateUsername(generateRandomUsername().toLowerCase()); }; _getPayload() { diff --git a/src/utils/Utils.js b/src/utils/Utils.js index f3b7149a..1ca3dd08 100644 --- a/src/utils/Utils.js +++ b/src/utils/Utils.js @@ -266,6 +266,17 @@ export function generatePassword() { return pass; } +export function generateRandomUsername(length = 8) { + const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + let username = ""; + + for (let i = 0; i < length; i++) { + const randomIndex = Math.floor(Math.random() * characters.length); + username += characters[randomIndex]; + } + + return username; +} /** * Checks whether the currently logged in user can view patient records of the specified institutions. * @@ -289,21 +300,6 @@ export function deviceIsMobile() { ); } -export function getRole(user) { - const userToTest = user; - if (!userToTest) { - return undefined; - } - if (userToTest.types) { - if (userToTest.types.indexOf(Vocabulary.ADMIN_TYPE) !== -1) { - return ROLE.ADMIN; - } else { - return ROLE.DOCTOR; - } - } - return undefined; -} - export function processInstitutions(institutions) { return institutions.map((item) => { return { label: item.name, value: item.uri }; diff --git a/tests/__tests__/actions/UserActions.spec.jsx b/tests/__tests__/actions/UserActions.spec.jsx index f9d0a0c2..57a1144b 100644 --- a/tests/__tests__/actions/UserActions.spec.jsx +++ b/tests/__tests__/actions/UserActions.spec.jsx @@ -187,7 +187,7 @@ describe("User asynchronous actions", function () { currentPassword: "1234", }, currentUserAdmin = { - role: ROLE.ADMIN, + roles: [ROLE.ADMIN], }, usernamePrefix = "doctor"; diff --git a/tests/__tests__/components/Institution.spec.jsx b/tests/__tests__/components/Institution.spec.jsx index 3d138f1d..35785a6b 100644 --- a/tests/__tests__/components/Institution.spec.jsx +++ b/tests/__tests__/components/Institution.spec.jsx @@ -35,11 +35,11 @@ describe("Institution", function () { user = { username: "doctor", - role: ROLE.DOCTOR, + roles: [ROLE.DOCTOR], }; admin = { username: "admin", - role: ROLE.ADMIN, + roles: [ROLE.ADMIN], }; institution = { diff --git a/tests/__tests__/components/InstitutionMembers.spec.jsx b/tests/__tests__/components/InstitutionMembers.spec.jsx index 30cad8f8..f8be670d 100644 --- a/tests/__tests__/components/InstitutionMembers.spec.jsx +++ b/tests/__tests__/components/InstitutionMembers.spec.jsx @@ -45,12 +45,12 @@ describe("InstitutionMembers", function () { user = { username: "user", - role: ROLE.DOCTOR, + roles: [ROLE.DOCTOR], }; admin = { username: "admin", - role: ROLE.ADMIN, + roles: [ROLE.ADMIN], }; userDeleted = { diff --git a/tests/__tests__/components/Records.spec.jsx b/tests/__tests__/components/Records.spec.jsx index 6acc7632..ef9f8c69 100644 --- a/tests/__tests__/components/Records.spec.jsx +++ b/tests/__tests__/components/Records.spec.jsx @@ -19,7 +19,7 @@ describe("Records", function () { handlers; admin = { username: "admin", - role: ROLE.ADMIN, + roles: [ROLE.ADMIN], }; records = [ { diff --git a/tests/__tests__/components/User.spec.jsx b/tests/__tests__/components/User.spec.jsx index dca53456..9d3e742c 100644 --- a/tests/__tests__/components/User.spec.jsx +++ b/tests/__tests__/components/User.spec.jsx @@ -29,11 +29,11 @@ describe("User", function () { currentUser = { username: "test", - role: ROLE.DOCTOR, + roles: [ROLE.DOCTOR], }; currentUserAdmin = { username: "test", - role: ROLE.ADMIN, + roles: [ROLE.ADMIN], }; newUser = { ...newUser, @@ -162,7 +162,7 @@ describe("User", function () { , ); const result = TestUtils.scryRenderedDOMComponentsWithTag(tree, "input"); - expect(result.length).toEqual(5); + expect(result.length).toEqual(7); for (let input of result) { switch (input.name) { case "firstName": @@ -189,9 +189,7 @@ describe("User", function () { } } const selects = TestUtils.scryRenderedDOMComponentsWithTag(tree, "select"); - expect(selects.length).toEqual(2); - expect(selects[1].value).toEqual(ROLE.DOCTOR); - + expect(selects.length).toEqual(1); const randomButton = TestUtils.scryRenderedDOMComponentsWithClass(tree, "button-random"); expect(randomButton.length).toEqual(1); }); @@ -295,7 +293,7 @@ describe("User", function () { , ); const result = TestUtils.scryRenderedDOMComponentsWithTag(tree, "input"); - expect(result.length).toEqual(4); + expect(result.length).toEqual(6); for (let input of result) { switch (input.name) { case "firstName": @@ -318,10 +316,8 @@ describe("User", function () { } } const selects = TestUtils.scryRenderedDOMComponentsWithTag(tree, "select"); - expect(selects.length).toEqual(2); + expect(selects.length).toEqual(1); expect(selects[0].disabled).toBeFalsy(); - expect(selects[1].disabled).toBeFalsy(); - const randomButton = TestUtils.scryRenderedDOMComponentsWithClass(tree, "glyphicon"); expect(randomButton.length).toEqual(0); }); @@ -342,7 +338,7 @@ describe("User", function () { , ); const result = TestUtils.scryRenderedDOMComponentsWithTag(tree, "input"); - expect(result.length).toEqual(4); + expect(result.length).toEqual(7); for (let input of result) { switch (input.name) { case "firstName": @@ -365,10 +361,8 @@ describe("User", function () { } } const selects = TestUtils.scryRenderedDOMComponentsWithTag(tree, "select"); - expect(selects.length).toEqual(2); - expect(selects[1].value).toEqual(ROLE.ADMIN); + expect(selects.length).toEqual(1); expect(selects[0].disabled).toBeFalsy(); - expect(selects[1].disabled).toBeFalsy(); }); it("renders filled user's form", function () { @@ -387,7 +381,7 @@ describe("User", function () { , ); const result = TestUtils.scryRenderedDOMComponentsWithTag(tree, "input"); - expect(result.length).toEqual(4); + expect(result.length).toEqual(5); for (let input of result) { switch (input.name) { case "firstName": @@ -409,10 +403,6 @@ describe("User", function () { break; } } - const selects = TestUtils.scryRenderedDOMComponentsWithTag(tree, "select"); - expect(selects.length).toEqual(1); - expect(selects[0].value).toEqual(ROLE.ADMIN); - expect(selects[0].disabled).toBeTruthy(); }); it('renders "Cancel" button and click on it', function () { diff --git a/tests/__tests__/reducers/AuthReducer.spec.jsx b/tests/__tests__/reducers/AuthReducer.spec.jsx index 77d62824..920ab64d 100644 --- a/tests/__tests__/reducers/AuthReducer.spec.jsx +++ b/tests/__tests__/reducers/AuthReducer.spec.jsx @@ -1,10 +1,10 @@ import React from "react"; import * as ActionConstants from "../../../src/constants/ActionConstants"; import { ACTION_STATUS } from "../../../src/constants/DefaultConstants"; -import { getRole } from "../../../src/utils/Utils"; import AuthReducer from "../../../src/reducers/AuthReducer"; import UserReducer from "../../../src/reducers/UserReducer"; import { describe, expect, it } from "vitest"; +import { getRoles } from "../../../src/utils/SecurityUtils.js"; describe("AuthReducer", function () { const user = { @@ -127,7 +127,7 @@ describe("AuthReducer", function () { status: ACTION_STATUS.SUCCESS, user: { ...user, - role: getRole(user), + roles: getRoles(user), }, testEntry: initialState.testEntry, };