backend-test-utils: update mockCredentials for cookie auth

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-02-26 01:25:27 +01:00
parent 982fc43d68
commit e210800545
2 changed files with 17 additions and 21 deletions
@@ -81,7 +81,6 @@ describe('mockCredentials', () => {
});
it('creates limited user tokens and headers', () => {
expect(mockCredentials.limitedUser.token()).toBe('mock-limited-user-token');
expect(mockCredentials.limitedUser.token('user:default/other')).toBe(
'mock-limited-user-token:{"sub":"user:default/other"}',
);
@@ -89,14 +88,11 @@ describe('mockCredentials', () => {
'mock-invalid-limited-user-token',
);
expect(mockCredentials.limitedUser.header()).toBe(
'Bearer mock-limited-user-token',
expect(mockCredentials.limitedUser.cookie('user:default/other')).toBe(
'backstage-auth=mock-limited-user-token:{"sub":"user:default/other"}',
);
expect(mockCredentials.limitedUser.header('user:default/other')).toBe(
'Bearer mock-limited-user-token:{"sub":"user:default/other"}',
);
expect(mockCredentials.limitedUser.invalidHeader()).toBe(
'Bearer mock-invalid-limited-user-token',
expect(mockCredentials.limitedUser.invalidCookie()).toBe(
'backstage-auth=mock-invalid-limited-user-token',
);
});
@@ -24,13 +24,14 @@ import {
export const DEFAULT_MOCK_USER_ENTITY_REF = 'user:default/mock';
export const DEFAULT_MOCK_SERVICE_SUBJECT = 'external:test-service';
export const MOCK_AUTH_COOKIE = 'backstage-auth';
export const MOCK_NONE_TOKEN = 'mock-none-token';
export const MOCK_USER_TOKEN = 'mock-user-token';
export const MOCK_USER_TOKEN_PREFIX = 'mock-user-token:';
export const MOCK_INVALID_USER_TOKEN = 'mock-invalid-user-token';
export const MOCK_USER_LIMITED_TOKEN = 'mock-limited-user-token';
export const MOCK_USER_LIMITED_TOKEN_PREFIX = 'mock-limited-user-token:';
export const MOCK_INVALID_USER_LIMITED_TOKEN =
'mock-invalid-limited-user-token';
@@ -171,14 +172,13 @@ export namespace mockCredentials {
* encoded into the token and forwarded to the credentials object when
* authenticated by the mock auth service.
*/
export function token(userEntityRef?: string): string {
if (userEntityRef) {
validateUserEntityRef(userEntityRef);
return `${MOCK_USER_LIMITED_TOKEN_PREFIX}${JSON.stringify({
sub: userEntityRef,
} satisfies UserTokenPayload)}`;
}
return MOCK_USER_LIMITED_TOKEN;
export function token(
userEntityRef: string = DEFAULT_MOCK_USER_ENTITY_REF,
): string {
validateUserEntityRef(userEntityRef);
return `${MOCK_USER_LIMITED_TOKEN_PREFIX}${JSON.stringify({
sub: userEntityRef,
} satisfies UserTokenPayload)}`;
}
/**
@@ -186,16 +186,16 @@ export namespace mockCredentials {
* payload is provided it will be encoded into the token and forwarded to
* the credentials object when authenticated by the mock auth service.
*/
export function header(userEntityRef?: string): string {
return `Bearer ${token(userEntityRef)}`;
export function cookie(userEntityRef?: string): string {
return `${MOCK_AUTH_COOKIE}=${token(userEntityRef)}`;
}
export function invalidToken(): string {
return MOCK_INVALID_USER_LIMITED_TOKEN;
}
export function invalidHeader(): string {
return `Bearer ${invalidToken()}`;
export function invalidCookie(): string {
return `${MOCK_AUTH_COOKIE}=${invalidToken()}`;
}
}