1
- from httpx import Response
1
+ import os
2
+ import pytest
3
+ from httpx import Response , Client , AsyncClient , Timeout
2
4
import onepasswordconnectsdk
3
5
from onepasswordconnectsdk import client
6
+ from onepasswordconnectsdk .config import ClientConfig , AsyncClientConfig
7
+ from onepasswordconnectsdk .utils import get_timeout
8
+
9
+ # Optional cert path for SSL verification in tests
10
+ # Set this to a valid cert path if available, otherwise verification will be disabled
11
+ CERT_PATH = os .getenv ('TEST_CERT_PATH' , False )
4
12
5
13
VAULT_ID = "abcdefghijklmnopqrstuvwxyz"
6
14
ITEM_NAME1 = "TEST USER"
9
17
ITEM_ID2 = "wepiqdxdzncjtnvmv5fegud4q2"
10
18
HOST = "https://mock_host"
11
19
TOKEN = "jwt_token"
12
- SS_CLIENT = client .new_client (HOST , TOKEN )
20
+
21
+ # Create client config
22
+ client_config = ClientConfig (url = HOST , token = TOKEN )
23
+ SS_CLIENT = client .Client (client_config )
13
24
14
25
USERNAME_VALUE = "new_user"
15
26
PASSWORD_VALUE = "password"
16
27
HOST_VALUE = "http://somehost"
17
28
29
+ def test_client_config_initialization ():
30
+ """Test ClientConfig initialization and inheritance"""
31
+ config = ClientConfig (url = HOST , token = TOKEN )
32
+ assert isinstance (config , Client )
33
+ assert config .url == HOST
34
+ assert config .token == TOKEN
35
+ assert config .timeout == get_timeout ()
36
+
37
+ def test_async_client_config_initialization ():
38
+ """Test AsyncClientConfig initialization and inheritance"""
39
+ config = AsyncClientConfig (url = HOST , token = TOKEN )
40
+ assert isinstance (config , AsyncClient )
41
+ assert config .url == HOST
42
+ assert config .token == TOKEN
43
+ assert config .timeout == get_timeout ()
44
+
45
+ def test_client_config_with_options ():
46
+ """Test ClientConfig with httpx options
47
+
48
+ Examples of certificate configuration:
49
+ # Using a certificate file
50
+ config = ClientConfig(url=HOST, token=TOKEN, verify="path/to/cert.pem")
51
+
52
+ # Using a certificate bundle
53
+ config = ClientConfig(url=HOST, token=TOKEN, verify="/etc/ssl/certs")
54
+
55
+ # Disable certificate verification (not recommended for production)
56
+ config = ClientConfig(url=HOST, token=TOKEN, verify=False)
57
+
58
+ # Custom client certificate
59
+ config = ClientConfig(
60
+ url=HOST,
61
+ token=TOKEN,
62
+ cert=("path/to/client.crt", "path/to/client.key")
63
+ )
64
+ """
65
+ # Test basic options
66
+ custom_timeout = 30.0
67
+ config = ClientConfig (
68
+ url = HOST ,
69
+ token = TOKEN ,
70
+ timeout = custom_timeout ,
71
+ verify = CERT_PATH , # Use configured cert path or disable verification
72
+ cert = None , # Client certificate (if needed)
73
+ follow_redirects = True
74
+ )
75
+ assert isinstance (config .timeout , Timeout )
76
+ assert config .timeout == Timeout (custom_timeout )
77
+ # If we got here without an error, the verify parameter was accepted
78
+ assert config .follow_redirects is True
79
+
80
+
81
+ # Just verify that these configurations are accepted without error
82
+ ClientConfig (
83
+ url = HOST ,
84
+ token = TOKEN ,
85
+ verify = False # Disable SSL verification
86
+ )
87
+
88
+
89
+
90
+ def test_async_client_config_with_options ():
91
+ """Test AsyncClientConfig with httpx options
92
+
93
+ Examples of certificate configuration:
94
+ # Using a certificate file
95
+ config = AsyncClientConfig(url=HOST, token=TOKEN, verify="path/to/cert.pem")
96
+
97
+ # Using a certificate bundle
98
+ config = AsyncClientConfig(url=HOST, token=TOKEN, verify="/etc/ssl/certs")
99
+
100
+ # Disable certificate verification (not recommended for production)
101
+ config = AsyncClientConfig(url=HOST, token=TOKEN, verify=False)
102
+
103
+ # Custom client certificate
104
+ config = AsyncClientConfig(
105
+ url=HOST,
106
+ token=TOKEN,
107
+ cert=("path/to/client.crt", "path/to/client.key")
108
+ )
109
+ """
110
+ custom_timeout = 30.0
111
+ config = AsyncClientConfig (
112
+ url = HOST ,
113
+ token = TOKEN ,
114
+ timeout = custom_timeout ,
115
+ verify = CERT_PATH , # Use configured cert path or disable verification
116
+ cert = None , # Client certificate (if needed)
117
+ follow_redirects = True
118
+ )
119
+ assert isinstance (config .timeout , Timeout )
120
+ assert config .timeout == Timeout (custom_timeout )
121
+ # If we got here without an error, the verify parameter was accepted
122
+ assert config .follow_redirects is True
123
+
124
+ # Just verify that these configurations are accepted without error
125
+ AsyncClientConfig (
126
+ url = HOST ,
127
+ token = TOKEN ,
128
+ verify = False # Disable SSL verification
129
+ )
130
+
131
+ # Note: Client certificate tests are skipped as they require actual certificate files
132
+
18
133
19
134
class Config :
20
135
username : f'opitem:"{ ITEM_NAME1 } " opfield:.username opvault:{ VAULT_ID } ' = None
@@ -25,6 +140,42 @@ class Config:
25
140
CONFIG_CLASS = Config ()
26
141
27
142
143
+ def test_client_config_errors ():
144
+ """Test ClientConfig error cases"""
145
+ with pytest .raises (TypeError ):
146
+ ClientConfig () # Missing required arguments
147
+
148
+ with pytest .raises (TypeError ):
149
+ ClientConfig (url = HOST ) # Missing token
150
+
151
+ with pytest .raises (TypeError ):
152
+ ClientConfig (token = TOKEN ) # Missing url
153
+
154
+ def test_async_client_config_errors ():
155
+ """Test AsyncClientConfig error cases"""
156
+ with pytest .raises (TypeError ):
157
+ AsyncClientConfig () # Missing required arguments
158
+
159
+ with pytest .raises (TypeError ):
160
+ AsyncClientConfig (url = HOST ) # Missing token
161
+
162
+ with pytest .raises (TypeError ):
163
+ AsyncClientConfig (token = TOKEN ) # Missing url
164
+
165
+ def test_client_config_headers ():
166
+ """Test that client config properly handles headers"""
167
+ config = ClientConfig (
168
+ url = HOST ,
169
+ token = TOKEN ,
170
+ headers = {"Custom-Header" : "test" }
171
+ )
172
+ # Headers should be merged, not overwritten
173
+ assert "Custom-Header" in config .headers
174
+ assert config .headers ["Custom-Header" ] == "test"
175
+ # Authorization header should still be present
176
+ assert "Authorization" in config .headers
177
+
178
+
28
179
def test_load (respx_mock ):
29
180
mock_items_list1 = respx_mock .get (f"v1/vaults/{ VAULT_ID } /items?filter=title eq \" { ITEM_NAME1 } \" " ).mock (
30
181
return_value = Response (200 , json = [item ])
0 commit comments