|
1 | | -import { assert, test } from 'vitest' |
| 1 | +import { assert, describe } from 'vitest' |
2 | 2 |
|
3 | | -import { Sandbox } from '../../../src' |
4 | | -import { template, isDebug } from '../../setup' |
| 3 | +import { sandboxTest, isDebug } from '../../setup' |
5 | 4 |
|
6 | | -const timeout = 20 * 1000 |
7 | | - |
8 | | -test.skipIf(isDebug)('test access file with expired signing', async () => { |
9 | | - const sbx = await Sandbox.create(template, { |
10 | | - timeoutMs: timeout, |
11 | | - secure: true, |
12 | | - }) |
13 | | - await sbx.files.write('hello.txt', 'hello world') |
14 | | - |
15 | | - const fileUrlWithSigning = await sbx.downloadUrl('hello.txt', { |
16 | | - useSignatureExpiration: -10_000, |
17 | | - }) |
18 | | - |
19 | | - const res = await fetch(fileUrlWithSigning) |
20 | | - const resBody = await res.text() |
21 | | - const resStatus = res.status |
22 | | - |
23 | | - assert.equal(resStatus, 401) |
24 | | - assert.deepEqual(JSON.parse(resBody), { |
25 | | - code: 401, |
26 | | - message: 'signature is already expired', |
27 | | - }) |
28 | | - |
29 | | - await sbx.kill() |
30 | | -}) |
31 | | - |
32 | | -test.skipIf(isDebug)('test access file with valid signing', async () => { |
33 | | - const sbx = await Sandbox.create(template, { |
34 | | - timeoutMs: timeout, |
35 | | - secure: true, |
36 | | - }) |
37 | | - await sbx.files.write('hello.txt', 'hello world') |
38 | | - |
39 | | - const fileUrlWithSigning = await sbx.downloadUrl('hello.txt', { |
40 | | - useSignatureExpiration: 10_000, |
41 | | - }) |
42 | | - |
43 | | - const res = await fetch(fileUrlWithSigning) |
44 | | - const resBody = await res.text() |
45 | | - const resStatus = res.status |
46 | | - |
47 | | - assert.equal(resStatus, 200) |
48 | | - assert.equal(resBody, 'hello world') |
49 | | - |
50 | | - await sbx.kill() |
51 | | -}) |
52 | | - |
53 | | -test.skipIf(isDebug)( |
54 | | - 'test access file with valid signing as root', |
55 | | - async () => { |
56 | | - const sbx = await Sandbox.create(template, { |
57 | | - timeoutMs: timeout, |
| 5 | +describe('file signing', () => { |
| 6 | + sandboxTest.scoped({ |
| 7 | + sandboxOpts: { |
58 | 8 | secure: true, |
59 | | - }) |
60 | | - await sbx.files.write('hello.txt', 'hello world', { user: 'root' }) |
61 | | - |
62 | | - const fileUrlWithSigning = await sbx.downloadUrl('hello.txt', { |
63 | | - user: 'root', |
64 | | - useSignatureExpiration: 10_000, |
65 | | - }) |
66 | | - |
67 | | - const res = await fetch(fileUrlWithSigning) |
68 | | - const resBody = await res.text() |
69 | | - const resStatus = res.status |
70 | | - |
71 | | - assert.equal(resStatus, 200) |
72 | | - assert.equal(resBody, 'hello world') |
73 | | - |
74 | | - await sbx.kill() |
75 | | - } |
76 | | -) |
77 | | - |
78 | | -test.skipIf(isDebug)('test upload file with valid signing', async () => { |
79 | | - const sbx = await Sandbox.create(template, { |
80 | | - timeoutMs: timeout, |
81 | | - secure: true, |
82 | | - }) |
83 | | - const fileUrlWithSigning = await sbx.uploadUrl('hello.txt', { |
84 | | - useSignatureExpiration: 10_000, |
85 | | - }) |
86 | | - |
87 | | - const form = new FormData() |
88 | | - form.append('file', 'file content') |
89 | | - |
90 | | - const res = await fetch(fileUrlWithSigning, { method: 'POST', body: form }) |
91 | | - const resBody = await res.text() |
92 | | - const resStatus = res.status |
93 | | - |
94 | | - assert.equal(resStatus, 200) |
95 | | - assert.deepEqual(JSON.parse(resBody), [ |
96 | | - { name: 'hello.txt', path: '/home/user/hello.txt', type: 'file' }, |
97 | | - ]) |
98 | | - |
99 | | - await sbx.kill() |
100 | | -}) |
101 | | - |
102 | | -test.skipIf(isDebug)( |
103 | | - 'test upload file with valid signing as root user', |
104 | | - async () => { |
105 | | - const sbx = await Sandbox.create(template, { |
106 | | - timeoutMs: timeout, |
107 | | - secure: true, |
108 | | - }) |
109 | | - |
110 | | - const fileUrlWithSigning = await sbx.uploadUrl('hello.txt', { |
111 | | - user: 'root', |
112 | | - useSignatureExpiration: 10_000, |
113 | | - }) |
114 | | - |
115 | | - const form = new FormData() |
116 | | - form.append('file', 'file content') |
117 | | - |
118 | | - const res = await fetch(fileUrlWithSigning, { method: 'POST', body: form }) |
119 | | - const resBody = await res.text() |
120 | | - const resStatus = res.status |
121 | | - |
122 | | - assert.equal(resStatus, 200) |
123 | | - assert.deepEqual(JSON.parse(resBody), [ |
124 | | - { name: 'hello.txt', path: '/root/hello.txt', type: 'file' }, |
125 | | - ]) |
126 | | - |
127 | | - await sbx.kill() |
128 | | - } |
129 | | -) |
130 | | - |
131 | | -test.skipIf(isDebug)('test upload file with invalid signing', async () => { |
132 | | - const sbx = await Sandbox.create(template, { |
133 | | - timeoutMs: timeout, |
134 | | - secure: true, |
135 | | - }) |
136 | | - const fileUrlWithSigning = await sbx.uploadUrl('hello.txt', { |
137 | | - useSignatureExpiration: -100_000, |
138 | | - }) |
139 | | - |
140 | | - const form = new FormData() |
141 | | - form.append('file', 'file content') |
142 | | - |
143 | | - const res = await fetch(fileUrlWithSigning, { method: 'POST', body: form }) |
144 | | - const resBody = await res.text() |
145 | | - const resStatus = res.status |
146 | | - |
147 | | - assert.equal(resStatus, 401) |
148 | | - assert.deepEqual(JSON.parse(resBody), { |
149 | | - code: 401, |
150 | | - message: 'signature is already expired', |
151 | | - }) |
152 | | - |
153 | | - await sbx.kill() |
154 | | -}) |
155 | | - |
156 | | -test.skipIf(isDebug)('test command run with secured sbx', async () => { |
157 | | - const sbx = await Sandbox.create(template, { |
158 | | - timeoutMs: timeout, |
159 | | - secure: true, |
| 9 | + }, |
160 | 10 | }) |
161 | | - const response = await sbx.commands.run('echo Hello World!') |
162 | 11 |
|
163 | | - assert.equal(response.stdout, 'Hello World!\n') |
| 12 | + sandboxTest.skipIf(isDebug)( |
| 13 | + 'test access file with expired signing', |
| 14 | + async ({ sandbox }) => { |
| 15 | + await sandbox.files.write('hello.txt', 'hello world') |
| 16 | + |
| 17 | + const fileUrlWithSigning = await sandbox.downloadUrl('hello.txt', { |
| 18 | + useSignatureExpiration: -10_000, |
| 19 | + }) |
| 20 | + |
| 21 | + const res = await fetch(fileUrlWithSigning) |
| 22 | + const resBody = await res.text() |
| 23 | + const resStatus = res.status |
| 24 | + |
| 25 | + assert.equal(resStatus, 401) |
| 26 | + assert.deepEqual(JSON.parse(resBody), { |
| 27 | + code: 401, |
| 28 | + message: 'signature is already expired', |
| 29 | + }) |
| 30 | + } |
| 31 | + ) |
| 32 | + |
| 33 | + sandboxTest.skipIf(isDebug)( |
| 34 | + 'test access file with valid signing', |
| 35 | + async ({ sandbox }) => { |
| 36 | + await sandbox.files.write('hello.txt', 'hello world') |
| 37 | + |
| 38 | + const fileUrlWithSigning = await sandbox.downloadUrl('hello.txt', { |
| 39 | + useSignatureExpiration: 10_000, |
| 40 | + }) |
| 41 | + |
| 42 | + const res = await fetch(fileUrlWithSigning) |
| 43 | + const resBody = await res.text() |
| 44 | + const resStatus = res.status |
| 45 | + |
| 46 | + assert.equal(resStatus, 200) |
| 47 | + assert.equal(resBody, 'hello world') |
| 48 | + } |
| 49 | + ) |
| 50 | + |
| 51 | + sandboxTest.skipIf(isDebug)( |
| 52 | + 'test access file with valid signing as root', |
| 53 | + async ({ sandbox }) => { |
| 54 | + await sandbox.files.write('hello.txt', 'hello world', { user: 'root' }) |
| 55 | + |
| 56 | + const fileUrlWithSigning = await sandbox.downloadUrl('hello.txt', { |
| 57 | + user: 'root', |
| 58 | + useSignatureExpiration: 10_000, |
| 59 | + }) |
| 60 | + |
| 61 | + const res = await fetch(fileUrlWithSigning) |
| 62 | + const resBody = await res.text() |
| 63 | + const resStatus = res.status |
| 64 | + |
| 65 | + assert.equal(resStatus, 200) |
| 66 | + assert.equal(resBody, 'hello world') |
| 67 | + } |
| 68 | + ) |
| 69 | + |
| 70 | + sandboxTest.skipIf(isDebug)( |
| 71 | + 'test upload file with valid signing', |
| 72 | + async ({ sandbox }) => { |
| 73 | + const fileUrlWithSigning = await sandbox.uploadUrl('hello.txt', { |
| 74 | + useSignatureExpiration: 10_000, |
| 75 | + }) |
| 76 | + |
| 77 | + const form = new FormData() |
| 78 | + form.append('file', 'file content') |
| 79 | + |
| 80 | + const res = await fetch(fileUrlWithSigning, { |
| 81 | + method: 'POST', |
| 82 | + body: form, |
| 83 | + }) |
| 84 | + const resBody = await res.text() |
| 85 | + const resStatus = res.status |
| 86 | + |
| 87 | + assert.equal(resStatus, 200) |
| 88 | + assert.deepEqual(JSON.parse(resBody), [ |
| 89 | + { name: 'hello.txt', path: '/home/user/hello.txt', type: 'file' }, |
| 90 | + ]) |
| 91 | + } |
| 92 | + ) |
| 93 | + |
| 94 | + sandboxTest.skipIf(isDebug)( |
| 95 | + 'test upload file with valid signing as root user', |
| 96 | + async ({ sandbox }) => { |
| 97 | + const fileUrlWithSigning = await sandbox.uploadUrl('hello.txt', { |
| 98 | + user: 'root', |
| 99 | + useSignatureExpiration: 10_000, |
| 100 | + }) |
| 101 | + |
| 102 | + const form = new FormData() |
| 103 | + form.append('file', 'file content') |
| 104 | + |
| 105 | + const res = await fetch(fileUrlWithSigning, { |
| 106 | + method: 'POST', |
| 107 | + body: form, |
| 108 | + }) |
| 109 | + const resBody = await res.text() |
| 110 | + const resStatus = res.status |
| 111 | + |
| 112 | + assert.equal(resStatus, 200) |
| 113 | + assert.deepEqual(JSON.parse(resBody), [ |
| 114 | + { name: 'hello.txt', path: '/root/hello.txt', type: 'file' }, |
| 115 | + ]) |
| 116 | + } |
| 117 | + ) |
| 118 | + |
| 119 | + sandboxTest.skipIf(isDebug)( |
| 120 | + 'test upload file with invalid signing', |
| 121 | + async ({ sandbox }) => { |
| 122 | + const fileUrlWithSigning = await sandbox.uploadUrl('hello.txt', { |
| 123 | + useSignatureExpiration: -100_000, |
| 124 | + }) |
| 125 | + |
| 126 | + const form = new FormData() |
| 127 | + form.append('file', 'file content') |
| 128 | + |
| 129 | + const res = await fetch(fileUrlWithSigning, { |
| 130 | + method: 'POST', |
| 131 | + body: form, |
| 132 | + }) |
| 133 | + const resBody = await res.text() |
| 134 | + const resStatus = res.status |
| 135 | + |
| 136 | + assert.equal(resStatus, 401) |
| 137 | + assert.deepEqual(JSON.parse(resBody), { |
| 138 | + code: 401, |
| 139 | + message: 'signature is already expired', |
| 140 | + }) |
| 141 | + } |
| 142 | + ) |
| 143 | + |
| 144 | + sandboxTest.skipIf(isDebug)( |
| 145 | + 'test command run with secured sbx', |
| 146 | + async ({ sandbox }) => { |
| 147 | + const response = await sandbox.commands.run('echo Hello World!') |
| 148 | + |
| 149 | + assert.equal(response.stdout, 'Hello World!\n') |
| 150 | + } |
| 151 | + ) |
164 | 152 | }) |
0 commit comments