|
| 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 |
0 commit comments