test/e2e/api/systems.e2e.spec.tsaddedimport {
expect,
app,
http,
Server,
cleanMockSetup,
createAuthToken,
setupIAM,
closeMockServer,
setupE2EMockEnvVariables,
Authorities,
authoriseProjectIamAccess,
chai,
uuidv4,
} from "../common/e2e-imports";
import { setupProjectAndGetDetails, getCommissioningSystemById } from "../util/db-helper";
/* eslint-disable @typescript-eslint/no-explicit-any */
describe("Systems API E2E tests", () => {
let server: Server;
let authToken: any;
let testProjectId: string;
let testSystemTypeId: string;
before(async () => {
setupE2EMockEnvVariables();
await cleanMockSetup();
await setupIAM();
server = http.createServer(app);
server.listen();
const projectDetails = await setupProjectAndGetDetails(uuidv4().substring(20));
testProjectId = projectDetails.ProjectId;
authToken = await createAuthToken("abc");
await authoriseProjectIamAccess(testProjectId, authToken.token, [Authorities.PROJECT_EDIT]);
// A system requires an existing system type (systemTypeId), which in turn needs a workflow.
const wfRes = await chai
.request(app)
.post(`/api/v2/projects/${testProjectId}/commissioning/workflows`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ name: "Systems E2E Workflow" });
const workflowId = wfRes.body.commissioningWorkflowId;
const stRes = await chai
.request(app)
.post(`/api/v2/projects/${testProjectId}/system-types`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ name: `Systems E2E SystemType ${uuidv4()}`, commissioningWorkflowId: workflowId });
testSystemTypeId = stRes.body.systemTypeId;
});
after(async () => {
closeMockServer(server);
});
describe("GET /api/v2/systems", () => {
before(async () => {
authToken = await createAuthToken("abc");
await authoriseProjectIamAccess(testProjectId, authToken.token, [Authorities.PROJECT_VIEW]);
});
it("should return 200 with a paginated envelope", async () => {
const res = await chai.request(app).get(`/api/v2/systems?projectId=${testProjectId}`).set("Authorization", `Bearer ${authToken.token}`);
expect(res.status).to.equal(200);
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("records").that.is.an("array");
expect(res.body).to.have.property("recordCount");
expect(res.body).to.have.property("lastFetchedIndexId");
});
it("should advance to the next page using lastFetchedIndexId and size", async () => {
const editToken = await createAuthToken("abc");
await authoriseProjectIamAccess(testProjectId, editToken.token, [Authorities.PROJECT_EDIT]);
// Ensure there are at least 2 records to paginate over
await chai
.request(app)
.post(`/api/v2/systems`)
.set("Authorization", `Bearer ${editToken.token}`)
.send({ projectId: testProjectId, name: `E2E Page System A ${uuidv4()}`, systemTypeId: testSystemTypeId });
await chai
.request(app)
.post(`/api/v2/systems`)
.set("Authorization", `Bearer ${editToken.token}`)
.send({ projectId: testProjectId, name: `E2E Page System B ${uuidv4()}`, systemTypeId: testSystemTypeId });
const firstPage = await chai.request(app).get(`/api/v2/systems?projectId=${testProjectId}&size=1`).set("Authorization", `Bearer ${authToken.token}`);
expect(firstPage.status).to.equal(200);
expect(firstPage.body.records).to.be.an("array");
expect(firstPage.body.records.length).to.equal(1);
expect(firstPage.body.lastFetchedIndexId).to.not.equal(-1);
const firstRecordId = firstPage.body.records[0].commissioningSystemId;
const nextPage = await chai
.request(app)
.get(`/api/v2/systems?projectId=${testProjectId}&size=1&lastFetchedIndexId=${firstPage.body.lastFetchedIndexId}`)
.set("Authorization", `Bearer ${authToken.token}`);
expect(nextPage.status).to.equal(200);
expect(nextPage.body.records).to.be.an("array");
if (nextPage.body.records.length > 0) {
expect(nextPage.body.records[0].commissioningSystemId).to.not.equal(firstRecordId);
}
});
it("should return 400 when size is not a number", async () => {
const res = await chai.request(app).get(`/api/v2/systems?projectId=${testProjectId}&size=abc`).set("Authorization", `Bearer ${authToken.token}`);
expect(res.status).to.equal(400);
expect(res.body).to.have.property("code", "InvalidRequest");
});
it("should return 400 when projectId is missing", async () => {
const res = await chai.request(app).get("/api/v2/systems").set("Authorization", `Bearer ${authToken.token}`);
expect(res.status).to.equal(400);
expect(res.body).to.have.property("code", "InvalidRequest");
});
it("should return 400 when projectId is not a valid UUID", async () => {
const res = await chai.request(app).get("/api/v2/systems?projectId=not-a-uuid").set("Authorization", `Bearer ${authToken.token}`);
expect(res.status).to.equal(400);
expect(res.body).to.have.property("code", "InvalidRequest");
});
});
describe("POST /api/v2/systems", () => {
before(async () => {
authToken = await createAuthToken("abc");
await authoriseProjectIamAccess(testProjectId, authToken.token, [Authorities.PROJECT_EDIT]);
});
it("should return 201 and create a system with valid payload", async () => {
const payload = { projectId: testProjectId, name: "E2E Chilled Water", systemTypeId: testSystemTypeId };
const res = await chai.request(app).post(`/api/v2/systems`).set("Authorization", `Bearer ${authToken.token}`).send(payload);
expect(res.status).to.equal(201);
expect(res.body).to.have.property("commissioningSystemId");
expect(res.body).to.have.property("name", "E2E Chilled Water");
expect(res.body).to.have.property("systemTypeId", testSystemTypeId);
expect(res.body).to.have.property("createdBy");
expect(res.body).to.have.property("insertedOn");
const dbRow = await getCommissioningSystemById(testProjectId, res.body.commissioningSystemId);
expect(dbRow).to.exist;
expect(dbRow.Name).to.equal("E2E Chilled Water");
});
it("should return 400 when name is missing", async () => {
const res = await chai
.request(app)
.post(`/api/v2/systems`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ projectId: testProjectId, systemTypeId: testSystemTypeId });
expect(res.status).to.equal(400);
expect(res.body).to.have.property("code", "InvalidRequest");
});
it("should return 400 when systemTypeId is missing", async () => {
const res = await chai
.request(app)
.post(`/api/v2/systems`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ projectId: testProjectId, name: "E2E Chilled Water" });
expect(res.status).to.equal(400);
expect(res.body).to.have.property("code", "InvalidRequest");
});
it("should return 400 when systemTypeId is not a valid UUID", async () => {
const res = await chai
.request(app)
.post(`/api/v2/systems`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ projectId: testProjectId, name: "E2E Chilled Water", systemTypeId: "not-a-uuid" });
expect(res.status).to.equal(400);
expect(res.body).to.have.property("code", "InvalidRequest");
});
it("should return 404 when systemTypeId does not exist", async () => {
const res = await chai
.request(app)
.post(`/api/v2/systems`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ projectId: testProjectId, name: "E2E Chilled Water", systemTypeId: uuidv4() });
expect(res.status).to.equal(404);
});
it("should return 400 when projectId is missing", async () => {
const res = await chai
.request(app)
.post("/api/v2/systems")
.set("Authorization", `Bearer ${authToken.token}`)
.send({ name: "E2E Chilled Water", systemTypeId: testSystemTypeId });
expect(res.status).to.equal(400);
});
});
describe("GET /api/v2/systems/:commissioningSystemId", () => {
let createdSystemId: string;
before(async () => {
authToken = await createAuthToken("abc");
await authoriseProjectIamAccess(testProjectId, authToken.token, [Authorities.PROJECT_EDIT]);
const res = await chai
.request(app)
.post(`/api/v2/systems`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ projectId: testProjectId, name: "E2E Get System Test", systemTypeId: testSystemTypeId });
createdSystemId = res.body.commissioningSystemId;
});
it("should return 200 with the system", async () => {
const res = await chai.request(app).get(`/api/v2/systems/${createdSystemId}?projectId=${testProjectId}`).set("Authorization", `Bearer ${authToken.token}`);
expect(res.status).to.equal(200);
expect(res.body).to.have.property("commissioningSystemId", createdSystemId);
expect(res.body).to.have.property("name", "E2E Get System Test");
});
it("should return 404 when system does not exist", async () => {
const res = await chai.request(app).get(`/api/v2/systems/${uuidv4()}?projectId=${testProjectId}`).set("Authorization", `Bearer ${authToken.token}`);
expect(res.status).to.equal(404);
});
it("should return 400 when commissioningSystemId is not a valid UUID", async () => {
const res = await chai.request(app).get(`/api/v2/systems/not-a-uuid?projectId=${testProjectId}`).set("Authorization", `Bearer ${authToken.token}`);
expect(res.status).to.equal(400);
});
});
});