Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions pkg/groups/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,73 @@
func (*mockWriteCloser) Close() error {
return nil
}


Check failure on line 639 in pkg/groups/group_test.go

View workflow job for this annotation

GitHub Actions / Linting / Lint

File is not properly formatted (gci)
// TestManager_GroupNameCaseInsensitive tests that group names are normalized and treated case-insensitively.
func TestManager_GroupNameCaseInsensitive(t *testing.T) {
t.Parallel()

tests := []struct {
name string
inputName string
existing string
setupMock func(*mocks.MockStore, string)
expectError bool
}{
{
name: "exists with different case",
inputName: "MyGroup",
existing: "mygroup",
setupMock: func(mock *mocks.MockStore, existing string) {
// Manager should lowercase input, so Exists is called with "mygroup"
mock.EXPECT().
Exists(gomock.Any(), existing).
Return(true, nil)
},
expectError: false,
},
{
name: "create duplicate with different case",
inputName: "MyGroup",
existing: "mygroup",
setupMock: func(mock *mocks.MockStore, existing string) {
// Manager should lowercase input, so Exists is called with "mygroup"
mock.EXPECT().
Exists(gomock.Any(), existing).
Return(true, nil)
},
expectError: true, // expect duplicate error
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockStore := mocks.NewMockStore(ctrl)
manager := &manager{groupStore: mockStore}

// set up the mock for this test
tt.setupMock(mockStore, tt.existing)

if tt.expectError {
err := manager.Create(context.Background(), tt.inputName)
if err == nil {
t.Errorf("expected error when creating duplicate with case-insensitive name, got nil")
}
} else {
exists, err := manager.Exists(context.Background(), tt.inputName)
if err != nil {
t.Fatalf("exists check failed: %v", err)
}
if !exists {
t.Errorf("expected group %q to exist regardless of case", tt.inputName)
}
}
})
}
}

5 changes: 5 additions & 0 deletions pkg/groups/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func NewManager() (Manager, error) {

// Create creates a new group with the given name
func (m *manager) Create(ctx context.Context, name string) error {
// Lowercase the group name before checking for existence
name = strings.ToLower(name)
// Check if group already exists
exists, err := m.groupStore.Exists(ctx, name)
if err != nil {
Expand All @@ -52,6 +54,7 @@ func (m *manager) Create(ctx context.Context, name string) error {

// Get retrieves a group by name
func (m *manager) Get(ctx context.Context, name string) (*Group, error) {
name = strings.ToLower(name)
reader, err := m.groupStore.GetReader(ctx, name)
if err != nil {
return nil, fmt.Errorf("failed to get reader for group: %w", err)
Expand Down Expand Up @@ -92,11 +95,13 @@ func (m *manager) List(ctx context.Context) ([]*Group, error) {

// Delete removes a group by name
func (m *manager) Delete(ctx context.Context, name string) error {
name = strings.ToLower(name)
return m.groupStore.Delete(ctx, name)
}

// Exists checks if a group exists
func (m *manager) Exists(ctx context.Context, name string) (bool, error) {
name = strings.ToLower(name)
return m.groupStore.Exists(ctx, name)
}

Expand Down
Loading