-
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 #389 from supertokens/appinfo-refactor
app info refactor
- Loading branch information
Showing
44 changed files
with
902 additions
and
222 deletions.
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
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
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 |
---|---|---|
|
@@ -455,3 +455,106 @@ func TestValidTokenInputAndPasswordHasChanged(t *testing.T) { | |
assert.Equal(t, userInfo["id"], result3["user"].(map[string]interface{})["id"].(string)) | ||
assert.Equal(t, userInfo["email"], result3["user"].(map[string]interface{})["email"].(string)) | ||
} | ||
|
||
func TestPasswordResetLinkUsesOriginFunctionIfProvided(t *testing.T) { | ||
resetURL := "" | ||
tokenInfo := "" | ||
ridInfo := "" | ||
sendEmailFunc := func(input emaildelivery.EmailType, userContext supertokens.UserContext) error { | ||
u, err := url.Parse(input.PasswordReset.PasswordResetLink) | ||
if err != nil { | ||
return err | ||
} | ||
resetURL = u.Scheme + "://" + u.Host + u.Path | ||
tokenInfo = u.Query().Get("token") | ||
ridInfo = u.Query().Get("rid") | ||
return nil | ||
} | ||
configValue := supertokens.TypeInput{ | ||
Supertokens: &supertokens.ConnectionInfo{ | ||
ConnectionURI: "http://localhost:8080", | ||
}, | ||
AppInfo: supertokens.AppInfo{ | ||
APIDomain: "api.supertokens.io", | ||
AppName: "SuperTokens", | ||
GetOrigin: func(request *http.Request, userContext supertokens.UserContext) (string, error) { | ||
// read request body | ||
decoder := json.NewDecoder(request.Body) | ||
var requestBody map[string]interface{} | ||
err := decoder.Decode(&requestBody) | ||
if err != nil { | ||
return "https://supertokens.com", nil | ||
} | ||
if requestBody["origin"] == nil { | ||
return "https://supertokens.com", nil | ||
} | ||
return requestBody["origin"].(string), nil | ||
}, | ||
}, | ||
RecipeList: []supertokens.Recipe{ | ||
Init(&epmodels.TypeInput{ | ||
EmailDelivery: &emaildelivery.TypeInput{ | ||
Service: &emaildelivery.EmailDeliveryInterface{ | ||
SendEmail: &sendEmailFunc, | ||
}, | ||
}, | ||
}), | ||
session.Init(nil), | ||
}, | ||
} | ||
|
||
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() | ||
|
||
res, err := unittesting.SignupRequest("[email protected]", "validpass123", testServer.URL) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
assert.NoError(t, err) | ||
dataInBytes, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
res.Body.Close() | ||
var result map[string]interface{} | ||
err = json.Unmarshal(dataInBytes, &result) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
assert.Equal(t, 200, res.StatusCode) | ||
assert.Equal(t, "OK", result["status"]) | ||
|
||
formFields := map[string]interface{}{ | ||
"origin": "localhost:2000", | ||
"formFields": []map[string]interface{}{{ | ||
"id": "email", | ||
"value": "[email protected]", | ||
}}, | ||
} | ||
|
||
postBody, err := json.Marshal(formFields) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
resp, err := http.Post(testServer.URL+"/auth/user/password/reset/token", "application/json", bytes.NewBuffer(postBody)) | ||
|
||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, 200, resp.StatusCode) | ||
assert.Equal(t, "http://localhost:2000/auth/reset-password", resetURL) | ||
assert.NotEmpty(t, tokenInfo) | ||
assert.True(t, strings.HasPrefix(ridInfo, "emailpassword")) | ||
} |
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
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 |
---|---|---|
|
@@ -26,6 +26,7 @@ import ( | |
"github.com/supertokens/supertokens-golang/recipe/session" | ||
"github.com/supertokens/supertokens-golang/recipe/session/sessmodels" | ||
"github.com/supertokens/supertokens-golang/supertokens" | ||
"github.com/supertokens/supertokens-golang/test/unittesting" | ||
) | ||
|
||
func TestBackwardCompatibilityServiceWithoutCustomFunction(t *testing.T) { | ||
|
@@ -286,6 +287,60 @@ func TestSMTPServiceOverrideDefaultEmailTemplate(t *testing.T) { | |
assert.Equal(t, sendRawEmailCalled, true) | ||
} | ||
|
||
func TestThatLinkUsesResultFromOriginFunction(t *testing.T) { | ||
link := "" | ||
configValue := supertokens.TypeInput{ | ||
Supertokens: &supertokens.ConnectionInfo{ | ||
ConnectionURI: "http://localhost:8080", | ||
}, | ||
AppInfo: supertokens.AppInfo{ | ||
APIDomain: "api.supertokens.io", | ||
AppName: "SuperTokens", | ||
GetOrigin: func(request *http.Request, userContext supertokens.UserContext) (string, error) { | ||
return (*userContext)["link"].(string), nil | ||
}, | ||
}, | ||
RecipeList: []supertokens.Recipe{ | ||
Init(evmodels.TypeInput{ | ||
Mode: "OPTIONAL", | ||
EmailDelivery: &emaildelivery.TypeInput{ | ||
Override: func(originalImplementation emaildelivery.EmailDeliveryInterface) emaildelivery.EmailDeliveryInterface { | ||
(*originalImplementation.SendEmail) = func(input emaildelivery.EmailType, userContext supertokens.UserContext) error { | ||
link = input.EmailVerification.EmailVerifyLink | ||
return nil | ||
} | ||
return originalImplementation | ||
}, | ||
}, | ||
}), | ||
session.Init(nil), | ||
}, | ||
} | ||
|
||
BeforeEach() | ||
unittesting.StartUpST("localhost", "8080") | ||
defer AfterEach() | ||
err := supertokens.Init(configValue) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
email := "[email protected]" | ||
resp, err := SendEmailVerificationEmail("public", "userId", &email, &map[string]interface{}{ | ||
"link": "localhost:8080", | ||
}) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
assert.True(t, resp.OK != nil) | ||
|
||
assert.Equal(t, EmailVerificationEmailSentForTest, false) | ||
// assert that link starts with http://localhost:8080. We use starts with because the link | ||
// can continue a path and random query params too | ||
assert.Equal(t, link[:21], "http://localhost:8080") | ||
|
||
} | ||
|
||
// func TestSMTPServiceManually(t *testing.T) { | ||
// targetEmail := "..." | ||
// fromEmail := "[email protected]" | ||
|
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
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
Oops, something went wrong.