Skip to content

Commit 84c222f

Browse files
committed
unit tests
1 parent fcfba5b commit 84c222f

1 file changed

Lines changed: 63 additions & 16 deletions

File tree

test/src/test_users.py

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
import pytest
1212
import base64
13-
from unittest.mock import MagicMock, patch
13+
from unittest.mock import MagicMock, patch, call
1414
from pytest import fixture
1515

16+
import os
17+
1618
from mas.devops.users import MASUserUtils
1719

1820
SUPERUSER_USERNAME = "superuser_username"
@@ -38,6 +40,8 @@
3840
MAS_ADMIN_URL = f"https://admin-dashboard.{MAS_CORE_NAMESPACE}.svc.cluster.local:{ADMIN_DASHBOARD_PORT}"
3941
MAS_API_URL = f'https://coreapi.{MAS_CORE_NAMESPACE}.svc.cluster.local:{COREAPI_PORT}'
4042

43+
PEM_PATH = "pempath"
44+
4145

4246
def get_secret(name, namespace):
4347
if name == f"{MAS_INSTANCE_ID}-credentials-superuser":
@@ -68,6 +72,21 @@ def get_secret(name, namespace):
6872
)
6973

7074

75+
@fixture
76+
def mock_atexit():
77+
with patch('atexit.register') as mock_atexit:
78+
yield mock_atexit
79+
80+
81+
@fixture
82+
def mock_named_temporary_file(mock_atexit):
83+
with patch('tempfile.NamedTemporaryFile') as mock_named_temporary_file:
84+
mock_file = MagicMock()
85+
mock_file.name = PEM_PATH
86+
mock_named_temporary_file.return_value.__enter__.return_value = mock_file
87+
yield mock_file
88+
89+
7190
@fixture
7291
def mock_v1_secrets():
7392
with patch('mas.devops.users.DynamicClient') as mock_DynamicClientCls:
@@ -78,7 +97,7 @@ def mock_v1_secrets():
7897

7998

8099
@fixture
81-
def user_utils(mock_v1_secrets, requests_mock):
100+
def user_utils(mock_v1_secrets, requests_mock, mock_named_temporary_file, mock_atexit):
82101
user_utils = MASUserUtils(
83102
MAS_INSTANCE_ID,
84103
MAS_WORKSPACE_ID,
@@ -90,6 +109,17 @@ def user_utils(mock_v1_secrets, requests_mock):
90109
yield user_utils
91110

92111

112+
def test_admin_internal_ca_pem_file_path(user_utils, mock_named_temporary_file, mock_atexit):
113+
assert str(user_utils.admin_internal_ca_pem_file_path) == PEM_PATH
114+
assert mock_named_temporary_file.mock_calls == [call.write(ADMINDASHBOARD_CA_CRT.encode()), call.flush(), call.close()]
115+
assert mock_atexit.mock_calls == [call(os.remove, PEM_PATH)]
116+
117+
# verify caching
118+
assert str(user_utils.admin_internal_ca_pem_file_path) == PEM_PATH
119+
assert mock_named_temporary_file.mock_calls == [call.write(ADMINDASHBOARD_CA_CRT.encode()), call.flush(), call.close()]
120+
assert mock_atexit.mock_calls == [call(os.remove, PEM_PATH)]
121+
122+
93123
def mock_get_user(requests_mock, user_id, json, status_code):
94124
return requests_mock.get(
95125
f"{MAS_API_URL}/v3/users/{user_id}",
@@ -134,11 +164,6 @@ def test_admin_internal_tls_secret(user_utils, mock_v1_secrets):
134164
assert mock_v1_secrets.get.call_count == 1
135165

136166

137-
def test_admin_internal_ca_pem_file_path():
138-
pass
139-
# TODO
140-
141-
142167
def test_core_internal_tls_secret(user_utils, mock_v1_secrets):
143168
assert mock_v1_secrets.get.call_count == 0
144169
assert user_utils.core_internal_tls_secret.data["ca.crt"] == base64.b64encode(COREAPI_CA_CRT.encode('utf-8'))
@@ -147,9 +172,19 @@ def test_core_internal_tls_secret(user_utils, mock_v1_secrets):
147172
assert mock_v1_secrets.get.call_count == 1
148173

149174

150-
def test_core_internal_ca_pem_file_path():
151-
pass
152-
# TODO
175+
def test_core_internal_ca_pem_file_path(user_utils, mock_named_temporary_file, mock_atexit):
176+
'''
177+
Check the correct content is written to core_internal_ca_pem_file_path tempfile, that an exit handler is registered to
178+
delete the temp file, and that the tempfile is only written once (with its path cached)
179+
'''
180+
assert str(user_utils.core_internal_ca_pem_file_path) == PEM_PATH
181+
assert mock_named_temporary_file.mock_calls == [call.write(COREAPI_CA_CRT.encode()), call.flush(), call.close()]
182+
assert mock_atexit.mock_calls == [call(os.remove, PEM_PATH)]
183+
184+
# verify caching
185+
assert str(user_utils.core_internal_ca_pem_file_path) == PEM_PATH
186+
assert mock_named_temporary_file.mock_calls == [call.write(COREAPI_CA_CRT.encode()), call.flush(), call.close()]
187+
assert mock_atexit.mock_calls == [call(os.remove, PEM_PATH)]
153188

154189

155190
def test_superuser_auth_token():
@@ -169,14 +204,26 @@ def test_manage_internal_tls_secret(user_utils, mock_v1_secrets):
169204
assert mock_v1_secrets.get.call_count == 1
170205

171206

172-
def test_manage_internal_client_pem_file_path():
173-
pass
174-
# TODO
207+
def test_manage_internal_client_pem_file_path(user_utils, mock_named_temporary_file, mock_atexit):
208+
assert str(user_utils.manage_internal_client_pem_file_path) == PEM_PATH
209+
assert mock_named_temporary_file.mock_calls == [call.write(MANAGE_TLS_KEY.encode()), call.write(MANAGE_TLS_CRT.encode()), call.flush(), call.close()]
210+
assert mock_atexit.mock_calls == [call(os.remove, PEM_PATH)]
175211

212+
# verify caching
213+
assert str(user_utils.manage_internal_client_pem_file_path) == PEM_PATH
214+
assert mock_named_temporary_file.mock_calls == [call.write(MANAGE_TLS_KEY.encode()), call.write(MANAGE_TLS_CRT.encode()), call.flush(), call.close()]
215+
assert mock_atexit.mock_calls == [call(os.remove, PEM_PATH)]
176216

177-
def test_manage_internal_ca_pem_file_path():
178-
pass
179-
# TODO
217+
218+
def test_manage_internal_ca_pem_file_path(user_utils, mock_named_temporary_file, mock_atexit):
219+
assert str(user_utils.manage_internal_ca_pem_file_path) == PEM_PATH
220+
assert mock_named_temporary_file.mock_calls == [call.write(MANAGE_CA_CRT.encode()), call.flush(), call.close()]
221+
assert mock_atexit.mock_calls == [call(os.remove, PEM_PATH)]
222+
223+
# verify caching
224+
assert str(user_utils.manage_internal_ca_pem_file_path) == PEM_PATH
225+
assert mock_named_temporary_file.mock_calls == [call.write(MANAGE_CA_CRT.encode()), call.flush(), call.close()]
226+
assert mock_atexit.mock_calls == [call(os.remove, PEM_PATH)]
180227

181228

182229
def test_manage_maxadmin_api_key():

0 commit comments

Comments
 (0)