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,105 @@
|
||||
package model_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"slackerbot/internal/config"
|
||||
"slackerbot/internal/model"
|
||||
)
|
||||
|
||||
func TestPermissions(t *testing.T) {
|
||||
t.Setenv("SLACK_APP_TOKEN", "xapp-test")
|
||||
t.Setenv("SLACK_BOT_TOKEN", "xoxb-test")
|
||||
t.Setenv("CLUSTER_SECRET", "cluster-secret-1234567890")
|
||||
c, err := config.LoadConfig("../../../config.example.toml", config.ModeMaster)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p := c.Permissions
|
||||
|
||||
cases := []struct {
|
||||
kind model.OwnerKind
|
||||
channel, user string
|
||||
action model.Action
|
||||
want bool
|
||||
}{
|
||||
{model.OwnerChannel, "C07ABC123", "U0111", model.ActMove, true},
|
||||
{model.OwnerChannel, "C07ABC123", "U0111", model.ActScale, false},
|
||||
{model.OwnerChannel, "C07ABC123", "U0222", model.ActKeep, true},
|
||||
{model.OwnerChannel, "C07ABC123", "U0ADMIN", model.ActMove, false},
|
||||
{model.OwnerChannel, "C0OTHER", "U0ADMIN", model.ActStorage, true},
|
||||
{model.OwnerChannel, "C0OTHER", "U0111", model.ActMove, false},
|
||||
{model.OwnerUser, "D1", "U9", model.ActScale, true},
|
||||
{model.OwnerUser, "D1", "U9", model.ActStorage, false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := p.Allowed(c.kind, c.channel, c.user, c.action); got != c.want {
|
||||
t.Errorf("%s %s %s %s = %v, want %v", c.kind, c.channel, c.user, c.action, got, c.want)
|
||||
}
|
||||
}
|
||||
if h := p.Holders("C07ABC123", model.ActMove); len(h) != 2 {
|
||||
t.Errorf("move holders = %v", h)
|
||||
}
|
||||
if a := p.ActionsFor("C0OTHER", "U0ADMIN"); len(a) != len(model.AllActions) {
|
||||
t.Errorf("all should expand to every action, got %v", a)
|
||||
}
|
||||
|
||||
open := model.PermissionsConfig{Default: map[string][]string{"U1": {"all"}}}
|
||||
if !open.Allowed(model.OwnerUser, "D1", "U9", model.ActMove) {
|
||||
t.Error("DM permissions should default to allowed")
|
||||
}
|
||||
if (model.PermissionsConfig{}).Validate() == nil {
|
||||
t.Error("a missing default super user should be refused")
|
||||
}
|
||||
bad := model.PermissionsConfig{Default: map[string][]string{"U1": {"delete-everything"}}}
|
||||
if bad.Validate() == nil {
|
||||
t.Error("unknown permission names should be refused")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrantRevoke(t *testing.T) {
|
||||
p := model.PermissionsConfig{Managers: []string{"UBOSS"}, Default: map[string][]string{"UDEF": {"all"}}}.Clone()
|
||||
|
||||
p.Grant("C1", []string{"UANA"}, []model.Action{model.ActMove})
|
||||
if !p.Allowed(model.OwnerChannel, "C1", "UDEF", model.ActScale) {
|
||||
t.Fatal("first grant in a channel should keep everyone from the default list")
|
||||
}
|
||||
if !p.Allowed(model.OwnerChannel, "C1", "UANA", model.ActMove) || p.Allowed(model.OwnerChannel, "C1", "UANA", model.ActScale) {
|
||||
t.Fatal("grant should add exactly what was asked")
|
||||
}
|
||||
if p.Allowed(model.OwnerChannel, "C2", "UANA", model.ActMove) {
|
||||
t.Fatal("grants stay in their channel")
|
||||
}
|
||||
|
||||
p.Grant("C1", []string{"UANA"}, []model.Action{"all"})
|
||||
if p.CanManage("C1", "UANA") {
|
||||
t.Fatal("all must not include manage")
|
||||
}
|
||||
p.Grant("C1", []string{"UANA"}, []model.Action{model.ActManage})
|
||||
if !p.CanManage("C1", "UANA") || p.CanManage("C2", "UANA") {
|
||||
t.Fatal("manage is per channel")
|
||||
}
|
||||
|
||||
p.Revoke("C1", []string{"UDEF"}, []model.Action{model.ActScale})
|
||||
if p.Allowed(model.OwnerChannel, "C1", "UDEF", model.ActScale) || !p.Allowed(model.OwnerChannel, "C1", "UDEF", model.ActMove) {
|
||||
t.Fatal("revoking one action out of all should keep the rest")
|
||||
}
|
||||
p.Revoke("C1", []string{"UANA"}, nil)
|
||||
if len(p.ActionsFor("C1", "UANA")) != 0 {
|
||||
t.Fatal("revoke with no actions removes the person")
|
||||
}
|
||||
|
||||
p.Grant("", []string{"UNEW"}, []model.Action{model.ActKeep})
|
||||
if !p.Allowed(model.OwnerChannel, "C9", "UNEW", model.ActKeep) {
|
||||
t.Fatal("default grants apply to channels without their own list")
|
||||
}
|
||||
|
||||
p.AddManagers([]string{"UTWO"})
|
||||
if err := p.RemoveManagers([]string{"UBOSS", "UTWO"}); !errors.Is(err, model.ErrLastManager) {
|
||||
t.Fatal("removing every manager must be refused")
|
||||
}
|
||||
if err := p.RemoveManagers([]string{"UBOSS"}); err != nil || p.IsManager("UBOSS") {
|
||||
t.Fatal("removing one of two managers should work")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user