Skip to content

Commit

Permalink
review unit tests, test in the same package
Browse files Browse the repository at this point in the history
  • Loading branch information
sc-david-voisin committed Sep 17, 2024
1 parent bb04b1c commit 9f13e0d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion db/users/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func CreateUser(ctx context.Context, app, addonUUID, username string, readonly b
return nil
}

if usernameValidation, ok := IsUsernameValid(username); !ok {
if usernameValidation, ok := isUsernameValid(username); !ok {
io.Error(usernameValidation)
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion db/users/update_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func UpdateUserPassword(ctx context.Context, app, addonUUID, username string) er
return nil
}

if usernameValidation, ok := IsUsernameValid(username); !ok {
if usernameValidation, ok := isUsernameValid(username); !ok {
io.Error(usernameValidation)
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions db/users/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func askForPasswordWithRetry(ctx context.Context, remainingRetries int) (string,
return "", "", errors.Wrap(ctx, err, "ask for password")
}

passwordValidation, ok := IsPasswordValid(password, confirmedPassword)
passwordValidation, ok := isPasswordValid(password, confirmedPassword)
if !ok {
if remainingRetries == 1 {
return "", "", errors.Newf(ctx, "%s. Too many retries", passwordValidation)
Expand Down Expand Up @@ -79,7 +79,7 @@ func askForPassword(ctx context.Context) (string, string, error) {
return string(password), string(confirmedPassword), nil
}

func IsPasswordValid(password, confirmedPassword string) (string, bool) {
func isPasswordValid(password, confirmedPassword string) (string, bool) {
if password == "" && confirmedPassword == "" {
return "", true
}
Expand All @@ -93,7 +93,7 @@ func IsPasswordValid(password, confirmedPassword string) (string, bool) {
return "", true
}

func IsUsernameValid(username string) (string, bool) {
func isUsernameValid(username string) (string, bool) {
if len(username) < 6 || len(username) > 32 {
return "Name must contain between 6 and 32 characters", false
}
Expand Down
12 changes: 5 additions & 7 deletions db/users/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package users_test
package users

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/Scalingo/cli/db/users"
)

func Test_IsPasswordValid(t *testing.T) {
func Test_isPasswordValid(t *testing.T) {
testPasswords := map[string]struct {
password string
confirmation string
Expand Down Expand Up @@ -55,15 +53,15 @@ func Test_IsPasswordValid(t *testing.T) {

for name, testCase := range testPasswords {
t.Run(name, func(t *testing.T) {
message, isValid := users.IsPasswordValid(testCase.password, testCase.confirmation)
message, isValid := isPasswordValid(testCase.password, testCase.confirmation)

assert.Equal(t, testCase.expectedValidity, isValid)
assert.Equal(t, testCase.expectedMessage, message)
})
}
}

func Test_IsUsernameValid(t *testing.T) {
func Test_isUsernameValid(t *testing.T) {
testPasswords := map[string]struct {
username string
expectedValidity bool
Expand Down Expand Up @@ -98,7 +96,7 @@ func Test_IsUsernameValid(t *testing.T) {

for name, testCase := range testPasswords {
t.Run(name, func(t *testing.T) {
message, isValid := users.IsUsernameValid(testCase.username)
message, isValid := isUsernameValid(testCase.username)

assert.Equal(t, testCase.expectedValidity, isValid)
assert.Equal(t, testCase.expectedMessage, message)
Expand Down

0 comments on commit 9f13e0d

Please sign in to comment.