acl_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package acl
  2. import (
  3. "testing"
  4. authpublic "github.com/OliveTin/OliveTin/internal/auth/authpublic"
  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 := &authpublic.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. }