From bbb35a3967de5bbc8aae4cc691ea7f570bd175e1 Mon Sep 17 00:00:00 2001 From: Lisa Kim Date: Tue, 7 Jun 2022 13:32:18 -0700 Subject: [PATCH] Fix null role response from users fetch (#871) (#872) --- packages/teleport/src/services/user/makeUser.ts | 4 ++-- packages/teleport/src/services/user/user.test.ts | 14 ++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/teleport/src/services/user/makeUser.ts b/packages/teleport/src/services/user/makeUser.ts index 4a43b5b9c..51ffca305 100644 --- a/packages/teleport/src/services/user/makeUser.ts +++ b/packages/teleport/src/services/user/makeUser.ts @@ -1,5 +1,5 @@ /** - * Copyright 2020 Gravitational, Inc. + * Copyright 2020-2022 Gravitational, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,7 @@ export default function makeUser(json): User { const [name, roles, authType] = at(json, ['name', 'roles', 'authType']); return { name, - roles, + roles: roles || [], authType: authType === 'local' ? 'teleport local user' : authType, isLocal: authType === 'local', }; diff --git a/packages/teleport/src/services/user/user.test.ts b/packages/teleport/src/services/user/user.test.ts index c24d246a6..09726e972 100644 --- a/packages/teleport/src/services/user/user.test.ts +++ b/packages/teleport/src/services/user/user.test.ts @@ -1,5 +1,5 @@ /** - * Copyright 2020 Gravitational, Inc. + * Copyright 2020-2022 Gravitational, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -170,9 +170,15 @@ test('undefined values in context response gives proper default values', async ( }); }); -test('fetch users, null response gives empty array', async () => { +test('fetch users, null response values gives empty array', async () => { jest.spyOn(api, 'get').mockResolvedValue(null); - - const response = await user.fetchUsers(); + let response = await user.fetchUsers(); expect(response).toStrictEqual([]); + + jest.spyOn(api, 'get').mockResolvedValue([{ name: '', authType: '' }]); + + response = await user.fetchUsers(); + expect(response).toStrictEqual([ + { authType: '', isLocal: false, name: '', roles: [] }, + ]); });