-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor config and increase test coverage
- Loading branch information
1 parent
a211e7b
commit 7f64444
Showing
8 changed files
with
247 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/voltgizerz/POS-restaurant/internal/app/constants" | ||
) | ||
|
||
func TestGetUserLoginFromCtx(t *testing.T) { | ||
type args struct { | ||
ctx context.Context | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *Auth | ||
wantErr bool | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "Valid context", | ||
args: args{ctx: context.WithValue( | ||
context.WithValue( | ||
context.WithValue( | ||
context.WithValue( | ||
context.WithValue(context.Background(), constants.CTXKeyUserID, float64(1)), | ||
constants.CTXKeyUsername, "testuser"), | ||
constants.CTXKeyRoleID, float64(2)), | ||
constants.CTXKeyIsActive, true), | ||
constants.CTXKeyRequestID, "request123")}, | ||
want: &Auth{ | ||
UserID: 1, | ||
Username: "testuser", | ||
RoleID: 2, | ||
IsUserActive: true, | ||
RequestID: "request123", | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Missing userID", | ||
args: args{ctx: context.WithValue( | ||
context.WithValue( | ||
context.WithValue( | ||
context.WithValue(context.Background(), constants.CTXKeyUsername, "testuser"), | ||
constants.CTXKeyRoleID, float64(2)), | ||
constants.CTXKeyIsActive, true), | ||
constants.CTXKeyRequestID, "request123")}, | ||
want: nil, | ||
wantErr: true, | ||
expectedErr: "user ID not found in context", | ||
}, | ||
{ | ||
name: "Missing username", | ||
args: args{ctx: context.WithValue( | ||
context.WithValue( | ||
context.WithValue( | ||
context.WithValue(context.Background(), constants.CTXKeyUserID, float64(1)), | ||
constants.CTXKeyRoleID, float64(2)), | ||
constants.CTXKeyIsActive, true), | ||
constants.CTXKeyRequestID, "request123")}, | ||
want: nil, | ||
wantErr: true, | ||
expectedErr: "username not found in context", | ||
}, | ||
{ | ||
name: "Missing roleID", | ||
args: args{ctx: context.WithValue( | ||
context.WithValue( | ||
context.WithValue( | ||
context.WithValue(context.Background(), constants.CTXKeyUserID, float64(1)), | ||
constants.CTXKeyUsername, "testuser"), | ||
constants.CTXKeyIsActive, true), | ||
constants.CTXKeyRequestID, "request123")}, | ||
want: nil, | ||
wantErr: true, | ||
expectedErr: "role ID not found in context", | ||
}, | ||
{ | ||
name: "Missing isUserActive", | ||
args: args{ctx: context.WithValue( | ||
context.WithValue( | ||
context.WithValue( | ||
context.WithValue(context.Background(), constants.CTXKeyUserID, float64(1)), | ||
constants.CTXKeyUsername, "testuser"), | ||
constants.CTXKeyRoleID, float64(2)), | ||
constants.CTXKeyRequestID, "request123")}, | ||
want: nil, | ||
wantErr: true, | ||
expectedErr: "active status not found in context", | ||
}, | ||
{ | ||
name: "Missing requestID", | ||
args: args{ctx: context.WithValue( | ||
context.WithValue( | ||
context.WithValue( | ||
context.WithValue(context.Background(), constants.CTXKeyUserID, float64(1)), | ||
constants.CTXKeyUsername, "testuser"), | ||
constants.CTXKeyRoleID, float64(2)), | ||
constants.CTXKeyIsActive, true)}, | ||
want: nil, | ||
wantErr: true, | ||
expectedErr: "request ID not found in context", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := GetUserLoginFromCtx(tt.args.ctx) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("GetUserLoginFromCtx() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if err != nil && err.Error() != tt.expectedErr { | ||
t.Errorf("GetUserLoginFromCtx() error = %v, expectedErr %v", err, tt.expectedErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("GetUserLoginFromCtx() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
func TestHashPassword(t *testing.T) { | ||
type args struct { | ||
password string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Valid Password", | ||
args: args{password: "mysecretpassword"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Empty Password", | ||
args: args{password: ""}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := HashPassword(tt.args.password) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("HashPassword() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
// Check if the hashed password can be verified with the original password | ||
if err := bcrypt.CompareHashAndPassword([]byte(got), []byte(tt.args.password)); err != nil { | ||
t.Errorf("Hashed password does not match the original password: %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestVerifyPassword(t *testing.T) { | ||
type args struct { | ||
password string | ||
hash string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Correct Password", | ||
args: args{ | ||
password: "mysecretpassword", | ||
hash: func() string { | ||
hash, _ := HashPassword("mysecretpassword") | ||
return hash | ||
}(), | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Incorrect Password", | ||
args: args{ | ||
password: "wrongpassword", | ||
hash: func() string { | ||
hash, _ := HashPassword("mysecretpassword") | ||
return hash | ||
}(), | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Empty Password", | ||
args: args{ | ||
password: "", | ||
hash: func() string { | ||
hash, _ := HashPassword("") | ||
return hash | ||
}(), | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := VerifyPassword(tt.args.password, tt.args.hash) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("VerifyPassword() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters