Skip to content

Commit 8a0348a

Browse files
committed
fix: fixed validation and tests
1 parent 95dcb7b commit 8a0348a

File tree

3 files changed

+131
-50
lines changed

3 files changed

+131
-50
lines changed

recipe/emailpassword/api/utils.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,49 @@ package api
1717

1818
import (
1919
"encoding/json"
20-
defaultErrors "errors"
2120
"strings"
2221

2322
"github.com/supertokens/supertokens-golang/recipe/emailpassword/epmodels"
2423
"github.com/supertokens/supertokens-golang/recipe/emailpassword/errors"
24+
"github.com/supertokens/supertokens-golang/supertokens"
2525
)
2626

2727
func validateFormFieldsOrThrowError(configFormFields []epmodels.NormalisedFormField, formFieldsRaw interface{}) ([]epmodels.TypeFormField, error) {
2828
if formFieldsRaw == nil {
29-
return nil, defaultErrors.New("Missing input param: formFields")
29+
return nil, supertokens.BadInputError{
30+
Msg: "Missing input param: formFields",
31+
}
3032
}
3133

3234
if _, ok := formFieldsRaw.([]interface{}); !ok {
33-
return nil, defaultErrors.New("formFields must be an array")
35+
return nil, supertokens.BadInputError{
36+
Msg: "formFields must be an array",
37+
}
3438
}
3539

3640
var formFields []epmodels.TypeFormField
3741
for _, rawFormField := range formFieldsRaw.([]interface{}) {
3842

3943
if _, ok := rawFormField.(map[string]interface{}); !ok {
40-
return nil, defaultErrors.New("formFields must be an array of objects containing id and value of type string")
44+
return nil, supertokens.BadInputError{
45+
Msg: "formFields must be an array of objects containing id and value of type string",
46+
}
4147
}
4248

43-
if _, ok := rawFormField.(map[string]interface{})["id"].(string); !ok {
44-
return nil, defaultErrors.New("formFields must be an array of objects containing id and value of type string")
49+
if rawFormField.(map[string]interface{})["id"] != nil {
50+
if _, ok := rawFormField.(map[string]interface{})["id"].(string); !ok {
51+
return nil, supertokens.BadInputError{
52+
Msg: "formFields must be an array of objects containing id and value of type string",
53+
}
54+
}
4555
}
4656

47-
if _, ok := rawFormField.(map[string]interface{})["value"].(string); !ok {
48-
return nil, defaultErrors.New("formFields must be an array of objects containing id and value of type string")
57+
if rawFormField.(map[string]interface{})["value"] != nil {
58+
if _, ok := rawFormField.(map[string]interface{})["value"].(string); !ok {
59+
return nil, supertokens.BadInputError{
60+
Msg: "formFields must be an array of objects containing id and value of type string",
61+
}
62+
}
4963
}
5064

5165
jsonformField, err := json.Marshal(rawFormField)
@@ -77,7 +91,9 @@ func validateFormFieldsOrThrowError(configFormFields []epmodels.NormalisedFormFi
7791
func validateFormOrThrowError(configFormFields []epmodels.NormalisedFormField, inputs []epmodels.TypeFormField) error {
7892
var validationErrors []errors.ErrorPayload
7993
if len(configFormFields) != len(inputs) {
80-
return defaultErrors.New("Are you sending too many / too few formFields?")
94+
return supertokens.BadInputError{
95+
Msg: "Are you sending too many / too few formFields?",
96+
}
8197
}
8298
for _, field := range configFormFields {
8399
var input epmodels.TypeFormField

recipe/emailpassword/authFlow_test.go

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -854,8 +854,13 @@ func TestFormFieldsHasNoEmailField(t *testing.T) {
854854

855855
resp.Body.Close()
856856

857-
assert.Equal(t, "Are you sending too many / too few formFields?\n", string(dataInBytes1))
858-
assert.Equal(t, 500, resp.StatusCode)
857+
assert.Equal(t, 400, resp.StatusCode)
858+
859+
err = json.Unmarshal(dataInBytes1, &data)
860+
if err != nil {
861+
t.Error(err.Error())
862+
}
863+
assert.Equal(t, "Are you sending too many / too few formFields?", data["message"].(string))
859864

860865
}
861866

@@ -938,8 +943,12 @@ func TestFormFieldsHasNoPasswordField(t *testing.T) {
938943

939944
resp.Body.Close()
940945

941-
assert.Equal(t, "Are you sending too many / too few formFields?\n", string(dataInBytes1))
942-
assert.Equal(t, 500, resp.StatusCode)
946+
assert.Equal(t, 400, resp.StatusCode)
947+
err = json.Unmarshal(dataInBytes1, &data)
948+
if err != nil {
949+
t.Error(err.Error())
950+
}
951+
assert.Equal(t, "Are you sending too many / too few formFields?", data["message"].(string))
943952

944953
}
945954

@@ -2147,8 +2156,13 @@ func TestFormFieldsAddedInConfigButNotInInputToSignupCheckErrorAboutItBeingMissi
21472156
t.Error(err.Error())
21482157
}
21492158
res.Body.Close()
2150-
assert.Equal(t, 500, res.StatusCode)
2151-
assert.Equal(t, "Are you sending too many / too few formFields?\n", string(dataInBytes))
2159+
assert.Equal(t, 400, res.StatusCode)
2160+
var data map[string]interface{}
2161+
err = json.Unmarshal(dataInBytes, &data)
2162+
if err != nil {
2163+
t.Error(err.Error())
2164+
}
2165+
assert.Equal(t, "Are you sending too many / too few formFields?", data["message"].(string))
21522166

21532167
}
21542168

@@ -2380,16 +2394,19 @@ func TestInputFormFieldWithoutEmailField(t *testing.T) {
23802394
t.Error(err.Error())
23812395
}
23822396

2383-
assert.Equal(t, 500, resp.StatusCode)
2384-
23852397
dataInBytes, err := io.ReadAll(resp.Body)
23862398
if err != nil {
23872399
t.Error(err.Error())
23882400
}
23892401
resp.Body.Close()
23902402

2391-
assert.Equal(t, 500, resp.StatusCode)
2392-
assert.Equal(t, "Are you sending too many / too few formFields?\n", string(dataInBytes))
2403+
assert.Equal(t, 400, resp.StatusCode)
2404+
var data map[string]interface{}
2405+
err = json.Unmarshal(dataInBytes, &data)
2406+
if err != nil {
2407+
t.Error(err.Error())
2408+
}
2409+
assert.Equal(t, "Are you sending too many / too few formFields?", data["message"].(string))
23932410

23942411
}
23952412

@@ -2444,17 +2461,19 @@ func TestInputFormFieldWithoutPasswordField(t *testing.T) {
24442461
t.Error(err.Error())
24452462
}
24462463

2447-
assert.Equal(t, 500, resp.StatusCode)
2448-
24492464
dataInBytes, err := io.ReadAll(resp.Body)
24502465
if err != nil {
24512466
t.Error(err.Error())
24522467
}
24532468
resp.Body.Close()
24542469

2455-
assert.Equal(t, 500, resp.StatusCode)
2456-
assert.Equal(t, "Are you sending too many / too few formFields?\n", string(dataInBytes))
2457-
2470+
assert.Equal(t, 400, resp.StatusCode)
2471+
var data map[string]interface{}
2472+
err = json.Unmarshal(dataInBytes, &data)
2473+
if err != nil {
2474+
t.Error(err.Error())
2475+
}
2476+
assert.Equal(t, "Are you sending too many / too few formFields?", data["message"].(string))
24582477
}
24592478

24602479
func TestInputFormFieldHasADifferentNumberOfCustomFiledsThanInConfigFormFields(t *testing.T) {
@@ -2529,16 +2548,19 @@ func TestInputFormFieldHasADifferentNumberOfCustomFiledsThanInConfigFormFields(t
25292548
t.Error(err.Error())
25302549
}
25312550

2532-
assert.Equal(t, 500, resp.StatusCode)
2533-
25342551
dataInBytes, err := io.ReadAll(resp.Body)
25352552
if err != nil {
25362553
t.Error(err.Error())
25372554
}
25382555
resp.Body.Close()
25392556

2540-
assert.Equal(t, 500, resp.StatusCode)
2541-
assert.Equal(t, "Are you sending too many / too few formFields?\n", string(dataInBytes))
2557+
assert.Equal(t, 400, resp.StatusCode)
2558+
var data map[string]interface{}
2559+
err = json.Unmarshal(dataInBytes, &data)
2560+
if err != nil {
2561+
t.Error(err.Error())
2562+
}
2563+
assert.Equal(t, "Are you sending too many / too few formFields?", data["message"].(string))
25422564

25432565
}
25442566

recipe/emailpassword/formFieldValidator_test.go

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ package emailpassword
1919
import (
2020
"bytes"
2121
"encoding/json"
22-
"io/ioutil"
22+
"fmt"
23+
"io"
2324
"net/http"
2425
"net/http/httptest"
25-
"strings"
2626
"testing"
2727

2828
"github.com/stretchr/testify/assert"
@@ -98,34 +98,42 @@ func TestInvalidAPIInputForFormFields(t *testing.T) {
9898
}
9999

100100
testCases := []struct {
101-
input interface{}
102-
expected string
101+
input interface{}
102+
expected string
103+
fieldError bool
103104
}{
104105
{
105-
input: map[string]interface{}{},
106-
expected: "Missing input param: formFields",
106+
input: map[string]interface{}{},
107+
expected: "Missing input param",
108+
fieldError: false,
107109
},
108110
{
109111
input: map[string]interface{}{
110112
"formFields": "abcd",
111113
},
112-
expected: "formFields must be an array",
114+
expected: "formFields must be an array",
115+
fieldError: false,
113116
},
114117
{
115118
input: map[string]interface{}{
116119
"formFields": []string{"hello"},
117120
},
118-
expected: "formFields must be an array of objects containing id and value of type string",
121+
expected: "formFields must be an array of objects containing id and value of type string",
122+
fieldError: false,
119123
},
120124
{
121125
input: map[string]interface{}{
122126
"formFields": []map[string]interface{}{
123127
{
124128
"hello": "world",
125129
},
130+
{
131+
"world": "hello",
132+
},
126133
},
127134
},
128-
expected: "formFields must be an array of objects containing id and value of type string",
135+
expected: "Field is not optional",
136+
fieldError: true,
129137
},
130138
{
131139
input: map[string]interface{}{
@@ -135,7 +143,8 @@ func TestInvalidAPIInputForFormFields(t *testing.T) {
135143
},
136144
},
137145
},
138-
expected: "formFields must be an array of objects containing id and value of type string",
146+
expected: "formFields must be an array of objects containing id and value of type string",
147+
fieldError: false,
139148
},
140149
{
141150
input: map[string]interface{}{
@@ -146,7 +155,8 @@ func TestInvalidAPIInputForFormFields(t *testing.T) {
146155
},
147156
},
148157
},
149-
expected: "formFields must be an array of objects containing id and value of type string",
158+
expected: "formFields must be an array of objects containing id and value of type string",
159+
fieldError: false,
150160
},
151161
{
152162
input: map[string]interface{}{
@@ -157,7 +167,8 @@ func TestInvalidAPIInputForFormFields(t *testing.T) {
157167
},
158168
},
159169
},
160-
expected: "formFields must be an array of objects containing id and value of type string",
170+
expected: "formFields must be an array of objects containing id and value of type string",
171+
fieldError: false,
161172
},
162173
{
163174
input: map[string]interface{}{
@@ -167,46 +178,78 @@ func TestInvalidAPIInputForFormFields(t *testing.T) {
167178
},
168179
},
169180
},
170-
expected: "formFields must be an array of objects containing id and value of type string",
181+
expected: "formFields must be an array of objects containing id and value of type string",
182+
fieldError: false,
171183
},
172184
{
173185
input: map[string]interface{}{
174186
"formFields": []map[string]interface{}{
175187
{
176188
"id": "hello",
177189
},
190+
{
191+
"id": "world",
192+
},
178193
},
179194
},
180-
expected: "formFields must be an array of objects containing id and value of type string",
195+
expected: "Field is not optional",
196+
fieldError: true,
181197
},
182198
{
183199
input: map[string]interface{}{
184200
"formFields": []map[string]interface{}{
201+
{
202+
"value": "hello",
203+
},
185204
{
186205
"value": "world",
187206
},
188207
},
189208
},
190-
expected: "formFields must be an array of objects containing id and value of type string",
209+
expected: "Field is not optional",
210+
fieldError: true,
191211
},
192212
}
193213

