Refactor Mailgun config to use getEnvVar and improve checks
- Replace getEnv with getEnvVar for environment variable access - Update MailgunConfig types to allow undefined values - Enhance isMailgunConfigured to check for undefined and empty values - Update isDevelopmentMode to check production status - Improve test mocks for environment variable handling
This commit is contained in:
@@ -308,6 +308,7 @@ describe('Mailgun Configuration', () => {
|
|||||||
describe('isDevelopmentMode', () => {
|
describe('isDevelopmentMode', () => {
|
||||||
test('should return true when not in production and Mailgun not configured', () => {
|
test('should return true when not in production and Mailgun not configured', () => {
|
||||||
mockIsProduction.mockReturnValue(false);
|
mockIsProduction.mockReturnValue(false);
|
||||||
|
mockGetEnvVar.mockReturnValue(undefined);
|
||||||
|
|
||||||
expect(isDevelopmentMode()).toBe(true);
|
expect(isDevelopmentMode()).toBe(true);
|
||||||
});
|
});
|
||||||
@@ -347,6 +348,8 @@ describe('Mailgun Configuration', () => {
|
|||||||
|
|
||||||
describe('integration scenarios', () => {
|
describe('integration scenarios', () => {
|
||||||
test('should work with real environment configuration flow', () => {
|
test('should work with real environment configuration flow', () => {
|
||||||
|
// Clear any previous mock implementations
|
||||||
|
mockGetEnvVar.mockReset();
|
||||||
// Provide stable implementation for multiple calls
|
// Provide stable implementation for multiple calls
|
||||||
mockGetEnvVar.mockImplementation((key: string, defaultValue?: string) => {
|
mockGetEnvVar.mockImplementation((key: string, defaultValue?: string) => {
|
||||||
switch (key) {
|
switch (key) {
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
// Mailgun Configuration
|
// Mailgun Configuration
|
||||||
// This file handles Mailgun credentials and configuration
|
// This file handles Mailgun credentials and configuration
|
||||||
|
|
||||||
import { getEnv } from '../utils/env';
|
import { getEnvVar, isProduction } from '../utils/env';
|
||||||
|
|
||||||
export interface MailgunConfig {
|
export interface MailgunConfig {
|
||||||
apiKey: string;
|
apiKey: string | undefined;
|
||||||
domain: string;
|
domain: string | undefined;
|
||||||
baseUrl: string;
|
baseUrl: string;
|
||||||
fromName: string;
|
fromName: string;
|
||||||
fromEmail: string;
|
fromEmail: string | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default configuration for development
|
// Default configuration for development
|
||||||
@@ -22,14 +22,12 @@ const defaultConfig: MailgunConfig = {
|
|||||||
|
|
||||||
// Load configuration from environment variables or use defaults
|
// Load configuration from environment variables or use defaults
|
||||||
export const getMailgunConfig = (): MailgunConfig => {
|
export const getMailgunConfig = (): MailgunConfig => {
|
||||||
const env = getEnv();
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
apiKey: env.VITE_MAILGUN_API_KEY || defaultConfig.apiKey,
|
apiKey: getEnvVar('VITE_MAILGUN_API_KEY'),
|
||||||
domain: env.VITE_MAILGUN_DOMAIN || defaultConfig.domain,
|
domain: getEnvVar('VITE_MAILGUN_DOMAIN'),
|
||||||
baseUrl: env.VITE_MAILGUN_BASE_URL || defaultConfig.baseUrl,
|
baseUrl: getEnvVar('VITE_MAILGUN_BASE_URL', defaultConfig.baseUrl),
|
||||||
fromName: env.VITE_MAILGUN_FROM_NAME || defaultConfig.fromName,
|
fromName: getEnvVar('VITE_MAILGUN_FROM_NAME', defaultConfig.fromName),
|
||||||
fromEmail: env.VITE_MAILGUN_FROM_EMAIL || defaultConfig.fromEmail,
|
fromEmail: getEnvVar('VITE_MAILGUN_FROM_EMAIL'),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -37,13 +35,20 @@ export const getMailgunConfig = (): MailgunConfig => {
|
|||||||
export const isMailgunConfigured = (): boolean => {
|
export const isMailgunConfigured = (): boolean => {
|
||||||
const config = getMailgunConfig();
|
const config = getMailgunConfig();
|
||||||
return (
|
return (
|
||||||
config.apiKey !== 'demo-key' &&
|
config.apiKey !== defaultConfig.apiKey &&
|
||||||
config.domain !== 'demo.mailgun.org' &&
|
config.domain !== defaultConfig.domain &&
|
||||||
config.apiKey.length > 0
|
config.fromEmail !== defaultConfig.fromEmail &&
|
||||||
|
config.apiKey !== undefined &&
|
||||||
|
config.apiKey !== '' &&
|
||||||
|
config.apiKey.trim().length > 0 &&
|
||||||
|
config.domain !== undefined &&
|
||||||
|
config.domain !== '' &&
|
||||||
|
config.fromEmail !== undefined &&
|
||||||
|
config.fromEmail !== ''
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Development mode check
|
// Development mode check
|
||||||
export const isDevelopmentMode = (): boolean => {
|
export const isDevelopmentMode = (): boolean => {
|
||||||
return !isMailgunConfigured();
|
return !isProduction() && !isMailgunConfigured();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user