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

feat: options and timestamps numbers should always be positive #293

Merged
merged 1 commit into from
Oct 16, 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
90 changes: 90 additions & 0 deletions src/types/relying-party-options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,54 @@ describe('RelyingPartyOptions', () => {
expect(result.success).toBe(true);
});

it('should fail validation with a negative pollingIntervalInMilliseconds', () => {
const invalidData = {
url: 'https://example.com',
connectionOptions: {
pollingIntervalInMilliseconds: -500
}
};

const result = RelyingPartyOptionsSchema.safeParse(invalidData);
expect(result.success).toBe(false);
});

it('should fail validation with zero pollingIntervalInMilliseconds', () => {
const invalidData = {
url: 'https://example.com',
connectionOptions: {
pollingIntervalInMilliseconds: 0
}
};

const result = RelyingPartyOptionsSchema.safeParse(invalidData);
expect(result.success).toBe(false);
});

it('should fail validation with a zero timeoutInMilliseconds', () => {
const invalidData = {
url: 'https://example.com',
connectionOptions: {
timeoutInMilliseconds: 0
}
};

const result = RelyingPartyOptionsSchema.safeParse(invalidData);
expect(result.success).toBe(false);
});

it('should fail validation with a negative timeoutInMilliseconds', () => {
const invalidData = {
url: 'https://example.com',
connectionOptions: {
timeoutInMilliseconds: -120000
}
};

const result = RelyingPartyOptionsSchema.safeParse(invalidData);
expect(result.success).toBe(false);
});

it('should validate with correct relying party options and string window options', () => {
const validData = {
url: 'https://example.com',
Expand Down Expand Up @@ -52,6 +100,48 @@ describe('RelyingPartyOptions', () => {
expect(result.success).toBe(false);
});

it('should fail validation with zero width for the signer window', () => {
const invalidData = {
url: 'https://example.com',
windowOptions: {
position: 'center',
width: 0,
height: 300
}
};

const result = RelyingPartyOptionsSchema.safeParse(invalidData);
expect(result.success).toBe(false);
});

it('should fail validation with zero height for the signer window', () => {
const invalidData = {
url: 'https://example.com',
windowOptions: {
position: 'center',
width: 400,
height: 0
}
};

const result = RelyingPartyOptionsSchema.safeParse(invalidData);
expect(result.success).toBe(false);
});

it('should pass validation with positive width and height for the signer window', () => {
const validData = {
url: 'https://example.com',
windowOptions: {
position: 'center',
width: 400,
height: 300
}
};

const result = RelyingPartyOptionsSchema.safeParse(validData);
expect(result.success).toBe(true);
});

it('should fail validation with incorrect connectionOptions object', () => {
const invalidData = {
url: 'https://example.com',
Expand Down
14 changes: 8 additions & 6 deletions src/types/relying-party-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ import {z} from 'zod';
const ConnectionOptionsSchema = z.object({
/**
* Specifies the interval in milliseconds at which the signer is checked (polled) to determine if it is ready.
* Must be a positive number.
*
* @default 500 - The default polling interval is set to 500 milliseconds.
*/
pollingIntervalInMilliseconds: z.number().optional(),
pollingIntervalInMilliseconds: z.number().positive().optional(),

/**
* Specifies the maximum duration in milliseconds for attempting to establish a connection to the signer.
* If the connection is not established within this duration, the process will time out.
* Must be a positive number.
*
* @default 120_000 - The default timeout is set to 120,000 milliseconds (2 minutes).
*/
timeoutInMilliseconds: z.number().optional()
timeoutInMilliseconds: z.number().positive().optional()
});

export type ConnectionOptions = z.infer<typeof ConnectionOptionsSchema>;
Expand All @@ -26,14 +28,14 @@ const WindowOptionsSchema = z.object({
position: z.enum(['top-right', 'center']),

/**
* Specifies the width of the signer window.
* Specifies a width greater than zero of the signer window.
*/
width: z.number(),
width: z.number().positive(),

/**
* Specifies the height of the signer window.
* Specifies a height greater than zero of the signer window.
*/
height: z.number(),
height: z.number().positive(),

/**
* Optional features for the signer Window object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,23 @@ describe('RelyingPartyRequests', () => {
const result = RelyingPartyRequestOptionsSchema.safeParse(invalidData);
expect(result.success).toBe(false);
});

it('should fail validation with a non-positive timeoutInMilliseconds', () => {
const invalidData = {
timeoutInMilliseconds: -500
};

const result = RelyingPartyRequestOptionsSchema.safeParse(invalidData);
expect(result.success).toBe(false);
});

it('should fail validation with a zero timeoutInMilliseconds', () => {
const invalidData = {
timeoutInMilliseconds: 0
};

const result = RelyingPartyRequestOptionsSchema.safeParse(invalidData);
expect(result.success).toBe(false);
});
});
});
3 changes: 2 additions & 1 deletion src/types/relying-party-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export const RelyingPartyRequestOptionsTimeoutSchema = z.object({
/**
* Specifies the maximum duration in milliseconds for attempting to request an interaction with the relying party.
* If the relying party does not answer within this duration, the process will time out.
* Must be a positive number.
*/
timeoutInMilliseconds: z.number()
timeoutInMilliseconds: z.number().positive()
});

export const RelyingPartyRequestOptionsSchema = z
Expand Down
40 changes: 40 additions & 0 deletions src/types/signer-sessions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ describe('Signer-sessions', () => {
expect(() => SessionPermissionsSchema.parse(invalidData)).toThrow();
});

it('should fail validation with a negative createdAt', () => {
const invalidData = {
scopes,
createdAt: -1000,
updatedAt: Date.now()
};

expect(() => SessionPermissionsSchema.parse(invalidData)).toThrow();
});

it('should fail validation with a zero createdAt', () => {
const invalidData = {
scopes,
createdAt: 0,
updatedAt: Date.now()
};

expect(() => SessionPermissionsSchema.parse(invalidData)).toThrow();
});

it('should fail validation if updatedAt is not a number', () => {
const invalidData = {
scopes,
Expand All @@ -67,6 +87,26 @@ describe('Signer-sessions', () => {
expect(() => SessionPermissionsSchema.parse(invalidData)).toThrow();
});

it('should fail validation with a negative updatedAt', () => {
const invalidData = {
scopes,
createdAt: Date.now(),
updatedAt: -1000
};

expect(() => SessionPermissionsSchema.parse(invalidData)).toThrow();
});

it('should fail validation with a zero updatedAt', () => {
const invalidData = {
scopes,
createdAt: Date.now(),
updatedAt: 0
};

expect(() => SessionPermissionsSchema.parse(invalidData)).toThrow();
});

it('should fail validation if scopes is missing', () => {
const invalidData = {
createdAt: Date.now(),
Expand Down
4 changes: 2 additions & 2 deletions src/types/signer-sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {z} from 'zod';
import {IcrcScopeSchema} from './icrc-responses';

export const SessionTimestampsSchema = z.object({
createdAt: z.number(),
updatedAt: z.number()
createdAt: z.number().positive(),
updatedAt: z.number().positive()
});

export const SessionIcrcScopeSchema = IcrcScopeSchema.merge(SessionTimestampsSchema);
Expand Down
Loading