clean up of the codebase
This commit is contained in:
1 parent
7822529a38
commit
0ac61b4ece
52 files changed
+2545
-2274
No files matched your search
@@ -0,0 +1,66 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"slackerbot/internal/model"
|
||||
)
|
||||
|
||||
func TestStore(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "state.json")
|
||||
s, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = s.PutOwner(&model.Owner{ID: "C1", Kind: model.OwnerChannel, Machine: "c-c1", Size: model.Size{MemoryMB: 1024, CPUs: 1}})
|
||||
_ = s.AddSession(&model.Session{Channel: "D1", ThreadTS: "1.0", OwnerID: "C1", ACPID: "a", CreatedAt: time.Now().Add(-time.Hour)})
|
||||
_ = s.AddSession(&model.Session{Channel: "D1", ThreadTS: "2.0", OwnerID: "C1", ACPID: "b", CreatedAt: time.Now()})
|
||||
|
||||
s2, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if u := s2.OwnerByMachine("c-c1"); u == nil || u.Size.MemoryMB != 1024 {
|
||||
t.Fatal("user not saved")
|
||||
}
|
||||
list := s2.Sessions("C1", 0)
|
||||
if len(list) != 2 || list[0].ACPID != "b" {
|
||||
t.Fatalf("sessions not newest-first: %+v", list)
|
||||
}
|
||||
if s2.Session("D1", "1.0").ACPID != "a" {
|
||||
t.Fatal("session lookup failed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPermissionsSavedAndSeededOnce(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "state.json")
|
||||
s, _ := Open(path)
|
||||
initial := model.PermissionsConfig{Managers: []string{"UBOSS"}, Default: map[string][]string{"UBOSS": {"all"}}}
|
||||
if seeded, err := s.SeedPermissions(initial); !seeded || err != nil {
|
||||
t.Fatalf("first start should seed: %v %v", seeded, err)
|
||||
}
|
||||
if err := s.UpdatePermissions(func(p *model.PermissionsConfig) error {
|
||||
p.Grant("C1", []string{"UANA"}, []model.Action{model.ActMove})
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
s2, _ := Open(path)
|
||||
changed := model.PermissionsConfig{Managers: []string{"USOMEONE"}, Default: map[string][]string{"USOMEONE": {"all"}}}
|
||||
if seeded, _ := s2.SeedPermissions(changed); seeded {
|
||||
t.Fatal("config must not override saved permissions")
|
||||
}
|
||||
p := s2.Permissions()
|
||||
if !p.IsManager("UBOSS") || p.IsManager("USOMEONE") || !p.Allowed(model.OwnerChannel, "C1", "UANA", model.ActMove) {
|
||||
t.Fatalf("saved permissions lost: %+v", p)
|
||||
}
|
||||
if err := s2.UpdatePermissions(func(p *model.PermissionsConfig) error {
|
||||
p.Default["UX"] = []string{"teleport"}
|
||||
return nil
|
||||
}); err == nil {
|
||||
t.Fatal("invalid changes must be refused")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user