chore: added caching for successful profile retrieval
Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright 2021 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { BackstageUserIdentity, ProfileInfo } from '@backstage/core-plugin-api';
|
||||
import { UserIdentity } from './UserIdentity';
|
||||
|
||||
describe('UserIdentity', () => {
|
||||
it('should cache a successful response from the AuthApi for getProfile', async () => {
|
||||
const mockIdentity: BackstageUserIdentity = {
|
||||
type: 'user',
|
||||
userEntityRef: 'user:default/blam',
|
||||
ownershipEntityRefs: [],
|
||||
};
|
||||
|
||||
const mockProfileInfo: ProfileInfo = {
|
||||
displayName: 'Blam',
|
||||
email: 'blob@boop.com',
|
||||
};
|
||||
|
||||
const mockAuthApi: any = {
|
||||
getProfile: jest.fn().mockResolvedValue(mockProfileInfo),
|
||||
};
|
||||
|
||||
const userIdentity = UserIdentity.create({
|
||||
authApi: mockAuthApi,
|
||||
identity: mockIdentity,
|
||||
});
|
||||
|
||||
await userIdentity.getProfileInfo();
|
||||
await userIdentity.getProfileInfo();
|
||||
|
||||
const response = await userIdentity.getProfileInfo();
|
||||
|
||||
expect(mockAuthApi.getProfile).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(response).toEqual(mockProfileInfo);
|
||||
});
|
||||
|
||||
it('should not cache failures for the AuthApi for getProfile', async () => {
|
||||
const mockIdentity: BackstageUserIdentity = {
|
||||
type: 'user',
|
||||
userEntityRef: 'user:default/blam',
|
||||
ownershipEntityRefs: [],
|
||||
};
|
||||
|
||||
const mockProfileInfo: ProfileInfo = {
|
||||
displayName: 'Blam',
|
||||
email: 'blob@boop.com',
|
||||
};
|
||||
|
||||
const mockAuthApi: any = {
|
||||
getProfile: jest
|
||||
.fn()
|
||||
.mockRejectedValueOnce(new Error('boop'))
|
||||
.mockResolvedValueOnce(mockProfileInfo),
|
||||
};
|
||||
|
||||
const userIdentity = UserIdentity.create({
|
||||
authApi: mockAuthApi,
|
||||
identity: mockIdentity,
|
||||
});
|
||||
|
||||
await expect(() => userIdentity.getProfileInfo()).rejects.toThrow('boop');
|
||||
const response = await userIdentity.getProfileInfo();
|
||||
|
||||
expect(mockAuthApi.getProfile).toHaveBeenCalledTimes(2);
|
||||
|
||||
expect(response).toEqual(mockProfileInfo);
|
||||
});
|
||||
});
|
||||
@@ -34,6 +34,7 @@ import { LegacyUserIdentity } from './LegacyUserIdentity';
|
||||
* @public
|
||||
*/
|
||||
export class UserIdentity implements IdentityApi {
|
||||
private profilePromise?: Promise<ProfileInfo>;
|
||||
/**
|
||||
* Creates a new IdentityApi that acts as a Guest User.
|
||||
*
|
||||
@@ -110,8 +111,17 @@ export class UserIdentity implements IdentityApi {
|
||||
|
||||
/** {@inheritdoc @backstage/core-plugin-api#IdentityApi.getProfileInfo} */
|
||||
async getProfileInfo(): Promise<ProfileInfo> {
|
||||
const profile = await this.authApi.getProfile();
|
||||
return profile!;
|
||||
if (this.profilePromise) {
|
||||
return await this.profilePromise;
|
||||
}
|
||||
|
||||
try {
|
||||
this.profilePromise = this.authApi.getProfile() as Promise<ProfileInfo>;
|
||||
return await this.profilePromise;
|
||||
} catch (ex) {
|
||||
this.profilePromise = undefined;
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritdoc @backstage/core-plugin-api#IdentityApi.getBackstageIdentity} */
|
||||
|
||||
@@ -220,6 +220,7 @@ describe('OAuthAdapter', () => {
|
||||
backstageIdentity: {
|
||||
id: mockResponseData.backstageIdentity.id,
|
||||
token: mockResponseData.backstageIdentity.token,
|
||||
idToken: mockResponseData.backstageIdentity.token,
|
||||
identity: {
|
||||
ownershipEntityRefs: ['user:default/jimmymarkum'],
|
||||
type: 'user',
|
||||
|
||||
@@ -139,6 +139,8 @@ describe('AwsAlbAuthProvider', () => {
|
||||
id: 'user.name',
|
||||
token:
|
||||
'eyblob.eyJzdWIiOiJqaW1teW1hcmt1bSIsImVudCI6WyJ1c2VyOmRlZmF1bHQvamltbXltYXJrdW0iXX0=.eyblob',
|
||||
idToken:
|
||||
'eyblob.eyJzdWIiOiJqaW1teW1hcmt1bSIsImVudCI6WyJ1c2VyOmRlZmF1bHQvamltbXltYXJrdW0iXX0=.eyblob',
|
||||
identity: {
|
||||
ownershipEntityRefs: ['user:default/jimmymarkum'],
|
||||
type: 'user',
|
||||
|
||||
Reference in New Issue
Block a user