| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package acl
- import (
- "github.com/stretchr/testify/assert"
- "testing"
- )
- func Test_hasGroupsMatch(t *testing.T) {
- tests := []struct {
- name string
- aclMatchUsergroups []string
- usergroupLine string
- matches bool
- sep string
- }{
- {
- name: "No groups match",
- aclMatchUsergroups: []string{"group1", "group2"},
- usergroupLine: "group3",
- matches: false,
- },
- {
- name: "Exact match",
- aclMatchUsergroups: []string{"group1", "group2"},
- usergroupLine: "group1",
- matches: true,
- },
- {
- name: "Multiple groups match",
- aclMatchUsergroups: []string{"group1", "group2"},
- usergroupLine: "group1 group2",
- matches: true,
- },
- {
- name: "Comma-separated groups match",
- aclMatchUsergroups: []string{"group1", "group2", "group3"},
- usergroupLine: "group1, group2",
- matches: true,
- sep: ",",
- },
- {
- name: "Comma-separated groups with default separator does not match",
- aclMatchUsergroups: []string{"group1"},
- usergroupLine: "group1, group2",
- matches: false,
- sep: "",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- user := &AuthenticatedUser{
- Username: "testuser",
- UsergroupLine: tt.usergroupLine,
- }
- if matches := user.matchesUsergroupAcl(tt.aclMatchUsergroups, tt.sep); matches != tt.matches {
- t.Errorf("AuthenticatedUser.matchesUsergroupAcl() = %v, want %v for usergroups %v", matches, tt.matches, tt.aclMatchUsergroups)
- }
- })
- }
- }
- func Test_parseUsergroupLine(t *testing.T) {
- tests := []struct {
- name string
- usergroupLine string
- expectedGroups []string
- sep string
- }{
- {
- name: "Default separator (space)",
- usergroupLine: "group1 group2",
- expectedGroups: []string{"group1", "group2"},
- },
- {
- name: "Comma-separated groups",
- usergroupLine: "group1 , group2",
- expectedGroups: []string{"group1", "group2"},
- sep: ",",
- },
- {
- name: "Multiple spaces",
- usergroupLine: "group1 , group2 , group3",
- expectedGroups: []string{"group1", "group2", "group3"},
- sep: ",",
- },
- {
- name: "Empty usergroup line",
- usergroupLine: "",
- expectedGroups: []string{},
- },
- {
- name: "Empty group names",
- usergroupLine: "|group1| | group3|",
- expectedGroups: []string{"group1", "group3"},
- sep: "|",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- user := &AuthenticatedUser{
- Username: "testuser",
- UsergroupLine: tt.usergroupLine,
- }
- assert.Equal(t, tt.expectedGroups, user.parseUsergroupLine(tt.sep))
- })
- }
- }
|