← back to Designer Wallcoverings

mailers/cc-api/cc-client-gate.test.js

42 lines

'use strict';

const test = require('node:test');
const assert = require('node:assert/strict');
const { execFileSync } = require('node:child_process');
const path = require('node:path');

const clientPath = path.join(__dirname, 'cc-client.js');

function probe(envChanges, confirm) {
  const env = { ...process.env, ...envChanges };
  for (const [key, value] of Object.entries(env)) {
    if (value === undefined) delete env[key];
  }
  const script = `
    const cc = require(${JSON.stringify(clientPath)});
    cc.createCustomCodeCampaign({
      name: 'gate-test', subject: 'gate-test', fromEmail: 'test@example.com',
      fromName: 'Gate Test', replyTo: 'test@example.com', htmlContent: '<p>test</p>',
      physicalAddress: { address_line1: '1 Test St', city: 'Test', state_code: 'CA', postal_code: '00000', country_code: 'US' },
      confirm: ${JSON.stringify(confirm)}
    }).then((result) => process.stdout.write('RESULT=' + result));
  `;
  return execFileSync(process.execPath, ['-e', script], { env, encoding: 'utf8' });
}

test('gitignored .env cannot arm a live write when caller leaves CC_LIVE unset', () => {
  const output = probe({ CC_LIVE: undefined }, true);
  assert.match(output, /REFUSING live campaign create/);
  assert.match(output, /CC_LIVE!=1 \(env gate closed\)/);
  assert.match(output, /NO-OP POST https:\/\/api\.cc\.email\/v3\/emails/);
  assert.doesNotMatch(output, /token refresh|LIVE POST/);
});

test('explicit CC_LIVE still requires the independent confirm gate', () => {
  const output = probe({ CC_LIVE: '1' }, false);
  assert.match(output, /REFUSING live campaign create/);
  assert.match(output, /--confirm not passed/);
  assert.match(output, /NO-OP POST https:\/\/api\.cc\.email\/v3\/emails/);
  assert.doesNotMatch(output, /token refresh|LIVE POST/);
});