Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

user_test.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 models
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/modules/setting"
  8. "code.gitea.io/gitea/modules/util"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestGetUserEmailsByNames(t *testing.T) {
  12. assert.NoError(t, PrepareTestDatabase())
  13. // ignore none active user email
  14. assert.Equal(t, []string{"user8@example.com"}, GetUserEmailsByNames([]string{"user8", "user9"}))
  15. assert.Equal(t, []string{"user8@example.com", "user5@example.com"}, GetUserEmailsByNames([]string{"user8", "user5"}))
  16. }
  17. func TestCanCreateOrganization(t *testing.T) {
  18. assert.NoError(t, PrepareTestDatabase())
  19. admin := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  20. assert.True(t, admin.CanCreateOrganization())
  21. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  22. assert.True(t, user.CanCreateOrganization())
  23. // Disable user create organization permission.
  24. user.AllowCreateOrganization = false
  25. assert.False(t, user.CanCreateOrganization())
  26. setting.Admin.DisableRegularOrgCreation = true
  27. user.AllowCreateOrganization = true
  28. assert.True(t, admin.CanCreateOrganization())
  29. assert.False(t, user.CanCreateOrganization())
  30. }
  31. func TestSearchUsers(t *testing.T) {
  32. assert.NoError(t, PrepareTestDatabase())
  33. testSuccess := func(opts *SearchUserOptions, expectedUserOrOrgIDs []int64) {
  34. users, _, err := SearchUsers(opts)
  35. assert.NoError(t, err)
  36. if assert.Len(t, users, len(expectedUserOrOrgIDs)) {
  37. for i, expectedID := range expectedUserOrOrgIDs {
  38. assert.EqualValues(t, expectedID, users[i].ID)
  39. }
  40. }
  41. }
  42. // test orgs
  43. testOrgSuccess := func(opts *SearchUserOptions, expectedOrgIDs []int64) {
  44. opts.Type = UserTypeOrganization
  45. testSuccess(opts, expectedOrgIDs)
  46. }
  47. testOrgSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 1, PageSize: 2},
  48. []int64{3, 6})
  49. testOrgSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 2, PageSize: 2},
  50. []int64{7, 17})
  51. testOrgSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 3, PageSize: 2},
  52. []int64{19})
  53. testOrgSuccess(&SearchUserOptions{Page: 4, PageSize: 2},
  54. []int64{})
  55. // test users
  56. testUserSuccess := func(opts *SearchUserOptions, expectedUserIDs []int64) {
  57. opts.Type = UserTypeIndividual
  58. testSuccess(opts, expectedUserIDs)
  59. }
  60. testUserSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 1},
  61. []int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20})
  62. testUserSuccess(&SearchUserOptions{Page: 1, IsActive: util.OptionalBoolFalse},
  63. []int64{9})
  64. testUserSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 1, IsActive: util.OptionalBoolTrue},
  65. []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20})
  66. testUserSuccess(&SearchUserOptions{Keyword: "user1", OrderBy: "id ASC", Page: 1, IsActive: util.OptionalBoolTrue},
  67. []int64{1, 10, 11, 12, 13, 14, 15, 16, 18})
  68. // order by name asc default
  69. testUserSuccess(&SearchUserOptions{Keyword: "user1", Page: 1, IsActive: util.OptionalBoolTrue},
  70. []int64{1, 10, 11, 12, 13, 14, 15, 16, 18})
  71. }
  72. func TestDeleteUser(t *testing.T) {
  73. test := func(userID int64) {
  74. assert.NoError(t, PrepareTestDatabase())
  75. user := AssertExistsAndLoadBean(t, &User{ID: userID}).(*User)
  76. ownedRepos := make([]*Repository, 0, 10)
  77. assert.NoError(t, x.Find(&ownedRepos, &Repository{OwnerID: userID}))
  78. if len(ownedRepos) > 0 {
  79. err := DeleteUser(user)
  80. assert.Error(t, err)
  81. assert.True(t, IsErrUserOwnRepos(err))
  82. return
  83. }
  84. orgUsers := make([]*OrgUser, 0, 10)
  85. assert.NoError(t, x.Find(&orgUsers, &OrgUser{UID: userID}))
  86. for _, orgUser := range orgUsers {
  87. if err := RemoveOrgUser(orgUser.OrgID, orgUser.UID); err != nil {
  88. assert.True(t, IsErrLastOrgOwner(err))
  89. return
  90. }
  91. }
  92. assert.NoError(t, DeleteUser(user))
  93. AssertNotExistsBean(t, &User{ID: userID})
  94. CheckConsistencyFor(t, &User{}, &Repository{})
  95. }
  96. test(2)
  97. test(4)
  98. test(8)
  99. test(11)
  100. }