mirror of
https://github.com/elastic/kibana.git
synced 2025-06-28 19:13:14 -04:00
## Summary While working on #188737 I had to move `supertestWithoutAuth` into `kbn-ftr-common-functional-services` package. This change seems to be bigger than initially planned. Moving it to the separate PR with following changes: - move FTR `SupertestWithoutAuthProvider` service to package - remove "duplicates" in favour of service from package - update service type where needed
94 lines
3 KiB
TypeScript
94 lines
3 KiB
TypeScript
/*
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
* or more contributor license agreements. Licensed under the Elastic License
|
|
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
* 2.0.
|
|
*/
|
|
|
|
import { SupertestWithoutAuthProviderType } from '@kbn/ftr-common-functional-services';
|
|
import supertest from 'supertest';
|
|
|
|
interface PrivilegeTestScenario {
|
|
user: {
|
|
username: string;
|
|
password: string;
|
|
};
|
|
statusCode: number;
|
|
}
|
|
|
|
interface PrivilegeTestRoute {
|
|
method: string;
|
|
path: string | (() => string);
|
|
send?: any;
|
|
beforeEach?: () => any;
|
|
afterEach?: () => any;
|
|
scenarios: PrivilegeTestScenario[];
|
|
}
|
|
|
|
export function runPrivilegeTests(
|
|
routes: PrivilegeTestRoute[],
|
|
supertestWithoutAuth: SupertestWithoutAuthProviderType
|
|
) {
|
|
for (const route of routes) {
|
|
describe(`${route.method} ${route.path}`, () => {
|
|
if (route.beforeEach) {
|
|
beforeEach(() => {
|
|
return route.beforeEach ? route.beforeEach() : undefined;
|
|
});
|
|
}
|
|
if (route.afterEach) {
|
|
afterEach(() => {
|
|
return route.afterEach ? route.afterEach() : undefined;
|
|
});
|
|
}
|
|
for (const scenario of route.scenarios) {
|
|
const expectFn = (res: supertest.Response) => {
|
|
if (res.status !== scenario.statusCode) {
|
|
let message = '';
|
|
try {
|
|
message = res.body.error
|
|
? `${res.body.error}:${res.body.message}`
|
|
: res.body.message ?? '';
|
|
} catch (err) {
|
|
// swallow error
|
|
}
|
|
throw new Error(
|
|
`Expected status ${scenario.statusCode}, got: ${res.status} ${message}`
|
|
);
|
|
}
|
|
};
|
|
it(`should return a ${scenario.statusCode} for user: ${scenario.user.username}`, async () => {
|
|
const path = typeof route.path === 'function' ? route.path() : route.path;
|
|
if (route.method === 'GET') {
|
|
return supertestWithoutAuth
|
|
.get(path)
|
|
.auth(scenario.user.username, scenario.user.password)
|
|
.expect(expectFn);
|
|
} else if (route.method === 'PUT') {
|
|
return supertestWithoutAuth
|
|
.put(path)
|
|
.set('kbn-xsrf', 'xx')
|
|
.auth(scenario.user.username, scenario.user.password)
|
|
.send(route.send)
|
|
.expect(expectFn);
|
|
} else if (route.method === 'DELETE') {
|
|
return supertestWithoutAuth
|
|
.delete(path)
|
|
.set('kbn-xsrf', 'xx')
|
|
.auth(scenario.user.username, scenario.user.password)
|
|
.expect(expectFn);
|
|
} else if (route.method === 'POST') {
|
|
await supertestWithoutAuth
|
|
.post(path)
|
|
.set('kbn-xsrf', 'xx')
|
|
.auth(scenario.user.username, scenario.user.password)
|
|
.send(route.send)
|
|
.expect(expectFn);
|
|
} else {
|
|
throw new Error('not implemented');
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|