BlueGit

projects-timfalken-com meteors

Files

meteors / test / deploy_guard.test.js

deploy_guard.test.js

deploy_guard.test.js 3.7 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/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
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 and never uploads favicon.ico', () =>
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/meteors", Path("build")))',
108 'assert "--delete" not in cmds, cmds',
109 'assert "--exclude-glob \\"favicon.ico\\"" in cmds, cmds',
110 'print("mirror ok")'
111 ].join('\n');
112 const r = spawnSync('python3', ['-c', py], {
113 cwd: path.join(__dirname, '..'),
114 encoding: 'utf8'
115 });
116 assert.equal(r.status, 0, r.stdout + r.stderr);
117 assert.match(r.stdout, /mirror ok/);
118 });
119});