permission-backend: fix initial test suite

Signed-off-by: Mike Lewis <mtlewis@users.noreply.github.com>
This commit is contained in:
Mike Lewis
2021-11-22 12:02:49 +00:00
parent 4ca6802b97
commit 671bf72ed6
3 changed files with 30 additions and 18 deletions
-3
View File
@@ -5,7 +5,6 @@
```ts
import { Config } from '@backstage/config';
import express from 'express';
import { IdentityClient } from '@backstage/plugin-auth-backend';
import { Logger as Logger_2 } from 'winston';
import { PermissionPolicy } from '@backstage/plugin-permission-node';
@@ -21,8 +20,6 @@ export interface RouterOptions {
// (undocumented)
config: Config;
// (undocumented)
identity?: IdentityClient;
// (undocumented)
logger: Logger_2;
// (undocumented)
policy: PermissionPolicy;
@@ -18,7 +18,6 @@ import express from 'express';
import request from 'supertest';
import { getVoidLogger } from '@backstage/backend-common';
import { ConfigReader } from '@backstage/config';
import { IdentityClient } from '@backstage/plugin-auth-backend';
import {
AuthorizeResult,
Permission,
@@ -27,9 +26,26 @@ import { PermissionPolicy } from '@backstage/plugin-permission-node';
import { createRouter } from './router';
const identityApi: Partial<IdentityClient> = {
authenticate: jest.fn().mockImplementation(_ => ({ id: 'test-user' })),
};
jest.mock('@backstage/plugin-auth-backend', () => {
class MockIdentityClient {
authenticate = jest.fn(token =>
Promise.resolve(
token
? {
id: 'test-user',
token,
}
: undefined,
),
);
static getBearerToken = jest.fn(authHeader =>
authHeader ? `<token for "${authHeader}">` : undefined,
);
}
return { IdentityClient: MockIdentityClient };
});
const policy: PermissionPolicy = {
handle: jest.fn().mockImplementation((_req, identity) => {
@@ -58,8 +74,8 @@ describe('createRouter', () => {
},
}),
policy,
identity: identityApi as IdentityClient,
});
app = express().use(router);
});
@@ -86,18 +102,20 @@ describe('createRouter', () => {
});
it('resolves identity from the Authorization header', async () => {
const token = 'token';
const token = 'test-token';
const response = await request(app)
.post('/authorize')
.auth(token, { type: 'bearer' })
.send([{ id: 123, permission }]);
expect(response.status).toEqual(200);
expect(identityApi.authenticate).toHaveBeenCalledWith('token');
expect(policy.handle).toHaveBeenCalledWith(
{ permission },
{ id: 'test-user' },
{ id: 'test-user', token: '<token for "Bearer test-token">' },
);
expect(response.body).toEqual([
{ id: 123, result: AuthorizeResult.ALLOW },
]);
});
});
});
@@ -46,7 +46,6 @@ export interface RouterOptions {
logger: Logger;
config: Config;
policy: PermissionPolicy;
identity?: IdentityClient;
}
// TODO(permission-backend): probably move this to a separate client
@@ -131,12 +130,10 @@ export async function createRouter(
): Promise<express.Router> {
const { config, policy } = options;
const discovery = SingleHostDiscovery.fromConfig(config);
const identity =
options.identity ??
new IdentityClient({
discovery,
issuer: await discovery.getExternalBaseUrl('auth'),
});
const identity = new IdentityClient({
discovery,
issuer: await discovery.getExternalBaseUrl('auth'),
});
const router = Router();
router.use(express.json());