Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sc-david-voisin committed Sep 16, 2024
1 parent b5375cd commit 1a8fec1
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 6 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
8 changes: 4 additions & 4 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,9 +93,9 @@ 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
return "Name must contain between 6 and 32 characters", false
}
return "", true
}
108 changes: 108 additions & 0 deletions db/users/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package users_test

import (
"testing"

"github.com/stretchr/testify/assert"

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

func Test_IsPasswordValid(t *testing.T) {
testPasswords := map[string]struct {
password string
confirmation string
expectedValidity bool
expectedMessage string
}{
"empty": {
password: "",
confirmation: "",
expectedValidity: true,
expectedMessage: "",
},
"confirmation doesn't match": {
password: "abc",
confirmation: "aBc",
expectedValidity: false,
expectedMessage: "Password confirmation doesn't match",
},
"too short": {
password: "123456789a123456789b123",
confirmation: "123456789a123456789b123",
expectedValidity: false,
expectedMessage: "Password must contain between 24 and 64 characters",
},
"too long": {
password: "123456789a123456789b123456789c123456789d123456789e123456789f12345",
confirmation: "123456789a123456789b123456789c123456789d123456789e123456789f12345",
expectedValidity: false,
expectedMessage: "Password must contain between 24 and 64 characters",
},
"valid, short password": {
password: "123456789a123456789b1234",
confirmation: "123456789a123456789b1234",
expectedValidity: true,
expectedMessage: "",
},
"valid, log password ": {
password: "123456789a123456789b123456789c123456789d123456789e123456789f1234",
confirmation: "123456789a123456789b123456789c123456789d123456789e123456789f1234",
expectedValidity: true,
expectedMessage: "",
},
}

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

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

func Test_IsUsernameValid(t *testing.T) {
testPasswords := map[string]struct {
username string
expectedValidity bool
expectedMessage string
}{
"empty": {
username: "",
expectedValidity: false,
expectedMessage: "Name must contain between 6 and 32 characters",
},
"too short": {
username: "12345",
expectedValidity: false,
expectedMessage: "Name must contain between 6 and 32 characters",
},
"too long": {
username: "123456789a123456789b123456789c123",
expectedValidity: false,
expectedMessage: "Name must contain between 6 and 32 characters",
},
"valid, short username": {
username: "123456",
expectedValidity: true,
expectedMessage: "",
},
"valid, long username": {
username: "123456789a123456789b123456789c12",
expectedValidity: true,
expectedMessage: "",
},
}

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

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

}

Check failure on line 108 in db/users/utils_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint on a PR or from a tag

unnecessary trailing newline (whitespace)

0 comments on commit 1a8fec1

Please sign in to comment.