|
17 | 17 | package emailpassword |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "bytes" |
| 21 | + "encoding/json" |
| 22 | + "io/ioutil" |
| 23 | + "net/http" |
| 24 | + "net/http/httptest" |
| 25 | + "strings" |
20 | 26 | "testing" |
21 | 27 |
|
22 | 28 | "github.com/stretchr/testify/assert" |
| 29 | + "github.com/supertokens/supertokens-golang/supertokens" |
| 30 | + "github.com/supertokens/supertokens-golang/test/unittesting" |
23 | 31 | ) |
24 | 32 |
|
25 | 33 | func TestDefaultEmailValidator(t *testing.T) { |
@@ -56,3 +64,149 @@ func TestDefaultPasswordValidator(t *testing.T) { |
56 | 64 |
|
57 | 65 | assert.Equal(t, "Password must contain at least one alphabet", *defaultPasswordValidator("234235234523")) |
58 | 66 | } |
| 67 | + |
| 68 | +func TestInvalidAPIInputForFormFields(t *testing.T) { |
| 69 | + configValue := supertokens.TypeInput{ |
| 70 | + Supertokens: &supertokens.ConnectionInfo{ |
| 71 | + ConnectionURI: "http://localhost:8080", |
| 72 | + }, |
| 73 | + AppInfo: supertokens.AppInfo{ |
| 74 | + APIDomain: "api.supertokens.io", |
| 75 | + AppName: "SuperTokens", |
| 76 | + WebsiteDomain: "supertokens.io", |
| 77 | + }, |
| 78 | + RecipeList: []supertokens.Recipe{ |
| 79 | + Init(nil), |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + BeforeEach() |
| 84 | + unittesting.StartUpST("localhost", "8080") |
| 85 | + defer AfterEach() |
| 86 | + err := supertokens.Init(configValue) |
| 87 | + if err != nil { |
| 88 | + t.Error(err.Error()) |
| 89 | + } |
| 90 | + mux := http.NewServeMux() |
| 91 | + testServer := httptest.NewServer(supertokens.Middleware(mux)) |
| 92 | + defer testServer.Close() |
| 93 | + |
| 94 | + objToJson := func(obj interface{}) []byte { |
| 95 | + jsonBytes, err := json.Marshal(obj) |
| 96 | + assert.NoError(t, err) |
| 97 | + return jsonBytes |
| 98 | + } |
| 99 | + |
| 100 | + testCases := []struct { |
| 101 | + input interface{} |
| 102 | + expected string |
| 103 | + }{ |
| 104 | + { |
| 105 | + input: map[string]interface{}{}, |
| 106 | + expected: "Missing input param: formFields", |
| 107 | + }, |
| 108 | + { |
| 109 | + input: map[string]interface{}{ |
| 110 | + "formFields": "abcd", |
| 111 | + }, |
| 112 | + expected: "formFields must be an array", |
| 113 | + }, |
| 114 | + { |
| 115 | + input: map[string]interface{}{ |
| 116 | + "formFields": []string{"hello"}, |
| 117 | + }, |
| 118 | + expected: "formFields must be an array of objects containing id and value of type string", |
| 119 | + }, |
| 120 | + { |
| 121 | + input: map[string]interface{}{ |
| 122 | + "formFields": []map[string]interface{}{ |
| 123 | + { |
| 124 | + "hello": "world", |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + expected: "formFields must be an array of objects containing id and value of type string", |
| 129 | + }, |
| 130 | + { |
| 131 | + input: map[string]interface{}{ |
| 132 | + "formFields": []map[string]interface{}{ |
| 133 | + { |
| 134 | + "id": 1, |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + expected: "formFields must be an array of objects containing id and value of type string", |
| 139 | + }, |
| 140 | + { |
| 141 | + input: map[string]interface{}{ |
| 142 | + "formFields": []map[string]interface{}{ |
| 143 | + { |
| 144 | + "id": 1, |
| 145 | + "value": "world", |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + expected: "formFields must be an array of objects containing id and value of type string", |
| 150 | + }, |
| 151 | + { |
| 152 | + input: map[string]interface{}{ |
| 153 | + "formFields": []map[string]interface{}{ |
| 154 | + { |
| 155 | + "id": "hello", |
| 156 | + "value": 1, |
| 157 | + }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + expected: "formFields must be an array of objects containing id and value of type string", |
| 161 | + }, |
| 162 | + { |
| 163 | + input: map[string]interface{}{ |
| 164 | + "formFields": []map[string]interface{}{ |
| 165 | + { |
| 166 | + "value": 1, |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + expected: "formFields must be an array of objects containing id and value of type string", |
| 171 | + }, |
| 172 | + { |
| 173 | + input: map[string]interface{}{ |
| 174 | + "formFields": []map[string]interface{}{ |
| 175 | + { |
| 176 | + "id": "hello", |
| 177 | + }, |
| 178 | + }, |
| 179 | + }, |
| 180 | + expected: "formFields must be an array of objects containing id and value of type string", |
| 181 | + }, |
| 182 | + { |
| 183 | + input: map[string]interface{}{ |
| 184 | + "formFields": []map[string]interface{}{ |
| 185 | + { |
| 186 | + "value": "world", |
| 187 | + }, |
| 188 | + }, |
| 189 | + }, |
| 190 | + expected: "formFields must be an array of objects containing id and value of type string", |
| 191 | + }, |
| 192 | + } |
| 193 | + |
| 194 | + APIs := []string{ |
| 195 | + "/auth/signup", |
| 196 | + "/auth/signin", |
| 197 | + "/auth/user/password/reset/token", |
| 198 | + "/auth/user/password/reset", |
| 199 | + } |
| 200 | + |
| 201 | + for _, testCase := range testCases { |
| 202 | + for _, api := range APIs { |
| 203 | + resp, err := http.Post(testServer.URL+api, "application/json", bytes.NewBuffer(objToJson(testCase.input))) |
| 204 | + 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) |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments