本站源代码
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.

95 lines
3.0KB

  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. "github.com/stretchr/testify/assert"
  8. )
  9. func TestRepository_AddCollaborator(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. testSuccess := func(repoID, userID int64) {
  12. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  13. assert.NoError(t, repo.GetOwner())
  14. user := AssertExistsAndLoadBean(t, &User{ID: userID}).(*User)
  15. assert.NoError(t, repo.AddCollaborator(user))
  16. CheckConsistencyFor(t, &Repository{ID: repoID}, &User{ID: userID})
  17. }
  18. testSuccess(1, 4)
  19. testSuccess(1, 4)
  20. testSuccess(3, 4)
  21. }
  22. func TestRepository_GetCollaborators(t *testing.T) {
  23. assert.NoError(t, PrepareTestDatabase())
  24. test := func(repoID int64) {
  25. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  26. collaborators, err := repo.GetCollaborators()
  27. assert.NoError(t, err)
  28. expectedLen, err := x.Count(&Collaboration{RepoID: repoID})
  29. assert.NoError(t, err)
  30. assert.Len(t, collaborators, int(expectedLen))
  31. for _, collaborator := range collaborators {
  32. assert.EqualValues(t, collaborator.User.ID, collaborator.Collaboration.UserID)
  33. assert.EqualValues(t, repoID, collaborator.Collaboration.RepoID)
  34. }
  35. }
  36. test(1)
  37. test(2)
  38. test(3)
  39. test(4)
  40. }
  41. func TestRepository_IsCollaborator(t *testing.T) {
  42. assert.NoError(t, PrepareTestDatabase())
  43. test := func(repoID, userID int64, expected bool) {
  44. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  45. actual, err := repo.IsCollaborator(userID)
  46. assert.NoError(t, err)
  47. assert.Equal(t, expected, actual)
  48. }
  49. test(3, 2, true)
  50. test(3, NonexistentID, false)
  51. test(4, 2, false)
  52. test(4, 4, true)
  53. }
  54. func TestRepository_ChangeCollaborationAccessMode(t *testing.T) {
  55. assert.NoError(t, PrepareTestDatabase())
  56. repo := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
  57. assert.NoError(t, repo.ChangeCollaborationAccessMode(4, AccessModeAdmin))
  58. collaboration := AssertExistsAndLoadBean(t, &Collaboration{RepoID: repo.ID, UserID: 4}).(*Collaboration)
  59. assert.EqualValues(t, AccessModeAdmin, collaboration.Mode)
  60. access := AssertExistsAndLoadBean(t, &Access{UserID: 4, RepoID: repo.ID}).(*Access)
  61. assert.EqualValues(t, AccessModeAdmin, access.Mode)
  62. assert.NoError(t, repo.ChangeCollaborationAccessMode(4, AccessModeAdmin))
  63. assert.NoError(t, repo.ChangeCollaborationAccessMode(NonexistentID, AccessModeAdmin))
  64. CheckConsistencyFor(t, &Repository{ID: repo.ID})
  65. }
  66. func TestRepository_DeleteCollaboration(t *testing.T) {
  67. assert.NoError(t, PrepareTestDatabase())
  68. repo := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
  69. assert.NoError(t, repo.GetOwner())
  70. assert.NoError(t, repo.DeleteCollaboration(4))
  71. AssertNotExistsBean(t, &Collaboration{RepoID: repo.ID, UserID: 4})
  72. assert.NoError(t, repo.DeleteCollaboration(4))
  73. AssertNotExistsBean(t, &Collaboration{RepoID: repo.ID, UserID: 4})
  74. CheckConsistencyFor(t, &Repository{ID: repo.ID})
  75. }
上海开阖软件有限公司 沪ICP备12045867号-1