Skip to content
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
6 changes: 4 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ jobs:
with:
useLockFile: false
- name: Run lint
run: CI=true yarn lint
run: "CI=true yarn lint"
- name: Run prettier
run: "CI=true yarn run prettier src -c"
Comment on lines +51 to +52
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the CI configuration

- name: Run Yarn tests
run: CI=true yarn only-run-tests
run: "CI=true yarn only-run-tests"
build-js-client:
name: Build and Test JS client
runs-on: "depot-ubuntu-24.04-small"
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/authzedapi
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ignores generated code

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@typescript-eslint/parser": "^8.29.1",
"eslint": "^9.24.0",
"grpc-tools": "^1.13.0",
"prettier": "^3.5.3",
"rollup": "^4.40.0",
"tsc-esm-fix": "^3.1.2",
"typescript": "^5.8",
Expand Down
8 changes: 4 additions & 4 deletions src/__utils__/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Generates a random token with a prefix to support
* unique, idempotent local testing.
* @param prefix
* @returns
* @param prefix
* @returns
*/
export function generateTestToken(prefix: string): string {
return `${prefix}-${Date.now().toString()}`
}
return `${prefix}-${Date.now().toString()}`;
}
24 changes: 15 additions & 9 deletions src/full-promises.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { ClientSecurity } from "./util.js";
import * as v1 from "./v1.js";
import { Consistency } from "./v1.js";
import { generateTestToken } from './__utils__/helpers.js'
import { describe, it, expect } from 'vitest'
import { generateTestToken } from "./__utils__/helpers.js";
import { describe, it, expect } from "vitest";

describe("a check following a write of schema and relationships", () => {
it("should succeed", async () => {
// Write the schema.
const token = generateTestToken('full-promises')
const { promises: v1client } = v1.NewClient(token, "localhost:50051", ClientSecurity.INSECURE_LOCALHOST_ALLOWED);
const token = generateTestToken("full-promises");
const { promises: v1client } = v1.NewClient(
token,
"localhost:50051",
ClientSecurity.INSECURE_LOCALHOST_ALLOWED,
);

const writeSchemaRequest = v1.WriteSchemaRequest.create({
schema: `
Expand All @@ -21,7 +25,7 @@ describe("a check following a write of schema and relationships", () => {
`,
});

await v1client.writeSchema(writeSchemaRequest)
await v1client.writeSchema(writeSchemaRequest);
// Create the relationship between the resource and the user.
const resource = v1.ObjectReference.create({
objectType: "test/resource",
Expand Down Expand Up @@ -54,7 +58,7 @@ describe("a check following a write of schema and relationships", () => {
updates: [update],
});

const response = await v1client.writeRelationships(writeRequest)
const response = await v1client.writeRelationships(writeRequest);
expect(response).toBeTruthy();

const checkPermissionRequest = v1.CheckPermissionRequest.create({
Expand All @@ -66,12 +70,14 @@ describe("a check following a write of schema and relationships", () => {
oneofKind: "fullyConsistent",
fullyConsistent: true,
},
})
}),
});

const permissionResponse = await v1client.checkPermission(checkPermissionRequest)
const permissionResponse = await v1client.checkPermission(
checkPermissionRequest,
);
expect(permissionResponse?.permissionship).toBe(
v1.CheckPermissionResponse_Permissionship.HAS_PERMISSION
v1.CheckPermissionResponse_Permissionship.HAS_PERMISSION,
);
});
});
121 changes: 63 additions & 58 deletions src/full.test.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,90 @@
import { ClientSecurity } from "./util.js";
import * as v1 from "./v1.js";
import { Consistency } from "./v1.js";
import { generateTestToken } from './__utils__/helpers.js'
import { describe, it, expect } from 'vitest'
import { generateTestToken } from "./__utils__/helpers.js";
import { describe, it, expect } from "vitest";

