From 694891b3f465d440160234d1ef87a3dac196330e Mon Sep 17 00:00:00 2001 From: Aschen Date: Fri, 6 Jan 2023 17:02:06 +0100 Subject: [PATCH 1/2] Fix asset and device creation from backend --- features/Asset/Controller.feature | 17 +++++--- features/fixtures/application/app.ts | 5 ++- .../fixtures/application/tests/controller.ts | 42 +++++++++++++++++++ .../{testPipes.ts => tests/pipes.ts} | 2 +- lib/modules/asset/AssetService.ts | 8 +++- lib/modules/device/DeviceService.ts | 8 +++- 6 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 features/fixtures/application/tests/controller.ts rename features/fixtures/application/{testPipes.ts => tests/pipes.ts} (99%) diff --git a/features/Asset/Controller.feature b/features/Asset/Controller.feature index dff3c3d7..4c449659 100644 --- a/features/Asset/Controller.feature +++ b/features/Asset/Controller.feature @@ -45,11 +45,11 @@ Feature: Asset Controller Then I should receive a "hits" array of objects matching: | _id | | "Container-A1" | - # Delete - When I successfully execute the action "device-manager/assets":"delete" with args: - | engineId | "engine-kuzzle" | - | _id | "Container-A1" | - Then The document "engine-kuzzle":"assets":"Container-A1" does not exists + # Delete + When I successfully execute the action "device-manager/assets":"delete" with args: + | engineId | "engine-kuzzle" | + | _id | "Container-A1" | + Then The document "engine-kuzzle":"assets":"Container-A1" does not exists Scenario: Error when creating Asset from unknown model When I execute the action "device-manager/assets":"create" with args: @@ -84,3 +84,10 @@ Feature: Asset Controller | _source.values.temperature | _source.asset._id | _source.origin._id | _source.asset.model | | 40 | "Container-linked1" | "DummyTemp-linked1" | "Container" | | 41 | "Container-linked1" | "DummyTemp-linked1" | "Container" | + + Scenario: Create asset from backend side + When I successfully execute the action "tests":"createDigitalTwinFromBackend" with args: + | engineId | "engine-kuzzle" | + | body.reference | "foobar" | + Then The document "engine-kuzzle":"assets":"Container-foobar" exists + Then The document "engine-kuzzle":"devices":"DummyTemp-foobar" exists diff --git a/features/fixtures/application/app.ts b/features/fixtures/application/app.ts index 5b940e56..41354fd8 100644 --- a/features/fixtures/application/app.ts +++ b/features/fixtures/application/app.ts @@ -4,7 +4,8 @@ import { Backend, KuzzleRequest } from "kuzzle"; import { DeviceManagerPlugin } from "../../../index"; import { DummyTempDecoder, DummyTempPositionDecoder } from "./decoders"; -import { registerTestPipes } from "./testPipes"; +import { registerTestPipes } from "./tests/pipes"; +import { TestsController } from "./tests/controller"; const app = new Backend("kuzzle"); @@ -60,6 +61,8 @@ registerTestPipes(app); app.plugin.use(deviceManager); +app.controller.use(new TestsController(app)); + app.hook.register("request:onError", async (request: KuzzleRequest) => { app.log.error(request.error); }); diff --git a/features/fixtures/application/tests/controller.ts b/features/fixtures/application/tests/controller.ts new file mode 100644 index 00000000..7e2c2e42 --- /dev/null +++ b/features/fixtures/application/tests/controller.ts @@ -0,0 +1,42 @@ +import { Backend, Controller, KuzzleRequest } from "kuzzle"; +import { ApiAssetCreateRequest, ApiAssetCreateResult } from "lib/modules/asset"; +import { ApiDeviceCreateRequest, ApiDeviceCreateResult } from "lib/modules/device"; + +export class TestsController extends Controller { + constructor (app: Backend) { + super(app); + + this.definition = { + actions: { + createDigitalTwinFromBackend: { + handler: this.createDigitalTwinFromBackend, + } + } + }; + } + + async createDigitalTwinFromBackend (request: KuzzleRequest) { + const engineId = request.getString('engineId'); + const reference = request.getBodyString('reference'); + + await this.app.sdk.query({ + controller: 'device-manager/assets', + action: 'create', + engineId, + body: { + model: 'Container', + reference, + } + }); + + await this.app.sdk.query({ + controller: 'device-manager/devices', + action: 'create', + engineId, + body: { + model: 'DummyTemp', + reference + } + }); + } +} \ No newline at end of file diff --git a/features/fixtures/application/testPipes.ts b/features/fixtures/application/tests/pipes.ts similarity index 99% rename from features/fixtures/application/testPipes.ts rename to features/fixtures/application/tests/pipes.ts index 5d84d25c..cba5fd0f 100644 --- a/features/fixtures/application/testPipes.ts +++ b/features/fixtures/application/tests/pipes.ts @@ -7,7 +7,7 @@ import { EventMeasureProcessBefore, AssetContent, DeviceContent, -} from "../../../index"; +} from "../../../../index"; function checkEventWithDocument(app: Backend, event: string) { app.pipe.register(event, async (payload) => { diff --git a/lib/modules/asset/AssetService.ts b/lib/modules/asset/AssetService.ts index 41931161..d17f2caa 100644 --- a/lib/modules/asset/AssetService.ts +++ b/lib/modules/asset/AssetService.ts @@ -43,7 +43,13 @@ export class AssetService { } private get impersonatedSdk() { - return (user: User) => this.sdk.as(user, { checkRights: false }); + return (user: User) => { + if (user?._id) { + return this.sdk.as(user, { checkRights: false }) + } + + return this.sdk + }; } constructor( diff --git a/lib/modules/device/DeviceService.ts b/lib/modules/device/DeviceService.ts index dd01f159..31f9f340 100644 --- a/lib/modules/device/DeviceService.ts +++ b/lib/modules/device/DeviceService.ts @@ -42,7 +42,13 @@ export class DeviceService { } private get impersonatedSdk() { - return (user: User) => this.sdk.as(user, { checkRights: false }); + return (user: User) => { + if (user?._id) { + return this.sdk.as(user, { checkRights: false }) + } + + return this.sdk + }; } constructor(plugin: Plugin) { From f4525b2afc8a7e73a204ee2f42da343617edb55c Mon Sep 17 00:00:00 2001 From: Aschen Date: Fri, 6 Jan 2023 17:02:23 +0100 Subject: [PATCH 2/2] prettier --- .../fixtures/application/tests/controller.ts | 37 ++++++++++--------- lib/modules/asset/AssetService.ts | 4 +- lib/modules/device/DeviceService.ts | 4 +- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/features/fixtures/application/tests/controller.ts b/features/fixtures/application/tests/controller.ts index 7e2c2e42..5d70909f 100644 --- a/features/fixtures/application/tests/controller.ts +++ b/features/fixtures/application/tests/controller.ts @@ -1,42 +1,45 @@ import { Backend, Controller, KuzzleRequest } from "kuzzle"; import { ApiAssetCreateRequest, ApiAssetCreateResult } from "lib/modules/asset"; -import { ApiDeviceCreateRequest, ApiDeviceCreateResult } from "lib/modules/device"; +import { + ApiDeviceCreateRequest, + ApiDeviceCreateResult, +} from "lib/modules/device"; export class TestsController extends Controller { - constructor (app: Backend) { + constructor(app: Backend) { super(app); this.definition = { actions: { createDigitalTwinFromBackend: { handler: this.createDigitalTwinFromBackend, - } - } + }, + }, }; } - async createDigitalTwinFromBackend (request: KuzzleRequest) { - const engineId = request.getString('engineId'); - const reference = request.getBodyString('reference'); + async createDigitalTwinFromBackend(request: KuzzleRequest) { + const engineId = request.getString("engineId"); + const reference = request.getBodyString("reference"); await this.app.sdk.query({ - controller: 'device-manager/assets', - action: 'create', + controller: "device-manager/assets", + action: "create", engineId, body: { - model: 'Container', + model: "Container", reference, - } + }, }); await this.app.sdk.query({ - controller: 'device-manager/devices', - action: 'create', + controller: "device-manager/devices", + action: "create", engineId, body: { - model: 'DummyTemp', - reference - } + model: "DummyTemp", + reference, + }, }); } -} \ No newline at end of file +} diff --git a/lib/modules/asset/AssetService.ts b/lib/modules/asset/AssetService.ts index d17f2caa..bebec827 100644 --- a/lib/modules/asset/AssetService.ts +++ b/lib/modules/asset/AssetService.ts @@ -45,10 +45,10 @@ export class AssetService { private get impersonatedSdk() { return (user: User) => { if (user?._id) { - return this.sdk.as(user, { checkRights: false }) + return this.sdk.as(user, { checkRights: false }); } - return this.sdk + return this.sdk; }; } diff --git a/lib/modules/device/DeviceService.ts b/lib/modules/device/DeviceService.ts index 31f9f340..5ca8d61a 100644 --- a/lib/modules/device/DeviceService.ts +++ b/lib/modules/device/DeviceService.ts @@ -44,10 +44,10 @@ export class DeviceService { private get impersonatedSdk() { return (user: User) => { if (user?._id) { - return this.sdk.as(user, { checkRights: false }) + return this.sdk.as(user, { checkRights: false }); } - return this.sdk + return this.sdk; }; }