Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@workos-inc/node": "^8.0.0",
"iron-webcrypto": "^2.0.0",
"jose": "~6.1.0"
},
Expand Down
215 changes: 215 additions & 0 deletions src/audit-logs/audit-logs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import fetch from 'jest-fetch-mock';
import { UnauthorizedException } from '../common/exceptions';
import { BadRequestException } from '../common/exceptions/bad-request.exception';
import { ListResponse } from '../common/interfaces';
import { mockWorkOsResponse } from '../common/utils/workos-mock-response';
import { WorkOS } from '../workos';
import {
AuditLogActionResponse,
AuditLogExport,
AuditLogExportOptions,
AuditLogExportResponse,
Expand Down Expand Up @@ -844,4 +846,217 @@ describe('AuditLogs', () => {
});
});
});

describe('listSchemas', () => {
describe('when the api responds with a 200', () => {
it('returns a paginated list of schemas', async () => {
const workosSpy = jest.spyOn(WorkOS.prototype, 'get');

const time = new Date().toISOString();

const schemaResponse: CreateAuditLogSchemaResponse = {
object: 'audit_log_schema',
version: 1,
targets: [
{
type: 'user',
metadata: {
type: 'object',
properties: {
user_id: { type: 'string' },
},
},
Comment thread
swaroopAkkineniWorkos marked this conversation as resolved.
},
],
actor: {
metadata: {
type: 'object',
properties: {
actor_id: { type: 'string' },
},
},
},
metadata: {
type: 'object',
properties: {
foo: { type: 'number' },
},
},
created_at: time,
};

const listResponse: ListResponse<CreateAuditLogSchemaResponse> = {
object: 'list',
data: [schemaResponse],
list_metadata: {
before: undefined,
after: undefined,
},
};

workosSpy.mockResolvedValueOnce(mockWorkOsResponse(200, listResponse));

const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');

const result = await workos.auditLogs.listSchemas({
action: 'user.logged_in',
});

expect(result.data).toHaveLength(1);
expect(result.data[0]).toEqual({
object: 'audit_log_schema',
version: 1,
targets: [{ type: 'user', metadata: { user_id: 'string' } }],
actor: { metadata: { actor_id: 'string' } },
metadata: { foo: 'number' },
createdAt: time,
});

expect(workosSpy).toHaveBeenCalledWith(
'/audit_logs/actions/user.logged_in/schemas',
{ query: { order: 'desc' } },
);
});
});

describe('with pagination options', () => {
it('passes pagination parameters to the API', async () => {
const workosSpy = jest.spyOn(WorkOS.prototype, 'get');

const listResponse: ListResponse<CreateAuditLogSchemaResponse> = {
object: 'list',
data: [],
list_metadata: {
before: undefined,
after: undefined,
},
};

workosSpy.mockResolvedValueOnce(mockWorkOsResponse(200, listResponse));

const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');

await workos.auditLogs.listSchemas({
action: 'user.logged_in',
limit: 10,
after: 'cursor_123',
order: 'asc',
});

expect(workosSpy).toHaveBeenCalledWith(
'/audit_logs/actions/user.logged_in/schemas',
{ query: { limit: 10, after: 'cursor_123', order: 'asc' } },
);
});
});

describe('when the api responds with a 401', () => {
it('throws an UnauthorizedException', async () => {
const workosSpy = jest.spyOn(WorkOS.prototype, 'get');

workosSpy.mockImplementationOnce(() => {
throw new UnauthorizedException('a-request-id');
});

const workos = new WorkOS('invalid apikey');

await expect(
workos.auditLogs.listSchemas({ action: 'user.logged_in' }),
).rejects.toThrow(UnauthorizedException);
});
});
});

describe('listActions', () => {
describe('when the api responds with a 200', () => {
it('returns a paginated list of actions', async () => {
const workosSpy = jest.spyOn(WorkOS.prototype, 'get');

const actionResponse: AuditLogActionResponse = {
object: 'audit_log_action',
name: 'user.logged_in',
};

const listResponse: ListResponse<AuditLogActionResponse> = {
object: 'list',
data: [
actionResponse,
{ object: 'audit_log_action', name: 'user.signed_out' },
],
list_metadata: {
before: undefined,
after: 'cursor_next',
},
};

workosSpy.mockResolvedValueOnce(mockWorkOsResponse(200, listResponse));

const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');

const result = await workos.auditLogs.listActions();

expect(result.data).toHaveLength(2);
expect(result.data[0]).toEqual({
object: 'audit_log_action',
name: 'user.logged_in',
});
expect(result.data[1]).toEqual({
object: 'audit_log_action',
name: 'user.signed_out',
});
expect(result.listMetadata).toMatchObject({
after: 'cursor_next',
});

expect(workosSpy).toHaveBeenCalledWith('/audit_logs/actions', {
query: { order: 'desc' },
});
});
});

describe('with pagination options', () => {
it('passes pagination parameters to the API', async () => {
const workosSpy = jest.spyOn(WorkOS.prototype, 'get');

const listResponse: ListResponse<AuditLogActionResponse> = {
object: 'list',
data: [],
list_metadata: {
before: undefined,
after: undefined,
},
};

workosSpy.mockResolvedValueOnce(mockWorkOsResponse(200, listResponse));

const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');

await workos.auditLogs.listActions({
limit: 5,
after: 'cursor_abc',
order: 'asc',
});

expect(workosSpy).toHaveBeenCalledWith('/audit_logs/actions', {
query: { limit: 5, after: 'cursor_abc', order: 'asc' },
});
});
});

describe('when the api responds with a 401', () => {
it('throws an UnauthorizedException', async () => {
const workosSpy = jest.spyOn(WorkOS.prototype, 'get');

workosSpy.mockImplementationOnce(() => {
throw new UnauthorizedException('a-request-id');
});

const workos = new WorkOS('invalid apikey');

await expect(workos.auditLogs.listActions()).rejects.toThrow(
UnauthorizedException,
);
});
});
});
});
Loading