|
| 1 | +package setup |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/fastclaw-ai/fastclaw/internal/auth" |
| 11 | + "github.com/fastclaw-ai/fastclaw/internal/store" |
| 12 | + "github.com/fastclaw-ai/fastclaw/internal/users" |
| 13 | +) |
| 14 | + |
| 15 | +func TestAuthorizeSkillInstallTargetRequiresAdminForGlobalInstalls(t *testing.T) { |
| 16 | + s := NewServer(0) |
| 17 | + |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + ident auth.Identity |
| 21 | + wantOK bool |
| 22 | + wantStatus int |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "regular user session rejected", |
| 26 | + ident: auth.Identity{ |
| 27 | + UserID: "u_user", |
| 28 | + Role: users.RoleUser, |
| 29 | + AuthMethod: "session", |
| 30 | + }, |
| 31 | + wantOK: false, |
| 32 | + wantStatus: http.StatusForbidden, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "super admin session allowed", |
| 36 | + ident: auth.Identity{ |
| 37 | + UserID: "u_admin", |
| 38 | + Role: users.RoleSuperAdmin, |
| 39 | + AuthMethod: "session", |
| 40 | + }, |
| 41 | + wantOK: true, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "admin api key allowed", |
| 45 | + ident: auth.Identity{ |
| 46 | + UserID: "u_user", |
| 47 | + Role: users.RoleUser, |
| 48 | + AuthMethod: "apikey", |
| 49 | + APIKeyType: users.APIKeyTypeAdmin, |
| 50 | + }, |
| 51 | + wantOK: true, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "actAs super admin rejected as read only", |
| 55 | + ident: auth.Identity{ |
| 56 | + UserID: "u_admin", |
| 57 | + Role: users.RoleSuperAdmin, |
| 58 | + AuthMethod: "session", |
| 59 | + ActAsUserID: "u_other", |
| 60 | + }, |
| 61 | + wantOK: false, |
| 62 | + wantStatus: http.StatusForbidden, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tt := range tests { |
| 67 | + t.Run(tt.name, func(t *testing.T) { |
| 68 | + rr := httptest.NewRecorder() |
| 69 | + ok := s.authorizeSkillInstallTarget(rr, skillInstallRequest(tt.ident), "") |
| 70 | + if ok != tt.wantOK { |
| 71 | + t.Fatalf("ok = %v, want %v", ok, tt.wantOK) |
| 72 | + } |
| 73 | + if !tt.wantOK && rr.Code != tt.wantStatus { |
| 74 | + t.Fatalf("status = %d, want %d", rr.Code, tt.wantStatus) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestAuthorizeSkillInstallTargetKeepsAgentInstallsOwnerScoped(t *testing.T) { |
| 81 | + ctx := context.Background() |
| 82 | + s, st, accts := newSkillInstallAuthServer(t, ctx) |
| 83 | + owner := createSkillInstallTestUser(t, ctx, accts, "owner", users.RoleUser) |
| 84 | + other := createSkillInstallTestUser(t, ctx, accts, "other", users.RoleUser) |
| 85 | + if err := st.SaveAgent(ctx, &store.AgentRecord{ |
| 86 | + ID: "agt_owner", |
| 87 | + UserID: owner.ID, |
| 88 | + Name: "Owner Agent", |
| 89 | + }); err != nil { |
| 90 | + t.Fatalf("SaveAgent: %v", err) |
| 91 | + } |
| 92 | + |
| 93 | + tests := []struct { |
| 94 | + name string |
| 95 | + ident auth.Identity |
| 96 | + wantOK bool |
| 97 | + wantStatus int |
| 98 | + }{ |
| 99 | + { |
| 100 | + name: "owner allowed", |
| 101 | + ident: auth.Identity{ |
| 102 | + UserID: owner.ID, |
| 103 | + Role: users.RoleUser, |
| 104 | + AuthMethod: "session", |
| 105 | + }, |
| 106 | + wantOK: true, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "non owner rejected", |
| 110 | + ident: auth.Identity{ |
| 111 | + UserID: other.ID, |
| 112 | + Role: users.RoleUser, |
| 113 | + AuthMethod: "session", |
| 114 | + }, |
| 115 | + wantOK: false, |
| 116 | + wantStatus: http.StatusForbidden, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "read only owner rejected", |
| 120 | + ident: auth.Identity{ |
| 121 | + UserID: "u_admin", |
| 122 | + Role: users.RoleSuperAdmin, |
| 123 | + AuthMethod: "session", |
| 124 | + ActAsUserID: owner.ID, |
| 125 | + }, |
| 126 | + wantOK: false, |
| 127 | + wantStatus: http.StatusForbidden, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + for _, tt := range tests { |
| 132 | + t.Run(tt.name, func(t *testing.T) { |
| 133 | + rr := httptest.NewRecorder() |
| 134 | + ok := s.authorizeSkillInstallTarget(rr, skillInstallRequest(tt.ident), "agt_owner") |
| 135 | + if ok != tt.wantOK { |
| 136 | + t.Fatalf("ok = %v, want %v", ok, tt.wantOK) |
| 137 | + } |
| 138 | + if !tt.wantOK && rr.Code != tt.wantStatus { |
| 139 | + t.Fatalf("status = %d, want %d", rr.Code, tt.wantStatus) |
| 140 | + } |
| 141 | + }) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func skillInstallRequest(ident auth.Identity) *http.Request { |
| 146 | + req := httptest.NewRequest(http.MethodPost, "/api/skills/install", nil) |
| 147 | + return req.WithContext(auth.WithIdentity(req.Context(), ident)) |
| 148 | +} |
| 149 | + |
| 150 | +func newSkillInstallAuthServer(t *testing.T, ctx context.Context) (*Server, store.Store, *users.Accounts) { |
| 151 | + t.Helper() |
| 152 | + |
| 153 | + dbPath := filepath.Join(t.TempDir(), "fastclaw.db") |
| 154 | + st, err := store.NewDBStore("sqlite", "file:"+dbPath+"?cache=shared") |
| 155 | + if err != nil { |
| 156 | + t.Fatalf("NewDBStore: %v", err) |
| 157 | + } |
| 158 | + t.Cleanup(func() { |
| 159 | + _ = st.Close() |
| 160 | + }) |
| 161 | + if err := st.Migrate(ctx); err != nil { |
| 162 | + t.Fatalf("Migrate: %v", err) |
| 163 | + } |
| 164 | + accts, err := users.NewAccounts(st) |
| 165 | + if err != nil { |
| 166 | + t.Fatalf("NewAccounts: %v", err) |
| 167 | + } |
| 168 | + s := NewServer(0) |
| 169 | + s.SetStore(st) |
| 170 | + return s, st, accts |
| 171 | +} |
| 172 | + |
| 173 | +func createSkillInstallTestUser(t *testing.T, ctx context.Context, accts *users.Accounts, username, role string) *users.Account { |
| 174 | + t.Helper() |
| 175 | + |
| 176 | + acct, err := accts.Create(ctx, users.CreateInput{ |
| 177 | + Username: username, |
| 178 | + Email: username + "@example.test", |
| 179 | + Password: "password", |
| 180 | + Role: role, |
| 181 | + }) |
| 182 | + if err != nil { |
| 183 | + t.Fatalf("Create(%s): %v", username, err) |
| 184 | + } |
| 185 | + return acct |
| 186 | +} |
0 commit comments