backend-test-utils: added header utils to mockCredentials
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -53,6 +53,7 @@ export namespace mockCredentials {
|
||||
subject?: string,
|
||||
): BackstageCredentials<BackstageServicePrincipal>;
|
||||
export namespace service {
|
||||
export function header(payload?: TokenPayload): string;
|
||||
export function token(payload?: TokenPayload): string;
|
||||
export type TokenPayload = {
|
||||
subject?: string;
|
||||
@@ -63,9 +64,10 @@ export namespace mockCredentials {
|
||||
userEntityRef?: string,
|
||||
): BackstageCredentials<BackstageUserPrincipal>;
|
||||
export namespace user {
|
||||
export function header(payload?: TokenPayload): string;
|
||||
export function token(payload?: TokenPayload): string;
|
||||
export type TokenPayload = {
|
||||
userEntityRef: string;
|
||||
userEntityRef?: string;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,6 +78,15 @@ export namespace mockCredentials {
|
||||
}
|
||||
return MOCK_USER_TOKEN;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an authorization header with a mocked user token. If a 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(payload?: TokenPayload): string {
|
||||
return `Bearer ${token(payload)}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -121,5 +130,14 @@ export namespace mockCredentials {
|
||||
}
|
||||
return MOCK_SERVICE_TOKEN;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an authorization header with a mocked service token. If a
|
||||
* 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(payload?: TokenPayload): string {
|
||||
return `Bearer ${token(payload)}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import { Router } from 'express';
|
||||
import request from 'supertest';
|
||||
|
||||
import { startTestBackend } from './TestBackend';
|
||||
import { mockCredentials } from '../services';
|
||||
|
||||
// This bit makes sure that test backends are cleaned up properly
|
||||
let globalTestBackendHasBeenStopped = false;
|
||||
@@ -199,9 +200,11 @@ describe('TestBackend', () => {
|
||||
scheduler: coreServices.scheduler,
|
||||
tokenManager: coreServices.tokenManager,
|
||||
urlReader: coreServices.urlReader,
|
||||
auth: coreServices.auth,
|
||||
httpAuth: coreServices.httpAuth,
|
||||
},
|
||||
async init(deps) {
|
||||
expect(Object.keys(deps)).toHaveLength(15);
|
||||
expect(Object.keys(deps)).toHaveLength(17);
|
||||
expect(Object.values(deps)).not.toContain(undefined);
|
||||
},
|
||||
});
|
||||
@@ -232,7 +235,9 @@ describe('TestBackend', () => {
|
||||
|
||||
const { server } = await startTestBackend({ features: [testPlugin()] });
|
||||
|
||||
const res = await request(server).get('/api/test/ping-me');
|
||||
const res = await request(server)
|
||||
.get('/api/test/ping-me')
|
||||
.set('authorization', mockCredentials.user.header());
|
||||
expect(res.status).toEqual(200);
|
||||
expect(res.body).toEqual({ message: 'pong' });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user