Skip to content

Commit

Permalink
Make metadata optional when creating audit log schema (#1175)
Browse files Browse the repository at this point in the history
Make metadata optional when creating audit log schema.
  • Loading branch information
mattgd authored Nov 22, 2024
1 parent 08ab75f commit 02592ee
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
75 changes: 75 additions & 0 deletions src/audit-logs/audit-logs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ const schema: CreateAuditLogSchemaOptions = {
},
};

const schemaWithoutMetadata = { ...schema, metadata: undefined };

describe('AuditLogs', () => {
beforeEach(() => fetch.resetMocks());

Expand Down Expand Up @@ -452,6 +454,79 @@ describe('AuditLogs', () => {
});
});

describe('without metadata', () => {
it('does not include metadata with the request', async () => {
const workosSpy = jest.spyOn(WorkOS.prototype, 'post');

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

const createSchemaResult: AuditLogSchema = {
object: 'audit_log_schema',
version: 1,
targets: [
{
type: 'user',
metadata: {
user_id: 'string',
},
},
],
actor: {
metadata: {
actor_id: 'string',
},
},
metadata: undefined,
createdAt: time,
};

const createSchemaResponse: CreateAuditLogSchemaResponse = {
object: 'audit_log_schema',
version: 1,
targets: [
{
type: 'user',
metadata: {
type: 'object',
properties: {
user_id: {
type: 'string',
},
},
},
},
],
actor: {
metadata: {
type: 'object',
properties: {
actor_id: {
type: 'string',
},
},
},
},
created_at: time,
};

workosSpy.mockResolvedValueOnce(
mockWorkOsResponse(201, createSchemaResponse),
);

const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');

await expect(
workos.auditLogs.createSchema(schemaWithoutMetadata),
).resolves.toEqual(createSchemaResult);

expect(workosSpy).toHaveBeenCalledWith(
'/audit_logs/actions/user.logged_in/schemas',
serializeCreateAuditLogSchemaOptions(schemaWithoutMetadata),
{},
);
});
});

describe('when the api responds with a 201', () => {
it('returns `audit_log_schema`', async () => {
const workosSpy = jest.spyOn(WorkOS.prototype, 'post');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface CreateAuditLogSchemaResponse {
properties: AuditLogSchemaMetadata;
};
};
metadata: {
metadata?: {
type: 'object';
properties: AuditLogSchemaMetadata;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ export const serializeCreateAuditLogSchemaOptions = (
: undefined,
};
}),
metadata: {
type: 'object',
properties: serializeMetadata(schema.metadata),
},
metadata: schema.metadata
? {
type: 'object',
properties: serializeMetadata(schema.metadata),
}
: undefined,
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export const deserializeAuditLogSchema = (
actor: {
metadata: deserializeMetadata(auditLogSchema.actor?.metadata),
},
metadata: deserializeMetadata(auditLogSchema.metadata),
metadata: auditLogSchema.metadata
? deserializeMetadata(auditLogSchema.metadata)
: undefined,
createdAt: auditLogSchema.created_at,
});

0 comments on commit 02592ee

Please sign in to comment.