Skip to content

Commit 4bec911

Browse files
Rasmus Oscar Welanderglpatcern
Rasmus Oscar Welander
authored andcommitted
Added test fixtures, test dependencies and tests for the cs3client class
1 parent 7d89902 commit 4bec911

File tree

3 files changed

+257
-0
lines changed

3 files changed

+257
-0
lines changed

tests/fixtures.py

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
"""
2+
fixtures.py
3+
4+
Contains the fixtures used in the tests.
5+
6+
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
7+
8+
Last updated: 26/07/2024
9+
10+
"""
11+
12+
import pytest
13+
from unittest.mock import Mock, patch
14+
from configparser import ConfigParser
15+
import cs3.rpc.v1beta1.code_pb2 as cs3code
16+
from cs3client import CS3Client
17+
from file import File
18+
from auth import Auth
19+
from config import Config
20+
import base64
21+
import json
22+
23+
24+
@pytest.fixture
25+
def mock_config():
26+
config = ConfigParser()
27+
config["cs3client"] = {
28+
# client parameters
29+
"host": "test_host:port",
30+
"grpc_timeout": "10",
31+
"chunk_size": "4194304",
32+
"http_timeout": "10",
33+
# TUS parameters
34+
"tus_enabled": "False",
35+
# Authentication parameters
36+
"auth_login_type": "basic",
37+
"auth_client_id": "einstein",
38+
# SSL parameters
39+
"ssl_enabled": "True",
40+
"ssl_verify": "True",
41+
"ssl_ca_cert": "test_ca_cert",
42+
"ssl_client_key": "test_client_key",
43+
"ssl_client_cert": "test_client_cert",
44+
# Lock parameters
45+
"lock_not_impl": "False",
46+
"lock_by_setting_attr": "False",
47+
"lock_expiration": "1800",
48+
}
49+
return config
50+
51+
52+
def create_mock_jwt():
53+
header = base64.urlsafe_b64encode(json.dumps({"alg": "HS256", "typ": "JWT"}).encode()).decode().strip("=")
54+
payload = (
55+
base64.urlsafe_b64encode(json.dumps({"sub": "1234567890", "name": "John Doe", "iat": 1516239022}).encode())
56+
.decode()
57+
.strip("=")
58+
)
59+
signature = base64.urlsafe_b64encode(b"signature").decode().strip("=")
60+
return f"{header}.{payload}.{signature}"
61+
62+
63+
@pytest.fixture
64+
def mock_logger():
65+
return Mock()
66+
67+
68+
# Here the order of patches correspond to the parameters of the function
69+
@pytest.fixture
70+
@patch("cs3.gateway.v1beta1.gateway_api_pb2_grpc.GatewayAPIStub")
71+
def mock_gateway(mock_gateway_stub_class):
72+
mock_gateway_stub = Mock()
73+
mock_gateway_stub_class.return_value = mock_gateway_stub
74+
# Set up mock response for Authenticate method
75+
mocked_token = create_mock_jwt()
76+
mock_authenticate_response = Mock()
77+
mock_authenticate_response.status.code = cs3code.CODE_OK
78+
mock_authenticate_response.status.message = ""
79+
mock_authenticate_response.token = mocked_token
80+
mock_gateway_stub.Authenticate.return_value = mock_authenticate_response
81+
return mock_gateway_stub
82+
83+
84+
# All the parameters are inferred by pytest from existing fixtures
85+
@pytest.fixture
86+
def mock_authentication(mock_gateway, mock_config, mock_logger):
87+
# Set up mock response for Authenticate method
88+
mock_authentication = Auth(Config(mock_config, "cs3client"), mock_logger, mock_gateway)
89+
mock_authentication.set_client_secret("test")
90+
return mock_authentication
91+
92+
93+
# Here the order of patches correspond to the parameters of the function
94+
# (patches are applied from the bottom up)
95+
# and the last two parameters are inferred by pytest from existing fixtures
96+
@pytest.fixture
97+
@patch("cs3client.grpc.secure_channel", autospec=True)
98+
@patch("cs3client.grpc.channel_ready_future", autospec=True)
99+
@patch("cs3client.grpc.insecure_channel", autospec=True)
100+
@patch("cs3client.cs3gw_grpc.GatewayAPIStub", autospec=True)
101+
@patch("cs3client.grpc.ssl_channel_credentials", autospec=True)
102+
def cs3_client_secure(
103+
mock_ssl_channel_credentials,
104+
mock_gateway_stub_class,
105+
mock_insecure_channel,
106+
mock_channel_ready_future,
107+
mock_secure_channel,
108+
mock_config,
109+
mock_logger,
110+
):
111+
112+
# Create CS3Client instance
113+
client = CS3Client(mock_config, "cs3client", mock_logger)
114+
client.auth.set_client_secret("test")
115+
116+
assert mock_secure_channel.called
117+
assert mock_channel_ready_future.called
118+
assert mock_ssl_channel_credentials.called
119+
assert mock_insecure_channel.assert_not_called
120+
121+
return client
122+
123+
124+
# Here the order of patches correspond to the parameters of the function
125+
# (patches are applied from the bottom up)
126+
# and the last two parameters are inferred by pytest from existing fixtures
127+
@pytest.fixture
128+
@patch("cs3client.grpc.secure_channel")
129+
@patch("cs3client.grpc.insecure_channel")
130+
@patch("cs3client.grpc.channel_ready_future")
131+
@patch("cs3client.cs3gw_grpc.GatewayAPIStub")
132+
@patch("cs3client.grpc.ssl_channel_credentials")
133+
def cs3_client_insecure(
134+
mock_ssl_channel_credentials,
135+
mock_gateway_stub_class,
136+
mock_channel_ready_future,
137+
mock_insecure_channel,
138+
mock_secure_channel,
139+
mock_config,
140+
mock_logger,
141+
):
142+
mock_config["cs3client"]["ssl_enabled"] = "False"
143+
144+
# Create CS3Client instance
145+
client = CS3Client(mock_config, "cs3client", mock_logger)
146+
client.auth.set_client_secret("test")
147+
148+
assert mock_insecure_channel.called
149+
assert mock_channel_ready_future.called
150+
assert mock_secure_channel.assert_not_called
151+
assert mock_ssl_channel_credentials.assert_not_called
152+
return client
153+
154+
155+
# All parameters are inferred by pytest from existing fixtures
156+
@pytest.fixture
157+
def file_instance(mock_authentication, mock_gateway, mock_config, mock_logger):
158+
file = File(Config(mock_config, "cs3client"), mock_logger, mock_gateway, mock_authentication)
159+
return file

