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:
William Valentin
2025-09-07 18:36:55 -07:00
parent 16d025e747
commit 22e996e698
2 changed files with 23 additions and 15 deletions

View File

@@ -308,6 +308,7 @@ describe('Mailgun Configuration', () => {
describe('isDevelopmentMode', () => {
test('should return true when not in production and Mailgun not configured', () => {
mockIsProduction.mockReturnValue(false);
mockGetEnvVar.mockReturnValue(undefined);
expect(isDevelopmentMode()).toBe(true);
});
@@ -347,6 +348,8 @@ describe('Mailgun Configuration', () => {
describe('integration scenarios', () => {
test('should work with real environment configuration flow', () => {
// Clear any previous mock implementations
mockGetEnvVar.mockReset();
// Provide stable implementation for multiple calls
mockGetEnvVar.mockImplementation((key: string, defaultValue?: string) => {
switch (key) {

View File

@@ -1,14 +1,14 @@
// Mailgun Configuration
// This file handles Mailgun credentials and configuration
import { getEnv } from '../utils/env';
import { getEnvVar, isProduction } from '../utils/env';
export interface MailgunConfig {
apiKey: string;
domain: string;
apiKey: string | undefined;
domain: string | undefined;
baseUrl: string;
fromName: string;
fromEmail: string;
fromEmail: string | undefined;
}
// Default configuration for development
@@ -22,14 +22,12 @@ const defaultConfig: MailgunConfig = {
// Load configuration from environment variables or use defaults
export const getMailgunConfig = (): MailgunConfig => {
const env = getEnv();
return {
apiKey: env.VITE_MAILGUN_API_KEY || defaultConfig.apiKey,
domain: env.VITE_MAILGUN_DOMAIN || defaultConfig.domain,
baseUrl: env.VITE_MAILGUN_BASE_URL || defaultConfig.baseUrl,
fromName: env.VITE_MAILGUN_FROM_NAME || defaultConfig.fromName,
fromEmail: env.VITE_MAILGUN_FROM_EMAIL || defaultConfig.fromEmail,
apiKey: getEnvVar('VITE_MAILGUN_API_KEY'),
domain: getEnvVar('VITE_MAILGUN_DOMAIN'),
baseUrl: getEnvVar('VITE_MAILGUN_BASE_URL', defaultConfig.baseUrl),
fromName: getEnvVar('VITE_MAILGUN_FROM_NAME', defaultConfig.fromName),
fromEmail: getEnvVar('VITE_MAILGUN_FROM_EMAIL'),
};
};
@@ -37,13 +35,20 @@ export const getMailgunConfig = (): MailgunConfig => {
export const isMailgunConfigured = (): boolean => {
const config = getMailgunConfig();
return (
config.apiKey !== 'demo-key' &&
config.domain !== 'demo.mailgun.org' &&
config.apiKey.length > 0
config.apiKey !== defaultConfig.apiKey &&
config.domain !== defaultConfig.domain &&
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
export const isDevelopmentMode = (): boolean => {
return !isMailgunConfigured();
return !isProduction() && !isMailgunConfigured();
};