194214
APIs := []string{
195215
"/auth/signup",
196216
"/auth/signin",
197-
"/auth/user/password/reset/token",
198-
"/auth/user/password/reset",
199217
}
200218

201219
for _, testCase := range testCases {
202220
for _, api := range APIs {
203221
resp, err := http.Post(testServer.URL+api, "application/json", bytes.NewBuffer(objToJson(testCase.input)))
204222
assert.NoError(t, err)
205-
assert.Equal(t, 500, resp.StatusCode)
206-
data, err := ioutil.ReadAll(resp.Body)
207-
assert.NoError(t, err)
208-
errorMessage := strings.Trim(string(data), "\n \t")
209-
assert.Equal(t, testCase.expected, errorMessage)
223+
224+
fmt.Println(testCase, api, resp.StatusCode)
225+
226+
if testCase.fieldError {
227+
assert.Equal(t, 200, resp.StatusCode)
228+
} else {
229+
assert.Equal(t, 400, resp.StatusCode)
230+
}
231+
dataInBytes1, err := io.ReadAll(resp.Body)
232+
if err != nil {
233+
t.Error(err.Error())
234+
}
235+
resp.Body.Close()
236+
var data map[string]interface{}
237+
err = json.Unmarshal(dataInBytes1, &data)
238+
if err != nil {
239+
t.Error(err.Error())
240+
}
241+
242+
// if testCase.fieldError {
243+
// assert.Equal(t, "FIELD_ERROR", data["status"].(string))
244+
245+
// for _, formField := range data["formFields"].([]interface{}) {
246+
// errorMessage := formField.(map[string]interface{})["error"]
247+
// assert.Equal(t, testCase.expected, errorMessage)
248+
// }
249+
// } else {
250+
// assert.Equal(t, testCase.expected, data["message"])
251+
// }
252+
210253
}
211254
}
212255
}

0 commit comments

Comments
 (0)