Skip to content

Commit

Permalink
Add more tests for the newly added validations
Browse files Browse the repository at this point in the history
  • Loading branch information
deepjyoti30-st committed Sep 24, 2024
1 parent fafe692 commit caea0a8
Show file tree
Hide file tree
Showing 4 changed files with 322 additions and 15 deletions.
36 changes: 24 additions & 12 deletions recipe/emailpassword/api/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ func MakeAPIImplementation() epmodels.APIInterface {
var email string
for _, formField := range formFields {
if formField.ID == "email" {
valueAsString, parseErr := withValueAsString(formField.Value, "Email value needs to be a string")
valueAsString, parseErr := withValueAsString(formField.Value, "email value needs to be a string")
if parseErr != nil {
return epmodels.GeneratePasswordResetTokenPOSTResponse{}, parseErr
return epmodels.GeneratePasswordResetTokenPOSTResponse{
GeneralError: &supertokens.GeneralErrorResponse{Message: parseErr.Error()},
}, nil
}
email = valueAsString
}
Expand Down Expand Up @@ -106,9 +108,11 @@ func MakeAPIImplementation() epmodels.APIInterface {
var newPassword string
for _, formField := range formFields {
if formField.ID == "password" {
valueAsString, parseErr := withValueAsString(formField.Value, "Password value needs to be a string")
valueAsString, parseErr := withValueAsString(formField.Value, "password value needs to be a string")
if parseErr != nil {
return epmodels.ResetPasswordPOSTResponse{}, parseErr
return epmodels.ResetPasswordPOSTResponse{
GeneralError: &supertokens.GeneralErrorResponse{Message: parseErr.Error()},
}, nil
}
newPassword = valueAsString
}
Expand All @@ -135,15 +139,19 @@ func MakeAPIImplementation() epmodels.APIInterface {
var password string
for _, formField := range formFields {
if formField.ID == "email" {
valueAsString, parseErr := withValueAsString(formField.Value, "Email value needs to be a string")
valueAsString, parseErr := withValueAsString(formField.Value, "email value needs to be a string")
if parseErr != nil {
return epmodels.SignInPOSTResponse{}, parseErr
return epmodels.SignInPOSTResponse{
WrongCredentialsError: &struct{}{},
}, nil
}
email = valueAsString
} else if formField.ID == "password" {
valueAsString, parseErr := withValueAsString(formField.Value, "Password value needs to be a string")
valueAsString, parseErr := withValueAsString(formField.Value, "password value needs to be a string")
if parseErr != nil {
return epmodels.SignInPOSTResponse{}, parseErr
return epmodels.SignInPOSTResponse{
WrongCredentialsError: &struct{}{},
}, nil
}
password = valueAsString
}
Expand Down Expand Up @@ -181,15 +189,19 @@ func MakeAPIImplementation() epmodels.APIInterface {
var password string
for _, formField := range formFields {
if formField.ID == "email" {
valueAsString, parseErr := withValueAsString(formField.Value, "Email value needs to be a string")
valueAsString, parseErr := withValueAsString(formField.Value, "email value needs to be a string")
if parseErr != nil {
return epmodels.SignUpPOSTResponse{}, parseErr
return epmodels.SignUpPOSTResponse{
GeneralError: &supertokens.GeneralErrorResponse{Message: parseErr.Error()},
}, nil
}
email = valueAsString
} else if formField.ID == "password" {
valueAsString, parseErr := withValueAsString(formField.Value, "Password value needs to be a string")
valueAsString, parseErr := withValueAsString(formField.Value, "password value needs to be a string")
if parseErr != nil {
return epmodels.SignUpPOSTResponse{}, parseErr
return epmodels.SignUpPOSTResponse{
GeneralError: &supertokens.GeneralErrorResponse{Message: parseErr.Error()},
}, nil
}
password = valueAsString
}
Expand Down
7 changes: 4 additions & 3 deletions recipe/emailpassword/api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ func validateFormFieldsOrThrowError(configFormFields []epmodels.NormalisedFormFi
return nil, err
}

if formField.ID == "email" {
valueAsString, parseErr := withValueAsString(formField.Value, "Email value needs to be a string")
if formField.ID == "email" || formField.ID == "password" {
valueAsString, parseErr := withValueAsString(formField.Value, fmt.Sprintf("%s value must be a string", formField.ID))
if parseErr != nil {
return nil, supertokens.BadInputError{
Msg: "Email value must be a string",
Msg: parseErr.Error(),
}
}

formFields = append(formFields, epmodels.TypeFormField{
ID: formField.ID,
Value: strings.TrimSpace(valueAsString),
Expand Down
202 changes: 202 additions & 0 deletions recipe/emailpassword/authFlow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,208 @@ import (
"github.com/supertokens/supertokens-golang/test/unittesting"
)

func TestInvalidTypeForPassword(t *testing.T) {
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{
SignUpFeature: &epmodels.TypeInputSignUp{
FormFields: []epmodels.TypeInputFormField{},
},
}),
session.Init(&sessmodels.TypeInput{
GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod {
return sessmodels.CookieTransferMethod
},
}),
},
}

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()

formFields := map[string][]map[string]interface{}{
"formFields": {
{
"id": "password",
"value": 123,
},
{
"id": "email",
"value": "[email protected]",
},
},
}

postBody, err := json.Marshal(formFields)
if err != nil {
t.Error(err.Error())
}

resp, err := http.Post(testServer.URL+"/auth/signup", "application/json", bytes.NewBuffer(postBody))

if err != nil {
t.Error(err.Error())
}

assert.Equal(t, 400, resp.StatusCode)

dataInBytes, err := io.ReadAll(resp.Body)
if err != nil {
t.Error(err.Error())
}
resp.Body.Close()

var data map[string]interface{}
err = json.Unmarshal(dataInBytes, &data)
if err != nil {
t.Error(err.Error())
}

assert.NotNil(t, data["message"].(string))
assert.Equal(t, "password value must be a string", data["message"].(string))

// Test the signin flow
respSignIn, err := http.Post(testServer.URL+"/auth/signin", "application/json", bytes.NewBuffer(postBody))

if err != nil {
t.Error(err.Error())
}

assert.Equal(t, 400, respSignIn.StatusCode)

dataInBytesSignIn, err := io.ReadAll(respSignIn.Body)
if err != nil {
t.Error(err.Error())
}
respSignIn.Body.Close()

var dataSignIn map[string]interface{}
err = json.Unmarshal(dataInBytesSignIn, &dataSignIn)
if err != nil {
t.Error(err.Error())
}

assert.NotNil(t, dataSignIn["message"].(string))
assert.Equal(t, "password value must be a string", dataSignIn["message"].(string))
}

func TestInvalidTypeForEmail(t *testing.T) {
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{
SignUpFeature: &epmodels.TypeInputSignUp{
FormFields: []epmodels.TypeInputFormField{},
},
}),
session.Init(&sessmodels.TypeInput{
GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod {
return sessmodels.CookieTransferMethod
},
}),
},
}

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()

formFields := map[string][]map[string]interface{}{
"formFields": {
{
"id": "password",
"value": "testpw1234",
},
{
"id": "email",
"value": 1234,
},
},
}

postBody, err := json.Marshal(formFields)
if err != nil {
t.Error(err.Error())
}

resp, err := http.Post(testServer.URL+"/auth/signup", "application/json", bytes.NewBuffer(postBody))

if err != nil {
t.Error(err.Error())
}

assert.Equal(t, 400, resp.StatusCode)

dataInBytes, err := io.ReadAll(resp.Body)
if err != nil {
t.Error(err.Error())
}
resp.Body.Close()

var data map[string]interface{}
err = json.Unmarshal(dataInBytes, &data)
if err != nil {
t.Error(err.Error())
}

assert.NotNil(t, data["message"].(string))
assert.Equal(t, "email value must be a string", data["message"].(string))

// Test the signin flow
respSignIn, err := http.Post(testServer.URL+"/auth/signin", "application/json", bytes.NewBuffer(postBody))

if err != nil {
t.Error(err.Error())
}

assert.Equal(t, 400, respSignIn.StatusCode)

dataInBytesSignIn, err := io.ReadAll(respSignIn.Body)
if err != nil {
t.Error(err.Error())
}
respSignIn.Body.Close()

var dataSignIn map[string]interface{}
err = json.Unmarshal(dataInBytesSignIn, &dataSignIn)
if err != nil {
t.Error(err.Error())
}

assert.NotNil(t, dataSignIn["message"].(string))
assert.Equal(t, "email value must be a string", dataSignIn["message"].(string))
}

func TestGoodCaseInputWithOptionalAndBoolean(t *testing.T) {
optionalVal := true
configValue := supertokens.TypeInput{
Expand Down
Loading

0 comments on commit caea0a8

Please sign in to comment.