Skip to content

Commit 9a338d7

Browse files
committed
Add tests for scope maps
1 parent 4d679fd commit 9a338d7

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed

pkg/scopes/map_test.go

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package scopes
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestGetToolScopeMap(t *testing.T) {
11+
// Reset and set up a test map
12+
SetGlobalToolScopeMap(ToolScopeMap{
13+
"test_tool": &ToolScopeInfo{
14+
RequiredScopes: []string{"read:org"},
15+
AcceptedScopes: []string{"read:org", "write:org", "admin:org"},
16+
},
17+
})
18+
19+
m, err := GetToolScopeMap()
20+
require.NoError(t, err)
21+
require.NotNil(t, m)
22+
require.Greater(t, len(m), 0, "expected at least one tool in the scope map")
23+
24+
testTool, ok := m["test_tool"]
25+
require.True(t, ok, "expected test_tool to be in the scope map")
26+
assert.Contains(t, testTool.RequiredScopes, "read:org")
27+
assert.Contains(t, testTool.AcceptedScopes, "read:org")
28+
assert.Contains(t, testTool.AcceptedScopes, "admin:org")
29+
}
30+
31+
func TestGetToolScopeInfo(t *testing.T) {
32+
// Set up test scope map
33+
SetGlobalToolScopeMap(ToolScopeMap{
34+
"search_orgs": &ToolScopeInfo{
35+
RequiredScopes: []string{"read:org"},
36+
AcceptedScopes: []string{"read:org", "write:org", "admin:org"},
37+
},
38+
})
39+
40+
info, err := GetToolScopeInfo("search_orgs")
41+
require.NoError(t, err)
42+
require.NotNil(t, info)
43+
44+
// Non-existent tool should return nil
45+
info, err = GetToolScopeInfo("nonexistent_tool")
46+
require.NoError(t, err)
47+
assert.Nil(t, info)
48+
}
49+
50+
func TestToolScopeInfo_HasAcceptedScope(t *testing.T) {
51+
testCases := []struct {
52+
name string
53+
scopeInfo *ToolScopeInfo
54+
userScopes []string
55+
expected bool
56+
}{
57+
{
58+
name: "has exact required scope",
59+
scopeInfo: &ToolScopeInfo{
60+
RequiredScopes: []string{"read:org"},
61+
AcceptedScopes: []string{"read:org", "write:org", "admin:org"},
62+
},
63+
userScopes: []string{"read:org"},
64+
expected: true,
65+
},
66+
{
67+
name: "has parent scope (admin:org grants read:org)",
68+
scopeInfo: &ToolScopeInfo{
69+
RequiredScopes: []string{"read:org"},
70+
AcceptedScopes: []string{"read:org", "write:org", "admin:org"},
71+
},
72+
userScopes: []string{"admin:org"},
73+
expected: true,
74+
},
75+
{
76+
name: "has parent scope (write:org grants read:org)",
77+
scopeInfo: &ToolScopeInfo{
78+
RequiredScopes: []string{"read:org"},
79+
AcceptedScopes: []string{"read:org", "write:org", "admin:org"},
80+
},
81+
userScopes: []string{"write:org"},
82+
expected: true,
83+
},
84+
{
85+
name: "missing required scope",
86+
scopeInfo: &ToolScopeInfo{
87+
RequiredScopes: []string{"read:org"},
88+
AcceptedScopes: []string{"read:org", "write:org", "admin:org"},
89+
},
90+
userScopes: []string{"repo"},
91+
expected: false,
92+
},
93+
{
94+
name: "no scope required",
95+
scopeInfo: &ToolScopeInfo{
96+
RequiredScopes: []string{},
97+
AcceptedScopes: []string{},
98+
},
99+
userScopes: []string{},
100+
expected: true,
101+
},
102+
{
103+
name: "nil scope info",
104+
scopeInfo: nil,
105+
userScopes: []string{},
106+
expected: true,
107+
},
108+
{
109+
name: "repo scope for tool requiring repo",
110+
scopeInfo: &ToolScopeInfo{
111+
RequiredScopes: []string{"repo"},
112+
AcceptedScopes: []string{"repo"},
113+
},
114+
userScopes: []string{"repo"},
115+
expected: true,
116+
},
117+
{
118+
name: "missing repo scope",
119+
scopeInfo: &ToolScopeInfo{
120+
RequiredScopes: []string{"repo"},
121+
AcceptedScopes: []string{"repo"},
122+
},
123+
userScopes: []string{"public_repo"},
124+
expected: false,
125+
},
126+
}
127+
128+
for _, tc := range testCases {
129+
t.Run(tc.name, func(t *testing.T) {
130+
result := tc.scopeInfo.HasAcceptedScope(tc.userScopes...)
131+
assert.Equal(t, tc.expected, result)
132+
})
133+
}
134+
}
135+
136+
func TestToolScopeInfo_MissingScopes(t *testing.T) {
137+
testCases := []struct {
138+
name string
139+
scopeInfo *ToolScopeInfo
140+
userScopes []string
141+
expectedLen int
142+
expectedScopes []string
143+
}{
144+
{
145+
name: "has required scope - no missing",
146+
scopeInfo: &ToolScopeInfo{
147+
RequiredScopes: []string{"read:org"},
148+
AcceptedScopes: []string{"read:org", "write:org", "admin:org"},
149+
},
150+
userScopes: []string{"read:org"},
151+
expectedLen: 0,
152+
expectedScopes: nil,
153+
},
154+
{
155+
name: "missing scope",
156+
scopeInfo: &ToolScopeInfo{
157+
RequiredScopes: []string{"read:org"},
158+
AcceptedScopes: []string{"read:org", "write:org", "admin:org"},
159+
},
160+
userScopes: []string{"repo"},
161+
expectedLen: 1,
162+
expectedScopes: []string{"read:org"},
163+
},
164+
{
165+
name: "no scope required - no missing",
166+
scopeInfo: &ToolScopeInfo{
167+
RequiredScopes: []string{},
168+
AcceptedScopes: []string{},
169+
},
170+
userScopes: []string{},
171+
expectedLen: 0,
172+
expectedScopes: nil,
173+
},
174+
{
175+
name: "nil scope info - no missing",
176+
scopeInfo: nil,
177+
userScopes: []string{},
178+
expectedLen: 0,
179+
expectedScopes: nil,
180+
},
181+
}
182+
183+
for _, tc := range testCases {
184+
t.Run(tc.name, func(t *testing.T) {
185+
missing := tc.scopeInfo.MissingScopes(tc.userScopes...)
186+
assert.Len(t, missing, tc.expectedLen)
187+
if tc.expectedScopes != nil {
188+
for _, expected := range tc.expectedScopes {
189+
assert.Contains(t, missing, expected)
190+
}
191+
}
192+
})
193+
}
194+
}

0 commit comments

Comments
 (0)