describe("a check following a write of schema and relationships", () => {
it("should succeed", () => new Promise<void>((done) => {
// Write the schema.
const token = generateTestToken('full-test')
const v1client = v1.NewClient(token, "localhost:50051", ClientSecurity.INSECURE_LOCALHOST_ALLOWED);
it("should succeed", () =>
new Promise<void>((done) => {
// Write the schema.
const token = generateTestToken("full-test");
const v1client = v1.NewClient(
token,
"localhost:50051",
ClientSecurity.INSECURE_LOCALHOST_ALLOWED,
);

const writeSchemaRequest = v1.WriteSchemaRequest.create({
schema: `
const writeSchemaRequest = v1.WriteSchemaRequest.create({
schema: `
definition test/user {}

definition test/resource {
relation viewer: test/user
permission view = viewer
}
`,
});

v1client.writeSchema(writeSchemaRequest, function (err) {
expect(err).toBe(null);

// Create the relationship between the resource and the user.
const resource = v1.ObjectReference.create({
objectType: "test/resource",
objectId: "someresource",
});

// Create the user reference.
const userref = v1.ObjectReference.create({
objectType: "test/user",
objectId: "someuser",
});

const user = v1.SubjectReference.create({
object: userref,
});

const relationship = v1.Relationship.create({
resource,
relation: "viewer",
subject: user,
});
v1client.writeSchema(writeSchemaRequest, function (err) {
expect(err).toBe(null);

const update = v1.RelationshipUpdate.create({
operation: v1.RelationshipUpdate_Operation.CREATE,
relationship: relationship,
});
// Create the relationship between the resource and the user.
const resource = v1.ObjectReference.create({
objectType: "test/resource",
objectId: "someresource",
});

// Write the relationship.
const writeRequest = v1.WriteRelationshipsRequest.create({
updates: [update],
});
// Create the user reference.
const userref = v1.ObjectReference.create({
objectType: "test/user",
objectId: "someuser",
});

v1client.writeRelationships(writeRequest, function (err, response) {
expect(err).toBe(null);
expect(response).toBeTruthy();
const user = v1.SubjectReference.create({
object: userref,
});

const checkPermissionRequest = v1.CheckPermissionRequest.create({
const relationship = v1.Relationship.create({
resource,
permission: "view",
relation: "viewer",
subject: user,
consistency: Consistency.create({
requirement: {
oneofKind: "fullyConsistent",
fullyConsistent: true,
},
})
});

v1client.checkPermission(checkPermissionRequest, (err, response) => {
const update = v1.RelationshipUpdate.create({
operation: v1.RelationshipUpdate_Operation.CREATE,
relationship: relationship,
});

// Write the relationship.
const writeRequest = v1.WriteRelationshipsRequest.create({
updates: [update],
});

v1client.writeRelationships(writeRequest, function (err, response) {
expect(err).toBe(null);
expect(response?.permissionship).toBe(
v1.CheckPermissionResponse_Permissionship.HAS_PERMISSION
);
done();
expect(response).toBeTruthy();

const checkPermissionRequest = v1.CheckPermissionRequest.create({
resource,
permission: "view",
subject: user,
consistency: Consistency.create({
requirement: {
oneofKind: "fullyConsistent",
fullyConsistent: true,
},
}),
});

v1client.checkPermission(checkPermissionRequest, (err, response) => {
expect(err).toBe(null);
expect(response?.permissionship).toBe(
v1.CheckPermissionResponse_Permissionship.HAS_PERMISSION,
);
done();
});
});
});
});
}));
}));
});
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use strict";

export * as protobuf from './protobuf.js';
export * as v1 from './v1.js';
export * as protobuf from "./protobuf.js";
export * as v1 from "./v1.js";
8 changes: 4 additions & 4 deletions src/protobuf.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './authzedapi/google/protobuf/descriptor.js';
export * from './authzedapi/google/protobuf/duration.js';
export * from './authzedapi/google/protobuf/struct.js';
export * from './authzedapi/google/protobuf/timestamp.js';
export * from "./authzedapi/google/protobuf/descriptor.js";
export * from "./authzedapi/google/protobuf/duration.js";
export * from "./authzedapi/google/protobuf/struct.js";
export * from "./authzedapi/google/protobuf/timestamp.js";
12 changes: 6 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ClientWritableStream,
Metadata,
ServiceError,
} from '@grpc/grpc-js';
} from "@grpc/grpc-js";

/**
* Picks all function properties of a type that except for those with a specific return type
Expand Down Expand Up @@ -37,7 +37,7 @@ export type OmitBaseMethods<C extends B, B> = {
export type StreamCall<T, U> = (
request: T,
metadata?: Metadata | CallOptions,
options?: CallOptions
options?: CallOptions,
) => ClientReadableStream<U>;

/**
Expand All @@ -54,8 +54,8 @@ export type WritableStreamCall<T, U> = (
| undefined,
callback?: (
err: ServiceError | null,
value?: U | undefined
) => void | undefined
value?: U | undefined,
) => void | undefined,
) => ClientWritableStream<T>;

/**
Expand All @@ -65,7 +65,7 @@ export type UnaryCall<T, U> = (
request: T,
metadata: Metadata | CallOptions,
options: Partial<CallOptions>,
callback: (err: ServiceError | null, res?: U) => void
callback: (err: ServiceError | null, res?: U) => void,
) => ClientUnaryCall;

/**
Expand All @@ -74,7 +74,7 @@ export type UnaryCall<T, U> = (
export type PromisifiedCall<T, U> = (
request: T,
metadata?: Metadata,
options?: Partial<CallOptions>
options?: Partial<CallOptions>,
) => Promise<U>;

/**
Expand Down
Loading