'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { spawnSync } = require('node:child_process'); const path = require('node:path'); const GUARD = path.join(__dirname, '..', 'ci', 'deploy_guard.py'); function run (env) { return spawnSync('python3', [GUARD], { env: { ...process.env, ...env }, encoding: 'utf8' }); } function projectDir (env) { const py = [ 'import os,sys', 'sys.path.insert(0, "ci")', 'from deploy_guard import project_dir', 'print(project_dir(os.environ))' ].join('; '); return spawnSync('python3', ['-c', py], { cwd: path.join(__dirname, '..'), env: { ...process.env, ...env }, encoding: 'utf8' }); } describe('deploy_guard', () => { it('expands $timftproot and ${timftproot} and collapses double slashes', () => { let r = projectDir({ timftproot: '/var/www/', projectftp: '$timftproot/meteors' }); assert.equal(r.status, 0, r.stderr); assert.equal(r.stdout.trim(), '/var/www/meteors'); r = projectDir({ timftproot: '/var/www', projectftp: '${timftproot}/meteors' }); assert.equal(r.status, 0, r.stderr); assert.equal(r.stdout.trim(), '/var/www/meteors'); r = projectDir({ timftproot: '/var/www/', projectftp: '${timftproot}/meteors' }); assert.equal(r.status, 0, r.stderr); assert.equal(r.stdout.trim(), '/var/www/meteors'); }); it('refuses the FTP root, a missing secret, and a non-meteors leaf', () => { let r = projectDir({ timftproot: '/var/www', projectftp: '$timftproot' }); assert.notEqual(r.status, 0); assert.match(r.stderr, /GEBLOKKEERD/); r = projectDir({ timftproot: '/var/www', projectftp: '$timftproot/other' }); assert.notEqual(r.status, 0); assert.match(r.stderr, /meteors/); r = projectDir({ timftproot: '/var/www' }); assert.notEqual(r.status, 0); }); it('the CLI exits 0 for a good path and does not print the path', () => { const r = run({ timftproot: '/var/www', projectftp: '$timftproot/meteors' }); assert.equal(r.status, 0, r.stderr); assert.match(r.stdout, /ok/i); assert.doesNotMatch(r.stdout + r.stderr, /\/var\/www/); }); }); describe('assert_safe_target', () => { it('allows a new, empty, previous, or leftover-files folder and only logs foreign names', () => { const py = [ 'import sys', 'sys.path.insert(0, "ci")', 'from deploy import assert_safe_target, deploy_commands', 'assert_safe_target(False, [])', 'assert_safe_target(True, [])', 'assert_safe_target(True, ["index.html", "game.js", "framework.js", "style.css", "images"])', // Leftovers, including a host favicon, must not abort. 'assert_safe_target(True, ["favicon.ico", "index.php", "notes.txt"])', 'print("safe-target ok")' ].join('\n'); const r = spawnSync('python3', ['-c', py], { cwd: path.join(__dirname, '..'), encoding: 'utf8' }); assert.equal(r.status, 0, r.stdout + r.stderr); assert.match(r.stdout, /safe-target ok/); assert.match(r.stdout, /favicon\.ico blijft ongemoeid/); assert.match(r.stdout, /index\.php/); assert.doesNotMatch(r.stdout + r.stderr, /GEBLOKKEERD/); }); it('the FTP mirror never deletes, never uploads favicon.ico, and leaves data/ alone', () => { const py = [ 'import sys', 'sys.path.insert(0, "ci")', 'from pathlib import Path', 'from deploy import deploy_commands', 'env = {"timftphost": "ftp://example.test", "timftpuser": "u", "timftppassword": "p"}', 'cmds = "\\n".join(deploy_commands(env, "/var/www/meteors", Path("build")))', 'assert "--delete" not in cmds, cmds', 'assert "--exclude-glob \\"favicon.ico\\"" in cmds, cmds', 'assert "--exclude-glob \\"data/**\\"" in cmds, cmds', 'assert "--exclude-glob \\"**/config.php\\"" in cmds, cmds', 'assert "data/.htaccess" in cmds, cmds', 'assert "put " in cmds, cmds', 'print("mirror ok")' ].join('\n'); const r = spawnSync('python3', ['-c', py], { cwd: path.join(__dirname, '..'), encoding: 'utf8' }); assert.equal(r.status, 0, r.stdout + r.stderr); assert.match(r.stdout, /mirror ok/); }); }); describe('auth_config_php', () => { it('is covered by ci/test_deploy_auth.py (PHP escaping, empty secret, no leak in lftp)', () => { const r = spawnSync('python3', ['ci/test_deploy_auth.py'], { cwd: path.join(__dirname, '..'), encoding: 'utf8' }); assert.equal(r.status, 0, r.stdout + r.stderr); assert.match(r.stdout, /ok/); }); });