@@ -14,6 +14,7 @@ import (
1414 "github.com/gotify/server/v2/test"
1515 "github.com/gotify/server/v2/test/testdb"
1616 "github.com/stretchr/testify/assert"
17+ "github.com/stretchr/testify/require"
1718 "github.com/stretchr/testify/suite"
1819)
1920
@@ -320,6 +321,24 @@ func (s *UserSuite) Test_CreateUser_NameAlreadyExists() {
320321 assert .Equal (s .T (), 400 , s .recorder .Code )
321322}
322323
324+ func (s * UserSuite ) Test_CreateUser_EmptyPassword_Expect400 () {
325+ s .loginAdmin ()
326+
327+ s .ctx .Request = httptest .NewRequest ("POST" , "/user" , strings .NewReader (`{"name": "admin", "pass": "", "admin": false}` ))
328+ s .ctx .Request .Header .Set ("Content-Type" , "application/json" )
329+ s .a .CreateUser (s .ctx )
330+ assert .Equal (s .T (), 400 , s .recorder .Code )
331+ }
332+
333+ func (s * UserSuite ) Test_CreateUser_TooLongPassword_Expect400 () {
334+ s .loginAdmin ()
335+
336+ s .ctx .Request = httptest .NewRequest ("POST" , "/user" , strings .NewReader (`{"name": "admin", "pass": "` + strings .Repeat ("a" , 100 )+ `", "admin": false}` ))
337+ s .ctx .Request .Header .Set ("Content-Type" , "application/json" )
338+ s .a .CreateUser (s .ctx )
339+ assert .Equal (s .T (), 400 , s .recorder .Code )
340+ }
341+
323342func (s * UserSuite ) Test_UpdateUserByID_InvalidID () {
324343 s .ctx .Params = gin.Params {{Key : "id" , Value : "abc" }}
325344
@@ -331,6 +350,28 @@ func (s *UserSuite) Test_UpdateUserByID_InvalidID() {
331350 assert .Equal (s .T (), 400 , s .recorder .Code )
332351}
333352
353+ func (s * UserSuite ) Test_UpdateUserByID_EmptyPassword_Expect400 () {
354+ s .loginAdmin ()
355+
356+ s .ctx .Params = gin.Params {{Key : "id" , Value : "1" }}
357+
358+ s .ctx .Request = httptest .NewRequest ("POST" , "/user/1" , strings .NewReader (`{"name": "admin", "pass": "", "admin": false}` ))
359+ s .ctx .Request .Header .Set ("Content-Type" , "application/json" )
360+ s .a .UpdateUserByID (s .ctx )
361+ assert .Equal (s .T (), 400 , s .recorder .Code )
362+ }
363+
364+ func (s * UserSuite ) Test_UpdateUserByID_TooLongPassword_Expect400 () {
365+ s .loginAdmin ()
366+
367+ s .ctx .Params = gin.Params {{Key : "id" , Value : "1" }}
368+
369+ s .ctx .Request = httptest .NewRequest ("POST" , "/user/1" , strings .NewReader (`{"name": "admin", "pass": "` + strings .Repeat ("a" , 100 )+ `", "admin": false}` ))
370+ s .ctx .Request .Header .Set ("Content-Type" , "application/json" )
371+ s .a .UpdateUserByID (s .ctx )
372+ assert .Equal (s .T (), 400 , s .recorder .Code )
373+ }
374+
334375func (s * UserSuite ) Test_UpdateUserByID_LastAdmin_Expect400 () {
335376 s .db .CreateUser (& model.User {
336377 ID : 7 ,
@@ -359,7 +400,9 @@ func (s *UserSuite) Test_UpdateUserByID_UnknownUser() {
359400}
360401
361402func (s * UserSuite ) Test_UpdateUserByID_UpdateNotPassword () {
362- s .db .CreateUser (& model.User {ID : 2 , Name : "nico" , Pass : password .CreatePassword ("old" , 5 )})
403+ pw , err := password .CreatePassword ("old" , 5 )
404+ require .NoError (s .T (), err )
405+ s .db .CreateUser (& model.User {ID : 2 , Name : "nico" , Pass : pw })
363406
364407 s .ctx .Params = gin.Params {{Key : "id" , Value : "2" }}
365408
@@ -376,7 +419,9 @@ func (s *UserSuite) Test_UpdateUserByID_UpdateNotPassword() {
376419}
377420
378421func (s * UserSuite ) Test_UpdateUserByID_UpdatePassword () {
379- s .db .CreateUser (& model.User {ID : 2 , Name : "tom" , Pass : password .CreatePassword ("old" , 5 )})
422+ pw , err := password .CreatePassword ("old" , 5 )
423+ require .NoError (s .T (), err )
424+ s .db .CreateUser (& model.User {ID : 2 , Name : "tom" , Pass : pw })
380425
381426 s .ctx .Params = gin.Params {{Key : "id" , Value : "2" }}
382427
@@ -413,7 +458,9 @@ func (s *UserSuite) Test_UpdateUserByID_PreservesOIDCID() {
413458}
414459
415460func (s * UserSuite ) Test_UpdatePassword () {
416- s .db .CreateUser (& model.User {ID : 1 , Name : "jmattheis" , Pass : password .CreatePassword ("old" , 5 )})
461+ pw , err := password .CreatePassword ("old" , 5 )
462+ require .NoError (s .T (), err )
463+ s .db .CreateUser (& model.User {ID : 1 , Name : "jmattheis" , Pass : pw })
417464
418465 test .WithUser (s .ctx , 1 )
419466 s .ctx .Request = httptest .NewRequest ("POST" , "/user/current/password" , strings .NewReader (`{"pass": "new"}` ))
@@ -429,7 +476,9 @@ func (s *UserSuite) Test_UpdatePassword() {
429476}
430477
431478func (s * UserSuite ) Test_UpdatePassword_EmptyPassword () {
432- s .db .CreateUser (& model.User {ID : 1 , Name : "jmattheis" , Pass : password .CreatePassword ("old" , 5 )})
479+ pw , err := password .CreatePassword ("old" , 5 )
480+ require .NoError (s .T (), err )
481+ s .db .CreateUser (& model.User {ID : 1 , Name : "jmattheis" , Pass : pw })
433482
434483 test .WithUser (s .ctx , 1 )
435484 s .ctx .Request = httptest .NewRequest ("POST" , "/user/current/password" , strings .NewReader (`{"pass":""}` ))
@@ -444,6 +493,18 @@ func (s *UserSuite) Test_UpdatePassword_EmptyPassword() {
444493 assert .True (s .T (), password .ComparePassword (user .Pass , []byte ("old" )))
445494}
446495
496+ func (s * UserSuite ) Test_UpdatePassword_TooLongPassword_Expect400 () {
497+ pw , err := password .CreatePassword ("old" , 5 )
498+ require .NoError (s .T (), err )
499+ s .db .CreateUser (& model.User {ID : 1 , Name : "jmattheis" , Pass : pw })
500+
501+ test .WithUser (s .ctx , 1 )
502+ s .ctx .Request = httptest .NewRequest ("POST" , "/user/current/password" , strings .NewReader (`{"pass": "` + strings .Repeat ("a" , 100 )+ `"}` ))
503+ s .ctx .Request .Header .Set ("Content-Type" , "application/json" )
504+ s .a .ChangePassword (s .ctx )
505+ assert .Equal (s .T (), 400 , s .recorder .Code )
506+ }
507+
447508func (s * UserSuite ) loginAdmin () {
448509 s .db .CreateUser (& model.User {ID : 1 , Name : "admin" , Admin : true })
449510 auth .RegisterUser (s .ctx , & model.User {ID : 1 , Admin : true })
0 commit comments