Skip to content

Commit

Permalink
Fix tests for verifying otp using mfa session
Browse files Browse the repository at this point in the history
  • Loading branch information
lakhansamani committed Aug 14, 2023
1 parent 09cfad9 commit cf96a00
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
18 changes: 18 additions & 0 deletions server/resolvers/verify_otp.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ func VerifyOtpResolver(ctx context.Context, params model.VerifyOTPRequest) (*mod
mfaSession, err := cookie.GetMfaSession(gc)
if err != nil {
log.Debug("Failed to get otp request by email: ", err)
// // Ignore mfa session error in test env
// // dont trigger email sending in case of test
// envKey, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyEnv)
// if err != nil {
// envKey = ""
// }
// if envKey != constants.TestEnv {
//
// }
return res, fmt.Errorf(`invalid session: %s`, err.Error())
}

Expand Down Expand Up @@ -76,6 +85,15 @@ func VerifyOtpResolver(ctx context.Context, params model.VerifyOTPRequest) (*mod

if _, err := memorystore.Provider.GetMfaSession(user.ID, mfaSession); err != nil {
log.Debug("Failed to get mfa session: ", err)
// Ignore mfa session error in test env
// dont trigger email sending in case of test
// envKey, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyEnv)
// if err != nil {
// envKey = ""
// }
// if envKey != constants.TestEnv {
//
// }
return res, fmt.Errorf(`invalid session: %s`, err.Error())
}

Expand Down
17 changes: 17 additions & 0 deletions server/test/mobile_login_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package test

import (
"fmt"
"strings"
"testing"
"time"

"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/refs"
"github.com/authorizerdev/authorizer/server/resolvers"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -48,6 +54,17 @@ func mobileLoginTests(t *testing.T, s TestSetup) {
smsRequest, err := db.Provider.GetOTPByPhoneNumber(ctx, phoneNumber)
assert.NoError(t, err)
assert.NotEmpty(t, smsRequest.Otp)
// Get user by phone number
user, err := db.Provider.GetUserByPhoneNumber(ctx, phoneNumber)
assert.NoError(t, err)
assert.NotNil(t, user)
// Set mfa cookie session
mfaSession := uuid.NewString()
memorystore.Provider.SetMfaSession(user.ID, mfaSession, time.Now().Add(1*time.Minute).Unix())
cookie := fmt.Sprintf("%s=%s;", constants.MfaCookieName+"_session", mfaSession)
cookie = strings.TrimSuffix(cookie, ";")
req, ctx := createContext(s)
req.Header.Set("Cookie", cookie)
verifySMSRequest, err := resolvers.VerifyOtpResolver(ctx, model.VerifyOTPRequest{
PhoneNumber: &phoneNumber,
Otp: smsRequest.Otp,
Expand Down
15 changes: 15 additions & 0 deletions server/test/mobile_signup_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package test

import (
"fmt"
"strings"
"testing"
"time"

"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/refs"
"github.com/authorizerdev/authorizer/server/resolvers"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -79,6 +83,17 @@ func mobileSingupTest(t *testing.T, s TestSetup) {
otp, err := db.Provider.GetOTPByPhoneNumber(ctx, phoneNumber)
assert.Nil(t, err)
assert.NotEmpty(t, otp.Otp)
// Get user by phone number
user, err := db.Provider.GetUserByPhoneNumber(ctx, phoneNumber)
assert.NoError(t, err)
assert.NotNil(t, user)
// Set mfa cookie session
mfaSession := uuid.NewString()
memorystore.Provider.SetMfaSession(user.ID, mfaSession, time.Now().Add(1*time.Minute).Unix())
cookie := fmt.Sprintf("%s=%s;", constants.MfaCookieName+"_session", mfaSession)
cookie = strings.TrimSuffix(cookie, ";")
req, ctx := createContext(s)
req.Header.Set("Cookie", cookie)
otpRes, err := resolvers.VerifyOtpResolver(ctx, model.VerifyOTPRequest{
PhoneNumber: &phoneNumber,
Otp: otp.Otp,
Expand Down
15 changes: 15 additions & 0 deletions server/test/resend_otp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package test

import (
"context"
"fmt"
"strings"
"testing"
"time"

"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/refs"
"github.com/authorizerdev/authorizer/server/resolvers"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -89,6 +94,16 @@ func resendOTPTest(t *testing.T, s TestSetup) {
})
assert.Error(t, err)
assert.Nil(t, verifyOtpRes)
// Get user by email
user, err := db.Provider.GetUserByEmail(ctx, email)
assert.NoError(t, err)
assert.NotNil(t, user)
// Set mfa cookie session
mfaSession := uuid.NewString()
memorystore.Provider.SetMfaSession(user.ID, mfaSession, time.Now().Add(1*time.Minute).Unix())
cookie := fmt.Sprintf("%s=%s;", constants.MfaCookieName+"_session", mfaSession)
cookie = strings.TrimSuffix(cookie, ";")
req.Header.Set("Cookie", cookie)
verifyOtpRes, err = resolvers.VerifyOtpResolver(ctx, model.VerifyOTPRequest{
Email: &email,
Otp: newOtp.Otp,
Expand Down
16 changes: 15 additions & 1 deletion server/test/verify_otp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package test

import (
"context"
"fmt"
"strings"
"testing"
"time"

"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/refs"
"github.com/authorizerdev/authorizer/server/resolvers"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -63,7 +68,16 @@ func verifyOTPTest(t *testing.T, s TestSetup) {
otp, err := db.Provider.GetOTPByEmail(ctx, email)
assert.NoError(t, err)
assert.NotEmpty(t, otp.Otp)

// Get user by email
user, err := db.Provider.GetUserByEmail(ctx, email)
assert.NoError(t, err)
assert.NotNil(t, user)
// Set mfa cookie session
mfaSession := uuid.NewString()
memorystore.Provider.SetMfaSession(user.ID, mfaSession, time.Now().Add(1*time.Minute).Unix())
cookie := fmt.Sprintf("%s=%s;", constants.MfaCookieName+"_session", mfaSession)
cookie = strings.TrimSuffix(cookie, ";")
req.Header.Set("Cookie", cookie)
verifyOtpRes, err := resolvers.VerifyOtpResolver(ctx, model.VerifyOTPRequest{
Email: &email,
Otp: otp.Otp,
Expand Down

0 comments on commit cf96a00

Please sign in to comment.