From 13be62e3bbf6954fa0a1e2cccab7d5f44caa7809 Mon Sep 17 00:00:00 2001 From: Jeet <68876606+jn1119@users.noreply.github.com> Date: Mon, 8 Feb 2021 07:58:29 -0700 Subject: [PATCH] fix: Added usernameInIdp property to update user schema (#292) This change fixes authentication of users who are added by admin from ServiceWorkbench UI directly --- .../lib/schema/update-user.json | 6 +++ .../lib/user/__tests__/user-service.test.js | 50 +++++++++++++++++++ .../services/lib/schema/update-user.json | 6 +++ .../lib/user/__tests__/user-service.test.js | 1 + 4 files changed, 63 insertions(+) diff --git a/addons/addon-base-raas/packages/base-raas-services/lib/schema/update-user.json b/addons/addon-base-raas/packages/base-raas-services/lib/schema/update-user.json index 52a722fe7a..7d80b0e4ef 100644 --- a/addons/addon-base-raas/packages/base-raas-services/lib/schema/update-user.json +++ b/addons/addon-base-raas/packages/base-raas-services/lib/schema/update-user.json @@ -10,6 +10,12 @@ "type": "string", "pattern": "^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$" }, + "usernameInIdp": { + "type": "string", + "minLength": 3, + "maxLength": 300, + "pattern": "^[A-Za-z0-9-_.]+$|^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$" + }, "firstName": { "type": "string", "maxLength": 500 diff --git a/addons/addon-base-raas/packages/base-raas-services/lib/user/__tests__/user-service.test.js b/addons/addon-base-raas/packages/base-raas-services/lib/user/__tests__/user-service.test.js index 503755f288..47d5dc69be 100644 --- a/addons/addon-base-raas/packages/base-raas-services/lib/user/__tests__/user-service.test.js +++ b/addons/addon-base-raas/packages/base-raas-services/lib/user/__tests__/user-service.test.js @@ -69,6 +69,56 @@ describe('UserService', () => { service.assertAuthorized = jest.fn(); }); + describe('updateUser', () => { + it('should not fail validation when usernameInIdp in schema', async () => { + const user = { + email: 'example@amazon.com', + usernameInIdp: 'example', + uid: 'user1', + rev: 0, + }; + + // mocked functions + service.toUserType = jest.fn(() => { + return { userType: 'root' }; + }); + service.getUserByPrincipal = jest.fn(() => { + return null; + }); + service.findUser = jest.fn(() => { + return user; + }); + await service.updateUser({}, user); + }); + + it('should fail validation when unknown property in schema', async () => { + const user = { + email: 'example@amazon.com', + usernameInIdp: 'example', + uid: 'user1', + rev: 0, + unknown: 'unknownProperty', + }; + + // mocked functions + service.toUserType = jest.fn(() => { + return { userType: 'root' }; + }); + service.getUserByPrincipal = jest.fn(() => { + return null; + }); + service.findUser = jest.fn(() => { + return user; + }); + try { + await service.updateUser({}, user); + expect.fail('Expected to throw error validation errors'); + } catch (err) { + expect(err.message).toEqual('Input has validation errors'); + } + }); + }); + describe('createUsers', () => { it('should try to create a user', async () => { const user = { diff --git a/addons/addon-base/packages/services/lib/schema/update-user.json b/addons/addon-base/packages/services/lib/schema/update-user.json index 92f1134099..f9fa412b1e 100644 --- a/addons/addon-base/packages/services/lib/schema/update-user.json +++ b/addons/addon-base/packages/services/lib/schema/update-user.json @@ -6,6 +6,12 @@ "uid": { "type": "string" }, + "usernameInIdp": { + "type": "string", + "minLength": 3, + "maxLength": 300, + "pattern": "^[A-Za-z0-9-_.]+$|^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$" + }, "email": { "type": "string", "pattern": "^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$" diff --git a/addons/addon-base/packages/services/lib/user/__tests__/user-service.test.js b/addons/addon-base/packages/services/lib/user/__tests__/user-service.test.js index a6aa64ea67..813843672d 100644 --- a/addons/addon-base/packages/services/lib/user/__tests__/user-service.test.js +++ b/addons/addon-base/packages/services/lib/user/__tests__/user-service.test.js @@ -243,6 +243,7 @@ describe('UserService', () => { // BUILD const toUpdate = { uid, + usernameInIdp: 'user1', rev: 2, };