acl_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package acl
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "testing"
  5. )
  6. func Test_hasGroupsMatch(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. aclMatchUsergroups []string
  10. usergroupLine string
  11. matches bool
  12. sep string
  13. }{
  14. {
  15. name: "No groups match",
  16. aclMatchUsergroups: []string{"group1", "group2"},
  17. usergroupLine: "group3",
  18. matches: false,
  19. },
  20. {
  21. name: "Exact match",
  22. aclMatchUsergroups: []string{"group1", "group2"},
  23. usergroupLine: "group1",
  24. matches: true,
  25. },
  26. {
  27. name: "Multiple groups match",
  28. aclMatchUsergroups: []string{"group1", "group2"},
  29. usergroupLine: "group1 group2",
  30. matches: true,
  31. },
  32. {
  33. name: "Comma-separated groups match",
  34. aclMatchUsergroups: []string{"group1", "group2", "group3"},
  35. usergroupLine: "group1, group2",
  36. matches: true,
  37. sep: ",",
  38. },
  39. {
  40. name: "Comma-separated groups with default separator does not match",
  41. aclMatchUsergroups: []string{"group1"},
  42. usergroupLine: "group1, group2",
  43. matches: false,
  44. sep: "",
  45. },
  46. }
  47. for _, tt := range tests {
  48. t.Run(tt.name, func(t *testing.T) {
  49. user := &AuthenticatedUser{
  50. Username: "testuser",
  51. UsergroupLine: tt.usergroupLine,
  52. }
  53. if matches := user.matchesUsergroupAcl(tt.aclMatchUsergroups, tt.sep); matches != tt.matches {
  54. t.Errorf("AuthenticatedUser.matchesUsergroupAcl() = %v, want %v for usergroups %v", matches, tt.matches, tt.aclMatchUsergroups)
  55. }
  56. })
  57. }
  58. }
  59. func Test_parseUsergroupLine(t *testing.T) {
  60. tests := []struct {
  61. name string
  62. usergroupLine string
  63. expectedGroups []string
  64. sep string
  65. }{
  66. {
  67. name: "Default separator (space)",
  68. usergroupLine: "group1 group2",
  69. expectedGroups: []string{"group1", "group2"},
  70. },
  71. {
  72. name: "Comma-separated groups",
  73. usergroupLine: "group1 , group2",
  74. expectedGroups: []string{"group1", "group2"},
  75. sep: ",",
  76. },
  77. {
  78. name: "Multiple spaces",
  79. usergroupLine: "group1 , group2 , group3",
  80. expectedGroups: []string{"group1", "group2", "group3"},
  81. sep: ",",
  82. },
  83. {
  84. name: "Empty usergroup line",
  85. usergroupLine: "",
  86. expectedGroups: []string{},
  87. },
  88. {
  89. name: "Empty group names",
  90. usergroupLine: "|group1| | group3|",
  91. expectedGroups: []string{"group1", "group3"},
  92. sep: "|",
  93. },
  94. }
  95. for _, tt := range tests {
  96. t.Run(tt.name, func(t *testing.T) {
  97. user := &AuthenticatedUser{
  98. Username: "testuser",
  99. UsergroupLine: tt.usergroupLine,
  100. }
  101. assert.Equal(t, tt.expectedGroups, user.parseUsergroupLine(tt.sep))
  102. })
  103. }
  104. }