Skip to content

tests: Added unit tests for user create command#688

Open
rkcoder101 wants to merge 3 commits intogoharbor:mainfrom
rkcoder101:test/user-create
Open

tests: Added unit tests for user create command#688
rkcoder101 wants to merge 3 commits intogoharbor:mainfrom
rkcoder101:test/user-create

Conversation

@rkcoder101
Copy link
Contributor

Description

This PR introduces unit tests for the user create command (cmd/harbor/root/user/create.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 :)

Copilot AI review requested due to automatic review settings February 7, 2026 09:15
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

This PR adds unit tests around the harbor user create command and introduces a small refactor in cmd/harbor/root/user/create.go to enable dependency injection for interactive prompting and API calls.

Changes:

  • Added a new create_test.go covering user creation flows (success, duplicates, unauthorized, fill-missing-fields, and basic command/flag wiring).
  • Refactored UserCreateCmd by extracting a CreateUser helper and introducing a UserCreator interface with a default implementation.
  • Hardened isUnauthorizedError to handle nil errors safely.

Reviewed changes

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

File Description
cmd/harbor/root/user/create_test.go New unit tests for create-user behavior and command configuration.
cmd/harbor/root/user/create.go Refactor to injectable user-creation logic + small nil guard in unauthorized detection.

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

Comment on lines +15 to +20
package user

import (
"bytes"
"cmp"
"fmt"
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.

PR metadata/title says this adds unit tests for the user password command, but the changes in this PR add tests/refactoring for the user create command. Please update the PR title (or description) so it accurately reflects what’s being changed/reviewed.

Copilot uses AI. Check for mistakes.
Comment on lines +26 to +41
type UserCreator interface {
FillUser(opts *create.CreateView)
UserCreate(opts create.CreateView) error
}
type DefaultUserCreator struct{}

func (d *DefaultUserCreator) FillUser(opts *create.CreateView) {
create.CreateUserView(opts)
}

func (d *DefaultUserCreator) UserCreate(opts create.CreateView) error {
return api.CreateUser(opts)
}
func CreateUser(opts *create.CreateView, userCreator UserCreator) {
var err error

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.

UserCreator, DefaultUserCreator, and CreateUser are newly exported but appear to be internal test seams for UserCreateCmd. Exporting them expands the package API surface unnecessarily (and generally implies a stability/compatibility promise). Consider making these unexported (e.g., userCreator, defaultUserCreator, createUser) and keep UserCreateCmd as the exported entry point.

Copilot uses AI. Check for mistakes.
if isUnauthorizedError(err) {
log.WithFields(log.Fields{
"action": "user create",
}).Error("Permission denied: The current account does not have the required permissions to create users.")
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.

The unauthorized/permission-denied message here differs from other user commands (most log/return "Permission denied: Admin privileges are required to execute this command."). This inconsistency can be confusing for users and makes it harder to match errors across commands/tests. Consider standardizing the message (and logging style) across the user subcommands.

Suggested change
}).Error("Permission denied: The current account does not have the required permissions to create users.")
}).Error("Permission denied: Admin privileges are required to execute this command.")

Copilot uses AI. Check for mistakes.
Comment on lines +88 to +103
usersAreEqual := func(u1, u2 []*create.CreateView) bool {
if len(u1) != len(u2) {
return false
}
slices.SortFunc(u1, func(a, b *create.CreateView) int {
return cmp.Compare(a.Username, b.Username)
})
slices.SortFunc(u2, func(a, b *create.CreateView) int {
return cmp.Compare(a.Username, b.Username)
})
for i := 0; i < len(u1); i++ {
if !reflect.DeepEqual(u1[i], u2[i]) {
return false
}
}
return true
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.

usersAreEqual sorts both input slices in-place, which mutates m.users and the table’s expectedUsers slice. This can make failures harder to diagnose and can introduce cross-test coupling if cases are ever re-used/expanded. Prefer comparing without mutation (e.g., copy before sorting, or use a matcher like assert.ElementsMatch).

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

codecov bot commented Feb 7, 2026

Codecov Report

❌ Patch coverage is 93.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 7.46%. Comparing base (60ad0bd) to head (d895b59).
⚠️ Report is 98 commits behind head on main.

Files with missing lines Patch % Lines
cmd/harbor/root/user/create.go 93.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             main    #688      +/-   ##
=========================================
- Coverage   10.99%   7.46%   -3.53%     
=========================================
  Files         173     260      +87     
  Lines        8671   12910    +4239     
=========================================
+ Hits          953     964      +11     
- Misses       7612   11837    +4225     
- Partials      106     109       +3     

☔ 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 marked this pull request as draft February 7, 2026 09:27
@rkcoder101 rkcoder101 marked this pull request as ready for review February 7, 2026 09:29
@rkcoder101 rkcoder101 changed the title tests: Added unit tests for user password command tests: Added unit tests for user create command Feb 7, 2026
@rkcoder101 rkcoder101 mentioned this pull request Feb 7, 2026
5 tasks
Signed-off-by: Rayyan Khan <rayyanrehman101@gmail.com>
Signed-off-by: Rayyan Khan <rayyanrehman101@gmail.com>
…rfaces

Signed-off-by: Rayyan Khan <rayyanrehman101@gmail.com>
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.

2 participants