Skip to content

Commit

Permalink
Query RelationshipTemplates for password protection (#353)
Browse files Browse the repository at this point in the history
* feat: query templates

* fix: use custom query

* test: cleanup tests

* fix: !true accepts undefined

* refactor: naming

* feat: !true to !

* chore: build schemas

* test: use queryconditions

* test: naming

* test: move password query into existing tests

* feat/refactor: passwordProtection, passwordIsPin query

* feat: restrict passwordIsPin query

* refactor: name values

* test: add passwordProtection query tests

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
Magnus-Kuhn and mergify[bot] authored Dec 9, 2024
1 parent 4a2eea3 commit 7071817
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 8 deletions.
27 changes: 27 additions & 0 deletions packages/runtime/src/useCases/common/Schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22772,6 +22772,33 @@ export const GetRelationshipTemplatesRequest: any = {
}
}
]
},
"passwordProtection": {
"type": "string",
"enum": [
"",
"!"
]
},
"passwordProtection.password": {
"anyOf": [
{
"type": "string"
},
{
"type": "array",
"items": {
"type": "string"
}
}
]
},
"passwordProtection.passwordIsPin": {
"type": "string",
"enum": [
"true",
"!"
]
}
},
"additionalProperties": false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { QueryTranslator } from "@js-soft/docdb-querytranslator";
import { Result } from "@js-soft/ts-utils";
import { CachedRelationshipTemplate, RelationshipTemplate, RelationshipTemplateController } from "@nmshd/transport";
import { CachedRelationshipTemplate, PasswordProtection, RelationshipTemplate, RelationshipTemplateController } from "@nmshd/transport";
import { Inject } from "@nmshd/typescript-ioc";
import { nameof } from "ts-simple-nameof";
import { RelationshipTemplateDTO } from "../../../types";
Expand All @@ -15,6 +15,9 @@ export interface GetRelationshipTemplatesQuery {
createdByDevice?: string | string[];
maxNumberOfAllocations?: string | string[];
forIdentity?: string | string[];
passwordProtection?: "" | "!";
"passwordProtection.password"?: string | string[];
"passwordProtection.passwordIsPin"?: "true" | "!";
}

export interface GetRelationshipTemplatesRequest {
Expand All @@ -37,7 +40,10 @@ export class GetRelationshipTemplatesUseCase extends UseCase<GetRelationshipTemp
[nameof<RelationshipTemplateDTO>((r) => r.createdBy)]: true,
[nameof<RelationshipTemplateDTO>((r) => r.createdByDevice)]: true,
[nameof<RelationshipTemplateDTO>((r) => r.maxNumberOfAllocations)]: true,
[nameof<RelationshipTemplateDTO>((r) => r.forIdentity)]: true
[nameof<RelationshipTemplateDTO>((r) => r.forIdentity)]: true,
[nameof<RelationshipTemplateDTO>((r) => r.passwordProtection)]: true,
[`${nameof<RelationshipTemplateDTO>((r) => r.passwordProtection)}.password`]: true,
[`${nameof<RelationshipTemplateDTO>((r) => r.passwordProtection)}.passwordIsPin`]: true
},
alias: {
[nameof<RelationshipTemplateDTO>((r) => r.isOwn)]: nameof<RelationshipTemplate>((r) => r.isOwn),
Expand All @@ -50,7 +56,23 @@ export class GetRelationshipTemplatesUseCase extends UseCase<GetRelationshipTemp
[nameof<RelationshipTemplateDTO>((r) => r.maxNumberOfAllocations)]: `${nameof<RelationshipTemplate>((r) => r.cache)}.${nameof<CachedRelationshipTemplate>(
(t) => t.maxNumberOfAllocations
)}`,
[nameof<RelationshipTemplateDTO>((r) => r.forIdentity)]: `${nameof<RelationshipTemplate>((r) => r.cache)}.${nameof<CachedRelationshipTemplate>((t) => t.forIdentity)}`
[nameof<RelationshipTemplateDTO>((r) => r.forIdentity)]: `${nameof<RelationshipTemplate>((r) => r.cache)}.${nameof<CachedRelationshipTemplate>((t) => t.forIdentity)}`,
[nameof<RelationshipTemplateDTO>((r) => r.passwordProtection)]: nameof<RelationshipTemplate>((r) => r.passwordProtection)
},
custom: {
[`${nameof<RelationshipTemplateDTO>((r) => r.passwordProtection)}.password`]: (query: any, input: string) => {
query[`${nameof<RelationshipTemplate>((t) => t.passwordProtection)}.${nameof<PasswordProtection>((t) => t.password)}`] = input;
},
[`${nameof<RelationshipTemplateDTO>((t) => t.passwordProtection)}.passwordIsPin`]: (query: any, input: string) => {
if (input === "true") {
query[`${nameof<RelationshipTemplate>((t) => t.passwordProtection)}.${nameof<PasswordProtection>((t) => t.passwordType)}`] = {
$regex: "^pin"
};
}
if (input === "!") {
query[`${nameof<RelationshipTemplate>((t) => t.passwordProtection)}.${nameof<PasswordProtection>((t) => t.passwordType)}`] = "pw";
}
}
}
});

