| 1 | 'use strict'; |
| 2 | |
| 3 | const { describe, it } = require('node:test'); |
| 4 | const assert = require('node:assert/strict'); |
| 5 | const { spawnSync } = require('node:child_process'); |
| 6 | const path = require('node:path'); |
| 7 | |
| 8 | const GUARD = path.join(__dirname, '..', 'ci', 'deploy_guard.py'); |
| 9 | |
| 10 | function run (env) |
| 11 | { |
| 12 | return spawnSync('python3', [GUARD], { |
| 13 | env: { ...process.env, ...env }, |
| 14 | encoding: 'utf8' |
| 15 | }); |
| 16 | } |
| 17 | |
| 18 | function projectDir (env) |
| 19 | { |
| 20 | const py = [ |
| 21 | 'import os,sys', |
| 22 | 'sys.path.insert(0, "ci")', |
| 23 | 'from deploy_guard import project_dir', |
| 24 | 'print(project_dir(os.environ))' |
| 25 | ].join('; '); |
| 26 | return spawnSync('python3', ['-c', py], { |
| 27 | cwd: path.join(__dirname, '..'), |
| 28 | env: { ...process.env, ...env }, |
| 29 | encoding: 'utf8' |
| 30 | }); |
| 31 | } |
| 32 | |
| 33 | describe('deploy_guard', () => |
| 34 | { |
| 35 | it('expands $timftproot and ${timftproot} and collapses double slashes', () => |
| 36 | { |
| 37 | let r = projectDir({ timftproot: '/var/www/', projectftp: '$timftproot/meteors' }); |
| 38 | assert.equal(r.status, 0, r.stderr); |
| 39 | assert.equal(r.stdout.trim(), '/var/www/meteors'); |
| 40 | |
| 41 | r = projectDir({ timftproot: '/var/www', projectftp: '${timftproot}/meteors' }); |
| 42 | assert.equal(r.status, 0, r.stderr); |
| 43 | assert.equal(r.stdout.trim(), '/var/www/meteors'); |
| 44 | |
| 45 | r = projectDir({ timftproot: '/var/www/', projectftp: '${timftproot}/meteors' }); |
| 46 | assert.equal(r.status, 0, r.stderr); |
| 47 | assert.equal(r.stdout.trim(), '/var/www/meteors'); |
| 48 | }); |
| 49 | |
| 50 | it('refuses the FTP root, a missing secret, and a non-meteors leaf', () => |
| 51 | { |
| 52 | let r = projectDir({ timftproot: '/var/www', projectftp: '$timftproot' }); |
| 53 | assert.notEqual(r.status, 0); |
| 54 | assert.match(r.stderr, /GEBLOKKEERD/); |
| 55 | |
| 56 | r = projectDir({ timftproot: '/var/www', projectftp: '$timftproot/other' }); |
| 57 | assert.notEqual(r.status, 0); |
| 58 | assert.match(r.stderr, /meteors/); |
| 59 | |
| 60 | r = projectDir({ timftproot: '/var/www' }); |
| 61 | assert.notEqual(r.status, 0); |
| 62 | }); |
| 63 | |
| 64 | it('the CLI exits 0 for a good path and does not print the path', () => |
| 65 | { |
| 66 | const r = run({ timftproot: '/var/www', projectftp: '$timftproot/meteors' }); |
| 67 | assert.equal(r.status, 0, r.stderr); |
| 68 | assert.match(r.stdout, /ok/i); |
| 69 | assert.doesNotMatch(r.stdout + r.stderr, /\/var\/www/); |
| 70 | }); |
| 71 | }); |
| 72 | |
| 73 | describe('assert_safe_target', () => |
| 74 | { |
| 75 | it('allows a new or empty folder, and a previous Meteors deploy, but not foreign files', () => |
| 76 | { |
| 77 | const py = [ |
| 78 | 'import sys', |
| 79 | 'sys.path.insert(0, "ci")', |
| 80 | 'from deploy import assert_safe_target', |
| 81 | 'assert_safe_target(False, [])', |
| 82 | 'assert_safe_target(True, [])', |
| 83 | 'assert_safe_target(True, ["index.html", "game.js", "framework.js", "style.css", "images", "favicon.ico"])', |
| 84 | 'try:', |
| 85 | ' assert_safe_target(True, ["index.php", "wp-config.php"])', |
| 86 | ' raise SystemExit("should have refused")', |
| 87 | 'except ValueError as e:', |
| 88 | ' assert "GEBLOKKEERD" in str(e)', |
| 89 | 'print("safe-target ok")' |
| 90 | ].join('\n'); |
| 91 | const r = spawnSync('python3', ['-c', py], { |
| 92 | cwd: path.join(__dirname, '..'), |
| 93 | encoding: 'utf8' |
| 94 | }); |
| 95 | assert.equal(r.status, 0, r.stdout + r.stderr); |
| 96 | assert.match(r.stdout, /safe-target ok/); |
| 97 | }); |
| 98 | }); |
projects-timfalken-com meteors