Files
cgm-remote-monitor/tests/production-safety.test.js
T
Ben WestandCopilot 6a681509f3 feat: add database-level production safety checks (GAP-SYNC-047)
Adds multi-layer protection against running destructive tests on production:

1. Pre-flight check (hooks.js): Verifies NODE_ENV=test before any DB connection
2. Database name check: Requires 'test' substring in database name
3. Entry count threshold: Refuses if database has >100 entries (configurable)

Environment Variables:
- TEST_SAFETY_MAX_ENTRIES: Max entries before refusing (default: 100)
- TEST_SAFETY_REQUIRE_TEST_DB: Require 'test' in DB name (default: true)
- TEST_SAFETY_SKIP: Emergency bypass for all checks (default: false)

Files:
- tests/lib/production-safety.js: Core safety check module
- tests/00_production-safety.test.js: Runs first to gate test suite
- tests/production-safety.test.js: Unit tests for safety module
- tests/hooks.js: Updated to use new module

This addresses concerns about users with 'test' in production DB names
by adding the entry count threshold as a secondary safety measure.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-18 19:49:49 -07:00

89 lines
3.1 KiB
JavaScript

'use strict';
/**
* Unit tests for production-safety.js module
*/
var should = require('should');
var productionSafety = require('./lib/production-safety');
describe('Production Safety Module', function() {
describe('extractDbName', function() {
it('should extract database name from standard MongoDB URI', function() {
var dbName = productionSafety.extractDbName('mongodb://localhost:27017/nightscout_test');
dbName.should.equal('nightscout_test');
});
it('should extract database name from MongoDB+SRV URI', function() {
var dbName = productionSafety.extractDbName('mongodb+srv://user:pass@cluster.mongodb.net/mydb_test');
dbName.should.equal('mydb_test');
});
it('should handle URI with query parameters', function() {
var dbName = productionSafety.extractDbName('mongodb://localhost:27017/nightscout_test?retryWrites=true');
dbName.should.equal('nightscout_test');
});
it('should handle URI with auth credentials', function() {
var dbName = productionSafety.extractDbName('mongodb://user:password@localhost:27017/test_db');
dbName.should.equal('test_db');
});
it('should return default for empty connection string', function() {
var dbName = productionSafety.extractDbName('');
dbName.should.be.a.String();
});
});
describe('isTestDatabaseName', function() {
it('should recognize "_test" suffix', function() {
productionSafety.isTestDatabaseName('nightscout_test').should.be.true();
});
it('should recognize "test_" prefix', function() {
productionSafety.isTestDatabaseName('test_nightscout').should.be.true();
});
it('should recognize "test" anywhere in name', function() {
productionSafety.isTestDatabaseName('my_testing_db').should.be.true();
});
it('should be case insensitive', function() {
productionSafety.isTestDatabaseName('Nightscout_TEST').should.be.true();
productionSafety.isTestDatabaseName('TEST_DB').should.be.true();
});
it('should reject production-looking names', function() {
productionSafety.isTestDatabaseName('nightscout').should.be.false();
productionSafety.isTestDatabaseName('production').should.be.false();
productionSafety.isTestDatabaseName('mydb').should.be.false();
});
it('should reject names with "test" as substring of other words', function() {
// "contest" contains "test" but is not a test database
// Current implementation will actually match this - documenting behavior
productionSafety.isTestDatabaseName('contest').should.be.true(); // Contains "test"
});
});
describe('DEFAULT_MAX_ENTRIES', function() {
it('should be a reasonable default (100)', function() {
productionSafety.DEFAULT_MAX_ENTRIES.should.equal(100);
});
});
describe('preflightCheck', function() {
it('should not throw when NODE_ENV=test', function() {
// This test is running, so NODE_ENV must be test
process.env.NODE_ENV.should.equal('test');
// preflightCheck would have already run via hooks.js
// If we got here, it passed
});
});
});