Skip to content

Commit

Permalink
feat(tests): add test for measure valuesDetails and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinRousselet committed May 23, 2024
1 parent d441a72 commit ce858a2
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 60 deletions.
2 changes: 2 additions & 0 deletions lib/modules/model/types/ModelApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
MetadataGroups,
MetadataMappings,
} from "./ModelContent";
import { MeasureValuesDetails } from "../../measure/types/MeasureDefinition";

interface ModelsControllerRequest {
controller: "device-manager/models";
Expand Down Expand Up @@ -67,6 +68,7 @@ export interface ApiModelWriteMeasureRequest extends ModelsControllerRequest {
body: {
type: string;
valuesMappings: JSONObject;
valuesDetails?: MeasureValuesDetails;
};
}
export type ApiModelWriteMeasureResult = KDocument<MeasureModelContent>;
Expand Down
31 changes: 31 additions & 0 deletions tests/fixtures/rights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export default {
},
},
},
"default-user": {
content: {
profileIds: ["default-user"],
},
credentials: {
local: { username: "default-user", password: "password" },
},
},
},
profiles: {
"ayse-admin": {
Expand Down Expand Up @@ -70,7 +78,21 @@ export default {
},
],
},
"default-user": {
rateLimit: 0,
policies: [
{
roleId: "default-user",
},
],
optimizedPolicies: [
{
roleId: "default-user",
},
],
},
},

roles: {
tests: {
controllers: {
Expand All @@ -92,5 +114,14 @@ export default {
},
},
},
"default-user": {
controllers: {
"device-manager/assets": {
actions: {
"*": true,
},
},
},
},
},
};
10 changes: 10 additions & 0 deletions tests/hooks/engines.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BaseRequest, JSONObject, Kuzzle } from "kuzzle-sdk";
import { loadSecurityDefault } from "./security";

