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.

transfer_test.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2019 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 repository
  5. import (
  6. "sync"
  7. "testing"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/models/db"
  10. "code.gitea.io/gitea/models/organization"
  11. access_model "code.gitea.io/gitea/models/perm/access"
  12. repo_model "code.gitea.io/gitea/models/repo"
  13. "code.gitea.io/gitea/models/unittest"
  14. user_model "code.gitea.io/gitea/models/user"
  15. "code.gitea.io/gitea/modules/notification"
  16. "code.gitea.io/gitea/modules/notification/action"
  17. "code.gitea.io/gitea/modules/util"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. var notifySync sync.Once
  21. func registerNotifier() {
  22. notifySync.Do(func() {
  23. notification.RegisterNotifier(action.NewNotifier())
  24. })
  25. }
  26. func TestTransferOwnership(t *testing.T) {
  27. registerNotifier()
  28. assert.NoError(t, unittest.PrepareTestDatabase())
  29. doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
  30. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
  31. repo.Owner = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  32. assert.NoError(t, TransferOwnership(doer, doer, repo, nil))
  33. transferredRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
  34. assert.EqualValues(t, 2, transferredRepo.OwnerID)
  35. exist, err := util.IsExist(repo_model.RepoPath("user3", "repo3"))
  36. assert.NoError(t, err)
  37. assert.False(t, exist)
  38. exist, err = util.IsExist(repo_model.RepoPath("user2", "repo3"))
  39. assert.NoError(t, err)
  40. assert.True(t, exist)
  41. unittest.AssertExistsAndLoadBean(t, &models.Action{
  42. OpType: models.ActionTransferRepo,
  43. ActUserID: 2,
  44. RepoID: 3,
  45. Content: "user3/repo3",
  46. })
  47. unittest.CheckConsistencyFor(t, &repo_model.Repository{}, &user_model.User{}, &organization.Team{})
  48. }
  49. func TestStartRepositoryTransferSetPermission(t *testing.T) {
  50. assert.NoError(t, unittest.PrepareTestDatabase())
  51. doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
  52. recipient := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
  53. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
  54. repo.Owner = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  55. hasAccess, err := access_model.HasAccess(db.DefaultContext, recipient.ID, repo)
  56. assert.NoError(t, err)
  57. assert.False(t, hasAccess)
  58. assert.NoError(t, StartRepositoryTransfer(doer, recipient, repo, nil))
  59. hasAccess, err = access_model.HasAccess(db.DefaultContext, recipient.ID, repo)
  60. assert.NoError(t, err)
  61. assert.True(t, hasAccess)
  62. unittest.CheckConsistencyFor(t, &repo_model.Repository{}, &user_model.User{}, &organization.Team{})
  63. }