2017-11-13 17:13:17 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCheckACLs(t *testing.T) {
|
|
|
|
Convey("Testing CheckACLs", t, func() {
|
|
|
|
// create tmp dir
|
|
|
|
tempDir, err := ioutil.TempDir("", "sshportal")
|
|
|
|
So(err, ShouldBeNil)
|
2017-12-04 01:18:17 +08:00
|
|
|
defer func() {
|
|
|
|
So(os.RemoveAll(tempDir), ShouldBeNil)
|
|
|
|
}()
|
2017-11-13 17:13:17 +08:00
|
|
|
|
|
|
|
// create sqlite db
|
|
|
|
db, err := gorm.Open("sqlite3", filepath.Join(tempDir, "sshportal.db"))
|
2017-12-04 01:18:17 +08:00
|
|
|
So(err, ShouldBeNil)
|
2017-11-13 17:13:17 +08:00
|
|
|
db.LogMode(false)
|
|
|
|
So(dbInit(db), ShouldBeNil)
|
|
|
|
|
|
|
|
// create dummy objects
|
2017-12-04 01:18:17 +08:00
|
|
|
var hostGroup HostGroup
|
|
|
|
err = HostGroupsByIdentifiers(db, []string{"default"}).First(&hostGroup).Error
|
2017-11-13 17:13:17 +08:00
|
|
|
So(err, ShouldBeNil)
|
2017-12-04 01:18:17 +08:00
|
|
|
db.Create(&Host{Groups: []*HostGroup{&hostGroup}})
|
2017-11-13 17:13:17 +08:00
|
|
|
|
|
|
|
//. load db
|
|
|
|
var (
|
|
|
|
hosts []Host
|
|
|
|
users []User
|
|
|
|
)
|
|
|
|
db.Preload("Groups").Preload("Groups.ACLs").Find(&hosts)
|
|
|
|
db.Preload("Groups").Preload("Groups.ACLs").Find(&users)
|
|
|
|
|
|
|
|
// test
|
|
|
|
action, err := CheckACLs(users[0], hosts[0])
|
|
|
|
So(err, ShouldBeNil)
|
2017-12-04 01:18:17 +08:00
|
|
|
So(action, ShouldEqual, ACLActionAllow)
|
2017-11-13 17:13:17 +08:00
|
|
|
})
|
|
|
|
}
|