Skip to content

Commit 1d8e99a

Browse files
committed
added test for custom validator
1 parent 37919ae commit 1d8e99a

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

recipe/emailpassword/updateEmailPass_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,90 @@ func TestUpdateEmailPass(t *testing.T) {
147147
assert.Equal(t, "Password must contain at least 8 characters, including a number", res3.PasswordPolicyViolatedError.FailureReason)
148148
}
149149

150+
func TestUpdateEmailPassWithCustomValidator(t *testing.T) {
151+
configValue := supertokens.TypeInput{
152+
Supertokens: &supertokens.ConnectionInfo{
153+
ConnectionURI: "http://localhost:8080",
154+
},
155+
AppInfo: supertokens.AppInfo{
156+
APIDomain: "api.supertokens.io",
157+
AppName: "SuperTokens",
158+
WebsiteDomain: "supertokens.io",
159+
},
160+
RecipeList: []supertokens.Recipe{
161+
Init(&epmodels.TypeInput{SignUpFeature: &epmodels.TypeInputSignUp{FormFields: []epmodels.TypeInputFormField{
162+
{
163+
ID: "password",
164+
Validate: func(value interface{}) *string {
165+
if len(value.(string)) > 5 {
166+
return nil
167+
}
168+
err := "Password length must be more than 5 characters"
169+
return &err
170+
},
171+
},
172+
}}}),
173+
session.Init(&sessmodels.TypeInput{
174+
GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod {
175+
return sessmodels.CookieTransferMethod
176+
},
177+
}),
178+
},
179+
}
180+
181+
BeforeEach()
182+
unittesting.StartUpST("localhost", "8080")
183+
defer AfterEach()
184+
err := supertokens.Init(configValue)
185+
if err != nil {
186+
t.Error(err.Error())
187+
}
188+
querier, err := supertokens.GetNewQuerierInstanceOrThrowError("")
189+
if err != nil {
190+
t.Error(err.Error())
191+
}
192+
cdiVersion, err := querier.GetQuerierAPIVersion()
193+
if err != nil {
194+
t.Error(err.Error())
195+
}
196+
if unittesting.MaxVersion("2.7", cdiVersion) == "2.7" {
197+
return
198+
}
199+
mux := http.NewServeMux()
200+
testServer := httptest.NewServer(supertokens.Middleware(mux))
201+
defer testServer.Close()
202+
203+
_, err = unittesting.SignupRequest("testrandom@gmail.com", "validpass123", testServer.URL)
204+
if err != nil {
205+
t.Error(err.Error())
206+
}
207+
208+
res, err := unittesting.SignInRequest("testrandom@gmail.com", "validpass123", testServer.URL)
209+
210+
if err != nil {
211+
t.Error(err.Error())
212+
}
213+
dataInBytes, err := io.ReadAll(res.Body)
214+
if err != nil {
215+
t.Error(err.Error())
216+
}
217+
res.Body.Close()
218+
219+
var data map[string]interface{}
220+
err = json.Unmarshal(dataInBytes, &data)
221+
if err != nil {
222+
t.Error(err.Error())
223+
}
224+
225+
email := "testrandom@gmail.com"
226+
password := "test"
227+
228+
res1, err := UpdateEmailOrPassword(data["user"].(map[string]interface{})["id"].(string), &email, &password, nil)
229+
assert.NotNil(t, res1.PasswordPolicyViolatedError)
230+
assert.Equal(t, "Password length must be more than 5 characters", res1.PasswordPolicyViolatedError.FailureReason)
231+
232+
}
233+
150234
func TestAPICustomResponse(t *testing.T) {
151235
configValue := supertokens.TypeInput{
152236
Supertokens: &supertokens.ConnectionInfo{

recipe/thirdpartyemailpassword/recipe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ func MakeRecipe(recipeId string, appInfo supertokens.NormalisedAppinfo, config *
5454
return Recipe{}, err
5555
}
5656
r.Config = verifiedConfig
57-
var emailPasswordRecipe emailpassword.Recipe
5857
{
5958
emailpasswordquerierInstance, err := supertokens.GetNewQuerierInstanceOrThrowError(emailpassword.RECIPE_ID)
6059
if err != nil {
@@ -71,6 +70,7 @@ func MakeRecipe(recipeId string, appInfo supertokens.NormalisedAppinfo, config *
7170
}
7271
r.APIImpl = verifiedConfig.Override.APIs(api.MakeAPIImplementation())
7372

73+
var emailPasswordRecipe emailpassword.Recipe
7474
emailPasswordRecipeImpl := recipeimplementation.MakeEmailPasswordRecipeImplementation(r.RecipeImpl)
7575
if emailDeliveryIngredient != nil {
7676
r.EmailDelivery = *emailDeliveryIngredient

0 commit comments

Comments
 (0)