access_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package agentserver
  2. import "testing"
  3. func TestRoleAllowed(t *testing.T) {
  4. cases := []struct {
  5. roles []string
  6. allowed string
  7. want bool
  8. }{
  9. {nil, "", true},
  10. {[]string{"dev"}, " ", true},
  11. {[]string{"dev"}, "ops,admin", false},
  12. {[]string{"dev", "ops"}, "ops, admin", true},
  13. {nil, "ops", false},
  14. }
  15. for _, c := range cases {
  16. if got := RoleAllowed(c.roles, c.allowed); got != c.want {
  17. t.Errorf("RoleAllowed(%v, %q)=%v, want %v", c.roles, c.allowed, got, c.want)
  18. }
  19. }
  20. }
  21. func TestSelectProfile(t *testing.T) {
  22. byRole := "ops:prf_ops, admin:prf_admin,bad, :prf_x,dev:"
  23. cases := []struct {
  24. roles []string
  25. want string
  26. }{
  27. {[]string{"dev"}, "prf_default"}, // dev 映射值为空,忽略
  28. {[]string{"admin"}, "prf_admin"},
  29. {[]string{"admin", "ops"}, "prf_ops"}, // 按配置顺序取第一个命中
  30. {nil, "prf_default"},
  31. }
  32. for _, c := range cases {
  33. if got := SelectProfile(c.roles, byRole, " prf_default "); got != c.want {
  34. t.Errorf("SelectProfile(%v)=%q, want %q", c.roles, got, c.want)
  35. }
  36. }
  37. if got := SelectProfile([]string{"x"}, "", ""); got != "" {
  38. t.Errorf("没有默认值时应返回空,得到 %q", got)
  39. }
  40. }