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.

161 lines
4.7 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. "math/rand"
  7. "testing"
  8. "code.gitea.io/gitea/modules/setting"
  9. "code.gitea.io/gitea/modules/util"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestGetUserEmailsByNames(t *testing.T) {
  13. assert.NoError(t, PrepareTestDatabase())
  14. // ignore none active user email
  15. assert.Equal(t, []string{"user8@example.com"}, GetUserEmailsByNames([]string{"user8", "user9"}))
  16. assert.Equal(t, []string{"user8@example.com", "user5@example.com"}, GetUserEmailsByNames([]string{"user8", "user5"}))
  17. }
  18. func TestCanCreateOrganization(t *testing.T) {
  19. assert.NoError(t, PrepareTestDatabase())
  20. admin := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  21. assert.True(t, admin.CanCreateOrganization())
  22. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  23. assert.True(t, user.CanCreateOrganization())
  24. // Disable user create organization permission.
  25. user.AllowCreateOrganization = false
  26. assert.False(t, user.CanCreateOrganization())
  27. setting.Admin.DisableRegularOrgCreation = true
  28. user.AllowCreateOrganization = true
  29. assert.True(t, admin.CanCreateOrganization())
  30. assert.False(t, user.CanCreateOrganization())
  31. }
  32. func TestSearchUsers(t *testing.T) {
  33. assert.NoError(t, PrepareTestDatabase())
  34. testSuccess := func(opts *SearchUserOptions, expectedUserOrOrgIDs []int64) {
  35. users, _, err := SearchUsers(opts)
  36. assert.NoError(t, err)
  37. if assert.Len(t, users, len(expectedUserOrOrgIDs)) {
  38. for i, expectedID := range expectedUserOrOrgIDs {
  39. assert.EqualValues(t, expectedID, users[i].ID)
  40. }
  41. }
  42. }
  43. // test orgs
  44. testOrgSuccess := func(opts *SearchUserOptions, expectedOrgIDs []int64) {
  45. opts.Type = UserTypeOrganization
  46. testSuccess(opts, expectedOrgIDs)
  47. }
  48. testOrgSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 1, PageSize: 2},
  49. []int64{3, 6})
  50. testOrgSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 2, PageSize: 2},
  51. []int64{7, 17})
  52. testOrgSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 3, PageSize: 2},
  53. []int64{19})
  54. testOrgSuccess(&SearchUserOptions{Page: 4, PageSize: 2},
  55. []int64{})
  56. // test users
  57. testUserSuccess := func(opts *SearchUserOptions, expectedUserIDs []int64) {
  58. opts.Type = UserTypeIndividual
  59. testSuccess(opts, expectedUserIDs)
  60. }
  61. testUserSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 1},
  62. []int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20})
  63. testUserSuccess(&SearchUserOptions{Page: 1, IsActive: util.OptionalBoolFalse},
  64. []int64{9})
  65. testUserSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 1, IsActive: util.OptionalBoolTrue},
  66. []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20})
  67. testUserSuccess(&SearchUserOptions{Keyword: "user1", OrderBy: "id ASC", Page: 1, IsActive: util.OptionalBoolTrue},
  68. []int64{1, 10, 11, 12, 13, 14, 15, 16, 18})
  69. // order by name asc default
  70. testUserSuccess(&SearchUserOptions{Keyword: "user1", Page: 1, IsActive: util.OptionalBoolTrue},
  71. []int64{1, 10, 11, 12, 13, 14, 15, 16, 18})
  72. }
  73. func TestDeleteUser(t *testing.T) {
  74. test := func(userID int64) {
  75. assert.NoError(t, PrepareTestDatabase())
  76. user := AssertExistsAndLoadBean(t, &User{ID: userID}).(*User)
  77. ownedRepos := make([]*Repository, 0, 10)
  78. assert.NoError(t, x.Find(&ownedRepos, &Repository{OwnerID: userID}))
  79. if len(ownedRepos) > 0 {
  80. err := DeleteUser(user)
  81. assert.Error(t, err)
  82. assert.True(t, IsErrUserOwnRepos(err))
  83. return
  84. }
  85. orgUsers := make([]*OrgUser, 0, 10)
  86. assert.NoError(t, x.Find(&orgUsers, &OrgUser{UID: userID}))
  87. for _, orgUser := range orgUsers {
  88. if err := RemoveOrgUser(orgUser.OrgID, orgUser.UID); err != nil {
  89. assert.True(t, IsErrLastOrgOwner(err))
  90. return
  91. }
  92. }
  93. assert.NoError(t, DeleteUser(user))
  94. AssertNotExistsBean(t, &User{ID: userID})
  95. CheckConsistencyFor(t, &User{}, &Repository{})
  96. }
  97. test(2)
  98. test(4)
  99. test(8)
  100. test(11)
  101. }
  102. func TestHashPasswordDeterministic(t *testing.T) {
  103. b := make([]byte, 16)
  104. rand.Read(b)
  105. u := &User{Salt: string(b)}
  106. for i := 0; i < 50; i++ {
  107. // generate a random password
  108. rand.Read(b)
  109. pass := string(b)
  110. // save the current password in the user - hash it and store the result
  111. u.HashPassword(pass)
  112. r1 := u.Passwd
  113. // run again
  114. u.HashPassword(pass)
  115. r2 := u.Passwd
  116. // assert equal (given the same salt+pass, the same result is produced)
  117. assert.Equal(t, r1, r2)
  118. }
  119. }
  120. func BenchmarkHashPassword(b *testing.B) {
  121. // BenchmarkHashPassword ensures that it takes a reasonable amount of time
  122. // to hash a password - in order to protect from brute-force attacks.
  123. pass := "password1337"
  124. bs := make([]byte, 16)
  125. rand.Read(bs)
  126. u := &User{Salt: string(bs), Passwd: pass}
  127. b.ResetTimer()
  128. for i := 0; i < b.N; i++ {
  129. u.HashPassword(pass)
  130. }
  131. }