2019-05-29 03:35:08 -05:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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/google/go-cmp/cmp"
|
|
|
|
"github.com/pborman/uuid"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestAuthenticate validates the behavior around authenticating users.
|
|
|
|
func TestAuthenticate(t *testing.T) {
|
|
|
|
defer tests.Recover(t)
|
|
|
|
|
|
|
|
t.Log("Given the need to authenticate users")
|
|
|
|
{
|
|
|
|
t.Log("\tWhen handling a single User.")
|
|
|
|
{
|
|
|
|
ctx := tests.Context()
|
|
|
|
|
2019-06-24 22:41:21 -08:00
|
|
|
tknGen := &MockTokenGenerator{}
|
2019-05-29 03:35:08 -05:00
|
|
|
|
|
|
|
// Auth tokens are valid for an our and is verified against current time.
|
|
|
|
// Issue the token one hour ago.
|
|
|
|
now := time.Now().Add(time.Hour * -1)
|
|
|
|
|
|
|
|
// Try to authenticate an invalid user.
|
2019-05-29 15:05:17 -05:00
|
|
|
_, err := Authenticate(ctx, test.MasterDB, tknGen, "doesnotexist@gmail.com", "xy7", time.Hour, now)
|
2019-05-29 03:35:08 -05:00
|
|
|
if errors.Cause(err) != ErrAuthenticationFailure {
|
|
|
|
t.Logf("\t\tGot : %+v", err)
|
|
|
|
t.Logf("\t\tWant: %+v", ErrAuthenticationFailure)
|
|
|
|
t.Fatalf("\t%s\tAuthenticate non existant user failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tAuthenticate non existant user ok.", tests.Success)
|
|
|
|
|
|
|
|
// Create a new user for testing.
|
|
|
|
initPass := uuid.NewRandom().String()
|
2019-06-24 22:41:21 -08:00
|
|
|
user, err := Create(ctx, auth.Claims{}, test.MasterDB, UserCreateRequest{
|
2019-05-29 03:35:08 -05:00
|
|
|
Name: "Lee Brown",
|
|
|
|
Email: uuid.NewRandom().String() + "@geeksinthewoods.com",
|
|
|
|
Password: initPass,
|
|
|
|
PasswordConfirm: initPass,
|
|
|
|
}, now)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("\t\tGot :", err)
|
|
|
|
t.Fatalf("\t%s\tCreate user failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tCreate user ok.", tests.Success)
|
|
|
|
|
2019-06-22 17:48:44 -08:00
|
|
|
// Create a new random account.
|
2019-05-29 15:05:17 -05:00
|
|
|
account1Id := uuid.NewRandom().String()
|
2019-06-22 17:48:44 -08:00
|
|
|
err = mockAccount(account1Id, user.CreatedAt)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("\t\tGot :", err)
|
|
|
|
t.Fatalf("\t%s\tCreate account failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Associate new account with user user. This defined role should be the claims.
|
|
|
|
account1Role := auth.RoleAdmin
|
|
|
|
err = mockUserAccount(user.ID, account1Id, user.CreatedAt, account1Role)
|
2019-05-29 03:35:08 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Log("\t\tGot :", err)
|
2019-06-22 17:48:44 -08:00
|
|
|
t.Fatalf("\t%s\tCreate user account failed.", tests.Failed)
|
2019-05-29 03:35:08 -05:00
|
|
|
}
|
|
|
|
|
2019-06-24 22:41:21 -08:00
|
|
|
// Create a second new random account. Need to ensure
|
2019-05-29 15:05:17 -05:00
|
|
|
account2Id := uuid.NewRandom().String()
|
2019-06-22 17:48:44 -08:00
|
|
|
err = mockAccount(account2Id, user.CreatedAt)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("\t\tGot :", err)
|
|
|
|
t.Fatalf("\t%s\tCreate account failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
|
2019-06-24 22:41:21 -08:00
|
|
|
// 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.
|
2019-06-22 17:48:44 -08:00
|
|
|
account2Role := auth.RoleUser
|
2019-06-24 22:41:21 -08:00
|
|
|
err = mockUserAccount(user.ID, account2Id, user.CreatedAt.Add(time.Second), account2Role)
|
2019-05-29 15:05:17 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Log("\t\tGot :", err)
|
2019-06-22 17:48:44 -08:00
|
|
|
t.Fatalf("\t%s\tCreate user account failed.", tests.Failed)
|
2019-05-29 15:05:17 -05:00
|
|
|
}
|
|
|
|
|
2019-05-29 03:35:08 -05:00
|
|
|
// Add 30 minutes to now to simulate time passing.
|
|
|
|
now = now.Add(time.Minute * 30)
|
|
|
|
|
|
|
|
// Try to authenticate valid user with invalid password.
|
2019-05-29 15:05:17 -05:00
|
|
|
_, err = Authenticate(ctx, test.MasterDB, tknGen, user.Email, "xy7", time.Hour, now)
|
2019-05-29 03:35:08 -05:00
|
|
|
if errors.Cause(err) != ErrAuthenticationFailure {
|
|
|
|
t.Logf("\t\tGot : %+v", err)
|
|
|
|
t.Logf("\t\tWant: %+v", ErrAuthenticationFailure)
|
|
|
|
t.Fatalf("\t%s\tAuthenticate user w/invalid password failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tAuthenticate user w/invalid password ok.", tests.Success)
|
|
|
|
|
|
|
|
// Verify that the user can be authenticated with the created user.
|
2019-05-29 15:05:17 -05:00
|
|
|
tkn1, err := Authenticate(ctx, test.MasterDB, tknGen, user.Email, initPass, time.Hour, now)
|
2019-05-29 03:35:08 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Log("\t\tGot :", err)
|
|
|
|
t.Fatalf("\t%s\tAuthenticate user failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tAuthenticate user ok.", tests.Success)
|
|
|
|
|
|
|
|
// Ensure the token string was correctly generated.
|
2019-06-25 06:25:55 -08:00
|
|
|
claims1, err := tknGen.ParseClaims(tkn1.AccessToken)
|
2019-05-29 15:05:17 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Log("\t\tGot :", err)
|
|
|
|
t.Fatalf("\t%s\tParse claims from token failed.", tests.Failed)
|
|
|
|
} else if diff := cmp.Diff(claims1, tkn1.claims); diff != "" {
|
|
|
|
t.Fatalf("\t%s\tExpected parsed claims to match from token. Diff:\n%s", tests.Failed, diff)
|
2019-06-22 17:48:44 -08:00
|
|
|
} else if diff := cmp.Diff(claims1.Roles, []string{account1Role}); diff != "" {
|
2019-05-29 15:05:17 -05:00
|
|
|
t.Fatalf("\t%s\tExpected parsed claims roles to match user account. Diff:\n%s", tests.Failed, diff)
|
|
|
|
} else if diff := cmp.Diff(claims1.AccountIds, []string{account1Id, account2Id}); diff != "" {
|
|
|
|
t.Fatalf("\t%s\tExpected parsed claims account IDs to match the single user account. Diff:\n%s", tests.Failed, diff)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tAuthenticate parse claims from token ok.", tests.Success)
|
|
|
|
|
|
|
|
// Try switching to a second account using the first set of claims.
|
|
|
|
tkn2, err := SwitchAccount(ctx, test.MasterDB, tknGen, claims1, account2Id, time.Hour, now)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("\t\tGot :", err)
|
|
|
|
t.Fatalf("\t%s\tSwitchAccount user failed.", tests.Failed)
|
|
|
|
}
|
|
|
|
t.Logf("\t%s\tSwitchAccount user ok.", tests.Success)
|
|
|
|
|
|
|
|
// Ensure the token string was correctly generated.
|
2019-06-25 06:25:55 -08:00
|
|
|
claims2, err := tknGen.ParseClaims(tkn2.AccessToken)
|
2019-05-29 03:35:08 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Log("\t\tGot :", err)
|
|
|
|
t.Fatalf("\t%s\tParse claims from token failed.", tests.Failed)
|
2019-05-29 15:05:17 -05:00
|
|
|
} else if diff := cmp.Diff(claims2, tkn2.claims); diff != "" {
|
2019-05-29 03:35:08 -05:00
|
|
|
t.Fatalf("\t%s\tExpected parsed claims to match from token. Diff:\n%s", tests.Failed, diff)
|
2019-06-22 17:48:44 -08:00
|
|
|
} else if diff := cmp.Diff(claims2.Roles, []string{account2Role}); diff != "" {
|
2019-05-29 03:35:08 -05:00
|
|
|
t.Fatalf("\t%s\tExpected parsed claims roles to match user account. Diff:\n%s", tests.Failed, diff)
|
2019-05-29 15:05:17 -05:00
|
|
|
} else if diff := cmp.Diff(claims2.AccountIds, []string{account1Id, account2Id}); diff != "" {
|
2019-05-29 03:35:08 -05:00
|
|
|
t.Fatalf("\t%s\tExpected parsed claims account IDs to match the single user account. Diff:\n%s", tests.Failed, diff)
|
|
|
|
}
|
2019-05-29 15:05:17 -05:00
|
|
|
t.Logf("\t%s\tSwitchAccount parse claims from token ok.", tests.Success)
|
2019-05-29 03:35:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|