Files
codit/backend/tests/store_ssh_principal_grants_test.go

52 lines
1.6 KiB
Go

package tests
import "testing"
import "time"
import "codit/internal/models"
func TestSSHPrincipalGrantStoresMultiplePrincipals(t *testing.T) {
var dbStore = openTestStore(t)
var user models.User
var created models.SSHPrincipalGrant
var loaded models.SSHPrincipalGrant
var active []models.SSHPrincipalGrant
var err error
defer dbStore.Close()
user = createTestUser(t, dbStore, "ssh-grant-user")
created, err = dbStore.CreateSSHPrincipalGrant(models.SSHPrincipalGrant{
Name: "hyung-hwan-login",
Principals: []string{"hyung-hwan", "ops"},
Targets: []models.SSHPrincipalGrantTarget{
{TargetType: "user", TargetID: user.ID},
},
})
if err != nil {
t.Fatalf("create ssh principal grant: %v", err)
}
loaded, err = dbStore.GetSSHPrincipalGrant(created.ID)
if err != nil {
t.Fatalf("get ssh principal grant: %v", err)
}
if loaded.Name != "hyung-hwan-login" {
t.Fatalf("unexpected grant name: got=%s want=hyung-hwan-login", loaded.Name)
}
if len(loaded.Principals) != 2 {
t.Fatalf("unexpected principal count: got=%d want=2", len(loaded.Principals))
}
if loaded.Principals[0] != "hyung-hwan" || loaded.Principals[1] != "ops" {
t.Fatalf("unexpected principals: got=%v", loaded.Principals)
}
active, err = dbStore.ListActiveSSHPrincipalGrantsForUser(user.ID, time.Now().UTC().Unix())
if err != nil {
t.Fatalf("list active ssh principal grants: %v", err)
}
if len(active) != 1 {
t.Fatalf("unexpected active grant count: got=%d want=1", len(active))
}
if len(active[0].Principals) != 2 {
t.Fatalf("unexpected active grant principal count: got=%d want=2", len(active[0].Principals))
}
}