2021-12-24 06:27:39 +05:30
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2022-01-09 17:35:37 +05:30
|
|
|
"fmt"
|
2022-03-02 17:42:31 +05:30
|
|
|
"strings"
|
2021-12-24 06:27:39 +05:30
|
|
|
"testing"
|
|
|
|
|
2022-01-09 17:35:37 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2021-12-24 06:27:39 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/db"
|
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-05-27 23:20:38 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/memorystore"
|
2021-12-24 06:27:39 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/resolvers"
|
2022-06-12 00:27:21 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/token"
|
2021-12-24 06:27:39 +05:30
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-01-17 11:32:13 +05:30
|
|
|
func sessionTests(t *testing.T, s TestSetup) {
|
|
|
|
t.Helper()
|
2021-12-24 06:27:39 +05:30
|
|
|
t.Run(`should allow access to profile with session only`, func(t *testing.T) {
|
|
|
|
req, ctx := createContext(s)
|
|
|
|
email := "session." + s.TestInfo.Email
|
|
|
|
|
2022-01-17 11:32:13 +05:30
|
|
|
resolvers.SignupResolver(ctx, model.SignUpInput{
|
2021-12-24 06:27:39 +05:30
|
|
|
Email: email,
|
|
|
|
Password: s.TestInfo.Password,
|
|
|
|
ConfirmPassword: s.TestInfo.Password,
|
|
|
|
})
|
|
|
|
|
2022-01-24 00:32:06 +05:30
|
|
|
_, err := resolvers.SessionResolver(ctx, &model.SessionQueryInput{})
|
2021-12-24 06:27:39 +05:30
|
|
|
assert.NotNil(t, err, "unauthorized")
|
|
|
|
|
2022-07-10 21:49:33 +05:30
|
|
|
verificationRequest, err := db.Provider.GetVerificationRequestByEmail(ctx, email, constants.VerificationTypeBasicAuthSignup)
|
2022-01-17 11:32:13 +05:30
|
|
|
verifyRes, err := resolvers.VerifyEmailResolver(ctx, model.VerifyEmailInput{
|
2021-12-24 06:27:39 +05:30
|
|
|
Token: verificationRequest.Token,
|
|
|
|
})
|
|
|
|
|
2022-06-12 00:27:21 +05:30
|
|
|
accessToken := *verifyRes.AccessToken
|
|
|
|
assert.NotEmpty(t, accessToken)
|
|
|
|
|
|
|
|
claims, err := token.ParseJWTToken(accessToken)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, claims)
|
|
|
|
|
2022-06-29 22:24:00 +05:30
|
|
|
sessionKey := constants.AuthRecipeMethodBasicAuth + ":" + verifyRes.User.ID
|
|
|
|
sessionToken, err := memorystore.Provider.GetUserSession(sessionKey, constants.TokenTypeSessionToken+"_"+claims["nonce"].(string))
|
2022-06-11 19:10:39 +05:30
|
|
|
assert.NoError(t, err)
|
2022-06-12 00:27:21 +05:30
|
|
|
assert.NotEmpty(t, sessionToken)
|
|
|
|
|
|
|
|
cookie := fmt.Sprintf("%s=%s;", constants.AppCookieName+"_session", sessionToken)
|
2022-03-02 17:42:31 +05:30
|
|
|
cookie = strings.TrimSuffix(cookie, ";")
|
2022-01-09 17:35:37 +05:30
|
|
|
|
2022-01-23 01:24:41 +05:30
|
|
|
req.Header.Set("Cookie", cookie)
|
2022-01-24 00:32:06 +05:30
|
|
|
_, err = resolvers.SessionResolver(ctx, &model.SessionQueryInput{})
|
2021-12-24 06:27:39 +05:30
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
cleanData(email)
|
|
|
|
})
|
|
|
|
}
|