Agromind-backend / backend /tests /mongoUrlValidation.test.js
gh-action-hf-auto
auto: sync backend from github@32fb9685
8a6248c
import { getValidatedMongoUrl } from '../utils/mongoUrlValidation.js';
describe('getValidatedMongoUrl', () => {
const originalMongoUrl = process.env.MONGO_URL;
afterEach(() => {
if (originalMongoUrl !== undefined) {
process.env.MONGO_URL = originalMongoUrl;
} else {
delete process.env.MONGO_URL;
}
});
test('throws when MONGO_URL is missing', () => {
delete process.env.MONGO_URL;
expect(() => getValidatedMongoUrl()).toThrow(
'FATAL: MONGO_URL environment variable is required and must include an explicit database name.'
);
});
test('throws when MONGO_URL does not include a database name', () => {
expect(() =>
getValidatedMongoUrl('mongodb+srv://testuser:testpass@cluster.mongodb.net/?appName=Agromind')
).toThrow('FATAL: MONGO_URL must include an explicit database name in the path');
});
test('accepts MONGO_URL with explicit database name', () => {
const url = 'mongodb+srv://testuser:testpass@cluster.mongodb.net/agromind?appName=Agromind';
expect(getValidatedMongoUrl(url)).toBe(url);
});
test('throws when database path ends with trailing slash', () => {
expect(() =>
getValidatedMongoUrl('mongodb+srv://testuser:testpass@cluster.mongodb.net/agromind/?appName=Agromind')
).toThrow('FATAL: MONGO_URL must include an explicit database name in the path');
});
});