Auth?
This commit is contained in:
110
internal/auth/auth_test.go
Normal file
110
internal/auth/auth_test.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCheckPasswordHash(t *testing.T) {
|
||||
// First, we need to create some hashed passwords for testing
|
||||
password1 := "correctPassword123!"
|
||||
password2 := "anotherPassword456!"
|
||||
hash1, _ := HashPassword(password1)
|
||||
hash2, _ := HashPassword(password2)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
password string
|
||||
hash string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Correct password",
|
||||
password: password1,
|
||||
hash: hash1,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Incorrect password",
|
||||
password: "wrongPassword",
|
||||
hash: hash1,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Password doesn't match different hash",
|
||||
password: password1,
|
||||
hash: hash2,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Empty password",
|
||||
password: "",
|
||||
hash: hash1,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Invalid hash",
|
||||
password: password1,
|
||||
hash: "invalidhash",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := CheckPasswordHash(tt.password, tt.hash)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("CheckPasswordHash() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateJWT(t *testing.T) {
|
||||
userID := uuid.New()
|
||||
validToken, _ := MakeJWT(userID, "secret", time.Hour)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
tokenString string
|
||||
tokenSecret string
|
||||
wantUserID uuid.UUID
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Valid token",
|
||||
tokenString: validToken,
|
||||
tokenSecret: "secret",
|
||||
wantUserID: userID,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Invalid token",
|
||||
tokenString: "invalid.token.string",
|
||||
tokenSecret: "secret",
|
||||
wantUserID: uuid.Nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Wrong secret",
|
||||
tokenString: validToken,
|
||||
tokenSecret: "wrong_secret",
|
||||
wantUserID: uuid.Nil,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotUserID, err := ValidateJWT(tt.tokenString, tt.tokenSecret)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ValidateJWT() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if gotUserID != tt.wantUserID {
|
||||
t.Errorf("ValidateJWT() gotUserID = %v, want %v", gotUserID, tt.wantUserID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user