Skip to content

Commit

Permalink
fix: check instance name is valid (#42)
Browse files Browse the repository at this point in the history
* fix: check instance name is valid

* fix: test should not depend on env variable

* fix: test that valid instance names can be used
  • Loading branch information
birme authored Dec 7, 2024
1 parent 422ea0c commit ecadf4e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
45 changes: 45 additions & 0 deletions packages/core/src/core.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Context } from './context';
import { createInstance, isValidInstanceName } from './core';
import { InvalidName } from './errors';
import { createFetch } from './fetch';

jest.mock('./fetch');

describe('Core functionalities', () => {
afterAll(() => {
jest.clearAllMocks();
});

test('validate instance name', () => {
expect(isValidInstanceName('myinstance')).toEqual(true);
expect(isValidInstanceName('my-instance')).toEqual(false);
});

test('an instance with an invalid name cannot be created', async () => {
await expect(
createInstance(
new Context({ personalAccessToken: 'dummy' }),
'eyevinn-test-adserver',
'my-token',
{
name: 'my-instance'
}
)
).rejects.toThrow(new InvalidName('my-instance'));
expect(createFetch).not.toHaveBeenCalled();
});

test('an instance with an valid name can be created', async () => {
await expect(
createInstance(
new Context({ personalAccessToken: 'dummy' }),
'eyevinn-test-adserver',
'my-token',
{
name: 'myinstance'
}
)
).rejects.not.toThrow(new InvalidName('myinstance'));
expect(createFetch).toHaveBeenCalled();
});
});
9 changes: 8 additions & 1 deletion packages/core/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context, Service } from './context';
import { UnauthorizedError } from './errors';
import { InvalidName, UnauthorizedError } from './errors';
import { FetchError, createFetch } from './fetch';
import { Log } from './log';

Expand All @@ -21,6 +21,10 @@ export async function getService(context: Context, serviceId: string) {
return service;
}

export const isValidInstanceName = (name: string) => {
return /^[a-z0-9]+$/.test(name);
};

export type Port = {
externalIp: string;
externalPort: number;
Expand Down Expand Up @@ -57,6 +61,9 @@ export async function createInstance(
token: string,
body: any
): Promise<any> {
if (!isValidInstanceName(body.name)) {
throw new InvalidName(body.name);
}
const service = await getService(context, serviceId);
const instanceUrl = new URL(service.apiUrl);

Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ export class UnauthorizedError extends Error {
super('Unauthorized');
}
}

export class InvalidName extends Error {
constructor(name: string) {
super(`Invalid name: ${name}. Only alphanumeric characters are allowed.`);
}
}

0 comments on commit ecadf4e

Please sign in to comment.