gitea源码

account_test.go 2.7KB

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