You've already forked golang-saas-starter-kit
mirror of
https://github.com/raseels-repos/golang-saas-starter-kit.git
synced 2025-12-21 23:57:41 +02:00
fixed internal package unittests
This commit is contained in:
@@ -1,82 +1,16 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/auth"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/tests"
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/pborman/uuid"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// mockTokenGenerator is used for testing that Authenticate calls its provided
|
||||
// token generator in a specific way.
|
||||
type mockTokenGenerator struct {
|
||||
// Private key generated by GenerateToken that is need for ParseClaims
|
||||
key *rsa.PrivateKey
|
||||
// algorithm is the method used to generate the private key.
|
||||
algorithm string
|
||||
}
|
||||
|
||||
// GenerateToken implements the TokenGenerator interface. It returns a "token"
|
||||
// that includes some information about the claims it was passed.
|
||||
func (g *mockTokenGenerator) GenerateToken(claims auth.Claims) (string, error) {
|
||||
privateKey, err := auth.Keygen()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
g.key, err = jwt.ParseRSAPrivateKeyFromPEM(privateKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
g.algorithm = "RS256"
|
||||
method := jwt.GetSigningMethod(g.algorithm)
|
||||
|
||||
tkn := jwt.NewWithClaims(method, claims)
|
||||
tkn.Header["kid"] = "1"
|
||||
|
||||
str, err := tkn.SignedString(g.key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return str, nil
|
||||
}
|
||||
|
||||
// ParseClaims recreates the Claims that were used to generate a token. It
|
||||
// verifies that the token was signed using our key.
|
||||
func (g *mockTokenGenerator) ParseClaims(tknStr string) (auth.Claims, error) {
|
||||
parser := jwt.Parser{
|
||||
ValidMethods: []string{g.algorithm},
|
||||
}
|
||||
|
||||
if g.key == nil {
|
||||
return auth.Claims{}, errors.New("Private key is empty.")
|
||||
}
|
||||
|
||||
f := func(t *jwt.Token) (interface{}, error) {
|
||||
return g.key.Public().(*rsa.PublicKey), nil
|
||||
}
|
||||
|
||||
var claims auth.Claims
|
||||
tkn, err := parser.ParseWithClaims(tknStr, &claims, f)
|
||||
if err != nil {
|
||||
return auth.Claims{}, errors.Wrap(err, "parsing token")
|
||||
}
|
||||
|
||||
if !tkn.Valid {
|
||||
return auth.Claims{}, errors.New("Invalid token")
|
||||
}
|
||||
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
// TestAuthenticate validates the behavior around authenticating users.
|
||||
func TestAuthenticate(t *testing.T) {
|
||||
defer tests.Recover(t)
|
||||
@@ -87,7 +21,7 @@ func TestAuthenticate(t *testing.T) {
|
||||
{
|
||||
ctx := tests.Context()
|
||||
|
||||
tknGen := &mockTokenGenerator{}
|
||||
tknGen := &MockTokenGenerator{}
|
||||
|
||||
// Auth tokens are valid for an our and is verified against current time.
|
||||
// Issue the token one hour ago.
|
||||
@@ -104,7 +38,7 @@ func TestAuthenticate(t *testing.T) {
|
||||
|
||||
// Create a new user for testing.
|
||||
initPass := uuid.NewRandom().String()
|
||||
user, err := Create(ctx, auth.Claims{}, test.MasterDB, CreateUserRequest{
|
||||
user, err := Create(ctx, auth.Claims{}, test.MasterDB, UserCreateRequest{
|
||||
Name: "Lee Brown",
|
||||
Email: uuid.NewRandom().String() + "@geeksinthewoods.com",
|
||||
Password: initPass,
|
||||
@@ -132,7 +66,7 @@ func TestAuthenticate(t *testing.T) {
|
||||
t.Fatalf("\t%s\tCreate user account failed.", tests.Failed)
|
||||
}
|
||||
|
||||
// Create a second new random account.
|
||||
// Create a second new random account. Need to ensure
|
||||
account2Id := uuid.NewRandom().String()
|
||||
err = mockAccount(account2Id, user.CreatedAt)
|
||||
if err != nil {
|
||||
@@ -140,9 +74,11 @@ func TestAuthenticate(t *testing.T) {
|
||||
t.Fatalf("\t%s\tCreate account failed.", tests.Failed)
|
||||
}
|
||||
|
||||
// Associate secoend new account with user user.
|
||||
// Associate second new account with user user. Need to ensure that now
|
||||
// is always greater than the first user_account entry created so it will
|
||||
// be returned consistently back in the same order, last.
|
||||
account2Role := auth.RoleUser
|
||||
err = mockUserAccount(user.ID, account2Id, user.CreatedAt, account2Role)
|
||||
err = mockUserAccount(user.ID, account2Id, user.CreatedAt.Add(time.Second), account2Role)
|
||||
if err != nil {
|
||||
t.Log("\t\tGot :", err)
|
||||
t.Fatalf("\t%s\tCreate user account failed.", tests.Failed)
|
||||
|
||||
Reference in New Issue
Block a user