BlueGit

projects-timfalken-com allianties

Files

allianties / test / deploy_guard.test.js

deploy_guard.test.js

deploy_guard.test.js 4.3 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const { spawnSync } = require('node:child_process');
6const path = require('node:path');
7
8const GUARD = path.join(__dirname, '..', 'ci', 'deploy_guard.py');
9
10function run (env)
11{
12 return spawnSync('python3', [GUARD], {
13 env: { ...process.env, ...env },
14 encoding: 'utf8'
15 });
16}
17
18function 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
33describe('deploy_guard', () =>
34{
35 it('expands $timftproot and ${timftproot} and collapses double slashes', () =>
36 {
37 let r = projectDir({ timftproot: '/var/www/', projectftp: '$timftproot/allianties' });
38 assert.equal(r.status, 0, r.stderr);
39 assert.equal(r.stdout.trim(), '/var/www/allianties');
40
41 r = projectDir({ timftproot: '/var/www', projectftp: '${timftproot}/allianties' });
42 assert.equal(r.status, 0, r.stderr);
43 assert.equal(r.stdout.trim(), '/var/www/allianties');
44
45 r = projectDir({ timftproot: '/var/www/', projectftp: '${timftproot}/allianties' });
46 assert.equal(r.status, 0, r.stderr);
47 assert.equal(r.stdout.trim(), '/var/www/allianties');
48 });
49
50 it('refuses the FTP root, a missing secret, and a non-allianties 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, /allianties/);
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/allianties' });
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
73describe('assert_safe_target', () =>
74{
75 it('allows a new, empty, previous, or leftover-files folder and only logs foreign names', () =>
76 {
77 const py = [
78 'import sys',
79 'sys.path.insert(0, "ci")',
80 'from deploy import assert_safe_target, deploy_commands',
81 'assert_safe_target(False, [])',
82 'assert_safe_target(True, [])',
83 'assert_safe_target(True, ["index.html", "game.js", "framework.js", "style.css", "images"])',
84 // Leftovers, including a host favicon, must not abort.
85 'assert_safe_target(True, ["favicon.ico", "index.php", "notes.txt"])',
86 'print("safe-target ok")'
87 ].join('\n');
88 const r = spawnSync('python3', ['-c', py], {
89 cwd: path.join(__dirname, '..'),
90 encoding: 'utf8'
91 });
92 assert.equal(r.status, 0, r.stdout + r.stderr);
93 assert.match(r.stdout, /safe-target ok/);
94 assert.match(r.stdout, /favicon\.ico blijft ongemoeid/);
95 assert.match(r.stdout, /index\.php/);
96 assert.doesNotMatch(r.stdout + r.stderr, /GEBLOKKEERD/);
97 });
98
99 it('the FTP mirror never deletes, never uploads favicon.ico, and leaves data/ alone', () =>
100 {
101 const py = [
102 'import sys',
103 'sys.path.insert(0, "ci")',
104 'from pathlib import Path',
105 'from deploy import deploy_commands',
106 'env = {"timftphost": "ftp://example.test", "timftpuser": "u", "timftppassword": "p"}',
107 'cmds = "\\n".join(deploy_commands(env, "/var/www/allianties", Path("build")))',
108 'assert "--delete" not in cmds, cmds',
109 'assert "--exclude-glob \\"favicon.ico\\"" in cmds, cmds',
110 'assert "--exclude-glob \\"data/**\\"" in cmds, cmds',
111 'assert "--exclude-glob \\"**/config.php\\"" in cmds, cmds',
112 'assert "data/.htaccess" in cmds, cmds',
113 'assert "put " in cmds, cmds',
114 'print("mirror ok")'
115 ].join('\n');
116 const r = spawnSync('python3', ['-c', py], {
117 cwd: path.join(__dirname, '..'),
118 encoding: 'utf8'
119 });
120 assert.equal(r.status, 0, r.stdout + r.stderr);
121 assert.match(r.stdout, /mirror ok/);
122 });
123});
124
125
126describe('auth_config_php', () =>
127{
128 it('is covered by ci/test_deploy_auth.py (PHP escaping, empty secret, no leak in lftp)', () =>
129 {
130 const r = spawnSync('python3', ['ci/test_deploy_auth.py'], {
131 cwd: path.join(__dirname, '..'),
132 encoding: 'utf8'
133 });
134 assert.equal(r.status, 0, r.stdout + r.stderr);
135 assert.match(r.stdout, /ok/);
136 });
137});