diff --git a/server/test/admin_login_test.go b/server/test/admin_login_test.go index 8ed80a1..4b8cc85 100644 --- a/server/test/admin_login_test.go +++ b/server/test/admin_login_test.go @@ -20,8 +20,10 @@ func adminLoginTests(t *testing.T, s TestSetup) { assert.NotNil(t, err) + adminSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret) + assert.Nil(t, err) _, err = resolvers.AdminLoginResolver(ctx, model.AdminLoginInput{ - AdminSecret: memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret), + AdminSecret: adminSecret, }) assert.Nil(t, err) diff --git a/server/test/admin_logout_test.go b/server/test/admin_logout_test.go index 44ad39a..d37b140 100644 --- a/server/test/admin_logout_test.go +++ b/server/test/admin_logout_test.go @@ -18,9 +18,12 @@ func adminLogoutTests(t *testing.T, s TestSetup) { _, err := resolvers.AdminLogoutResolver(ctx) assert.NotNil(t, err) - h, err := crypto.EncryptPassword(memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret)) + adminSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret) assert.Nil(t, err) - req.Header.Set("Cookie", fmt.Sprintf("%s=%s", memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminCookieName), h)) + + h, err := crypto.EncryptPassword(adminSecret) + assert.Nil(t, err) + req.Header.Set("Cookie", fmt.Sprintf("%s=%s", constants.AdminCookieName, h)) _, err = resolvers.AdminLogoutResolver(ctx) assert.Nil(t, err) diff --git a/server/test/admin_session_test.go b/server/test/admin_session_test.go index 48c84a8..fb28c96 100644 --- a/server/test/admin_session_test.go +++ b/server/test/admin_session_test.go @@ -18,9 +18,12 @@ func adminSessionTests(t *testing.T, s TestSetup) { _, err := resolvers.AdminSessionResolver(ctx) assert.NotNil(t, err) - h, err := crypto.EncryptPassword(memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret)) + adminSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret) assert.Nil(t, err) - req.Header.Set("Cookie", fmt.Sprintf("%s=%s", memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminCookieName), h)) + + h, err := crypto.EncryptPassword(adminSecret) + assert.Nil(t, err) + req.Header.Set("Cookie", fmt.Sprintf("%s=%s", constants.AdminCookieName, h)) _, err = resolvers.AdminSessionResolver(ctx) assert.Nil(t, err) diff --git a/server/test/admin_signup_test.go b/server/test/admin_signup_test.go index 2f1f006..6aedc17 100644 --- a/server/test/admin_signup_test.go +++ b/server/test/admin_signup_test.go @@ -20,7 +20,7 @@ func adminSignupTests(t *testing.T, s TestSetup) { assert.NotNil(t, err) // reset env for test to pass - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyAdminSecret, "") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyAdminSecret, "") _, err = resolvers.AdminSignupResolver(ctx, model.AdminSignupInput{ AdminSecret: "admin123", diff --git a/server/test/delete_user_test.go b/server/test/delete_user_test.go index baf0419..2e11cf2 100644 --- a/server/test/delete_user_test.go +++ b/server/test/delete_user_test.go @@ -27,10 +27,12 @@ func deleteUserTest(t *testing.T, s TestSetup) { Email: email, }) assert.NotNil(t, err, "unauthorized") - - h, err := crypto.EncryptPassword(memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret)) + adminSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret) assert.Nil(t, err) - req.Header.Set("Cookie", fmt.Sprintf("%s=%s", memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminCookieName), h)) + + h, err := crypto.EncryptPassword(adminSecret) + assert.Nil(t, err) + req.Header.Set("Cookie", fmt.Sprintf("%s=%s", constants.AdminCookieName, h)) _, err = resolvers.DeleteUserResolver(ctx, model.DeleteUserInput{ Email: email, diff --git a/server/test/enable_access_test.go b/server/test/enable_access_test.go index 45fce78..cc57be3 100644 --- a/server/test/enable_access_test.go +++ b/server/test/enable_access_test.go @@ -28,10 +28,11 @@ func enableAccessTest(t *testing.T, s TestSetup) { }) assert.NoError(t, err) assert.NotNil(t, verifyRes.AccessToken) - - h, err := crypto.EncryptPassword(memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret)) + adminSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret) assert.Nil(t, err) - req.Header.Set("Cookie", fmt.Sprintf("%s=%s", memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminCookieName), h)) + h, err := crypto.EncryptPassword(adminSecret) + assert.Nil(t, err) + req.Header.Set("Cookie", fmt.Sprintf("%s=%s", constants.AdminCookieName, h)) res, err := resolvers.RevokeAccessResolver(ctx, model.UpdateAccessInput{ UserID: verifyRes.User.ID, diff --git a/server/test/env_file_test.go b/server/test/env_file_test.go index 021d164..113a066 100644 --- a/server/test/env_file_test.go +++ b/server/test/env_file_test.go @@ -6,21 +6,23 @@ import ( "github.com/authorizerdev/authorizer/server/constants" "github.com/authorizerdev/authorizer/server/env" "github.com/authorizerdev/authorizer/server/memorystore" + "github.com/authorizerdev/authorizer/server/utils" "github.com/stretchr/testify/assert" ) func TestEnvs(t *testing.T) { - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyEnvPath, "../../.env.sample") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyEnvPath, "../../.env.sample") env.InitAllEnv() - store := memorystore.Provider.GetEnvStoreClone() + store, err := memorystore.Provider.GetEnvStore() + assert.Nil(t, err) - assert.Equal(t, store.StringEnv[constants.EnvKeyEnv], "production") - assert.False(t, store.BoolEnv[constants.EnvKeyDisableEmailVerification]) - assert.False(t, store.BoolEnv[constants.EnvKeyDisableMagicLinkLogin]) - assert.False(t, store.BoolEnv[constants.EnvKeyDisableBasicAuthentication]) - assert.Equal(t, store.StringEnv[constants.EnvKeyJwtType], "RS256") - assert.Equal(t, store.StringEnv[constants.EnvKeyJwtRoleClaim], "role") - assert.EqualValues(t, store.SliceEnv[constants.EnvKeyRoles], []string{"user"}) - assert.EqualValues(t, store.SliceEnv[constants.EnvKeyDefaultRoles], []string{"user"}) - assert.EqualValues(t, store.SliceEnv[constants.EnvKeyAllowedOrigins], []string{"*"}) + assert.Equal(t, store[constants.EnvKeyEnv].(string), "production") + assert.False(t, store[constants.EnvKeyDisableEmailVerification].(bool)) + assert.False(t, store[constants.EnvKeyDisableMagicLinkLogin].(bool)) + assert.False(t, store[constants.EnvKeyDisableBasicAuthentication].(bool)) + assert.Equal(t, store[constants.EnvKeyJwtType].(string), "RS256") + assert.Equal(t, store[constants.EnvKeyJwtRoleClaim].(string), "role") + assert.EqualValues(t, utils.ConvertInterfaceToStringSlice(store[constants.EnvKeyRoles]), []string{"user"}) + assert.EqualValues(t, utils.ConvertInterfaceToStringSlice(store[constants.EnvKeyDefaultRoles]), []string{"user"}) + assert.EqualValues(t, utils.ConvertInterfaceToStringSlice(store[constants.EnvKeyAllowedOrigins]), []string{"*"}) } diff --git a/server/test/env_test.go b/server/test/env_test.go index ad18873..b02cca7 100644 --- a/server/test/env_test.go +++ b/server/test/env_test.go @@ -18,12 +18,15 @@ func envTests(t *testing.T, s TestSetup) { _, err := resolvers.EnvResolver(ctx) assert.NotNil(t, err) - h, err := crypto.EncryptPassword(memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret)) + adminSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret) assert.Nil(t, err) - req.Header.Set("Cookie", fmt.Sprintf("%s=%s", memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminCookieName), h)) + + h, err := crypto.EncryptPassword(adminSecret) + assert.Nil(t, err) + req.Header.Set("Cookie", fmt.Sprintf("%s=%s", constants.AdminCookieName, h)) res, err := resolvers.EnvResolver(ctx) assert.Nil(t, err) - assert.Equal(t, *res.AdminSecret, memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret)) + assert.Equal(t, *res.AdminSecret, adminSecret) }) } diff --git a/server/test/generate_jwt_keys_test.go b/server/test/generate_jwt_keys_test.go index 9362209..e9ef639 100644 --- a/server/test/generate_jwt_keys_test.go +++ b/server/test/generate_jwt_keys_test.go @@ -30,9 +30,13 @@ func generateJWTkeyTest(t *testing.T, s TestSetup) { assert.Error(t, err) assert.Nil(t, res) }) - h, err := crypto.EncryptPassword(memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret)) + + adminSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret) assert.Nil(t, err) - req.Header.Set("Cookie", fmt.Sprintf("%s=%s", memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminCookieName), h)) + + h, err := crypto.EncryptPassword(adminSecret) + assert.Nil(t, err) + req.Header.Set("Cookie", fmt.Sprintf("%s=%s", constants.AdminCookieName, h)) t.Run(`should generate HS256 secret`, func(t *testing.T) { res, err := resolvers.GenerateJWTKeysResolver(ctx, model.GenerateJWTKeysInput{ Type: "HS256", diff --git a/server/test/invite_member_test.go b/server/test/invite_member_test.go index ee7298d..42bc017 100644 --- a/server/test/invite_member_test.go +++ b/server/test/invite_member_test.go @@ -26,9 +26,12 @@ func inviteUserTest(t *testing.T, s TestSetup) { assert.Error(t, err) assert.Nil(t, res) - h, err := crypto.EncryptPassword(memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret)) + adminSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret) assert.Nil(t, err) - req.Header.Set("Cookie", fmt.Sprintf("%s=%s", memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminCookieName), h)) + + h, err := crypto.EncryptPassword(adminSecret) + assert.Nil(t, err) + req.Header.Set("Cookie", fmt.Sprintf("%s=%s", constants.AdminCookieName, h)) // invalid emails test invalidEmailsTest := []string{ diff --git a/server/test/jwt_test.go b/server/test/jwt_test.go index a0ae203..9695406 100644 --- a/server/test/jwt_test.go +++ b/server/test/jwt_test.go @@ -15,10 +15,14 @@ import ( func TestJwt(t *testing.T) { // persist older data till test is done and then reset it - jwtType := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyJwtType) - publicKey := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyJwtPublicKey) - privateKey := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyJwtPrivateKey) - clientID := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyClientID) + jwtType, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyJwtType) + assert.Nil(t, err) + publicKey, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyJwtPublicKey) + assert.Nil(t, err) + privateKey, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyJwtPrivateKey) + assert.Nil(t, err) + clientID, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyClientID) + assert.Nil(t, err) nonce := uuid.New().String() hostname := "localhost" subject := "test" @@ -33,14 +37,14 @@ func TestJwt(t *testing.T) { } t.Run("invalid jwt type", func(t *testing.T) { - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtType, "invalid") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtType, "invalid") token, err := token.SignJWTToken(claims) assert.Error(t, err, "unsupported signing method") assert.Empty(t, token) }) t.Run("expired jwt token", func(t *testing.T) { - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtType, "HS256") - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtSecret, "test") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtType, "HS256") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtSecret, "test") expiredClaims := jwt.MapClaims{ "exp": time.Now().Add(-time.Minute * 30).Unix(), "iat": time.Now().Unix(), @@ -52,9 +56,9 @@ func TestJwt(t *testing.T) { assert.Error(t, err, err.Error(), "Token is expired") }) t.Run("HMAC algorithms", func(t *testing.T) { - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtSecret, "test") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtSecret, "test") t.Run("HS256", func(t *testing.T) { - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtType, "HS256") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtType, "HS256") jwtToken, err := token.SignJWTToken(claims) assert.NoError(t, err) assert.NotEmpty(t, jwtToken) @@ -63,7 +67,7 @@ func TestJwt(t *testing.T) { assert.Equal(t, c["email"].(string), claims["email"]) }) t.Run("HS384", func(t *testing.T) { - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtType, "HS384") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtType, "HS384") jwtToken, err := token.SignJWTToken(claims) assert.NoError(t, err) assert.NotEmpty(t, jwtToken) @@ -72,7 +76,7 @@ func TestJwt(t *testing.T) { assert.Equal(t, c["email"].(string), claims["email"]) }) t.Run("HS512", func(t *testing.T) { - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtType, "HS512") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtType, "HS512") jwtToken, err := token.SignJWTToken(claims) assert.NoError(t, err) assert.NotEmpty(t, jwtToken) @@ -86,9 +90,9 @@ func TestJwt(t *testing.T) { t.Run("RS256", func(t *testing.T) { _, privateKey, publickKey, _, err := crypto.NewRSAKey("RS256", clientID) assert.NoError(t, err) - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtType, "RS256") - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtPrivateKey, privateKey) - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtPublicKey, publickKey) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtType, "RS256") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtPrivateKey, privateKey) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtPublicKey, publickKey) jwtToken, err := token.SignJWTToken(claims) assert.NoError(t, err) assert.NotEmpty(t, jwtToken) @@ -99,9 +103,9 @@ func TestJwt(t *testing.T) { t.Run("RS384", func(t *testing.T) { _, privateKey, publickKey, _, err := crypto.NewRSAKey("RS384", clientID) assert.NoError(t, err) - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtType, "RS384") - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtPrivateKey, privateKey) - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtPublicKey, publickKey) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtType, "RS384") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtPrivateKey, privateKey) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtPublicKey, publickKey) jwtToken, err := token.SignJWTToken(claims) assert.NoError(t, err) assert.NotEmpty(t, jwtToken) @@ -112,9 +116,9 @@ func TestJwt(t *testing.T) { t.Run("RS512", func(t *testing.T) { _, privateKey, publickKey, _, err := crypto.NewRSAKey("RS512", clientID) assert.NoError(t, err) - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtType, "RS512") - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtPrivateKey, privateKey) - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtPublicKey, publickKey) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtType, "RS512") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtPrivateKey, privateKey) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtPublicKey, publickKey) jwtToken, err := token.SignJWTToken(claims) assert.NoError(t, err) assert.NotEmpty(t, jwtToken) @@ -128,9 +132,9 @@ func TestJwt(t *testing.T) { t.Run("ES256", func(t *testing.T) { _, privateKey, publickKey, _, err := crypto.NewECDSAKey("ES256", clientID) assert.NoError(t, err) - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtType, "ES256") - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtPrivateKey, privateKey) - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtPublicKey, publickKey) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtType, "ES256") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtPrivateKey, privateKey) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtPublicKey, publickKey) jwtToken, err := token.SignJWTToken(claims) assert.NoError(t, err) assert.NotEmpty(t, jwtToken) @@ -141,9 +145,9 @@ func TestJwt(t *testing.T) { t.Run("ES384", func(t *testing.T) { _, privateKey, publickKey, _, err := crypto.NewECDSAKey("ES384", clientID) assert.NoError(t, err) - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtType, "ES384") - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtPrivateKey, privateKey) - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtPublicKey, publickKey) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtType, "ES384") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtPrivateKey, privateKey) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtPublicKey, publickKey) jwtToken, err := token.SignJWTToken(claims) assert.NoError(t, err) assert.NotEmpty(t, jwtToken) @@ -154,9 +158,9 @@ func TestJwt(t *testing.T) { t.Run("ES512", func(t *testing.T) { _, privateKey, publickKey, _, err := crypto.NewECDSAKey("ES512", clientID) assert.NoError(t, err) - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtType, "ES512") - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtPrivateKey, privateKey) - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtPublicKey, publickKey) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtType, "ES512") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtPrivateKey, privateKey) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtPublicKey, publickKey) jwtToken, err := token.SignJWTToken(claims) assert.NoError(t, err) assert.NotEmpty(t, jwtToken) @@ -166,7 +170,7 @@ func TestJwt(t *testing.T) { }) }) - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtType, jwtType) - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtPublicKey, publicKey) - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyJwtPrivateKey, privateKey) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtType, jwtType) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtPublicKey, publicKey) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyJwtPrivateKey, privateKey) } diff --git a/server/test/login_test.go b/server/test/login_test.go index c1ef347..b2243e9 100644 --- a/server/test/login_test.go +++ b/server/test/login_test.go @@ -6,7 +6,6 @@ import ( "github.com/authorizerdev/authorizer/server/constants" "github.com/authorizerdev/authorizer/server/db" "github.com/authorizerdev/authorizer/server/graph/model" - "github.com/authorizerdev/authorizer/server/memorystore" "github.com/authorizerdev/authorizer/server/resolvers" "github.com/authorizerdev/authorizer/server/utils" "github.com/stretchr/testify/assert" @@ -15,7 +14,6 @@ import ( func loginTests(t *testing.T, s TestSetup) { t.Helper() t.Run(`should login`, func(t *testing.T) { - t.Logf("=> is enabled: %v", memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableEmailVerification)) _, ctx := createContext(s) email := "login." + s.TestInfo.Email _, err := resolvers.SignupResolver(ctx, model.SignUpInput{ diff --git a/server/test/logout_test.go b/server/test/logout_test.go index f71f25f..3b38e3f 100644 --- a/server/test/logout_test.go +++ b/server/test/logout_test.go @@ -33,7 +33,7 @@ func logoutTests(t *testing.T, s TestSetup) { // set all they keys in cookie one of them should be session cookie for key := range sessions { if key != token { - cookie += fmt.Sprintf("%s=%s;", memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyCookieName)+"_session", key) + cookie += fmt.Sprintf("%s=%s;", constants.AppCookieName+"_session", key) } } diff --git a/server/test/magic_link_login_test.go b/server/test/magic_link_login_test.go index fcc64bc..cb6df35 100644 --- a/server/test/magic_link_login_test.go +++ b/server/test/magic_link_login_test.go @@ -17,13 +17,13 @@ func magicLinkLoginTests(t *testing.T, s TestSetup) { t.Run(`should login with magic link`, func(t *testing.T) { req, ctx := createContext(s) email := "magic_link_login." + s.TestInfo.Email - memorystore.Provider.UpdateEnvVariable(constants.BoolStoreIdentifier, constants.EnvKeyDisableSignUp, true) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyDisableSignUp, true) _, err := resolvers.MagicLinkLoginResolver(ctx, model.MagicLinkLoginInput{ Email: email, }) assert.NotNil(t, err, "signup disabled") - memorystore.Provider.UpdateEnvVariable(constants.BoolStoreIdentifier, constants.EnvKeyDisableSignUp, false) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyDisableSignUp, false) _, err = resolvers.MagicLinkLoginResolver(ctx, model.MagicLinkLoginInput{ Email: email, }) diff --git a/server/test/resolvers_test.go b/server/test/resolvers_test.go index aadfc74..710d840 100644 --- a/server/test/resolvers_test.go +++ b/server/test/resolvers_test.go @@ -19,8 +19,8 @@ func TestResolvers(t *testing.T) { for dbType, dbURL := range databases { s := testSetup() - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyDatabaseURL, dbURL) - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyDatabaseType, dbType) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyDatabaseURL, dbURL) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyDatabaseType, dbType) defer s.Server.Close() err := db.InitDB() if err != nil { @@ -35,8 +35,8 @@ func TestResolvers(t *testing.T) { } env.PersistEnv() - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyEnv, "test") - memorystore.Provider.UpdateEnvVariable(constants.BoolStoreIdentifier, constants.EnvKeyIsProd, false) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyEnv, "test") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyIsProd, false) t.Run("should pass tests for "+dbType, func(t *testing.T) { // admin tests adminSignupTests(t, s) diff --git a/server/test/revoke_access_test.go b/server/test/revoke_access_test.go index 2a86e53..88fe167 100644 --- a/server/test/revoke_access_test.go +++ b/server/test/revoke_access_test.go @@ -34,9 +34,11 @@ func revokeAccessTest(t *testing.T, s TestSetup) { }) assert.Error(t, err) - h, err := crypto.EncryptPassword(memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret)) + adminSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret) assert.Nil(t, err) - req.Header.Set("Cookie", fmt.Sprintf("%s=%s", memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminCookieName), h)) + h, err := crypto.EncryptPassword(adminSecret) + assert.Nil(t, err) + req.Header.Set("Cookie", fmt.Sprintf("%s=%s", constants.AdminCookieName, h)) res, err = resolvers.RevokeAccessResolver(ctx, model.UpdateAccessInput{ UserID: verifyRes.User.ID, diff --git a/server/test/session_test.go b/server/test/session_test.go index 94d18d2..2c4de19 100644 --- a/server/test/session_test.go +++ b/server/test/session_test.go @@ -39,7 +39,7 @@ func sessionTests(t *testing.T, s TestSetup) { // set all they keys in cookie one of them should be session cookie for key := range sessions { if key != token { - cookie += fmt.Sprintf("%s=%s;", memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyCookieName)+"_session", key) + cookie += fmt.Sprintf("%s=%s;", constants.AppCookieName+"_session", key) } } cookie = strings.TrimSuffix(cookie, ";") diff --git a/server/test/signup_test.go b/server/test/signup_test.go index 85afe3c..1e34429 100644 --- a/server/test/signup_test.go +++ b/server/test/signup_test.go @@ -30,7 +30,7 @@ func signupTests(t *testing.T, s TestSetup) { }) assert.NotNil(t, err, "invalid password") - memorystore.Provider.UpdateEnvVariable(constants.BoolStoreIdentifier, constants.EnvKeyDisableSignUp, true) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyDisableSignUp, true) res, err = resolvers.SignupResolver(ctx, model.SignUpInput{ Email: email, Password: s.TestInfo.Password, @@ -38,7 +38,7 @@ func signupTests(t *testing.T, s TestSetup) { }) assert.NotNil(t, err, "singup disabled") - memorystore.Provider.UpdateEnvVariable(constants.BoolStoreIdentifier, constants.EnvKeyDisableSignUp, false) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyDisableSignUp, false) res, err = resolvers.SignupResolver(ctx, model.SignUpInput{ Email: email, Password: s.TestInfo.Password, diff --git a/server/test/test.go b/server/test/test.go index efed0d7..4a4a4ba 100644 --- a/server/test/test.go +++ b/server/test/test.go @@ -75,14 +75,14 @@ func testSetup() TestSetup { Password: "Test@123", } - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyEnvPath, "../../.env.sample") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyEnvPath, "../../.env.sample") memorystore.InitMemStore() - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeySmtpHost, "smtp.yopmail.com") - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeySmtpPort, "2525") - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeySmtpUsername, "lakhan@yopmail.com") - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeySmtpPassword, "test") - memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeySenderEmail, "info@yopmail.com") - memorystore.Provider.UpdateEnvVariable(constants.SliceStoreIdentifier, constants.EnvKeyProtectedRoles, []string{"admin"}) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeySmtpHost, "smtp.yopmail.com") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeySmtpPort, "2525") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeySmtpUsername, "lakhan@yopmail.com") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeySmtpPassword, "test") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeySenderEmail, "info@yopmail.com") + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyProtectedRoles, []string{"admin"}) memorystore.InitMemStore() db.InitDB() env.InitAllEnv() diff --git a/server/test/update_env_test.go b/server/test/update_env_test.go index 172f329..8c09c17 100644 --- a/server/test/update_env_test.go +++ b/server/test/update_env_test.go @@ -16,16 +16,19 @@ func updateEnvTests(t *testing.T, s TestSetup) { t.Helper() t.Run(`should update envs`, func(t *testing.T) { req, ctx := createContext(s) - originalAppURL := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAppURL) + originalAppURL, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAppURL) + assert.Nil(t, err) data := model.UpdateEnvInput{} - _, err := resolvers.UpdateEnvResolver(ctx, data) + _, err = resolvers.UpdateEnvResolver(ctx, data) assert.NotNil(t, err) - h, err := crypto.EncryptPassword(memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret)) + adminSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret) assert.Nil(t, err) - req.Header.Set("Cookie", fmt.Sprintf("%s=%s", memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminCookieName), h)) + h, err := crypto.EncryptPassword(adminSecret) + assert.Nil(t, err) + req.Header.Set("Cookie", fmt.Sprintf("%s=%s", constants.AdminCookieName, h)) newURL := "https://test.com" disableLoginPage := true allowedOrigins := []string{"http://localhost:8080"} @@ -35,11 +38,19 @@ func updateEnvTests(t *testing.T, s TestSetup) { AllowedOrigins: allowedOrigins, } _, err = resolvers.UpdateEnvResolver(ctx, data) - assert.Nil(t, err) - assert.Equal(t, memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAppURL), newURL) - assert.True(t, memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableLoginPage)) - assert.Equal(t, memorystore.Provider.GetSliceStoreEnvVariable(constants.EnvKeyAllowedOrigins), allowedOrigins) + + appURL, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAppURL) + assert.Nil(t, err) + assert.Equal(t, appURL, newURL) + + isLoginPageDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableLoginPage) + assert.Nil(t, err) + assert.True(t, isLoginPageDisabled) + + storedOrigins, err := memorystore.Provider.GetSliceStoreEnvVariable(constants.EnvKeyAllowedOrigins) + assert.Nil(t, err) + assert.Equal(t, storedOrigins, allowedOrigins) disableLoginPage = false data = model.UpdateEnvInput{ diff --git a/server/test/update_user_test.go b/server/test/update_user_test.go index f820bf1..5fc2cd9 100644 --- a/server/test/update_user_test.go +++ b/server/test/update_user_test.go @@ -33,10 +33,11 @@ func updateUserTest(t *testing.T, s TestSetup) { Roles: newRoles, }) assert.NotNil(t, err, "unauthorized") - - h, err := crypto.EncryptPassword(memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret)) + adminSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret) assert.Nil(t, err) - req.Header.Set("Cookie", fmt.Sprintf("%s=%s", memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminCookieName), h)) + h, err := crypto.EncryptPassword(adminSecret) + assert.Nil(t, err) + req.Header.Set("Cookie", fmt.Sprintf("%s=%s", constants.AdminCookieName, h)) _, err = resolvers.UpdateUserResolver(ctx, model.UpdateUserInput{ ID: user.ID, Roles: newRoles, @@ -44,7 +45,7 @@ func updateUserTest(t *testing.T, s TestSetup) { // supplier is not part of envs assert.Error(t, err) adminRole = "admin" - memorystore.Provider.UpdateEnvVariable(constants.SliceStoreIdentifier, constants.EnvKeyProtectedRoles, []string{adminRole}) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyProtectedRoles, []string{adminRole}) newRoles = []*string{&adminRole, &userRole} _, err = resolvers.UpdateUserResolver(ctx, model.UpdateUserInput{ ID: user.ID, diff --git a/server/test/urls_test.go b/server/test/urls_test.go index 9ec5119..3ec2a53 100644 --- a/server/test/urls_test.go +++ b/server/test/urls_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/authorizerdev/authorizer/server/parsers" - "github.com/authorizerdev/authorizer/server/utils" "github.com/stretchr/testify/assert" ) @@ -21,7 +20,7 @@ func TestGetHostName(t *testing.T) { func TestGetDomainName(t *testing.T) { url := "http://test.herokuapp.com" - got := utils.GetDomainName(url) + got := parsers.GetDomainName(url) want := "herokuapp.com" assert.Equal(t, got, want, "domain name should be equal") diff --git a/server/test/users_test.go b/server/test/users_test.go index 4f73aab..96e6537 100644 --- a/server/test/users_test.go +++ b/server/test/users_test.go @@ -35,9 +35,11 @@ func usersTest(t *testing.T, s TestSetup) { usersRes, err := resolvers.UsersResolver(ctx, pagination) assert.NotNil(t, err, "unauthorized") - h, err := crypto.EncryptPassword(memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret)) + adminSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret) assert.Nil(t, err) - req.Header.Set("Cookie", fmt.Sprintf("%s=%s", memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminCookieName), h)) + h, err := crypto.EncryptPassword(adminSecret) + assert.Nil(t, err) + req.Header.Set("Cookie", fmt.Sprintf("%s=%s", constants.AdminCookieName, h)) usersRes, err = resolvers.UsersResolver(ctx, pagination) assert.Nil(t, err) diff --git a/server/test/validator_test.go b/server/test/validator_test.go index 037673b..ffa68bf 100644 --- a/server/test/validator_test.go +++ b/server/test/validator_test.go @@ -5,7 +5,6 @@ import ( "github.com/authorizerdev/authorizer/server/constants" "github.com/authorizerdev/authorizer/server/memorystore" - "github.com/authorizerdev/authorizer/server/utils" "github.com/authorizerdev/authorizer/server/validators" "github.com/stretchr/testify/assert" ) @@ -23,7 +22,7 @@ func TestIsValidEmail(t *testing.T) { func TestIsValidOrigin(t *testing.T) { // don't use portocal(http/https) for ALLOWED_ORIGINS while testing, // as we trim them off while running the main function - memorystore.Provider.UpdateEnvVariable(constants.SliceStoreIdentifier, constants.EnvKeyAllowedOrigins, []string{"localhost:8080", "*.google.com", "*.google.in", "*abc.*"}) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyAllowedOrigins, []string{"localhost:8080", "*.google.com", "*.google.in", "*abc.*"}) assert.False(t, validators.IsValidOrigin("http://myapp.com"), "it should be invalid origin") assert.False(t, validators.IsValidOrigin("http://appgoogle.com"), "it should be invalid origin") assert.True(t, validators.IsValidOrigin("http://app.google.com"), "it should be valid origin") @@ -33,20 +32,20 @@ func TestIsValidOrigin(t *testing.T) { assert.True(t, validators.IsValidOrigin("http://xyx.abc.in"), "it should be valid origin") assert.True(t, validators.IsValidOrigin("http://xyxabc.in"), "it should be valid origin") assert.True(t, validators.IsValidOrigin("http://localhost:8080"), "it should be valid origin") - memorystore.Provider.UpdateEnvVariable(constants.SliceStoreIdentifier, constants.EnvKeyAllowedOrigins, []string{"*"}) + memorystore.Provider.UpdateEnvVariable(constants.EnvKeyAllowedOrigins, []string{"*"}) } func TestIsValidIdentifier(t *testing.T) { - assert.False(t, utils.IsValidVerificationIdentifier("test"), "it should be invalid identifier") - assert.True(t, utils.IsValidVerificationIdentifier(constants.VerificationTypeBasicAuthSignup), "it should be valid identifier") - assert.True(t, utils.IsValidVerificationIdentifier(constants.VerificationTypeUpdateEmail), "it should be valid identifier") - assert.True(t, utils.IsValidVerificationIdentifier(constants.VerificationTypeForgotPassword), "it should be valid identifier") + assert.False(t, validators.IsValidVerificationIdentifier("test"), "it should be invalid identifier") + assert.True(t, validators.IsValidVerificationIdentifier(constants.VerificationTypeBasicAuthSignup), "it should be valid identifier") + assert.True(t, validators.IsValidVerificationIdentifier(constants.VerificationTypeUpdateEmail), "it should be valid identifier") + assert.True(t, validators.IsValidVerificationIdentifier(constants.VerificationTypeForgotPassword), "it should be valid identifier") } func TestIsValidPassword(t *testing.T) { - assert.False(t, utils.IsValidPassword("test"), "it should be invalid password") - assert.False(t, utils.IsValidPassword("Te@1"), "it should be invalid password") - assert.False(t, utils.IsValidPassword("n*rp7GGTd29V{xx%{pDb@7n{](SD.!+.Mp#*$EHDGk&$pAMf7e#432Sg,Gr](j3n]jV/3F8BJJT+9u9{q=8zK:8u!rpQBaXJp%A+7r!jQj)M(vC$UX,h;;WKm$U6i#7dBnC&2ryKzKd+(y&=Ud)hErT/j;v3t..CM).8nS)9qLtV7pmP;@2QuzDyGfL7KB()k:BpjAGL@bxD%r5gcBfh7$&wutk!wzMfPFY#nkjjqyZbEHku,{jc;gvbYq2)3w=KExnYz9Vbv:;*;?f##faxkULdMpmm&yEfePixzx+[{[38zGN;3TzF;6M#Xy_tMtx:yK*n$bc(bPyGz%EYkC&]ttUF@#aZ%$QZ:u!icF@+"), "it should be invalid password") - assert.False(t, utils.IsValidPassword("test@123"), "it should be invalid password") - assert.True(t, utils.IsValidPassword("Test@123"), "it should be valid password") + assert.False(t, validators.IsValidPassword("test"), "it should be invalid password") + assert.False(t, validators.IsValidPassword("Te@1"), "it should be invalid password") + assert.False(t, validators.IsValidPassword("n*rp7GGTd29V{xx%{pDb@7n{](SD.!+.Mp#*$EHDGk&$pAMf7e#432Sg,Gr](j3n]jV/3F8BJJT+9u9{q=8zK:8u!rpQBaXJp%A+7r!jQj)M(vC$UX,h;;WKm$U6i#7dBnC&2ryKzKd+(y&=Ud)hErT/j;v3t..CM).8nS)9qLtV7pmP;@2QuzDyGfL7KB()k:BpjAGL@bxD%r5gcBfh7$&wutk!wzMfPFY#nkjjqyZbEHku,{jc;gvbYq2)3w=KExnYz9Vbv:;*;?f##faxkULdMpmm&yEfePixzx+[{[38zGN;3TzF;6M#Xy_tMtx:yK*n$bc(bPyGz%EYkC&]ttUF@#aZ%$QZ:u!icF@+"), "it should be invalid password") + assert.False(t, validators.IsValidPassword("test@123"), "it should be invalid password") + assert.True(t, validators.IsValidPassword("Test@123"), "it should be valid password") } diff --git a/server/test/verification_requests_test.go b/server/test/verification_requests_test.go index e61ae56..8cbb762 100644 --- a/server/test/verification_requests_test.go +++ b/server/test/verification_requests_test.go @@ -39,10 +39,12 @@ func verificationRequestsTest(t *testing.T, s TestSetup) { requests, err := resolvers.VerificationRequestsResolver(ctx, pagination) assert.NotNil(t, err, "unauthorized") - - h, err := crypto.EncryptPassword(memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret)) + adminSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret) assert.Nil(t, err) - req.Header.Set("Cookie", fmt.Sprintf("%s=%s", memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAdminCookieName), h)) + + h, err := crypto.EncryptPassword(adminSecret) + assert.Nil(t, err) + req.Header.Set("Cookie", fmt.Sprintf("%s=%s", constants.AdminCookieName, h)) requests, err = resolvers.VerificationRequestsResolver(ctx, pagination) assert.Nil(t, err)