fetch_ml/cmd/tui/internal/config/cli_config_test.go
Jeremie Fraeys 803677be57 feat: implement Go backend with comprehensive API and internal packages
- Add API server with WebSocket support and REST endpoints
- Implement authentication system with API keys and permissions
- Add task queue system with Redis backend and error handling
- Include storage layer with database migrations and schemas
- Add comprehensive logging, metrics, and telemetry
- Implement security middleware and network utilities
- Add experiment management and container orchestration
- Include configuration management with smart defaults
2025-12-04 16:53:53 -05:00

194 lines
3.8 KiB
Go

package config
import (
"testing"
)
func TestCLIConfig_CheckPermission(t *testing.T) {
tests := []struct {
name string
config *CLIConfig
permission string
want bool
}{
{
name: "Admin has all permissions",
config: &CLIConfig{
CurrentUser: &UserContext{
Name: "admin",
Admin: true,
},
},
permission: "any:permission",
want: true,
},
{
name: "User with explicit permission",
config: &CLIConfig{
CurrentUser: &UserContext{
Name: "user",
Admin: false,
Permissions: map[string]bool{"jobs:create": true},
},
},
permission: "jobs:create",
want: true,
},
{
name: "User without permission",
config: &CLIConfig{
CurrentUser: &UserContext{
Name: "user",
Admin: false,
Permissions: map[string]bool{"jobs:read": true},
},
},
permission: "jobs:create",
want: false,
},
{
name: "No current user",
config: &CLIConfig{
CurrentUser: nil,
},
permission: "jobs:create",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.config.CheckPermission(tt.permission)
if got != tt.want {
t.Errorf("CheckPermission() = %v, want %v", got, tt.want)
}
})
}
}
func TestCLIConfig_CanViewJob(t *testing.T) {
tests := []struct {
name string
config *CLIConfig
jobUserID string
want bool
}{
{
name: "Admin can view any job",
config: &CLIConfig{
CurrentUser: &UserContext{
Name: "admin",
Admin: true,
},
},
jobUserID: "other_user",
want: true,
},
{
name: "User can view own job",
config: &CLIConfig{
CurrentUser: &UserContext{
Name: "user1",
Admin: false,
},
},
jobUserID: "user1",
want: true,
},
{
name: "User cannot view other's job",
config: &CLIConfig{
CurrentUser: &UserContext{
Name: "user1",
Admin: false,
},
},
jobUserID: "user2",
want: false,
},
{
name: "No current user cannot view",
config: &CLIConfig{
CurrentUser: nil,
},
jobUserID: "user1",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.config.CanViewJob(tt.jobUserID)
if got != tt.want {
t.Errorf("CanViewJob() = %v, want %v", got, tt.want)
}
})
}
}
func TestCLIConfig_CanModifyJob(t *testing.T) {
tests := []struct {
name string
config *CLIConfig
jobUserID string
want bool
}{
{
name: "Admin can modify any job",
config: &CLIConfig{
CurrentUser: &UserContext{
Name: "admin",
Admin: true,
Permissions: map[string]bool{"jobs:update": true},
},
},
jobUserID: "other_user",
want: true,
},
{
name: "User with permission can modify own job",
config: &CLIConfig{
CurrentUser: &UserContext{
Name: "user1",
Admin: false,
Permissions: map[string]bool{"jobs:update": true},
},
},
jobUserID: "user1",
want: true,
},
{
name: "User without permission cannot modify",
config: &CLIConfig{
CurrentUser: &UserContext{
Name: "user1",
Admin: false,
Permissions: map[string]bool{"jobs:read": true},
},
},
jobUserID: "user1",
want: false,
},
{
name: "User cannot modify other's job",
config: &CLIConfig{
CurrentUser: &UserContext{
Name: "user1",
Admin: false,
Permissions: map[string]bool{"jobs:update": true},
},
},
jobUserID: "user2",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.config.CanModifyJob(tt.jobUserID)
if got != tt.want {
t.Errorf("CanModifyJob() = %v, want %v", got, tt.want)
}
})
}
}