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

chore(api): Enable preview deployments #7200

Merged
merged 4 commits into from
Dec 3, 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
2 changes: 1 addition & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@novu/api",
"version": "2.1.0",
"version": "2.1.1",
"description": "description",
"author": "",
"private": "true",
Expand Down
2 changes: 0 additions & 2 deletions apps/api/src/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ API_RATE_LIMIT_MAXIMUM_UNLIMITED_TRIGGER=
API_RATE_LIMIT_MAXIMUM_UNLIMITED_CONFIGURATION=
API_RATE_LIMIT_MAXIMUM_UNLIMITED_GLOBAL=

PR_PREVIEW_ROOT_URL=dev-web-novu.netlify.app

HUBSPOT_INVITE_NUDGE_EMAIL_USER_LIST_ID=
HUBSPOT_PRIVATE_APP_ACCESS_TOKEN=

Expand Down
4 changes: 3 additions & 1 deletion apps/api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ modules.push(
client: new Client({
secretKey: process.env.NOVU_INTERNAL_SECRET_KEY,
strictAuthentication:
process.env.NODE_ENV === 'production' || process.env.NOVU_STRICT_AUTHENTICATION_ENABLED === 'true',
process.env.NODE_ENV === 'production' ||
process.env.NODE_ENV === 'dev' ||
process.env.NOVU_STRICT_AUTHENTICATION_ENABLED === 'true',
}),
controllerDecorators: [ApiExcludeController()],
workflows: [usageLimitsWorkflow],
Expand Down
69 changes: 15 additions & 54 deletions apps/api/src/config/cors.config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { spy } from 'sinon';
import { expect } from 'chai';
import { corsOptionsDelegate, isPermittedDeployPreviewOrigin } from './cors.config';
import { corsOptionsDelegate } from './cors.config';

describe('CORS Configuration', () => {
describe('Local Environment', () => {
Expand Down Expand Up @@ -32,7 +32,6 @@ describe('CORS Configuration', () => {
process.env.FRONT_BASE_URL = 'https://test.com';
process.env.LEGACY_STAGING_DASHBOARD_URL = 'https://test-legacy-staging-dashboard.com';
process.env.WIDGET_BASE_URL = 'https://widget.com';
process.env.PR_PREVIEW_ROOT_URL = 'https://pr-preview.com';
});

afterEach(() => {
Expand All @@ -43,14 +42,26 @@ describe('CORS Configuration', () => {
const callbackSpy = spy();

// @ts-expect-error - corsOptionsDelegate is not typed correctly
corsOptionsDelegate({ url: '/v1/test' }, callbackSpy);
corsOptionsDelegate(
{
url: '/v1/test',
headers: {
origin: 'https://test.novu.com',
},
},
callbackSpy
);

expect(callbackSpy.calledOnce).to.be.ok;
expect(callbackSpy.firstCall.firstArg).to.be.null;
expect(callbackSpy.firstCall.lastArg.origin.length).to.equal(3);
expect(callbackSpy.firstCall.lastArg.origin.length).to.equal(environment === 'dev' ? 4 : 3);
expect(callbackSpy.firstCall.lastArg.origin[0]).to.equal(process.env.FRONT_BASE_URL);
expect(callbackSpy.firstCall.lastArg.origin[1]).to.equal(process.env.LEGACY_STAGING_DASHBOARD_URL);
expect(callbackSpy.firstCall.lastArg.origin[2]).to.equal(process.env.WIDGET_BASE_URL);

if (environment === 'dev') {
expect(callbackSpy.firstCall.lastArg.origin[3]).to.equal('https://test.novu.com');
}
});

it('widget routes should be wildcarded', () => {
Expand All @@ -74,56 +85,6 @@ describe('CORS Configuration', () => {
expect(callbackSpy.firstCall.firstArg).to.be.null;
expect(callbackSpy.firstCall.lastArg.origin).to.equal('*');
});

if (environment === 'dev') {
it('should allow all origins for dev environment from pr preview', () => {
const callbackSpy = spy();

// @ts-expect-error - corsOptionsDelegate is not typed correctly
corsOptionsDelegate(
{
url: '/v1/test',
headers: {
origin: `https://test--${process.env.PR_PREVIEW_ROOT_URL}`,
},
},
callbackSpy
);

expect(callbackSpy.calledOnce).to.be.ok;
expect(callbackSpy.firstCall.firstArg).to.be.null;
expect(callbackSpy.firstCall.lastArg.origin).to.equal('*');
});
}
});
});

describe('isPermittedDeployPreviewOrigin', () => {
afterEach(() => {
process.env.NODE_ENV = 'test';
});

it('should return false when NODE_ENV is not dev', () => {
process.env.NODE_ENV = 'production';
expect(isPermittedDeployPreviewOrigin('https://someorigin.com')).to.be.false;
});

it('should return false when PR_PREVIEW_ROOT_URL is not set', () => {
process.env.NODE_ENV = 'dev';
delete process.env.PR_PREVIEW_ROOT_URL;
expect(isPermittedDeployPreviewOrigin('https://someorigin.com')).to.be.false;
});

it('should return false for origins not matching PR_PREVIEW_ROOT_URL (string)', () => {
process.env.NODE_ENV = 'dev';
process.env.PR_PREVIEW_ROOT_URL = 'https://pr-preview.com';
expect(isPermittedDeployPreviewOrigin('https://anotherorigin.com')).to.be.false;
});

it('should return true for origin matching PR_PREVIEW_ROOT_URL', () => {
process.env.NODE_ENV = 'dev';
process.env.PR_PREVIEW_ROOT_URL = 'https://pr-preview.com';
expect(isPermittedDeployPreviewOrigin('https://netlify-https://pr-preview.com')).to.be.true;
});
});
});
32 changes: 6 additions & 26 deletions apps/api/src/config/cors.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export const corsOptionsDelegate: Parameters<INestApplication['enableCors']>[0]
methods: ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
};

const origin = extractOrigin(req);

if (enableWildcard(req)) {
corsOptions.origin = '*';
} else {
Expand All @@ -29,27 +27,17 @@ export const corsOptionsDelegate: Parameters<INestApplication['enableCors']>[0]
if (process.env.WIDGET_BASE_URL) {
corsOptions.origin.push(process.env.WIDGET_BASE_URL);
}
// Enable preview deployments in staging environment for Netlify and Vercel
if (process.env.NODE_ENV === 'dev') {
corsOptions.origin.push(origin(req));
}
}

const shouldDisableCorsForPreviewUrls = isPermittedDeployPreviewOrigin(origin);

Logger.verbose(`Should allow deploy preview? ${shouldDisableCorsForPreviewUrls ? 'Yes' : 'No'}.`, {
curEnv: process.env.NODE_ENV,
previewUrlRoot: process.env.PR_PREVIEW_ROOT_URL,
origin,
});

callback(null as unknown as Error, corsOptions);
};

function enableWildcard(req: Request): boolean {
return (
isSandboxEnvironment() ||
isWidgetRoute(req.url) ||
isInboxRoute(req.url) ||
isBlueprintRoute(req.url) ||
isPermittedDeployPreviewOrigin(extractOrigin(req))
);
return isSandboxEnvironment() || isWidgetRoute(req.url) || isInboxRoute(req.url) || isBlueprintRoute(req.url);
}

function isWidgetRoute(url: string): boolean {
Expand All @@ -68,14 +56,6 @@ function isSandboxEnvironment(): boolean {
return ['test', 'local'].includes(process.env.NODE_ENV);
}

export function isPermittedDeployPreviewOrigin(origin: string | string[]): boolean {
if (!process.env.PR_PREVIEW_ROOT_URL || process.env.NODE_ENV !== 'dev') {
return false;
}

return origin.includes(process.env.PR_PREVIEW_ROOT_URL);
}

function extractOrigin(req: Request): string {
function origin(req: Request): string {
return (req.headers as any)?.origin || '';
}
Empty file removed apps/web/env-config.js
Empty file.
10 changes: 10 additions & 0 deletions packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 2.0.5 (2024-12-02)

### 🩹 Fixes

- **node:** Allow setting includeInactiveChannels to false ([129355e269](https://github.com/novuhq/novu/commit/129355e269))

### ❤️ Thank You

- Sokratis Vidros @SokratisVidros

## 2.0.4 (2024-11-29)

### 🚀 Features
Expand Down
2 changes: 1 addition & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@novu/node",
"version": "2.0.4",
"version": "2.0.5",
"description": "Notification Management Framework",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand Down
Loading