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.

88 lines
1.9 KiB

  1. // Copyright 2017 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 admin
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/auth"
  9. "code.gitea.io/gitea/modules/test"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestNewUserPost_MustChangePassword(t *testing.T) {
  13. models.PrepareTestEnv(t)
  14. ctx := test.MockContext(t, "admin/users/new")
  15. u := models.AssertExistsAndLoadBean(t, &models.User{
  16. IsAdmin: true,
  17. ID: 2,
  18. }).(*models.User)
  19. ctx.User = u
  20. username := "gitea"
  21. email := "gitea@gitea.io"
  22. form := auth.AdminCreateUserForm{
  23. LoginType: "local",
  24. LoginName: "local",
  25. UserName: username,
  26. Email: email,
  27. Password: "xxxxxxxx",
  28. SendNotify: false,
  29. MustChangePassword: true,
  30. }
  31. NewUserPost(ctx, form)
  32. assert.NotEmpty(t, ctx.Flash.SuccessMsg)
  33. u, err := models.GetUserByName(username)
  34. assert.NoError(t, err)
  35. assert.Equal(t, username, u.Name)
  36. assert.Equal(t, email, u.Email)
  37. assert.True(t, u.MustChangePassword)
  38. }
  39. func TestNewUserPost_MustChangePasswordFalse(t *testing.T) {
  40. models.PrepareTestEnv(t)
  41. ctx := test.MockContext(t, "admin/users/new")
  42. u := models.AssertExistsAndLoadBean(t, &models.User{
  43. IsAdmin: true,
  44. ID: 2,
  45. }).(*models.User)
  46. ctx.User = u
  47. username := "gitea"
  48. email := "gitea@gitea.io"
  49. form := auth.AdminCreateUserForm{
  50. LoginType: "local",
  51. LoginName: "local",
  52. UserName: username,
  53. Email: email,
  54. Password: "xxxxxxxx",
  55. SendNotify: false,
  56. MustChangePassword: false,
  57. }
  58. NewUserPost(ctx, form)
  59. assert.NotEmpty(t, ctx.Flash.SuccessMsg)
  60. u, err := models.GetUserByName(username)
  61. assert.NoError(t, err)
  62. assert.Equal(t, username, u.Name)
  63. assert.Equal(t, email, u.Email)
  64. assert.False(t, u.MustChangePassword)
  65. }