-
Notifications
You must be signed in to change notification settings - Fork 887
Expand file tree
/
Copy pathtest_secured.py
More file actions
71 lines (53 loc) · 2.17 KB
/
test_secured.py
File metadata and controls
71 lines (53 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import urllib.request
import urllib.error
import json
import pytest
from e2b import Sandbox
@pytest.mark.skip_debug()
async def test_download_url_with_signing(template):
sbx = Sandbox(template, timeout=100, secure=True)
file_path = "test_download_url_with_signing.txt"
file_content = "This file will be watched."
try:
sbx.files.write(file_path, file_content)
signed_url = sbx.download_url(file_path, "user")
with urllib.request.urlopen(signed_url) as resp:
assert resp.status == 200
body_bytes = resp.read()
body_text = body_bytes.decode()
assert body_text == file_content
finally:
sbx.kill()
@pytest.mark.skip_debug()
async def test_download_url_with_signing_and_expiration(template):
sbx = Sandbox(template, timeout=100, secure=True)
file_path = "test_download_url_with_signing.txt"
file_content = "This file will be watched."
try:
sbx.files.write(file_path, file_content)
signed_url = sbx.download_url(file_path, "user", 120)
with urllib.request.urlopen(signed_url) as resp:
assert resp.status == 200
body_bytes = resp.read()
body_text = body_bytes.decode()
assert body_text == file_content
finally:
sbx.kill()
@pytest.mark.skip_debug()
async def test_download_url_with_expired_signing(template):
sbx = Sandbox(template, timeout=100, secure=True)
file_path = "test_download_url_with_signing.txt"
file_content = "This file will be watched."
try:
sbx.files.write(file_path, file_content)
signed_url = sbx.download_url(file_path, "user", use_signature_expiration=-120)
with pytest.raises(urllib.error.HTTPError) as exc_info:
urllib.request.urlopen(signed_url)
err = exc_info.value
assert err.code == 401, f"Unexpected status {err.code}"
error_json_str = err.read().decode() # bytes ➜ str
error_payload = json.loads(error_json_str) # str ➜ dict
expected_payload = {"code": 401, "message": "signature is already expired"}
assert error_payload == expected_payload
finally:
sbx.kill()