tests/requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pytest
2+
pytest-cov
3+
pytest-mock
4+
coverage

tests/test_cs3client.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
test_cs3client.py
3+
4+
Tests the initialization of the CS3Client class.
5+
6+
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
7+
8+
Last updated: 26/07/2024
9+
"""
10+
11+
from fixtures import ( # noqa: F401 (they are used, the framework is not detecting it)
12+
cs3_client_insecure,
13+
cs3_client_secure,
14+
mock_config,
15+
mock_logger,
16+
mock_gateway,
17+
create_mock_jwt,
18+
)
19+
20+
21+
def test_cs3client_initialization_secure(cs3_client_secure): # noqa: F811 (not a redefinition)
22+
client = cs3_client_secure
23+
24+
# Make sure configuration is correctly set
25+
assert client._config.host == "test_host:port"
26+
assert client._config.grpc_timeout == 10
27+
assert client._config.http_timeout == 10
28+
assert client._config.chunk_size == 4194304
29+
assert client._config.auth_login_type == "basic"
30+
assert client._config.auth_client_id == "einstein"
31+
assert client._config.tus_enabled is False
32+
assert client._config.ssl_enabled is True
33+
assert client._config.ssl_verify is True
34+
assert client._config.ssl_client_cert == "test_client_cert"
35+
assert client._config.ssl_client_key == "test_client_key"
36+
assert client._config.ssl_ca_cert == "test_ca_cert"
37+
assert client._config.lock_by_setting_attr is False
38+
assert client._config.lock_not_impl is False
39+
assert client._config.lock_expiration == 1800
40+
41+
# Make sure the gRPC channel is correctly created
42+
assert client.channel is not None
43+
assert client._gateway is not None
44+
assert client.auth is not None
45+
assert client.file is not None
46+
47+
# Make sure auth objects are correctly set
48+
assert client.auth._gateway is not None
49+
assert client.auth._config is not None
50+
assert client.auth._log is not None
51+
52+
# Make sure file objects are correctly set
53+
assert client.file._gateway is not None
54+
assert client.file._auth is not None
55+
assert client.file._config is not None
56+
assert client.file._log is not None
57+
58+
59+
def test_cs3client_initialization_insecure(cs3_client_insecure): # noqa: F811 (not a redefinition)
60+
client = cs3_client_insecure
61+
62+
# Make sure configuration is correctly set
63+
assert client._config.host == "test_host:port"
64+
assert client._config.grpc_timeout == 10
65+
assert client._config.http_timeout == 10
66+
assert client._config.chunk_size == 4194304
67+
assert client._config.auth_login_type == "basic"
68+
assert client._config.auth_client_id == "einstein"
69+
assert client._config.tus_enabled is False
70+
assert client._config.ssl_enabled is False
71+
assert client._config.ssl_verify is True
72+
assert client._config.ssl_client_cert == "test_client_cert"
73+
assert client._config.ssl_client_key == "test_client_key"
74+
assert client._config.ssl_ca_cert == "test_ca_cert"
75+
assert client._config.lock_by_setting_attr is False
76+
assert client._config.lock_not_impl is False
77+
assert client._config.lock_expiration == 1800
78+
79+
# Make sure the gRPC channel is correctly created
80+
assert client.channel is not None
81+
assert client._gateway is not None
82+
assert client.auth is not None
83+
assert client.file is not None
84+
85+
# Make sure auth objects are correctly set
86+
assert client.auth._gateway is not None
87+
assert client.auth._config is not None
88+
assert client.auth._log is not None
89+
90+
# Make sure file objects are correctly set
91+
assert client.file._gateway is not None
92+
assert client.file._auth is not None
93+
assert client.file._config is not None
94+
assert client.file._log is not None

0 commit comments

Comments
 (0)