Files
canina/scripts/tests/test_secure_build_install.py

53 lines
2.4 KiB
Python

"""Regression gates for CI1286: trusted routing and fail-closed frontend installs."""
from pathlib import Path
import os
import re
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
REGISTRY = 'https://registry.parsaaghayi.ir/repository/npm/'
class SecureBuildInstallTests(unittest.TestCase):
def test_stage_supplies_operational_tls_registry(self):
compose = (ROOT / 'scripts/compose.stage.yml').read_text()
self.assertEqual(compose.count('NPM_REGISTRY: ${NPM_REGISTRY:-' + REGISTRY + '}'), 2)
def test_frontend_uses_supported_node_lts_in_all_stages(self):
images = re.findall(r'^FROM node:(\S+)', (ROOT / 'Dockerfile').read_text(), re.M)
self.assertEqual(images, ['22-alpine'] * 3)
def test_registry_configuration_never_disables_tls(self):
for name in ['Dockerfile', 'backend/Dockerfile']:
self.assertNotIn('strict-ssl false', (ROOT / name).read_text())
def test_false_successful_incomplete_install_aborts_layer(self):
dockerfile = (ROOT / 'Dockerfile').read_text().replace('\\\n', '')
commands = re.findall(r'^RUN (npm ci[^\n]+)', dockerfile, re.M)
self.assertEqual(len(commands), 2)
for command, binary in zip(commands, ['next', 'vite']):
with self.subTest(binary=binary), tempfile.TemporaryDirectory() as d:
root = Path(d)
npm = root / 'npm'
# Reproduce npm's observed zero exit with no complete node_modules.
npm.write_text('#!/bin/sh\nexit 0\n')
npm.chmod(0o755)
result = subprocess.run(['sh', '-c', command], cwd=d,
env={**os.environ, 'PATH': d + ':' + os.environ['PATH']},
capture_output=True)
self.assertNotEqual(result.returncode, 0, 'incomplete install was accepted')
target = root / 'node_modules/.bin' / binary
target.parent.mkdir(parents=True)
target.write_text('#!/bin/sh\nexit 0\n')
target.chmod(0o755)
result = subprocess.run(['sh', '-c', command], cwd=d,
env={**os.environ, 'PATH': d + ':' + os.environ['PATH']},
capture_output=True)
self.assertEqual(result.returncode, 0, result.stderr)
if __name__ == '__main__':
unittest.main()