async function createEngineIfNotExists(
sdk: Kuzzle,
Expand All @@ -24,9 +25,18 @@ async function createEngineIfNotExists(
}

export async function beforeAllCreateEngines(sdk: Kuzzle) {
await loadSecurityDefault(sdk);
if ((await sdk.auth.getCurrentUser())._id !== "test-admin") {
await sdk.auth.login("local", {
username: "test-admin",
password: "password",
});
}
await Promise.all([
createEngineIfNotExists(sdk, "engine-ayse"),
createEngineIfNotExists(sdk, "engine-kuzzle"),
createEngineIfNotExists(sdk, "engine-other-group", "other-group"),
// Timeout to avoid too many login per seconds error
new Promise((resolve) => setTimeout(resolve, 300)),
]);
}
6 changes: 6 additions & 0 deletions tests/hooks/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import rights from "../fixtures/rights";

export async function loadSecurityDefault(sdk: Kuzzle) {
sdk.jwt = null;
if (!(await sdk.auth.getCurrentUser())._id.includes("admin")) {
await sdk.auth.login("local", {
username: "admin",
password: "password",
});
}

await sdk.query({
controller: "admin",
Expand Down
4 changes: 2 additions & 2 deletions tests/scenario/migrated/asset-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe("features/Asset/Controller", () => {
position: null,
},
linkedDevices: [],
_kuzzle_info: { author: "-1" },
_kuzzle_info: { author: "test-admin" },
},
});

Expand All @@ -62,7 +62,7 @@ describe("features/Asset/Controller", () => {
).resolves.toMatchObject({
_source: {
metadata: { height: 5, weight: 1250 },
_kuzzle_info: { updater: "-1" },
_kuzzle_info: { updater: "test-admin" },
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ describe("features/Device/Controller/AttachEngine", () => {
await expect(
sdk.document.get("device-manager", "devices", "DummyTemp-detached1")
).resolves.toMatchObject({
_source: { engineId: "engine-kuzzle", _kuzzle_info: { updater: "-1" } },
_source: { engineId: "engine-kuzzle", _kuzzle_info: { updater: "test-admin" } },
});

await expect(
sdk.document.get("engine-kuzzle", "devices", "DummyTemp-detached1")
).resolves.toMatchObject({
_source: { engineId: "engine-kuzzle", _kuzzle_info: { author: "-1" } },
_source: { engineId: "engine-kuzzle", _kuzzle_info: { author: "test-admin" } },
});

response = await sendPayloads(sdk, "dummy-temp", [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("features/Device/Controller/DetachEngine", () => {
await expect(
sdk.document.get("device-manager", "devices", "DummyTemp-detached1")
).resolves.toMatchObject({
_source: { engineId: null, _kuzzle_info: { updater: "-1" } },
_source: { engineId: null, _kuzzle_info: { updater: "test-admin" } },
});

await expect(
Expand Down
53 changes: 31 additions & 22 deletions tests/scenario/migrated/model-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,11 +474,12 @@ describe("features/Model/Controller", () => {
});

expect(response.result).toMatchObject({
total: 7,
total: 8,
models: [
{ _id: "model-measure-acceleration" },
{ _id: "model-measure-battery" },
{ _id: "model-measure-humidity" },
{ _id: "model-measure-light" },
{ _id: "model-measure-movement" },
{ _id: "model-measure-position" },
{ _id: "model-measure-presence" },
Expand All @@ -503,55 +504,63 @@ describe("features/Model/Controller", () => {
model: "AdvancedPlane",
metadataMappings: {
company: { type: "keyword" },
year: { type: "integer" }
year: { type: "integer" },
},
measures: [
{ name: "temperatureExt", type: "temperature" },
],
measures: [{ name: "temperatureExt", type: "temperature" }],
metadataDetails: {
company: {
group: "companyInfo",
locales: {
en: {
friendlyName: "Manufacturer",
description: "The company that manufactured the plane"
description: "The company that manufactured the plane",
},
fr: {
friendlyName: "Fabricant",
description: "L'entreprise qui a fabriqué l'avion"
}
}
}
description: "L'entreprise qui a fabriqué l'avion",
},
},
},
},
metadataGroups: {
companyInfo: {
locales: {
en: { groupFriendlyName: "Company Information", description: "All company related informations" },
fr: { groupFriendlyName: "Informations sur l'entreprise", description: "Toutes les informations relatives a l'entreprise" }
}
}
}
en: {
groupFriendlyName: "Company Information",
description: "All company related informations",
},
fr: {
groupFriendlyName: "Informations sur l'entreprise",
description: "Toutes les informations relatives a l'entreprise",
},
},
},
},
};

// Write the asset model with metadata details and groups
await sdk.query({
controller: "device-manager/models",
action: "writeAsset",
body: assetModelWithDetailsAndGroups
body: assetModelWithDetailsAndGroups,
});

// Retrieve and assert the asset model
const response = await sdk.document.get("device-manager", "models", "model-asset-AdvancedPlane");
expect(response._source.asset).toHaveProperty('metadataDetails');
expect(response._source.asset).toHaveProperty('metadataGroups');
delete assetModelWithDetailsAndGroups.engineGroup
const response = await sdk.document.get(
"device-manager",
"models",
"model-asset-AdvancedPlane"
);
expect(response._source.asset).toHaveProperty("metadataDetails");
expect(response._source.asset).toHaveProperty("metadataGroups");
delete assetModelWithDetailsAndGroups.engineGroup;
expect(response._source).toMatchObject({
type: "asset",
engineGroup: "commons",
asset: assetModelWithDetailsAndGroups
asset: assetModelWithDetailsAndGroups,
});
});

it("Register models from the framework", async () => {
let response;
let promise;
Expand Down
37 changes: 20 additions & 17 deletions tests/scenario/modules/assets/asset-migrate-tenant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,14 @@ describe("AssetsController:migrateTenant", () => {
sdk.disconnect();
});

it("should fail if the user is not an admin", async () => {
await expect(
sdk.query({
controller: "device-manager/assets",
action: "migrateTenant",
engineId: "engine-ayse",
body: {
assetsList: ["Container-linked1", "Container-linked2"],
newEngineId: "engine-kuzzle",
},
}),
).rejects.toThrow("User -1 is not authorized to migrate assets");
});

it("should fail if both engine does not belong to same group", async () => {
// We connect only here to avoid failing the first test
// If we do it in the beforeAll hook, the first test will fail
// And if we run it each time we might encounter "Too many login attempts per second"
await sdk.auth.login("local", {
username: "test-admin",
password: "password",
});
// We connect only here to avoid failing the first test
// If we do it in the beforeAll hook, the first test will fail
// And if we run it each time we might encounter "Too many login attempts per second"

await expect(
sdk.query({
Expand Down Expand Up @@ -158,4 +144,21 @@ describe("AssetsController:migrateTenant", () => {
expect(response.status).toBe(200);
expect(assets.result.hits).toHaveLength(2);
});
it("should fail if the user is not an admin", async () => {
await sdk.auth.login("local", {
username: "default-user",
password: "password",
});
await expect(
sdk.query({
controller: "device-manager/assets",
action: "migrateTenant",
engineId: "engine-ayse",
body: {
assetsList: ["Container-linked1", "Container-linked2"],
newEngineId: "engine-kuzzle",
},
}),
).rejects.toThrow("User default-user is not authorized to migrate assets");
});
});
4 changes: 2 additions & 2 deletions tests/scenario/modules/devices/action-attach-engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("DevicesController:attachEngine", () => {
_source: {
engineId: "engine-kuzzle",
_kuzzle_info: {
updater: "-1",
updater: "test-admin",
},
},
});
Expand All @@ -32,7 +32,7 @@ describe("DevicesController:attachEngine", () => {
_source: {
engineId: "engine-kuzzle",
_kuzzle_info: {
author: "-1",
author: "test-admin",
},
},
});
Expand Down
5 changes: 0 additions & 5 deletions tests/scenario/modules/devices/action-export-measures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,6 @@ describe("DevicesController:exportMeasures", () => {
});

it("should generate a authenticated link", async () => {
await sdk.auth.login("local", {
username: "test-admin",
password: "password",
});

await sendPayloads(sdk, "dummy-temp", [
{ deviceEUI: "linked1", temperature: 37 },
]);
Expand Down
8 changes: 4 additions & 4 deletions tests/scenario/modules/devices/action-link-asset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ describe("DeviceController: receiveMeasure", () => {
).resolves.toMatchObject({
assetId: "Container-unlinked1",
_kuzzle_info: {
updater: "-1",
updater: "test-admin",
},
});
await expect(
documentGet(sdk, "engine-ayse", "devices", "DummyTemp-unlinked1"),
).resolves.toMatchObject({
assetId: "Container-unlinked1",
_kuzzle_info: {
updater: "-1",
updater: "test-admin",
},
});
await expect(
Expand All @@ -68,7 +68,7 @@ describe("DeviceController: receiveMeasure", () => {
},
],
_kuzzle_info: {
updater: "-1",
updater: "test-admin",
},
});

Expand Down Expand Up @@ -103,7 +103,7 @@ describe("DeviceController: receiveMeasure", () => {
},
],
_kuzzle_info: {
updater: "-1",
updater: "test-admin",
},
});
});
Expand Down
4 changes: 2 additions & 2 deletions tests/scenario/modules/devices/action-scrud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ describe("Device SCRUD", () => {
color: "RED",
},
_kuzzle_info: {
author: "-1",
updater: "-1",
author: "test-admin",
updater: "test-admin",
},
});

Expand Down
Loading

0 comments on commit ce858a2

Please sign in to comment.