Skip to content

Commit

Permalink
refactor(framework): Add env-var for strict authentication (#5866)
Browse files Browse the repository at this point in the history
* test(client): add tests for strictAuthentication and secretKey

* test: Correct variable naming in client tests
  • Loading branch information
rifont authored Jun 28, 2024
1 parent 61cb9ce commit 405d971
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
36 changes: 29 additions & 7 deletions packages/framework/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,24 @@ describe('Novu Client', () => {

describe('client constructor', () => {
it('should set secretKey to process.env.NOVU_SECRET_KEY by default', () => {
const originalsecretKey = process.env.NOVU_SECRET_KEY;
const testsecretKey = 'test-env-secret-key';
process.env = { ...process.env, NOVU_SECRET_KEY: testsecretKey };
const originalSecretKey = process.env.NOVU_SECRET_KEY;
const testSecretKey = 'test-env-secret-key';
process.env = { ...process.env, NOVU_SECRET_KEY: testSecretKey };
const newClient = new Client();
expect(newClient.secretKey).toBe(process.env.NOVU_SECRET_KEY);
process.env = { ...process.env, NOVU_SECRET_KEY: originalsecretKey };
process.env = { ...process.env, NOVU_SECRET_KEY: originalSecretKey };
});

it('should set secretKey to provided secretKey', () => {
const testsecretKey = 'test-provided-secret-key';
const newClient = new Client({ secretKey: testsecretKey });
expect(newClient.secretKey).toBe(testsecretKey);
const testSecretKey = 'test-provided-secret-key';
const newClient = new Client({ secretKey: testSecretKey });
expect(newClient.secretKey).toBe(testSecretKey);
});

it('should throw an error when secretKey is not provided', () => {
expect(() => new Client({ secretKey: undefined })).toThrow(
'Missing secret key. Set the NOVU_SECRET_KEY environment variable or pass `secretKey` to the client options.'
);
});

it('should set strictAuthentication to false when NODE_ENV is development', () => {
Expand All @@ -66,6 +72,22 @@ describe('Novu Client', () => {
const newClient = new Client({ secretKey: 'some-secret-key', strictAuthentication: testStrictAuthentication });
expect(newClient.strictAuthentication).toBe(testStrictAuthentication);
});

it('should set strictAuthentication to false when NOVU_STRICT_AUTHENTICATION_ENABLED is false', () => {
const originalEnv = process.env.NOVU_STRICT_AUTHENTICATION_ENABLED;
process.env = { ...process.env, NOVU_STRICT_AUTHENTICATION_ENABLED: 'false' };
const newClient = new Client({ secretKey: 'some-secret-key' });
expect(newClient.strictAuthentication).toBe(false);
process.env = { ...process.env, NOVU_STRICT_AUTHENTICATION_ENABLED: originalEnv };
});

it('should set strictAuthentication to true when NOVU_STRICT_AUTHENTICATION_ENABLED is true', () => {
const originalEnv = process.env.NOVU_STRICT_AUTHENTICATION_ENABLED;
process.env = { ...process.env, NOVU_STRICT_AUTHENTICATION_ENABLED: 'true' };
const newClient = new Client({ secretKey: 'some-secret-key' });
expect(newClient.strictAuthentication).toBe(true);
process.env = { ...process.env, NOVU_STRICT_AUTHENTICATION_ENABLED: originalEnv };
});
});

describe('discover method', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/framework/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,22 @@ export class Client {
private buildOptions(providedOptions?: ClientOptions) {
const builtConfiguration: { secretKey?: string; strictAuthentication: boolean } = {
secretKey: undefined,
strictAuthentication: true,
strictAuthentication: isRuntimeInDevelopment() ? false : true,
};

builtConfiguration.secretKey =
providedOptions?.secretKey || process.env.NOVU_SECRET_KEY || process.env.NOVU_API_KEY;

if (!isRuntimeInDevelopment() && !builtConfiguration.secretKey) {
throw new Error(
'Missing secret key. Set the NOVU_SECRET_KEY environment variable or pass secretKey to the client options.'
'Missing secret key. Set the NOVU_SECRET_KEY environment variable or pass `secretKey` to the client options.'
);
}

if (providedOptions?.strictAuthentication !== undefined) {
builtConfiguration.strictAuthentication = providedOptions.strictAuthentication;
} else if (isRuntimeInDevelopment()) {
builtConfiguration.strictAuthentication = false;
} else if (process.env.NOVU_STRICT_AUTHENTICATION_ENABLED !== undefined) {
builtConfiguration.strictAuthentication = process.env.NOVU_STRICT_AUTHENTICATION_ENABLED === 'true';
}

return builtConfiguration;
Expand Down

0 comments on commit 405d971

Please sign in to comment.