-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #286 from supertokens/feat/usercontext-request-helper
feat: Add a helper function to read the original request from the user context inside overrides
- Loading branch information
Showing
5 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,3 +122,101 @@ func TestDefaultUserContext(t *testing.T) { | |
assert.True(t, signInAPIContextWorks) | ||
assert.True(t, createNewSessionContextWorks) | ||
} | ||
|
||
func TestGetRequestFromUserContext(t *testing.T) { | ||
signInContextWorks := false | ||
signInAPIContextWorks := false | ||
createNewSessionContextWorks := false | ||
|
||
configValue := supertokens.TypeInput{ | ||
Supertokens: &supertokens.ConnectionInfo{ | ||
ConnectionURI: "http://localhost:8080", | ||
}, | ||
AppInfo: supertokens.AppInfo{ | ||
APIDomain: "api.supertokens.io", | ||
AppName: "SuperTokens", | ||
WebsiteDomain: "supertokens.io", | ||
}, | ||
RecipeList: []supertokens.Recipe{ | ||
Init(&epmodels.TypeInput{ | ||
Override: &epmodels.OverrideStruct{ | ||
Functions: func(originalImplementation epmodels.RecipeInterface) epmodels.RecipeInterface { | ||
originalSignIn := *originalImplementation.SignIn | ||
newSignIn := func(email string, password string, userContext supertokens.UserContext) (epmodels.SignInResponse, error) { | ||
requestFromUserContext := supertokens.GetRequestFromUserContext(userContext) | ||
if requestFromUserContext != nil { | ||
assert.True(t, requestFromUserContext.Method == "POST") | ||
assert.True(t, requestFromUserContext.RequestURI == "/auth/signin") | ||
signInContextWorks = true | ||
} | ||
return originalSignIn(email, password, userContext) | ||
} | ||
*originalImplementation.SignIn = newSignIn | ||
return originalImplementation | ||
}, | ||
|
||
APIs: func(originalImplementation epmodels.APIInterface) epmodels.APIInterface { | ||
originalSignInPOST := *originalImplementation.SignInPOST | ||
newSignInPOST := func(formFields []epmodels.TypeFormField, options epmodels.APIOptions, userContext supertokens.UserContext) (epmodels.SignInPOSTResponse, error) { | ||
requestFromUserContext := supertokens.GetRequestFromUserContext(userContext) | ||
if requestFromUserContext != nil { | ||
assert.True(t, requestFromUserContext.Method == "POST") | ||
assert.True(t, requestFromUserContext.RequestURI == "/auth/signin") | ||
signInAPIContextWorks = true | ||
} | ||
return originalSignInPOST(formFields, options, userContext) | ||
} | ||
*originalImplementation.SignInPOST = newSignInPOST | ||
return originalImplementation | ||
}, | ||
}, | ||
}), | ||
session.Init(&sessmodels.TypeInput{ | ||
GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod { | ||
return sessmodels.CookieTransferMethod | ||
}, | ||
|
||
Override: &sessmodels.OverrideStruct{ | ||
Functions: func(originalImplementation sessmodels.RecipeInterface) sessmodels.RecipeInterface { | ||
originalCreateNewSession := *originalImplementation.CreateNewSession | ||
newCreateNewSession := func(userID string, accessTokenPayload map[string]interface{}, sessionDataInDatabase map[string]interface{}, disableAntiCsrf *bool, userContext supertokens.UserContext) (sessmodels.SessionContainer, error) { | ||
requestFromUserContext := supertokens.GetRequestFromUserContext(userContext) | ||
if requestFromUserContext != nil { | ||
assert.True(t, requestFromUserContext.Method == "POST") | ||
assert.True(t, requestFromUserContext.RequestURI == "/auth/signin" || requestFromUserContext.RequestURI == "/auth/signup") | ||
createNewSessionContextWorks = true | ||
} | ||
return originalCreateNewSession(userID, accessTokenPayload, sessionDataInDatabase, disableAntiCsrf, userContext) | ||
} | ||
*originalImplementation.CreateNewSession = newCreateNewSession | ||
return originalImplementation | ||
}, | ||
}, | ||
}), | ||
}, | ||
} | ||
|
||
BeforeEach() | ||
unittesting.StartUpST("localhost", "8080") | ||
defer AfterEach() | ||
err := supertokens.Init(configValue) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
mux := http.NewServeMux() | ||
testServer := httptest.NewServer(supertokens.Middleware(mux)) | ||
defer testServer.Close() | ||
|
||
unittesting.SignupRequest("[email protected]", "validpass123", testServer.URL) | ||
|
||
res1, err := unittesting.SignInRequest("[email protected]", "validpass123", testServer.URL) | ||
|
||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
assert.Equal(t, 200, res1.StatusCode) | ||
assert.True(t, signInContextWorks) | ||
assert.True(t, signInAPIContextWorks) | ||
assert.True(t, createNewSessionContextWorks) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters