Skip to content

Commit 4ada771

Browse files
authored
Fix isAuthExempt (#49)
<!-- CURSOR_SUMMARY --> > [!NOTE] > Restricts auth exemption to root and certain top-level commands (not subcommands) and adds tests for isAuthExempt. > > - **Auth Handling**: > - Update `isAuthExempt` to exempt only the root and specific top-level commands (`login`, `logout`, `auth`, `help`, `completion`, `create`), not their subcommands. > - **Tests**: > - Add `cmd/root_test.go` with unit tests validating exemption for root/top-level and requiring auth for subcommands (e.g., `browser-pools create`, `browsers create`, `profiles create`, `list`, `deploy`, `invoke`). > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 3adf913. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 80cfeb4 commit 4ada771

File tree

2 files changed

+93
-8
lines changed

2 files changed

+93
-8
lines changed

cmd/root.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,25 @@ func getKernelClient(cmd *cobra.Command) kernel.Client {
7171
return util.GetKernelClient(cmd)
7272
}
7373

74-
// isAuthExempt returns true if the command or any of its parents should skip auth.
74+
// isAuthExempt returns true if the command should skip auth.
7575
func isAuthExempt(cmd *cobra.Command) bool {
76-
// bare root command does not need auth
76+
// Root command doesn't need auth
7777
if cmd == rootCmd {
7878
return true
7979
}
80-
for c := cmd; c != nil; c = c.Parent() {
81-
switch c.Name() {
82-
case "login", "logout", "auth", "help", "completion",
83-
"create":
84-
return true
85-
}
80+
81+
// Walk up to find the top-level command (direct child of rootCmd)
82+
topLevel := cmd
83+
for topLevel.Parent() != nil && topLevel.Parent() != rootCmd {
84+
topLevel = topLevel.Parent()
85+
}
86+
87+
// Check if the top-level command is in the exempt list
88+
switch topLevel.Name() {
89+
case "login", "logout", "auth", "help", "completion", "create":
90+
return true
8691
}
92+
8793
return false
8894
}
8995

cmd/root_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/spf13/cobra"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestIsAuthExempt(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
cmd *cobra.Command
14+
expected bool
15+
}{
16+
{
17+
name: "root command is exempt",
18+
cmd: rootCmd,
19+
expected: true,
20+
},
21+
{
22+
name: "login command is exempt",
23+
cmd: loginCmd,
24+
expected: true,
25+
},
26+
{
27+
name: "logout command is exempt",
28+
cmd: logoutCmd,
29+
expected: true,
30+
},
31+
{
32+
name: "top-level create command is exempt",
33+
cmd: createCmd,
34+
expected: true,
35+
},
36+
{
37+
name: "browser-pools create subcommand requires auth",
38+
cmd: browserPoolsCreateCmd,
39+
expected: false,
40+
},
41+
{
42+
name: "browsers create subcommand requires auth",
43+
cmd: browsersCreateCmd,
44+
expected: false,
45+
},
46+
{
47+
name: "profiles create subcommand requires auth",
48+
cmd: profilesCreateCmd,
49+
expected: false,
50+
},
51+
{
52+
name: "browser-pools list requires auth",
53+
cmd: browserPoolsListCmd,
54+
expected: false,
55+
},
56+
{
57+
name: "browsers list requires auth",
58+
cmd: browsersListCmd,
59+
expected: false,
60+
},
61+
{
62+
name: "deploy command requires auth",
63+
cmd: deployCmd,
64+
expected: false,
65+
},
66+
{
67+
name: "invoke command requires auth",
68+
cmd: invokeCmd,
69+
expected: false,
70+
},
71+
}
72+
73+
for _, tt := range tests {
74+
t.Run(tt.name, func(t *testing.T) {
75+
result := isAuthExempt(tt.cmd)
76+
assert.Equal(t, tt.expected, result, "isAuthExempt(%s) = %v, want %v", tt.cmd.Name(), result, tt.expected)
77+
})
78+
}
79+
}

0 commit comments

Comments
 (0)