fix: Fix ordering issue when checking if staging environment (#17385)

This commit is contained in:
Jon
2025-07-21 10:55:34 +01:00
committed by GitHub
parent b14ad784cb
commit b8e21876d1
2 changed files with 81 additions and 2 deletions

View File

@@ -0,0 +1,72 @@
import { inProduction } from '@n8n/backend-common';
import { getCommunityNodeTypes } from '../../utils/community-node-types-utils';
import { CommunityNodeTypesService } from '../community-node-types.service';
jest.mock('@n8n/backend-common', () => ({
inProduction: jest.fn().mockReturnValue(false),
logger: jest.fn().mockImplementation(() => ({
error: jest.fn(),
})),
}));
jest.mock('../../utils/community-node-types-utils', () => ({
getCommunityNodeTypes: jest.fn().mockResolvedValue([]),
}));
describe('CommunityNodeTypesService', () => {
let service: CommunityNodeTypesService;
let globalConfigMock: any;
let communityPackagesServiceMock: any;
let loggerMock: any;
beforeEach(() => {
jest.clearAllMocks();
delete process.env.ENVIRONMENT;
loggerMock = { error: jest.fn() };
globalConfigMock = {
nodes: {
communityPackages: {
enabled: true,
verifiedEnabled: true,
},
},
};
communityPackagesServiceMock = {};
service = new CommunityNodeTypesService(
loggerMock,
globalConfigMock,
communityPackagesServiceMock,
);
});
describe('fetchNodeTypes', () => {
it('should use staging environment when ENVIRONMENT=staging', async () => {
process.env.ENVIRONMENT = 'staging';
await (service as any).fetchNodeTypes();
expect(getCommunityNodeTypes).toHaveBeenCalledWith('staging');
});
it('should use production environment when inProduction=true', async () => {
(inProduction as unknown as jest.Mock).mockReturnValue(true);
await (service as any).fetchNodeTypes();
expect(getCommunityNodeTypes).toHaveBeenCalledWith('production');
});
it('should use production environment when ENVIRONMENT=production', async () => {
process.env.ENVIRONMENT = 'production';
await (service as any).fetchNodeTypes();
expect(getCommunityNodeTypes).toHaveBeenCalledWith('production');
});
it('should prioritize ENVIRONMENT=staging over inProduction=true', async () => {
process.env.ENVIRONMENT = 'staging';
(inProduction as unknown as jest.Mock).mockReturnValue(true);
await (service as any).fetchNodeTypes();
expect(getCommunityNodeTypes).toHaveBeenCalledWith('staging');
});
});
});

View File

@@ -32,8 +32,7 @@ export class CommunityNodeTypesService {
this.globalConfig.nodes.communityPackages.verifiedEnabled
) {
// Cloud sets ENVIRONMENT to 'production' or 'staging' depending on the environment
const environment =
inProduction || process.env.ENVIRONMENT === 'production' ? 'production' : 'staging';
const environment = this.detectEnvironment();
data = await getCommunityNodeTypes(environment);
}
@@ -43,6 +42,14 @@ export class CommunityNodeTypesService {
}
}
private detectEnvironment() {
const environment = process.env.ENVIRONMENT;
if (environment === 'staging') return 'staging';
if (inProduction) return 'production';
if (environment === 'production') return 'production';
return 'staging';
}
private updateCommunityNodeTypes(nodeTypes: StrapiCommunityNodeType[]) {
if (!nodeTypes?.length) return;