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

Add context support to query method #56

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
})
});
});
Loading