-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathtest_claims_identity.py
More file actions
195 lines (156 loc) · 7.04 KB
/
Copy pathtest_claims_identity.py
File metadata and controls
195 lines (156 loc) · 7.04 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import pytest
from microsoft_agents.hosting.core.authorization.claims_identity import ClaimsIdentity
from microsoft_agents.hosting.core.authorization.authentication_constants import (
AuthenticationConstants,
)
class TestClaimsIdentity:
"""Unit tests for ClaimsIdentity class, specifically testing get_token_scope method."""
def test_get_token_scope_agent_claim_with_app_id(self):
"""
Test get_token_scope returns correct format for agent claims.
When is_agent_claim() returns True, the scope should be "{app_id}/.default"
"""
# Setup claims that will make is_agent_claim() return True
# Requirements for agent claim:
# - version claim exists
# - audience claim exists and is not AGENTS_SDK_TOKEN_ISSUER
# - get_outgoing_app_id() returns a value
# - app_id != audience
app_id = "test-app-id-123"
audience = "different-audience-456"
claims = {
AuthenticationConstants.VERSION_CLAIM: "2.0",
AuthenticationConstants.AUDIENCE_CLAIM: audience,
AuthenticationConstants.AUTHORIZED_PARTY: app_id, # Used for version 2.0
}
claims_identity = ClaimsIdentity(
claims=claims, is_authenticated=True, authentication_type="Bearer"
)
# Verify this is an agent claim
assert claims_identity.is_agent_claim() is True
# Test get_token_scope
result = claims_identity.get_token_scope()
# Verify result format
assert isinstance(result, list)
assert len(result) == 1
assert result[0] == f"{app_id}/.default"
def test_get_token_scope_non_agent_claim(self):
"""
Test get_token_scope returns correct format for non-agent claims.
When is_agent_claim() returns False, the scope should be
"https://api.botframework.com/.default"
"""
# Setup claims that will make is_agent_claim() return False
# This can happen when:
# - version claim is missing
# - audience is AGENTS_SDK_TOKEN_ISSUER
# - app_id equals audience
app_id = "test-app-id-789"
claims = {
AuthenticationConstants.VERSION_CLAIM: "1.0",
AuthenticationConstants.AUDIENCE_CLAIM: AuthenticationConstants.AGENTS_SDK_TOKEN_ISSUER,
AuthenticationConstants.APP_ID_CLAIM: app_id, # Used for version 1.0
}
claims_identity = ClaimsIdentity(
claims=claims, is_authenticated=True, authentication_type="Bearer"
)
# Verify this is NOT an agent claim
assert claims_identity.is_agent_claim() is False
# Test get_token_scope
result = claims_identity.get_token_scope()
# Verify result format
assert isinstance(result, list)
assert len(result) == 1
assert result[0] == f"{AuthenticationConstants.AGENTS_SDK_SCOPE}/.default"
def test_get_token_scope_edge_case_no_outgoing_app_id(self):
"""
Test get_token_scope behavior when get_outgoing_app_id returns None.
This is an edge case where the app_id is not present in the claims.
"""
# Setup claims with no app_id information
claims = {
AuthenticationConstants.VERSION_CLAIM: "2.0",
AuthenticationConstants.AUDIENCE_CLAIM: "some-audience",
# No APP_ID_CLAIM or AUTHORIZED_PARTY
}
claims_identity = ClaimsIdentity(
claims=claims, is_authenticated=True, authentication_type="Bearer"
)
# Verify get_outgoing_app_id returns None
assert claims_identity.get_outgoing_app_id() is None
# Verify is_agent_claim returns False (because get_outgoing_app_id is None)
assert claims_identity.is_agent_claim() is False
# Test get_token_scope - should fall back to AGENTS_SDK_SCOPE
result = claims_identity.get_token_scope()
# Verify result format
assert isinstance(result, list)
assert len(result) == 1
assert result[0] == f"{AuthenticationConstants.AGENTS_SDK_SCOPE}/.default"
def test_get_token_scope_edge_case_missing_version(self):
"""
Test get_token_scope when version claim is missing.
This should result in is_agent_claim() returning False.
"""
app_id = "test-app-id-no-version"
claims = {
# No VERSION_CLAIM
AuthenticationConstants.AUDIENCE_CLAIM: "some-audience",
AuthenticationConstants.APP_ID_CLAIM: app_id,
}
claims_identity = ClaimsIdentity(
claims=claims, is_authenticated=True, authentication_type="Bearer"
)
# Verify is_agent_claim returns False (because version is missing)
assert claims_identity.is_agent_claim() is False
# Test get_token_scope
result = claims_identity.get_token_scope()
# Verify result format
assert isinstance(result, list)
assert len(result) == 1
assert result[0] == f"{AuthenticationConstants.AGENTS_SDK_SCOPE}/.default"
def test_get_token_scope_agent_claim_version_1_0(self):
"""
Test get_token_scope with version 1.0 token that qualifies as agent claim.
"""
app_id = "test-app-id-v1"
audience = "different-audience-v1"
claims = {
AuthenticationConstants.VERSION_CLAIM: "1.0",
AuthenticationConstants.AUDIENCE_CLAIM: audience,
AuthenticationConstants.APP_ID_CLAIM: app_id, # Used for version 1.0
}
claims_identity = ClaimsIdentity(
claims=claims, is_authenticated=True, authentication_type="Bearer"
)
# Verify this is an agent claim (app_id != audience)
assert claims_identity.is_agent_claim() is True
# Test get_token_scope
result = claims_identity.get_token_scope()
# Verify result format
assert isinstance(result, list)
assert len(result) == 1
assert result[0] == f"{app_id}/.default"
def test_get_token_scope_non_agent_claim_same_app_id_and_audience(self):
"""
Test get_token_scope when app_id equals audience.
This should result in is_agent_claim() returning False.
"""
app_id = "same-id-for-both"
claims = {
AuthenticationConstants.VERSION_CLAIM: "2.0",
AuthenticationConstants.AUDIENCE_CLAIM: app_id, # Same as app_id
AuthenticationConstants.AUTHORIZED_PARTY: app_id, # Same as audience
}
claims_identity = ClaimsIdentity(
claims=claims, is_authenticated=True, authentication_type="Bearer"
)
# Verify is_agent_claim returns False (because app_id == audience)
assert claims_identity.is_agent_claim() is False
# Test get_token_scope
result = claims_identity.get_token_scope()
# Verify result format
assert isinstance(result, list)
assert len(result) == 1
assert result[0] == f"{AuthenticationConstants.AGENTS_SDK_SCOPE}/.default"