"""Production CI code-only boundary; no daemon or real credentials required.""" import copy import importlib.util from pathlib import Path import subprocess import tempfile import unittest ROOT = Path(__file__).resolve().parents[2] class ProductionCI(unittest.TestCase): def test_main_dispatches_exact_sha_code_only_implementation(self): source = (ROOT / 'scripts/deploy.sh').read_text() self.assertIn('release-cache-prod.sh', source) for unsafe in ('migrate ', 'db push', 'seed', 'canina-bot', 'reset --hard', 'prune '): self.assertNotIn(unsafe, source) workflow = (ROOT / '.gitea/workflows/deploy.yml').read_text() self.assertIn('DEPLOY_SHA: ${{ github.sha }}', workflow) self.assertIn('/raw/commit/${DEPLOY_SHA}/scripts/release-cache-prod.sh', workflow) self.assertIn('./deploy.sh "${BRANCH}"', workflow) with tempfile.TemporaryDirectory() as d: root = Path(d) (root / 'deploy.sh').write_text(source) for branch, environment in [('main', 'prod'), ('develop', 'stage')]: (root / ('release-cache-' + environment + '.sh')).write_text('printf "%s" "$DEPLOY_SHA"') result = subprocess.run(['bash', str(root / 'deploy.sh'), branch], env={'DEPLOY_SHA': 'a' * 40}, capture_output=True, text=True) self.assertEqual(0, result.returncode) self.assertEqual('a' * 40, result.stdout) self.assertNotEqual(0, subprocess.run(['bash', str(root / 'deploy.sh'), 'other'], capture_output=True).returncode) def test_prod_release_preserves_authority_and_scopes_services(self): path = ROOT / 'scripts/release-cache-prod.sh' self.assertTrue(path.exists(), 'production code-only path missing') source = path.read_text() for required in ('git fetch origin main', 'git rev-parse origin/main', 'git archive "$DEPLOY_SHA"', 'production-deploy.lock', 'canina-production-deploy-lock', '--environment prod', '--source "$PROD_DIR/.revalidation.env"', '--project-name prod --environment prod', 'check_prod_lock', 'supervise_release', '--force-recreate backend_prod frontend_prod', '--progress plain build backend_prod frontend_prod', 'http://frontend_prod:3000', 'http://backend_prod:3000/api', 'https://canina.ir', "['node','dist/main.js']"): self.assertIn(required, source) for unsafe in ('migrate ', 'db push', 'seed', 'canina-bot', 'reset --hard', '--remove-orphans', 'prune ', 'backend_stage', 'frontend_stage', '--repair-empty'): self.assertNotIn(unsafe, source) self.assertLess(source.index('verify-stage-runtime.py'), source.index('--progress plain build')) def test_production_runtime_authority_rejects_stage_origin_and_preserves_secrets(self): spec = importlib.util.spec_from_file_location('authority', ROOT / 'scripts/verify-stage-runtime.py') module = importlib.util.module_from_spec(spec); spec.loader.exec_module(module) fixture_spec = importlib.util.spec_from_file_location('fixtures', Path(__file__).with_name('test_stage_runtime_authority.py')) fixtures = importlib.util.module_from_spec(fixture_spec); fixture_spec.loader.exec_module(fixtures) base, planned, live = fixtures.fixture() for config in (base, planned): config['services'] = {k.replace('_stage', '_prod'): v for k, v in config['services'].items()} config['services']['frontend_prod']['build']['args'].update( NEXT_PUBLIC_API_URL='https://api.canina.ir/api', VITE_API_URL='https://api.canina.ir') live = {k.replace('_stage', '_prod'): v for k, v in live.items()} planned['services']['frontend_prod']['environment']['NEXT_PUBLIC_SITE_URL'] = 'https://canina.ir' self.assertEqual([], module.verify(base, planned, live, b'n', b'n', environment='prod')) planned['services']['frontend_prod']['environment']['NEXT_PUBLIC_SITE_URL'] = 'https://stage.canina.ir' self.assertIn('frontend_prod.environment.NEXT_PUBLIC_SITE_URL', module.verify(base, planned, live, b'n', b'n', environment='prod')) planned['services']['backend_prod']['environment']['OPERATIONAL'] = 'DO-NOT-DISCLOSE' errors = module.verify(base, planned, live, b'n', b'n', environment='prod') self.assertIn('backend_prod.environment.OPERATIONAL', errors) self.assertNotIn('DO-NOT-DISCLOSE', ' '.join(errors)) if __name__ == '__main__': unittest.main()