Skip to content

tests: Added unit tests for user delete command#689

Open
rkcoder101 wants to merge 1 commit intogoharbor:mainfrom
rkcoder101:test/user-delete
Open

tests: Added unit tests for user delete command#689
rkcoder101 wants to merge 1 commit intogoharbor:mainfrom
rkcoder101:test/user-delete

Conversation

@rkcoder101
Copy link
Contributor

Description

This PR introduces unit tests for the user delete command (cmd/harbor/root/user/delete.go)

Relates to #591

Is a sub-part of #653

Note

No changes have been done to the core logic of the application, only a little refactoring for the purpose of writing some nice and clean test code :)

Signed-off-by: Rayyan Khan <rayyanrehman101@gmail.com>
Copilot AI review requested due to automatic review settings February 7, 2026 09:23
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds unit tests around the user delete CLI command by refactoring the delete implementation to be dependency-injectable, enabling mock-based testing without hitting real API/prompt code.

Changes:

  • Added a new unit test suite for delete behavior, including multi-user and error paths.
  • Refactored user delete command logic into a helper function and introduced a deleter abstraction for mocking.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
cmd/harbor/root/user/delete.go Refactors delete command to call an injected deleter implementation, enabling unit testing.
cmd/harbor/root/user/delete_test.go Adds table-driven unit tests for delete behavior using a mock deleter.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +25 to +39
type MockUserDeleter struct {
id map[string]int64
user map[int64]string
userCnt int
expectAuthError bool
}

func (m *MockUserDeleter) UserDelete(userID int64) error {
if m.expectAuthError {
return fmt.Errorf("403")
}
if v, ok := m.user[userID]; ok {
delete(m.id, v)
delete(m.user, userID)
return nil
Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DeleteUser deletes users concurrently, but MockUserDeleter.UserDelete mutates Go maps (m.id, m.user) without synchronization. When multiple usernames are passed (e.g. the multi-user test cases), this can trigger data races and/or a runtime panic due to concurrent map writes. Make the mock thread-safe (e.g., guard map access with a sync.Mutex or use sync.Map), or change the mock to avoid shared mutable state across goroutines.

Copilot uses AI. Check for mistakes.
Comment on lines +25 to +43
type UserDeleter interface {
UserDelete(userID int64) error
GetUserIDByName(username string) (int64, error)
GetUserIDFromUser() int64
}
type DefaultUserDeleter struct{}

for _, arg := range args {
// Retrieve user ID by name.
userID, err := api.GetUsersIdByName(arg)
if err != nil {
log.Errorf("failed to get user id for '%s': %v", arg, err)
continue
}
wg.Add(1)
go func(userID int64) {
defer wg.Done()
if err := api.DeleteUser(userID); err != nil {
errChan <- err
}
}(userID)
}
func (d *DefaultUserDeleter) UserDelete(userID int64) error {
return api.DeleteUser(userID)
}
func (d *DefaultUserDeleter) GetUserIDByName(username string) (int64, error) {
return api.GetUsersIdByName(username)
}
func (d *DefaultUserDeleter) GetUserIDFromUser() int64 {
return prompt.GetUserIdFromUser()
}

// Wait for all goroutines to finish and then close the error channel.
go func() {
wg.Wait()
close(errChan)
}()
func DeleteUser(args []string, userDeleter UserDeleter) {
// If there are command line arguments, process them concurrently.
Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UserDeleter, DefaultUserDeleter, and DeleteUser are exported but appear to be used only within this package (and by tests in the same package). Consider making them unexported (e.g., userDeleter, defaultUserDeleter, deleteUser) to avoid unintentionally expanding the public surface area of cmd/harbor/root/user.

Copilot uses AI. Check for mistakes.
@codecov
Copy link

codecov bot commented Feb 7, 2026

Codecov Report

❌ Patch coverage is 53.48837% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 7.36%. Comparing base (60ad0bd) to head (0564fec).
⚠️ Report is 83 commits behind head on main.

Files with missing lines Patch % Lines
cmd/harbor/root/user/delete.go 53.48% 19 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             main    #689      +/-   ##
=========================================
- Coverage   10.99%   7.36%   -3.63%     
=========================================
  Files         173     260      +87     
  Lines        8671   12857    +4186     
=========================================
- Hits          953     947       -6     
- Misses       7612   11800    +4188     
- Partials      106     110       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rkcoder101 rkcoder101 mentioned this pull request Feb 7, 2026
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant