authorizer/server/test/session_test.go

58 lines
1.8 KiB
Go
Raw Normal View History

2021-12-24 00:57:39 +00:00
package test
import (
2022-01-09 12:05:37 +00:00
"fmt"
2022-03-02 12:12:31 +00:00
"strings"
2021-12-24 00:57:39 +00:00
"testing"
2022-01-09 12:05:37 +00:00
"github.com/authorizerdev/authorizer/server/constants"
2021-12-24 00:57:39 +00:00
"github.com/authorizerdev/authorizer/server/db"
"github.com/authorizerdev/authorizer/server/graph/model"
2022-05-27 17:50:38 +00:00
"github.com/authorizerdev/authorizer/server/memorystore"
2021-12-24 00:57:39 +00:00
"github.com/authorizerdev/authorizer/server/resolvers"
2022-06-11 18:57:21 +00:00
"github.com/authorizerdev/authorizer/server/token"
2021-12-24 00:57:39 +00:00
"github.com/stretchr/testify/assert"
)
2022-01-17 06:02:13 +00:00
func sessionTests(t *testing.T, s TestSetup) {
t.Helper()
2021-12-24 00:57:39 +00:00
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 06:02:13 +00:00
resolvers.SignupResolver(ctx, model.SignUpInput{
2021-12-24 00:57:39 +00:00
Email: email,
Password: s.TestInfo.Password,
ConfirmPassword: s.TestInfo.Password,
})
2022-01-23 19:02:06 +00:00
_, err := resolvers.SessionResolver(ctx, &model.SessionQueryInput{})
2021-12-24 00:57:39 +00:00
assert.NotNil(t, err, "unauthorized")
2022-01-21 08:04:04 +00:00
verificationRequest, err := db.Provider.GetVerificationRequestByEmail(email, constants.VerificationTypeBasicAuthSignup)
2022-01-17 06:02:13 +00:00
verifyRes, err := resolvers.VerifyEmailResolver(ctx, model.VerifyEmailInput{
2021-12-24 00:57:39 +00:00
Token: verificationRequest.Token,
})
2022-06-11 18:57:21 +00:00
accessToken := *verifyRes.AccessToken
assert.NotEmpty(t, accessToken)
claims, err := token.ParseJWTToken(accessToken)
assert.NoError(t, err)
assert.NotEmpty(t, claims)
sessionToken, err := memorystore.Provider.GetUserSession(verifyRes.User.ID, constants.TokenTypeSessionToken+"_"+claims["nonce"].(string))
2022-06-11 13:40:39 +00:00
assert.NoError(t, err)
2022-06-11 18:57:21 +00:00
assert.NotEmpty(t, sessionToken)
cookie := fmt.Sprintf("%s=%s;", constants.AppCookieName+"_session", sessionToken)
2022-03-02 12:12:31 +00:00
cookie = strings.TrimSuffix(cookie, ";")
2022-01-09 12:05:37 +00:00
req.Header.Set("Cookie", cookie)
2022-01-23 19:02:06 +00:00
_, err = resolvers.SessionResolver(ctx, &model.SessionQueryInput{})
2021-12-24 00:57:39 +00:00
assert.Nil(t, err)
cleanData(email)
})
}