You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

account_test.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package setting
  5. import (
  6. "net/http"
  7. "testing"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/modules/test"
  11. "code.gitea.io/gitea/modules/web"
  12. "code.gitea.io/gitea/services/forms"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestChangePassword(t *testing.T) {
  16. oldPassword := "password"
  17. setting.MinPasswordLength = 6
  18. var pcALL = []string{"lower", "upper", "digit", "spec"}
  19. var pcLUN = []string{"lower", "upper", "digit"}
  20. var pcLU = []string{"lower", "upper"}
  21. for _, req := range []struct {
  22. OldPassword string
  23. NewPassword string
  24. Retype string
  25. Message string
  26. PasswordComplexity []string
  27. }{
  28. {
  29. OldPassword: oldPassword,
  30. NewPassword: "Qwerty123456-",
  31. Retype: "Qwerty123456-",
  32. Message: "",
  33. PasswordComplexity: pcALL,
  34. },
  35. {
  36. OldPassword: oldPassword,
  37. NewPassword: "12345",
  38. Retype: "12345",
  39. Message: "auth.password_too_short",
  40. PasswordComplexity: pcALL,
  41. },
  42. {
  43. OldPassword: "12334",
  44. NewPassword: "123456",
  45. Retype: "123456",
  46. Message: "settings.password_incorrect",
  47. PasswordComplexity: pcALL,
  48. },
  49. {
  50. OldPassword: oldPassword,
  51. NewPassword: "123456",
  52. Retype: "12345",
  53. Message: "form.password_not_match",
  54. PasswordComplexity: pcALL,
  55. },
  56. {
  57. OldPassword: oldPassword,
  58. NewPassword: "Qwerty",
  59. Retype: "Qwerty",
  60. Message: "form.password_complexity",
  61. PasswordComplexity: pcALL,
  62. },
  63. {
  64. OldPassword: oldPassword,
  65. NewPassword: "Qwerty",
  66. Retype: "Qwerty",
  67. Message: "form.password_complexity",
  68. PasswordComplexity: pcLUN,
  69. },
  70. {
  71. OldPassword: oldPassword,
  72. NewPassword: "QWERTY",
  73. Retype: "QWERTY",
  74. Message: "form.password_complexity",
  75. PasswordComplexity: pcLU,
  76. },
  77. } {
  78. models.PrepareTestEnv(t)
  79. ctx := test.MockContext(t, "user/settings/security")
  80. test.LoadUser(t, ctx, 2)
  81. test.LoadRepo(t, ctx, 1)
  82. web.SetForm(ctx, &forms.ChangePasswordForm{
  83. OldPassword: req.OldPassword,
  84. Password: req.NewPassword,
  85. Retype: req.Retype,
  86. })
  87. AccountPost(ctx)
  88. assert.Contains(t, ctx.Flash.ErrorMsg, req.Message)
  89. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  90. }
  91. }