This repository was archived by the owner on Mar 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_init.py
More file actions
129 lines (100 loc) · 4.33 KB
/
test_init.py
File metadata and controls
129 lines (100 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""Tests for the convenience functions in agentvisa.__init__."""
import pytest
import responses
import agentvisa
from agentvisa import AgentVisaClient
class TestInitModule:
"""Test cases for agentvisa module convenience functions."""
def setup_method(self):
"""Reset the global client before each test."""
agentvisa.default_client = None
def test_init_function(self, api_key):
"""Test the init function creates a global client."""
agentvisa.init(api_key=api_key)
assert agentvisa.default_client is not None
assert isinstance(agentvisa.default_client, AgentVisaClient)
assert agentvisa.default_client.api_key == api_key
def test_init_function_overwrites_existing_client(self, api_key):
"""Test that init overwrites an existing client."""
# Initialize first client
agentvisa.init(api_key="first_key")
first_client = agentvisa.default_client
# Initialize second client
agentvisa.init(api_key=api_key)
second_client = agentvisa.default_client
assert first_client is not second_client
assert second_client.api_key == api_key
@responses.activate
def test_create_delegation_success(self, api_key, sample_delegation_response):
"""Test successful delegation creation using convenience function."""
# Initialize the SDK
agentvisa.init(api_key=api_key)
# Mock the API response
responses.add(
responses.POST,
f"{agentvisa.default_client.base_url}/agents",
json=sample_delegation_response,
status=201,
)
result = agentvisa.create_delegation(
end_user_identifier="user123", scopes=["read", "write"], expires_in=3600
)
assert result == sample_delegation_response
assert len(responses.calls) == 1
@responses.activate
def test_create_delegation_with_defaults(self, api_key, sample_delegation_response):
"""Test delegation creation with default parameters."""
agentvisa.init(api_key=api_key)
responses.add(
responses.POST,
f"{agentvisa.default_client.base_url}/agents",
json=sample_delegation_response,
status=201,
)
result = agentvisa.create_delegation(
end_user_identifier="user123", scopes=["read", "write"]
)
assert result == sample_delegation_response
# Check default expires_in was used
import json
payload = json.loads(responses.calls[0].request.body)
assert payload["expires_in"] == 3600
def test_create_delegation_without_init(self):
"""Test delegation creation fails when init hasn't been called."""
with pytest.raises(Exception, match="Please call agentvisa.init"):
agentvisa.create_delegation(
end_user_identifier="user123", scopes=["read", "write"]
)
def test_create_delegation_after_reset(self, api_key):
"""Test delegation creation fails after resetting default_client."""
agentvisa.init(api_key=api_key)
agentvisa.default_client = None # Simulate reset
with pytest.raises(Exception, match="Please call agentvisa.init"):
agentvisa.create_delegation(
end_user_identifier="user123", scopes=["read", "write"]
)
@responses.activate
def test_create_delegation_passes_all_parameters(
self, api_key, sample_delegation_response
):
"""Test that create_delegation passes all parameters correctly."""
agentvisa.init(api_key=api_key)
responses.add(
responses.POST,
f"{agentvisa.default_client.base_url}/agents",
json=sample_delegation_response,
status=201,
)
end_user_id = "test_user_456"
scopes = ["admin", "read", "write"]
expires_in = 7200
agentvisa.create_delegation(
end_user_identifier=end_user_id, scopes=scopes, expires_in=expires_in
)
# Verify all parameters were passed correctly
import json
payload = json.loads(responses.calls[0].request.body)
assert payload["type"] == "ephemeral"
assert payload["end_user_identifier"] == end_user_id
assert payload["scopes"] == scopes
assert payload["expires_in"] == expires_in