Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Asset and Device creation from backend #252

Merged
merged 3 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions features/Asset/Controller.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
5 changes: 4 additions & 1 deletion features/fixtures/application/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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);
});
Expand Down
45 changes: 45 additions & 0 deletions features/fixtures/application/tests/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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<ApiAssetCreateRequest, ApiAssetCreateResult>({
controller: "device-manager/assets",
action: "create",
engineId,
body: {
model: "Container",
reference,
},
});

await this.app.sdk.query<ApiDeviceCreateRequest, ApiDeviceCreateResult>({
controller: "device-manager/devices",
action: "create",
engineId,
body: {
model: "DummyTemp",
reference,
},
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
EventMeasureProcessBefore,
AssetContent,
DeviceContent,
} from "../../../index";
} from "../../../../index";

function checkEventWithDocument(app: Backend, event: string) {
app.pipe.register(event, async (payload) => {
Expand Down
8 changes: 7 additions & 1 deletion lib/modules/asset/AssetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion lib/modules/device/DeviceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
Comment on lines 44 to 52
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a very useful method should we provide it as well in the Framework?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well actually Kuzzle could just return the normal SDK if the user is null but I'm not sure about potential side effects for users who relied on the fact that the method will throw if an incorrect user is passed.

Also, I don't know where we could put this method in the framework. Let's see if we need it another time and talk about this


constructor(plugin: Plugin) {
Expand Down