Skip to content

Commit aa5d7f6

Browse files
committed
Add unit tests for create_secret_data
1 parent 500c6d1 commit aa5d7f6

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

tests/unit/test_secrets.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2023 Canonical Ltd.
2+
# Licensed under the Apache V2, see LICENCE file for details.
3+
4+
from pathlib import Path
5+
import tempfile
6+
import unittest
7+
import pytest
8+
9+
from juju.secrets import create_secret_data
10+
11+
12+
class TestCreateSecretData(unittest.TestCase):
13+
def test_bad_key(self):
14+
with pytest.raises(ValueError):
15+
create_secret_data(["foo"])
16+
with pytest.raises(ValueError):
17+
create_secret_data(["=bar"])
18+
19+
def test_good_key_values(self):
20+
self.assertDictEqual(create_secret_data(["foo=bar", "hello=world", "goodbye#base64=world"]),
21+
{
22+
"foo": "YmFy",
23+
"hello": "d29ybGQ=",
24+
"goodbye": "world"})
25+
26+
def test_key_content_too_large(self):
27+
with pytest.raises(ValueError):
28+
create_secret_data(["foo=" + ('a' * 8 * 1024)])
29+
30+
def test_total_content_too_large(self):
31+
args = []
32+
content = 'a' * 4 * 1024
33+
for i in range(20):
34+
args.append(f'key{i}={content}')
35+
with pytest.raises(ValueError):
36+
create_secret_data(args)
37+
38+
def test_secret_key_from_file(self):
39+
content = """
40+
-----BEGIN CERTIFICATE-----
41+
MIIFYjCCA0qgAwIBAgIQKaPND9YggIG6+jOcgmpk3DANBgkqhkiG9w0BAQsFADAz
42+
MRwwGgYDVQQKExNsaW51eGNvbnRhaW5lcnMub3JnMRMwEQYDVQQDDAp0aW1AZWx3
43+
-----END CERTIFICATE-----"""[1:]
44+
45+
with tempfile.TemporaryDirectory() as temp_dir:
46+
dir_path = Path(temp_dir)
47+
file_path = dir_path / "secret-data.bin"
48+
49+
with open(file_path, "w") as file:
50+
file.write(content)
51+
52+
data = create_secret_data(["key1=value1", f"key2#file={file_path}"])
53+
54+
self.assertDictEqual(data, {
55+
"key1": "dmFsdWUx",
56+
"key2": (
57+
'ICAgICAgICAgIC0tLS0tQkVHSU4gQ0VSVElGSUNBVEUtLS0tLQogICAgICAgICAgTUlJRllqQ0NBMHFnQXdJQkFnSVFLYVBORDlZZ2dJRzYrak9jZ21wazNEQU5CZ2txaGtpRzl3MEJBUXNGQURBegogICAgICAgICAgTVJ3d0dnWURWUVFLRXhOc2FXNTFlR052Ym5SaGFXNWxjbk11YjNKbk1STXdFUVlEVlFRRERBcDBhVzFBWld4MwogICAgICAgICAgLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQ=='
58+
),
59+
})

0 commit comments

Comments
 (0)