@@ -17,6 +17,7 @@ import (
17
17
"github.com/SkynetLabs/skynet-accounts/jwt"
18
18
"github.com/SkynetLabs/skynet-accounts/skynet"
19
19
"github.com/SkynetLabs/skynet-accounts/test"
20
+ "github.com/SkynetLabs/skynet-accounts/types"
20
21
"gitlab.com/NebulousLabs/errors"
21
22
"gitlab.com/NebulousLabs/fastrand"
22
23
"gitlab.com/SkynetLabs/skyd/skymodules"
@@ -101,7 +102,7 @@ func testHandlerHealthGET(t *testing.T, at *test.AccountsTester) {
101
102
func testHandlerUserPOST (t * testing.T , at * test.AccountsTester ) {
102
103
// Use the test's name as an email-compatible identifier.
103
104
name := test .DBNameForTest (t .Name ())
104
- emailAddr := name + "@siasky.net"
105
+ emailAddr := types . NewEmail ( name + "@siasky.net" )
105
106
password := hex .EncodeToString (fastrand .Bytes (16 ))
106
107
// Try to create a user with a missing email.
107
108
_ , _ , err := at .UserPOST ("" , password )
@@ -119,12 +120,12 @@ func testHandlerUserPOST(t *testing.T, at *test.AccountsTester) {
119
120
t .Fatalf ("Expected user creation to fail with '%s', got '%s'. Body: '%s'" , badRequest , err , string (b ))
120
121
}
121
122
// Try to create a user with an empty password.
122
- _ , b , err = at .UserPOST (emailAddr , "" )
123
+ _ , b , err = at .UserPOST (emailAddr . String () , "" )
123
124
if err == nil || ! strings .Contains (err .Error (), badRequest ) {
124
125
t .Fatalf ("Expected user creation to fail with '%s', got '%s'. Body: '%s" , badRequest , err , string (b ))
125
126
}
126
127
// Create a user.
127
- _ , b , err = at .UserPOST (emailAddr , password )
128
+ _ , b , err = at .UserPOST (emailAddr . String () , password )
128
129
if err != nil {
129
130
t .Fatalf ("User creation failed. Error: '%s'. Body: '%s' " , err .Error (), string (b ))
130
131
}
@@ -147,23 +148,23 @@ func testHandlerUserPOST(t *testing.T, at *test.AccountsTester) {
147
148
}
148
149
}(u )
149
150
// Log in with that user in order to make sure it exists.
150
- _ , b , err = at .LoginCredentialsPOST (emailAddr , password )
151
+ _ , b , err = at .LoginCredentialsPOST (emailAddr . String () , password )
151
152
if err != nil {
152
153
t .Fatalf ("Login failed. Error: '%s'. Body: '%s'" , err .Error (), string (b ))
153
154
}
154
155
// try to create a user with an already taken email
155
- _ , b , err = at .UserPOST (emailAddr , "password" )
156
+ _ , b , err = at .UserPOST (emailAddr . String () , "password" )
156
157
if err == nil || ! strings .Contains (err .Error (), badRequest ) {
157
158
t .Fatalf ("Expected user creation to fail with '%s', got '%s'. Body: '%s'" , badRequest , err , string (b ))
158
159
}
159
160
}
160
161
161
162
// testHandlerLoginPOST tests the /login endpoint.
162
163
func testHandlerLoginPOST (t * testing.T , at * test.AccountsTester ) {
163
- emailAddr := test .DBNameForTest (t .Name ()) + "@siasky.net"
164
+ emailAddr := types . NewEmail ( test .DBNameForTest (t .Name ()) + "@siasky.net" )
164
165
password := hex .EncodeToString (fastrand .Bytes (16 ))
165
166
// Try logging in with a non-existent user.
166
- _ , _ , err := at .LoginCredentialsPOST (emailAddr , password )
167
+ _ , _ , err := at .LoginCredentialsPOST (emailAddr . String () , password )
167
168
if err == nil || ! strings .Contains (err .Error (), unauthorized ) {
168
169
t .Fatalf ("Expected '%s', got '%s'" , unauthorized , err )
169
170
}
@@ -177,7 +178,7 @@ func testHandlerLoginPOST(t *testing.T, at *test.AccountsTester) {
177
178
}
178
179
}()
179
180
// Login with an existing user.
180
- r , _ , err := at .LoginCredentialsPOST (emailAddr , password )
181
+ r , _ , err := at .LoginCredentialsPOST (emailAddr . String () , password )
181
182
if err != nil {
182
183
t .Fatal (err )
183
184
}
@@ -186,6 +187,12 @@ func testHandlerLoginPOST(t *testing.T, at *test.AccountsTester) {
186
187
if c == nil {
187
188
t .Fatal ("Expected a cookie." )
188
189
}
190
+ // Login with an email with a different capitalisation.
191
+ // Expect this to succeed.
192
+ _ , _ , err = at .LoginCredentialsPOST (strings .ToUpper (emailAddr .String ()), password )
193
+ if err != nil {
194
+ t .Fatal (err )
195
+ }
189
196
// Make sure the returned cookie is usable for making requests.
190
197
at .SetCookie (c )
191
198
defer at .ClearCredentials ()
@@ -222,7 +229,7 @@ func testHandlerLoginPOST(t *testing.T, at *test.AccountsTester) {
222
229
t .Fatalf ("Expected %s, got %s" , unauthorized , err )
223
230
}
224
231
// Try logging in with a bad password.
225
- _ , _ , err = at .LoginCredentialsPOST (emailAddr , "bad password" )
232
+ _ , _ , err = at .LoginCredentialsPOST (emailAddr . String () , "bad password" )
226
233
if err == nil || ! strings .Contains (err .Error (), unauthorized ) {
227
234
t .Fatalf ("Expected '%s', got '%s'" , unauthorized , err )
228
235
}
@@ -295,17 +302,17 @@ func testUserPUT(t *testing.T, at *test.AccountsTester) {
295
302
}
296
303
// Check if we can login with the new password.
297
304
params := url.Values {}
298
- params .Set ("email" , u .Email )
305
+ params .Set ("email" , u .Email . String () )
299
306
params .Set ("password" , pw )
300
307
// Try logging in with a non-existent user.
301
- _ , _ , err = at .LoginCredentialsPOST (u .Email , pw )
308
+ _ , _ , err = at .LoginCredentialsPOST (u .Email . String () , pw )
302
309
if err != nil {
303
310
t .Fatal (err )
304
311
}
305
312
306
313
// Update the user's email.
307
- emailAddr := name + "_new@siasky.net"
308
- _ , status , err = at .UserPUT (emailAddr , "" , "" )
314
+ emailAddr := types . NewEmail ( name + "_new@siasky.net" )
315
+ _ , status , err = at .UserPUT (emailAddr . String () , "" , "" )
309
316
if err != nil || status != http .StatusOK {
310
317
t .Fatal (status , err )
311
318
}
@@ -323,14 +330,34 @@ func testUserPUT(t *testing.T, at *test.AccountsTester) {
323
330
t .Fatalf ("Expected the user to have a non-empty confirmation token, got '%s'" , u3 .EmailConfirmationToken )
324
331
}
325
332
// Expect to find a confirmation email queued for sending.
326
- filer := bson.M {"to" : emailAddr }
333
+ filer := bson.M {"to" : emailAddr . String () }
327
334
_ , msgs , err := at .DB .FindEmails (at .Ctx , filer , & options.FindOptions {})
328
335
if err != nil {
329
336
t .Fatal (err )
330
337
}
331
338
if len (msgs ) != 1 || msgs [0 ].Subject != "Please verify your email address" {
332
339
t .Fatal ("Expected to find a single confirmation email but didn't." )
333
340
}
341
+ // Update the user's email to a mixed-case string, expect it to be persisted
342
+ // as lowercase only.
343
+ emailStr := name + "_ThIsIsMiXeDcAsE@siasky.net"
344
+ _ , status , err = at .UserPUT (emailStr , "" , "" )
345
+ if err != nil || status != http .StatusOK {
346
+ t .Fatal (status , err )
347
+ }
348
+ // Fetch the user by the mixed-case email. Expect this to succeed because we
349
+ // cast the email to lowercase in the UserPUT handler.
350
+ u4 , err := at .DB .UserByEmail (at .Ctx , types .NewEmail (emailStr ))
351
+ if err != nil {
352
+ t .Fatal (err )
353
+ }
354
+ // Make sure the email field is lowercase. Make sure to not use String()
355
+ // because that will cast it to lowercase even if it's not.
356
+ // We disable gocritic here, so it doesn't suggest to use strings.EqualFold().
357
+ //nolint:gocritic
358
+ if string (u4 .Email ) != strings .ToLower (emailStr ) {
359
+ t .Fatalf ("Expected the email to be '%s', got '%s" , strings .ToLower (emailStr ), u4 .Email )
360
+ }
334
361
}
335
362
336
363
// testUserDELETE tests the DELETE /user endpoint.
@@ -719,8 +746,8 @@ func testUserAccountRecovery(t *testing.T, at *test.AccountsTester) {
719
746
// person requesting a recovery and they just forgot which email they used
720
747
// to sign up. While we can't tell them that, we can indicate tht recovery
721
748
// process works as expected and they should try their other emails.
722
- attemptedEmail := hex .EncodeToString (fastrand .Bytes (16 )) + "@siasky.net"
723
- _ , err = at .UserRecoverRequestPOST (attemptedEmail )
749
+ attemptedEmail := types . NewEmail ( hex .EncodeToString (fastrand .Bytes (16 )) + "@siasky.net" )
750
+ _ , err = at .UserRecoverRequestPOST (attemptedEmail . String () )
724
751
if err != nil {
725
752
t .Fatal (err )
726
753
}
@@ -736,8 +763,8 @@ func testUserAccountRecovery(t *testing.T, at *test.AccountsTester) {
736
763
// Request recovery with a valid email. We expect there to be a single email
737
764
// with the recovery token. The email is unconfirmed but we don't mind that.
738
765
bodyParams := url.Values {}
739
- bodyParams .Set ("email" , u .Email )
740
- _ , err = at .UserRecoverRequestPOST (u .Email )
766
+ bodyParams .Set ("email" , u .Email . String () )
767
+ _ , err = at .UserRecoverRequestPOST (u .Email . String () )
741
768
if err != nil {
742
769
t .Fatal (err )
743
770
}
@@ -809,7 +836,7 @@ func testUserAccountRecovery(t *testing.T, at *test.AccountsTester) {
809
836
t .Fatal (err )
810
837
}
811
838
// Make sure the user's password is now successfully changed.
812
- _ , b , err := at .LoginCredentialsPOST (u .Email , newPassword )
839
+ _ , b , err := at .LoginCredentialsPOST (u .Email . String () , newPassword )
813
840
if err != nil {
814
841
t .Fatal (err , string (b ))
815
842
}
@@ -940,7 +967,7 @@ func testUserFlow(t *testing.T, at *test.AccountsTester) {
940
967
queryParams .Set ("email" , emailAddr )
941
968
queryParams .Set ("password" , password )
942
969
// Create a user.
943
- u , err := test .CreateUser (at , queryParams .Get ("email" ), queryParams .Get ("password" ))
970
+ u , err := test .CreateUser (at , types . NewEmail ( queryParams .Get ("email" ) ), queryParams .Get ("password" ))
944
971
if err != nil {
945
972
t .Fatal (err )
946
973
}
@@ -975,14 +1002,14 @@ func testUserFlow(t *testing.T, at *test.AccountsTester) {
975
1002
}
976
1003
at .SetCookie (c )
977
1004
// Change the user's email.
978
- newEmail := name + "_new@siasky.net"
979
- _ , _ , err = at .UserPUT (newEmail , "" , "" )
1005
+ newEmail := types . NewEmail ( name + "_new@siasky.net" )
1006
+ _ , _ , err = at .UserPUT (newEmail . String () , "" , "" )
980
1007
if err != nil {
981
1008
t .Fatalf ("Failed to update user. Error: %s" , err .Error ())
982
1009
}
983
1010
// Grab the new cookie. It has changed because of the user edit.
984
1011
at .ClearCredentials ()
985
- r , _ , err = at .LoginCredentialsPOST (newEmail , password )
1012
+ r , _ , err = at .LoginCredentialsPOST (newEmail . String () , password )
986
1013
if err != nil {
987
1014
t .Fatal (err )
988
1015
}
0 commit comments