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.

71 lines
2.1 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 models
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/modules/setting"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestGetUserEmailsByNames(t *testing.T) {
  11. assert.NoError(t, PrepareTestDatabase())
  12. // ignore none active user email
  13. assert.Equal(t, []string{"user8@example.com"}, GetUserEmailsByNames([]string{"user8", "user9"}))
  14. assert.Equal(t, []string{"user8@example.com", "user5@example.com"}, GetUserEmailsByNames([]string{"user8", "user5"}))
  15. }
  16. func TestCanCreateOrganization(t *testing.T) {
  17. assert.NoError(t, PrepareTestDatabase())
  18. admin := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  19. assert.True(t, admin.CanCreateOrganization())
  20. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  21. assert.True(t, user.CanCreateOrganization())
  22. // Disable user create organization permission.
  23. user.AllowCreateOrganization = false
  24. assert.False(t, user.CanCreateOrganization())
  25. setting.Admin.DisableRegularOrgCreation = true
  26. user.AllowCreateOrganization = true
  27. assert.True(t, admin.CanCreateOrganization())
  28. assert.False(t, user.CanCreateOrganization())
  29. }
  30. func TestDeleteUser(t *testing.T) {
  31. test := func(userID int64) {
  32. assert.NoError(t, PrepareTestDatabase())
  33. user := AssertExistsAndLoadBean(t, &User{ID: userID}).(*User)
  34. ownedRepos := make([]*Repository, 0, 10)
  35. assert.NoError(t, x.Find(&ownedRepos, &Repository{OwnerID: userID}))
  36. if len(ownedRepos) > 0 {
  37. err := DeleteUser(user)
  38. assert.Error(t, err)
  39. assert.True(t, IsErrUserOwnRepos(err))
  40. return
  41. }
  42. orgUsers := make([]*OrgUser, 0, 10)
  43. assert.NoError(t, x.Find(&orgUsers, &OrgUser{UID: userID}))
  44. for _, orgUser := range orgUsers {
  45. if err := RemoveOrgUser(orgUser.OrgID, orgUser.UID); err != nil {
  46. assert.True(t, IsErrLastOrgOwner(err))
  47. return
  48. }
  49. }
  50. assert.NoError(t, DeleteUser(user))
  51. AssertNotExistsBean(t, &User{ID: userID})
  52. CheckConsistencyFor(t, &User{}, &Repository{})
  53. }
  54. test(2)
  55. test(4)
  56. test(8)
  57. test(11)
  58. }