2022-10-11 15:29:23 +02:00
|
|
|
import { requestHeaders } from './apiauth';
|
|
|
|
import { findFromList as mapFromList, searchSomething } from './search';
|
|
|
|
import { API, Entity, SearchResult } from './types';
|
2022-04-28 12:35:02 +02:00
|
|
|
|
2022-10-11 15:29:23 +02:00
|
|
|
export function ensureItemExists(
|
|
|
|
api: API,
|
2022-09-19 19:49:46 +02:00
|
|
|
searchPath: string,
|
2022-10-11 15:29:23 +02:00
|
|
|
findInList: (entity: Entity) => boolean,
|
2022-09-19 19:49:46 +02:00
|
|
|
createPath: string,
|
2022-10-11 15:29:23 +02:00
|
|
|
body: Entity,
|
|
|
|
orgId?: number,
|
|
|
|
newItemIdField: string = 'id',
|
|
|
|
searchItemIdField?: string,
|
2022-09-19 19:49:46 +02:00
|
|
|
): Cypress.Chainable<number> {
|
2022-10-11 15:29:23 +02:00
|
|
|
return ensureSomething(
|
|
|
|
api,
|
|
|
|
() => searchSomething(api, searchPath, 'POST', mapFromList(findInList, searchItemIdField), orgId),
|
|
|
|
() => createPath,
|
|
|
|
'POST',
|
|
|
|
body,
|
|
|
|
(entity) => !!entity,
|
|
|
|
(body) => body[newItemIdField],
|
|
|
|
orgId,
|
|
|
|
);
|
2022-04-28 12:35:02 +02:00
|
|
|
}
|
|
|
|
|
2022-10-11 15:29:23 +02:00
|
|
|
export function ensureItemDoesntExist(
|
|
|
|
api: API,
|
|
|
|
searchPath: string,
|
|
|
|
findInList: (entity: Entity) => boolean,
|
|
|
|
deletePath: (entity: Entity) => string,
|
|
|
|
orgId?: number,
|
|
|
|
): Cypress.Chainable<null> {
|
|
|
|
return ensureSomething(
|
|
|
|
api,
|
|
|
|
() => searchSomething(api, searchPath, 'POST', mapFromList(findInList), orgId),
|
|
|
|
deletePath,
|
|
|
|
'DELETE',
|
|
|
|
null,
|
|
|
|
(entity) => !entity,
|
|
|
|
).then(() => null);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ensureSetting(
|
|
|
|
api: API,
|
2022-09-27 11:53:49 +01:00
|
|
|
path: string,
|
2022-10-11 15:29:23 +02:00
|
|
|
mapResult: (entity: any) => SearchResult,
|
2022-09-27 11:53:49 +01:00
|
|
|
createPath: string,
|
|
|
|
body: any,
|
2022-10-11 15:29:23 +02:00
|
|
|
orgId?: number,
|
2022-09-27 11:53:49 +01:00
|
|
|
): Cypress.Chainable<number> {
|
2022-10-11 15:29:23 +02:00
|
|
|
return ensureSomething(
|
|
|
|
api,
|
|
|
|
() => searchSomething(api, path, 'GET', mapResult, orgId),
|
|
|
|
() => createPath,
|
|
|
|
'PUT',
|
|
|
|
body,
|
|
|
|
(entity) => !!entity,
|
|
|
|
(body) => body?.settings?.id,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function awaitDesired(
|
|
|
|
trials: number,
|
|
|
|
expectEntity: (entity: Entity) => boolean,
|
|
|
|
search: () => Cypress.Chainable<SearchResult>,
|
|
|
|
initialSequence?: number,
|
|
|
|
) {
|
|
|
|
search().then((resp) => {
|
|
|
|
const foundExpectedEntity = expectEntity(resp.entity);
|
|
|
|
const foundExpectedSequence = !initialSequence || resp.sequence >= initialSequence;
|
|
|
|
|
|
|
|
if (!foundExpectedEntity || !foundExpectedSequence) {
|
|
|
|
expect(trials, `trying ${trials} more times`).to.be.greaterThan(0);
|
|
|
|
cy.wait(1000);
|
|
|
|
awaitDesired(trials - 1, expectEntity, search, initialSequence);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
interface EnsuredResult {
|
|
|
|
id: number;
|
|
|
|
sequence: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ensureSomething(
|
|
|
|
api: API,
|
|
|
|
search: () => Cypress.Chainable<SearchResult>,
|
|
|
|
apiPath: (entity: Entity) => string,
|
|
|
|
ensureMethod: string,
|
|
|
|
body: Entity,
|
|
|
|
expectEntity: (entity: Entity) => boolean,
|
|
|
|
mapId?: (body: any) => number,
|
|
|
|
orgId?: number,
|
|
|
|
): Cypress.Chainable<number> {
|
|
|
|
return search()
|
|
|
|
.then<EnsuredResult>((sRes) => {
|
|
|
|
if (expectEntity(sRes.entity)) {
|
|
|
|
return cy.wrap({ id: sRes.id, sequence: sRes.sequence });
|
2022-09-27 11:53:49 +01:00
|
|
|
}
|
2022-10-11 15:29:23 +02:00
|
|
|
|
2022-09-27 11:53:49 +01:00
|
|
|
return cy
|
|
|
|
.request({
|
2022-10-11 15:29:23 +02:00
|
|
|
method: ensureMethod,
|
|
|
|
url: apiPath(sRes.entity),
|
|
|
|
headers: requestHeaders(api, orgId),
|
2022-09-27 11:53:49 +01:00
|
|
|
body: body,
|
|
|
|
failOnStatusCode: false,
|
|
|
|
followRedirect: false,
|
|
|
|
})
|
|
|
|
.then((cRes) => {
|
|
|
|
expect(cRes.status).to.equal(200);
|
|
|
|
return {
|
2022-10-11 15:29:23 +02:00
|
|
|
id: mapId ? mapId(cRes.body) : undefined,
|
|
|
|
sequence: sRes.sequence,
|
2022-09-27 11:53:49 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then((data) => {
|
2022-10-11 15:29:23 +02:00
|
|
|
awaitDesired(90, expectEntity, search, data.sequence);
|
2022-09-27 11:53:49 +01:00
|
|
|
return cy.wrap<number>(data.id);
|
|
|
|
});
|
|
|
|
}
|