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