Skip to content

Commit

Permalink
fixes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dopeamin committed Sep 13, 2024
1 parent 817e1ed commit 8725236
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .example.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CORBADO_PROJECT_ID=pro-xxxxxxxxxxxxxxxxxxx
CORBADO_API_SECRET=corbado1_xxxxxxxxxxxxxxxxxxxxxxxxxx
CORBADO_BACKEND_API=https://backendapi.cloud.corbado.io/v2
CORBADO_BACKEND_API=https://backendapi.cloud.corbado.io
CORBADO_BACKEND_API=https://[project-id].frontendapi.cloud.corbado.io
36 changes: 24 additions & 12 deletions tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import { Config } from '../src/index.js';
describe('Configuration class', () => {
let projectID: string;
let apiSecret: string;
let frontendAPI: string;
let backendAPI: string;

beforeEach(() => {
projectID = process.env.CORBADO_PROJECT_ID as string; // necessary to mitigate TS error
apiSecret = process.env.CORBADO_API_SECRET as string; // Same here
frontendAPI = process.env.CORBADO_FRONTEND_API as string; // Same here
backendAPI = process.env.CORBADO_BACKEND_API as string; // Same here

if (!projectID || !apiSecret) {
throw new BaseError('Env Error', 5001, 'Both projectID and apiSecret must be defined', true);
}
});

const createAndAssertConfig = (config: Config) => {
expect(config).toBeInstanceOf(Config);
expect(config.ProjectID).toBe(projectID);
Expand All @@ -23,36 +29,42 @@ describe('Configuration class', () => {
expect(config.CacheMaxAge).toBe(DefaultCacheMaxAge);
};

it('should instantiate Configuration with valid project ID and API secret', () => {
const config = new Config(projectID, apiSecret);
it('should instantiate Configuration with valid project ID and API secret and APIs', () => {
const config = new Config(projectID, apiSecret, frontendAPI, backendAPI);
createAndAssertConfig(config);
});

it('should assign default values to BackendAPI, ShortSessionCookieName, CacheMaxAge, and JWTIssuer', () => {
const config = new Config(projectID, apiSecret);
expect(config.BackendAPI).toBe(DefaultBackendAPI);
const config = new Config(projectID, apiSecret, frontendAPI, backendAPI);
expect(config.BackendAPI).toBe(backendAPI);
expect(config.FrontendAPI).toBe(frontendAPI);
expect(config.ShortSessionCookieName).toBe(DefaultShortSessionCookieName);
expect(config.CacheMaxAge).toBe(DefaultCacheMaxAge);
});

it('should generate DefaultFrontendAPI using process.env.CORBADO_PROJECT_ID and provided project ID', () => {
const config = new Config(projectID, apiSecret);
expect(config.FrontendAPI).toBe(`https://${projectID}.frontendapi.cloud.corbado.io`);
});

it('should throw an error when instantiated with an invalid project ID', () => {
expect(() => new Config('invalid', apiSecret)).toThrow('ProjectID must not be empty and must start with "pro-".');
expect(() => new Config('invalid', apiSecret, frontendAPI, backendAPI)).toThrow(
'ProjectID must not be empty and must start with "pro-".',
);
});

it('should throw an error when instantiated with an invalid API secret', () => {
expect(() => new Config(projectID, 'invalid')).toThrow(
expect(() => new Config(projectID, 'invalid', frontendAPI, backendAPI)).toThrow(
'APISecret must not be empty and must start with "corbado1_".',
);
});

it('should throw an error when project ID is undefined', () => {
expect(() => new Config(undefined as unknown as string, apiSecret)).toThrow(
expect(() => new Config(undefined as unknown as string, apiSecret, frontendAPI, backendAPI)).toThrow(
'ProjectID must not be empty and must start with "pro-".',
);
});

it('should throw an error when frontendAPI is wrong', () => {
expect(() => new Config(projectID, apiSecret, `${frontendAPI}/v2`, backendAPI)).toThrow('path needs to be empty');
});

it('should throw an error when backendAPI is wrong', () => {
expect(() => new Config(projectID, apiSecret, frontendAPI, `${backendAPI}/v2`)).toThrow('path needs to be empty');
});
});
7 changes: 6 additions & 1 deletion tests/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ describe('SDK class', () => {
throw new BaseError('Env Error', 5001, 'Both projectID and apiSecret must be defined', true);
}

config = new Config(projectID, apiSecret);
config = new Config(
projectID,
apiSecret,
`https://${projectID}.frontendapi.cloud.corbado.io`,
`https://backendapi.cloud.corbado.io`,
);
sdk = new SDK(config);
});

Expand Down
7 changes: 6 additions & 1 deletion tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { User } from '../src/generated';

class Utils {
public static SDK(): SDK {
const config = new Config(this.getEnv('CORBADO_PROJECT_ID'), this.getEnv('CORBADO_API_SECRET'));
const config = new Config(
this.getEnv('CORBADO_PROJECT_ID'),
this.getEnv('CORBADO_API_SECRET'),
this.getEnv('CORBADO_FRONTEND_API'),
this.getEnv('CORBADO_BACKEND_API'),
);

return new SDK(config);
}
Expand Down

0 comments on commit 8725236

Please sign in to comment.