1
0
mirror of https://github.com/raseels-repos/golang-saas-starter-kit.git synced 2025-06-15 00:15:15 +02:00

fixed internal package unittests

This commit is contained in:
Lee Brown
2019-06-24 22:41:21 -08:00
parent ca8670eadf
commit 8994ee4d1a
33 changed files with 1285 additions and 343 deletions

View File

@ -20,13 +20,77 @@ func TestMain(m *testing.M) {
}
func testMain(m *testing.M) int {
tests.DisableDb = true
test = tests.New()
defer test.TearDown()
return m.Run()
}
func TestAuthenticator(t *testing.T) {
// TestAuthenticatorFile validates File storage.
func TestAuthenticatorFile(t *testing.T) {
var authTests = []struct {
name string
now time.Time
keyExpiration time.Duration
error error
}{
{"NoKeyExpiration", time.Now(), time.Duration(0), nil},
{"KeyExpirationOk", time.Now(), time.Duration(time.Second * 3600), nil},
{"KeyExpirationDisabled", time.Now().Add(time.Second * 3600 * 3), time.Duration(time.Second * 3600), nil},
}
// Generate the token.
signedClaims := auth.Claims{
Roles: []string{auth.RoleAdmin},
}
t.Log("Given the need to validate initiating a new Authenticator using File storage by key expiration.")
{
for i, tt := range authTests {
t.Logf("\tTest: %d\tWhen running test: %s", i, tt.name)
{
a, err := auth.NewAuthenticatorFile("", tt.now, tt.keyExpiration)
if err != tt.error {
t.Log("\t\tGot :", err)
t.Log("\t\tWant:", tt.error)
t.Fatalf("\t%s\tNewAuthenticatorFile failed.", tests.Failed)
}
tknStr, err := a.GenerateToken(signedClaims)
if err != nil {
t.Log("\t\tGot :", err)
t.Fatalf("\t%s\tGenerateToken failed.", tests.Failed)
}
parsedClaims, err := a.ParseClaims(tknStr)
if err != nil {
t.Log("\t\tGot :", err)
t.Fatalf("\t%s\tParseClaims failed.", tests.Failed)
}
// Assert expected claims.
if exp, got := len(signedClaims.Roles), len(parsedClaims.Roles); exp != got {
t.Log("\t\tGot :", got)
t.Log("\t\tWant:", exp)
t.Fatalf("\t%s\tShould got the same number of roles.", tests.Failed)
}
if exp, got := signedClaims.Roles[0], parsedClaims.Roles[0]; exp != got {
t.Log("\t\tGot :", got)
t.Log("\t\tWant:", exp)
t.Fatalf("\t%s\tShould got the same role name.", tests.Failed)
}
t.Logf("\t%s\tNewAuthenticatorFile ok.", tests.Success)
}
}
}
}
// TestAuthenticatorAws validates AWS storage.
func TestAuthenticatorAws(t *testing.T) {
awsSecretID := "jwt-key" + uuid.NewRandom().String()
@ -58,16 +122,16 @@ func TestAuthenticator(t *testing.T) {
Roles: []string{auth.RoleAdmin},
}
t.Log("Given the need to validate initiating a new Authenticator by key expiration.")
t.Log("Given the need to validate initiating a new Authenticator using AWS storage by key expiration.")
{
for i, tt := range authTests {
t.Logf("\tTest: %d\tWhen running test: %s", i, tt.name)
{
a, err := auth.NewAuthenticator(test.AwsSession, tt.awsSecretID, tt.now, tt.keyExpiration)
a, err := auth.NewAuthenticatorAws(test.AwsSession, tt.awsSecretID, tt.now, tt.keyExpiration)
if err != tt.error {
t.Log("\t\tGot :", err)
t.Log("\t\tWant:", tt.error)
t.Fatalf("\t%s\tNewAuthenticator failed.", tests.Failed)
t.Fatalf("\t%s\tNewAuthenticatorAws failed.", tests.Failed)
}
tknStr, err := a.GenerateToken(signedClaims)
@ -94,7 +158,7 @@ func TestAuthenticator(t *testing.T) {
t.Fatalf("\t%s\tShould got the same role name.", tests.Failed)
}
t.Logf("\t%s\tNewAuthenticator ok.", tests.Success)
t.Logf("\t%s\tNewAuthenticatorAws ok.", tests.Success)
}
}
}