Files
grafana/pkg/services/accesscontrol/models_test.go
Gabriel MABILLE 8c6b5a4319 RBAC: Add a function to save external service roles (#66299)
* AuthN: Save external services RBAC roles

* Add missing test

* Placing roles in the same group

* Split function to gen role and assignment

* add test case and comments

* Ensure we check external service roles are assigned once only

* Update pkg/services/accesscontrol/models_test.go

Co-authored-by: Misi <mgyongyosi@users.noreply.github.com>

---------

Co-authored-by: Misi <mgyongyosi@users.noreply.github.com>
2023-05-09 13:19:38 +02:00

81 lines
1.9 KiB
Go

package accesscontrol
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestSaveExternalServiceRoleCommand_Validate(t *testing.T) {
tests := []struct {
name string
cmd SaveExternalServiceRoleCommand
wantID string
wantErr bool
}{
{
name: "invalid global statement",
cmd: SaveExternalServiceRoleCommand{
OrgID: 1,
Global: true,
ExternalServiceID: "app 1",
ServiceAccountID: 2,
Permissions: []Permission{{Action: "users:read", Scope: "users:id:1"}},
},
wantErr: true,
},
{
name: "invalid no permissions",
cmd: SaveExternalServiceRoleCommand{
OrgID: 1,
ExternalServiceID: "app 1",
ServiceAccountID: 2,
Permissions: []Permission{},
},
wantErr: true,
},
{
name: "invalid service account id",
cmd: SaveExternalServiceRoleCommand{
OrgID: 1,
ExternalServiceID: "app 1",
ServiceAccountID: -1,
Permissions: []Permission{{Action: "users:read", Scope: "users:id:1"}},
},
wantErr: true,
},
{
name: "invalid no Ext Service ID",
cmd: SaveExternalServiceRoleCommand{
OrgID: 1,
ServiceAccountID: 2,
Permissions: []Permission{{Action: "users:read", Scope: "users:id:1"}},
},
wantErr: true,
},
{
name: "slugify the external service ID correctly",
cmd: SaveExternalServiceRoleCommand{
ExternalServiceID: "ThisIs a Very Strange ___ App Name?",
Global: true,
ServiceAccountID: 2,
Permissions: []Permission{{Action: "users:read", Scope: "users:id:1"}},
},
wantErr: false,
wantID: "thisis-a-very-strange-app-name",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.cmd.Validate()
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tt.wantID, tt.cmd.ExternalServiceID)
})
}
}