Expand Down
59 changes: 54 additions & 5 deletions packages/runtime/test/transport/relationshipTemplates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@ describe("RelationshipTemplates query", () => {
maxNumberOfAllocations: 1,
expiresAt: DateTime.utc().plus({ minutes: 10 }).toString(),
content: emptyRelationshipTemplateContent,
forIdentity: runtimeServices1.address
forIdentity: runtimeServices1.address,
passwordProtection: {
password: "1234",
passwordIsPin: true
}
})
).value;
const conditions = new QueryParamConditions<GetRelationshipTemplatesQuery>(template, runtimeServices1.transport)
Expand All @@ -287,7 +291,28 @@ describe("RelationshipTemplates query", () => {
.addStringSet("createdBy")
.addStringSet("createdByDevice")
.addNumberSet("maxNumberOfAllocations")
.addStringSet("forIdentity");
.addStringSet("forIdentity")
.addSingleCondition({
expectedResult: true,
key: "passwordProtection",
value: ""
})
.addSingleCondition({
expectedResult: false,
key: "passwordProtection",
value: "!"
})
.addStringSet("passwordProtection.password")
.addSingleCondition({
expectedResult: true,
key: "passwordProtection.passwordIsPin",
value: "true"
})
.addSingleCondition({
expectedResult: false,
key: "passwordProtection.passwordIsPin",
value: "!"
});

await conditions.executeTests((c, q) => c.relationshipTemplates.getRelationshipTemplates({ query: q }));
});
Expand All @@ -297,15 +322,29 @@ describe("RelationshipTemplates query", () => {
await runtimeServices1.transport.relationshipTemplates.createOwnRelationshipTemplate({
maxNumberOfAllocations: 1,
expiresAt: DateTime.utc().plus({ minutes: 10 }).toString(),
content: emptyRelationshipTemplateContent
content: emptyRelationshipTemplateContent,
passwordProtection: {
password: "password"
}
})
).value;
const conditions = new QueryParamConditions<GetRelationshipTemplatesQuery>(template, runtimeServices1.transport)
.addDateSet("createdAt")
.addDateSet("expiresAt")
.addStringSet("createdBy")
.addStringSet("createdByDevice")
.addNumberSet("maxNumberOfAllocations");
.addNumberSet("maxNumberOfAllocations")
.addStringSet("passwordProtection.password")
.addSingleCondition({
expectedResult: false,
key: "passwordProtection.passwordIsPin",
value: "true"
})
.addSingleCondition({
expectedResult: true,
key: "passwordProtection.passwordIsPin",
value: "!"
});
await conditions.executeTests((c, q) => c.relationshipTemplates.getRelationshipTemplates({ query: q, ownerRestriction: OwnerRestriction.Own }));
});

Expand All @@ -323,7 +362,17 @@ describe("RelationshipTemplates query", () => {
.addDateSet("expiresAt")
.addStringSet("createdBy")
.addStringSet("createdByDevice")
.addNumberSet("maxNumberOfAllocations");
.addNumberSet("maxNumberOfAllocations")
.addSingleCondition({
expectedResult: false,
key: "passwordProtection",
value: ""
})
.addSingleCondition({
expectedResult: true,
key: "passwordProtection",
value: "!"
});

await conditions.executeTests((c, q) => c.relationshipTemplates.getRelationshipTemplates({ query: q, ownerRestriction: OwnerRestriction.Peer }));
});
Expand Down

0 comments on commit 7071817

Please sign in to comment.