MCPcopy Create free account
hub / github.com/authorizerdev/authorizer / TestSession

Function TestSession

internal/integration_tests/session_test.go:15–103  ·  view source on GitHub ↗

TestSession tests the session functionality

(t *testing.T)

Source from the content-addressed store, hash-verified

13
14// TestSession tests the session functionality
15func TestSession(t *testing.T) {
16 // Initialize test setup
17 cfg := getTestConfig()
18 ts := initTestSetup(t, cfg)
19 req, ctx := createContext(ts)
20
21 // Test setup - create a test user
22 email := "session_test_" + uuid.New().String() + "@authorizer.dev"
23 password := "Password@123"
24
25 signupReq := &model.SignUpRequest{
26 Email: &email,
27 Password: password,
28 ConfirmPassword: password,
29 }
30 res, err := ts.GraphQLProvider.SignUp(ctx, signupReq)
31 assert.NoError(t, err)
32 assert.NotNil(t, res)
33
34 // Session tests
35 t.Run("after login", func(t *testing.T) {
36 loginReq := &model.LoginRequest{
37 Email: &email,
38 Password: password,
39 }
40 loginRes, err := ts.GraphQLProvider.Login(ctx, loginReq)
41 assert.NoError(t, err)
42 assert.NotNil(t, loginRes)
43
44 // Verify response contains expected tokens
45 assert.NotEmpty(t, loginRes.AccessToken)
46 assert.NotNil(t, loginRes.User)
47 assert.Equal(t, email, *loginRes.User.Email)
48 assert.True(t, loginRes.User.EmailVerified)
49
50 t.Run("should fail without cookie", func(t *testing.T) {
51 res, err := ts.GraphQLProvider.Session(ctx, &model.SessionQueryRequest{})
52 assert.Error(t, err)
53 assert.Nil(t, res)
54 })
55
56 t.Run("should return new access token with cookie", func(t *testing.T) {
57 // Use the cookie that Login() just wrote to the response. Reading
58 // from memory store via map iteration is racy: Session()'s async
59 // session rollover leaves the previous token transiently present
60 // in memory, and a random pick can land on a token about to be
61 // deleted by the rollover goroutine.
62 sessionToken := latestAppSessionCookie(ts)
63 require.NotEmpty(t, sessionToken)
64 req.Header.Set("Cookie", fmt.Sprintf("%s=%s", constants.AppCookieName+"_session", sessionToken))
65 res, err := ts.GraphQLProvider.Session(ctx, &model.SessionQueryRequest{})
66 require.NoError(t, err)
67 require.NotNil(t, res)
68 assert.NotEmpty(t, res.AccessToken)
69 assert.NotEqual(t, res.AccessToken, res.RefreshToken)
70 assert.Equal(t, email, *res.User.Email)
71 })
72

Callers

nothing calls this directly

Calls 12

getTestConfigFunction · 0.85
initTestSetupFunction · 0.85
createContextFunction · 0.85
latestAppSessionCookieFunction · 0.85
ErrorMethod · 0.80
SignUpMethod · 0.65
LoginMethod · 0.65
SessionMethod · 0.65
SetMethod · 0.65
ParseJWTTokenMethod · 0.65
StringMethod · 0.45
RunMethod · 0.45

Tested by

no test coverage detected