Skip to content

Commit

Permalink
Config import tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceMacD committed Jul 29, 2021
1 parent f149d54 commit 3472717
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 29 deletions.
152 changes: 125 additions & 27 deletions internal/registry/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)

func TestImportCurrentValidConfig(t *testing.T) {
Expand All @@ -21,30 +22,127 @@ func TestImportCurrentValidConfig(t *testing.T) {
assert.NoError(t, ImportConfig(db, conf))
}

// func TestImportUsersThatDoNotExist(t *testing.T) {
// confFile, err := ioutil.ReadFile("_testdata/infra.yaml")
// if err != nil {
// t.Fatal(err)
// }
// config := NewConfig()
// err = yaml.Unmarshal(confFile, &config)
// if err != nil {
// t.Fatal(err)
// }

// db, err := NewDB("file::memory:")
// if err != nil {
// t.Fatal(err)
// }

// ImportUserMappings(db, config.Users)
// for user, userMapping := range config.Users {
// for roleName, role := range userMapping.Roles {
// var permission Permission
// err = db.Where(&permission, &Permission{Role: roleName, Kind: role.Kind, UserId: user.Id, DestinationId: destination.Id, FromConfig: true}).Error
// if err != nil {
// return nil, err
// }
// }
// }
// }
func TestImportRolesForExistingUsersAndDestinations(t *testing.T) {
confFile, err := ioutil.ReadFile("_testdata/infra.yaml")
if err != nil {
t.Fatal(err)
}
config := NewConfig()
err = yaml.Unmarshal(confFile, &config)
if err != nil {
t.Fatal(err)
}

db, err := NewDB("file::memory:")
if err != nil {
t.Fatal(err)
}

// Create the users and destinations that exist in the sample infra.yaml
adminUser := User{Email: "admin@example.com"}
err = db.Create(&adminUser).Error
if err != nil {
t.Fatal(err)
}
standardUser := User{Email: "user@example.com"}
err = db.Create(&standardUser).Error
if err != nil {
t.Fatal(err)
}
clusterA := &Destination{Name: "cluster-AAA"}
err = db.Create(&clusterA).Error
if err != nil {
t.Fatal(err)
}
clusterB := &Destination{Name: "cluster-BBB"}
err = db.Create(&clusterB).Error
if err != nil {
t.Fatal(err)
}

ImportUserMappings(db, config.Users)

var roles []Role
err = db.Preload("User").Preload("Destination").Find(&roles).Error
if err != nil {
t.Fatal(err)
}
assert.True(t, containsUserRoleForDestination(roles, adminUser.Id, clusterA.Id, "admin"), "admin@example.com should have the admin role in cluster-AAA")
assert.True(t, containsUserRoleForDestination(roles, adminUser.Id, clusterB.Id, "admin"), "admin@example.com should have the admin role in cluster-BBB")
assert.True(t, containsUserRoleForDestination(roles, standardUser.Id, clusterA.Id, "writer"), "user@example.com should have the writer role in cluster-AAA")
assert.True(t, containsUserRoleForDestination(roles, standardUser.Id, clusterB.Id, "reader"), "user@example.com should have the reader role in cluster-BBB")
}

func TestImportRolesForUnknownUsers(t *testing.T) {
confFile, err := ioutil.ReadFile("_testdata/infra.yaml")
if err != nil {
t.Fatal(err)
}
config := NewConfig()
err = yaml.Unmarshal(confFile, &config)
if err != nil {
t.Fatal(err)
}

db, err := NewDB("file::memory:")
if err != nil {
t.Fatal(err)
}

adminUser := User{Email: "admin@example.com"}
err = db.Create(&adminUser).Error
if err != nil {
t.Fatal(err)
}
standardUser := User{Email: "user@example.com"}
err = db.Create(&standardUser).Error
if err != nil {
t.Fatal(err)
}

// users exist, but there are no destinations

ImportUserMappings(db, config.Users)
var roles []Role
err = db.Preload("User").Preload("Destination").Find(&roles).Error
if err != nil {
t.Fatal(err)
}
assert.Empty(t, roles, "roles mappings were created when no destinations exist")
}

func TestImportRolesForUnknownDestinations(t *testing.T) {
confFile, err := ioutil.ReadFile("_testdata/infra.yaml")
if err != nil {
t.Fatal(err)
}
config := NewConfig()
err = yaml.Unmarshal(confFile, &config)
if err != nil {
t.Fatal(err)
}

db, err := NewDB("file::memory:")
if err != nil {
t.Fatal(err)
}

// no users created in this database

ImportUserMappings(db, config.Users)
var roles []Role
err = db.Preload("User").Preload("Destination").Find(&roles).Error
if err != nil {
t.Fatal(err)
}
assert.Empty(t, roles, "roles mappings were created when no users exist")
}

func containsUserRoleForDestination(roles []Role, userId string, destinationId string, roleName string) bool {
for _, role := range roles {
if role.UserId == userId && role.DestinationId == destinationId && role.Role == roleName {
return true
}
}
return false
}
2 changes: 0 additions & 2 deletions internal/registry/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"crypto/subtle"
"encoding/base64"
"errors"
"fmt"
"log"
"os"
"path"
Expand Down Expand Up @@ -142,7 +141,6 @@ func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
}

func (u *User) AfterCreate(tx *gorm.DB) error {
fmt.Println(initialConfig.Users)
_, err := ApplyUserMapping(tx, initialConfig.Users)
return err
}
Expand Down

0 comments on commit 3472717

Please sign in to comment.