Files
atlantis/server/user_config_test.go
Ken Kaizu f09a9d4c01 feat: --allow-commands restricts available atlantis commands (#2877)
* feat: --allow-command configuration restricts available atlantis commands

* add version and approve_policies into allow commands defaults

* allow-commands accept all keyword which allows all commands

* remove redundant nest

* more detail abount allow-commands all keyword

* Update server/events/comment_parser.go
2022-12-27 22:52:39 -06:00

107 lines
2.3 KiB
Go

package server_test
import (
"testing"
"github.com/runatlantis/atlantis/server"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/logging"
. "github.com/runatlantis/atlantis/testing"
"github.com/stretchr/testify/assert"
)
func TestUserConfig_ToAllowCommandNames(t *testing.T) {
tests := []struct {
name string
allowCommands string
want []command.Name
wantErr string
}{
{
name: "full commands can be parsed by comma",
allowCommands: "apply,plan,unlock,policy_check,approve_policies,version,import",
want: []command.Name{
command.Apply, command.Plan, command.Unlock, command.PolicyCheck, command.ApprovePolicies, command.Version, command.Import,
},
},
{
name: "all",
allowCommands: "all",
want: []command.Name{
command.Version, command.Plan, command.Apply, command.Unlock, command.ApprovePolicies, command.Import,
},
},
{
name: "all with others returns same with all result",
allowCommands: "all,plan",
want: []command.Name{
command.Version, command.Plan, command.Apply, command.Unlock, command.ApprovePolicies, command.Import,
},
},
{
name: "empty",
allowCommands: "",
want: nil,
},
{
name: "invalid command",
allowCommands: "plan,all,invalid",
wantErr: "unknown command name: invalid",
},
{
name: "invalid command",
allowCommands: "invalid,plan,all",
wantErr: "unknown command name: invalid",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := server.UserConfig{
AllowCommands: tt.allowCommands,
}
got, err := u.ToAllowCommandNames()
if err != nil {
assert.ErrorContains(t, err, tt.wantErr, "ToAllowCommandNames()")
}
assert.Equalf(t, tt.want, got, "ToAllowCommandNames()")
})
}
}
func TestUserConfig_ToLogLevel(t *testing.T) {
cases := []struct {
userLvl string
expLvl logging.LogLevel
}{
{
"debug",
logging.Debug,
},
{
"info",
logging.Info,
},
{
"warn",
logging.Warn,
},
{
"error",
logging.Error,
},
{
"unknown",
logging.Info,
},
}
for _, c := range cases {
t.Run(c.userLvl, func(t *testing.T) {
u := server.UserConfig{
LogLevel: c.userLvl,
}
Equals(t, c.expLvl, u.ToLogLevel())
})
}
}