Skip to content

Commit

Permalink
Add context support to query method (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkajla12 authored Apr 1, 2024
1 parent 1a8c75d commit a1ab6b2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
22 changes: 17 additions & 5 deletions src/modules/WarrantModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { API_VERSION } from "../constants";
import {
ListResponse,
ListWarrantParams,
QueryParams,
QueryListParams,
QueryResult,
Warrant,
Expand Down Expand Up @@ -49,13 +50,24 @@ export default class WarrantModule {
});
}

public static async query(query: string, listParams: QueryListParams = {}, options: WarrantRequestOptions = {}): Promise<ListResponse<QueryResult>> {
public static async query(query: string | QueryParams, listParams: QueryListParams = {}, options: WarrantRequestOptions = {}): Promise<ListResponse<QueryResult>> {
const params: any = {
...listParams,
};

// preserve backwards compatibility
if (typeof query === "string") {
params.q = query;
} else {
params.q = query.query;
if (query.context) {
params.context = JSON.stringify(query.context);
}
}

return await WarrantClient.httpClient.get({
url: `/${API_VERSION}/query`,
params: {
q: query,
...listParams,
},
params: params,
options,
});
}
Expand Down
7 changes: 6 additions & 1 deletion src/types/Query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Warrant } from "./Warrant";
import { Warrant, PolicyContext } from "./Warrant";
import { ListParams } from "./List";

export interface QueryParams {
query: string;
context?: PolicyContext;
}

export interface QueryResult {
objectType: string;
objectId: string;
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export {
Subject,
} from "./Warrant";
export {
QueryParams,
QueryResult,
QueryListParams,
} from "./Query";
Expand Down
22 changes: 21 additions & 1 deletion test/LiveTest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,12 @@ describe.skip('Live Test', function () {
relation: "member",
subject: role1,
});
await this.warrant.Warrant.create({
object: permission1,
relation: "member",
subject: role2,
policy: "tenantId == 123",
});
await this.warrant.Role.assignRoleToUser(userA.userId, role1.roleId);
await this.warrant.Role.assignRoleToUser(userB.userId, role2.roleId);

Expand All @@ -958,6 +964,20 @@ describe.skip('Live Test', function () {
assert.strictEqual("role", resultSet.results[0].warrant.subject.objectType);
assert.strictEqual("role1", resultSet.results[0].warrant.subject.objectId);

resultSet = await this.warrant.Warrant.query({
query: "select permission where user:userB is member",
context: {
tenantId: 123,
},
});
assert.strictEqual(3, resultSet.results.length);
assert.strictEqual("permission", resultSet.results[0].objectType);
assert.strictEqual("perm1", resultSet.results[0].objectId);
assert.strictEqual("permission", resultSet.results[1].objectType);
assert.strictEqual("perm2", resultSet.results[1].objectId);
assert.strictEqual("permission", resultSet.results[2].objectType);
assert.strictEqual("perm3", resultSet.results[2].objectId);

let warrantToken = await this.warrant.Role.delete(role1.roleId);
assert(warrantToken);
warrantToken = await this.warrant.Role.delete(role2.roleId);
Expand All @@ -972,5 +992,5 @@ describe.skip('Live Test', function () {
assert(warrantToken);
warrantToken = await this.warrant.User.delete(userB.userId);
assert(warrantToken);
})
});
});

0 comments on commit a1ab6b2

Please sign in to comment.