test/e2e/api/asset.type.systemtype.mappings.e2e.spec.tsaddedimport {
expect,
app,
http,
Server,
cleanMockSetup,
createAuthToken,
setupIAM,
closeMockServer,
setupE2EMockEnvVariables,
Authorities,
authoriseProjectIamAccess,
chai,
uuidv4,
} from "../common/e2e-imports";
import { setupProjectAndGetDetails, getAssetTypeSystemTypeMappingById } from "../util/db-helper";
/* eslint-disable @typescript-eslint/no-explicit-any */
describe("AssetType SystemType Mappings API E2E tests", () => {
let server: Server;
let authToken: any;
let testProjectId: string;
let testAssetTypeId: string;
let testSystemTypeId: string;
const createAssetType = async () => {
const wfRes = await chai
.request(app)
.post(`/api/v2/projects/${testProjectId}/commissioning/workflows`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ name: `Mappings E2E Workflow ${uuidv4()}` });
const atRes = await chai
.request(app)
.post(`/api/v2/projects/${testProjectId}/asset-types`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ name: `Mappings E2E AssetType ${uuidv4()}`, commissioningWorkflowId: wfRes.body.commissioningWorkflowId });
return atRes.body.assetTypeId;
};
const createSystemType = async () => {
const wfRes = await chai
.request(app)
.post(`/api/v2/projects/${testProjectId}/commissioning/workflows`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ name: `Mappings E2E ST Workflow ${uuidv4()}` });
const stRes = await chai
.request(app)
.post(`/api/v2/projects/${testProjectId}/system-types`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ name: `Mappings E2E SystemType ${uuidv4()}`, commissioningWorkflowId: wfRes.body.commissioningWorkflowId });
return stRes.body.systemTypeId;
};
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]);
testAssetTypeId = await createAssetType();
testSystemTypeId = await createSystemType();
});
after(async () => {
closeMockServer(server);
});
describe("POST /api/v2/projects/:projectId/asset-types/system-type-mappings", () => {
it("should return 201 and create a mapping with a valid payload", async () => {
const assetTypeId = await createAssetType();
const res = await chai
.request(app)
.post(`/api/v2/projects/${testProjectId}/asset-types/system-type-mappings`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ assetTypeId, systemTypeId: testSystemTypeId });
expect(res.status).to.equal(201);
expect(res.body).to.have.property("mappingId");
expect(res.body).to.have.property("assetTypeId", assetTypeId);
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 getAssetTypeSystemTypeMappingById(testProjectId, res.body.mappingId);
expect(dbRow).to.exist;
expect(dbRow.AssetTypeId).to.equal(assetTypeId);
});
it("should return 400 when assetTypeId is missing", async () => {
const res = await chai
.request(app)
.post(`/api/v2/projects/${testProjectId}/asset-types/system-type-mappings`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ systemTypeId: testSystemTypeId });
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/projects/${testProjectId}/asset-types/system-type-mappings`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ assetTypeId: testAssetTypeId, systemTypeId: "not-a-uuid" });
expect(res.status).to.equal(400);
expect(res.body).to.have.property("code", "InvalidRequest");
});
it("should return 404 when the asset type does not exist", async () => {
const res = await chai
.request(app)
.post(`/api/v2/projects/${testProjectId}/asset-types/system-type-mappings`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ assetTypeId: uuidv4(), systemTypeId: testSystemTypeId });
expect(res.status).to.equal(404);
});
it("should return 404 when the system type does not exist", async () => {
const assetTypeId = await createAssetType();
const res = await chai
.request(app)
.post(`/api/v2/projects/${testProjectId}/asset-types/system-type-mappings`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ assetTypeId, systemTypeId: uuidv4() });
expect(res.status).to.equal(404);
});
});
describe("GET /api/v2/projects/:projectId/asset-types/system-type-mappings", () => {
before(async () => {
authToken = await createAuthToken("abc");
await authoriseProjectIamAccess(testProjectId, authToken.token, [Authorities.PROJECT_EDIT]);
const assetTypeId = await createAssetType();
await chai
.request(app)
.post(`/api/v2/projects/${testProjectId}/asset-types/system-type-mappings`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ assetTypeId, systemTypeId: testSystemTypeId });
});
it("should return 200 with an array of mappings", async () => {
const res = await chai.request(app).get(`/api/v2/projects/${testProjectId}/asset-types/system-type-mappings`).set("Authorization", `Bearer ${authToken.token}`);
expect(res.status).to.equal(200);
expect(res.body).to.be.an("array");
expect(res.body.length).to.be.greaterThan(0);
expect(res.body[0]).to.have.property("mappingId");
expect(res.body[0]).to.have.property("assetTypeId");
expect(res.body[0]).to.have.property("systemTypeId");
});
it("should return 400 when projectId is not a valid UUID", async () => {
const res = await chai.request(app).get("/api/v2/projects/not-a-uuid/asset-types/system-type-mappings").set("Authorization", `Bearer ${authToken.token}`);
expect(res.status).to.equal(400);
expect(res.body).to.have.property("code", "InvalidRequest");
});
});
describe("GET /api/v2/projects/:projectId/asset-types/system-type-mappings/:mappingId", () => {
let createdMappingId: string;
before(async () => {
authToken = await createAuthToken("abc");
await authoriseProjectIamAccess(testProjectId, authToken.token, [Authorities.PROJECT_EDIT]);
const assetTypeId = await createAssetType();
const res = await chai
.request(app)
.post(`/api/v2/projects/${testProjectId}/asset-types/system-type-mappings`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ assetTypeId, systemTypeId: testSystemTypeId });
createdMappingId = res.body.mappingId;
});
it("should return 200 with the mapping", async () => {
const res = await chai
.request(app)
.get(`/api/v2/projects/${testProjectId}/asset-types/system-type-mappings/${createdMappingId}`)
.set("Authorization", `Bearer ${authToken.token}`);
expect(res.status).to.equal(200);
expect(res.body).to.have.property("mappingId", createdMappingId);
});
it("should return 404 when the mapping does not exist", async () => {
const res = await chai
.request(app)
.get(`/api/v2/projects/${testProjectId}/asset-types/system-type-mappings/${uuidv4()}`)
.set("Authorization", `Bearer ${authToken.token}`);
expect(res.status).to.equal(404);
});
it("should return 400 when mappingId is not a valid UUID", async () => {
const res = await chai
.request(app)
.get(`/api/v2/projects/${testProjectId}/asset-types/system-type-mappings/not-a-uuid`)
.set("Authorization", `Bearer ${authToken.token}`);
expect(res.status).to.equal(400);
});
});
describe("DELETE /api/v2/projects/:projectId/asset-types/system-type-mappings/:mappingId", () => {
let createdMappingId: string;
before(async () => {
authToken = await createAuthToken("abc");
await authoriseProjectIamAccess(testProjectId, authToken.token, [Authorities.PROJECT_EDIT]);
const assetTypeId = await createAssetType();
const res = await chai
.request(app)
.post(`/api/v2/projects/${testProjectId}/asset-types/system-type-mappings`)
.set("Authorization", `Bearer ${authToken.token}`)
.send({ assetTypeId, systemTypeId: testSystemTypeId });
createdMappingId = res.body.mappingId;
});
it("should return 204 and remove the mapping", async () => {
const res = await chai
.request(app)
.delete(`/api/v2/projects/${testProjectId}/asset-types/system-type-mappings/${createdMappingId}`)
.set("Authorization", `Bearer ${authToken.token}`);
expect(res.status).to.equal(204);
const dbRow = await getAssetTypeSystemTypeMappingById(testProjectId, createdMappingId);
expect(dbRow).to.not.exist;
});
it("should return 404 when the mapping does not exist", async () => {
const res = await chai
.request(app)
.delete(`/api/v2/projects/${testProjectId}/asset-types/system-type-mappings/${uuidv4()}`)
.set("Authorization", `Bearer ${authToken.token}`);
expect(res.status).to.equal(404);
});
});
});