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

repo_collaboration_test.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 TestCollaboration_ModeI18nKey(t *testing.T) {
  10. assert.Equal(t, "repo.settings.collaboration.read",
  11. (&Collaboration{Mode: AccessModeRead}).ModeI18nKey())
  12. assert.Equal(t, "repo.settings.collaboration.write",
  13. (&Collaboration{Mode: AccessModeWrite}).ModeI18nKey())
  14. assert.Equal(t, "repo.settings.collaboration.admin",
  15. (&Collaboration{Mode: AccessModeAdmin}).ModeI18nKey())
  16. assert.Equal(t, "repo.settings.collaboration.undefined",
  17. (&Collaboration{Mode: AccessModeNone}).ModeI18nKey())
  18. }
  19. func TestRepository_AddCollaborator(t *testing.T) {
  20. assert.NoError(t, PrepareTestDatabase())
  21. testSuccess := func(repoID, userID int64) {
  22. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  23. assert.NoError(t, repo.GetOwner())
  24. user := AssertExistsAndLoadBean(t, &User{ID: userID}).(*User)
  25. assert.NoError(t, repo.AddCollaborator(user))
  26. CheckConsistencyFor(t, &Repository{ID: repoID}, &User{ID: userID})
  27. }
  28. testSuccess(1, 4)
  29. testSuccess(1, 4)
  30. testSuccess(3, 4)
  31. }
  32. func TestRepository_GetCollaborators(t *testing.T) {
  33. assert.NoError(t, PrepareTestDatabase())
  34. test := func(repoID int64) {
  35. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  36. collaborators, err := repo.GetCollaborators()
  37. assert.NoError(t, err)
  38. expectedLen, err := x.Count(&Collaboration{RepoID: repoID})
  39. assert.NoError(t, err)
  40. assert.Len(t, collaborators, int(expectedLen))
  41. for _, collaborator := range collaborators {
  42. assert.EqualValues(t, collaborator.User.ID, collaborator.Collaboration.UserID)
  43. assert.EqualValues(t, repoID, collaborator.Collaboration.RepoID)
  44. }
  45. }
  46. test(1)
  47. test(2)
  48. test(3)
  49. test(4)
  50. }
  51. func TestRepository_IsCollaborator(t *testing.T) {
  52. assert.NoError(t, PrepareTestDatabase())
  53. test := func(repoID, userID int64, expected bool) {
  54. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  55. actual, err := repo.IsCollaborator(userID)
  56. assert.NoError(t, err)
  57. assert.Equal(t, expected, actual)
  58. }
  59. test(3, 2, true)
  60. test(3, NonexistentID, false)
  61. test(4, 2, false)
  62. test(4, 4, true)
  63. }
  64. func TestRepository_ChangeCollaborationAccessMode(t *testing.T) {
  65. assert.NoError(t, PrepareTestDatabase())
  66. repo := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
  67. assert.NoError(t, repo.ChangeCollaborationAccessMode(4, AccessModeAdmin))
  68. collaboration := AssertExistsAndLoadBean(t, &Collaboration{RepoID: repo.ID, UserID: 4}).(*Collaboration)
  69. assert.EqualValues(t, AccessModeAdmin, collaboration.Mode)
  70. access := AssertExistsAndLoadBean(t, &Access{UserID: 4, RepoID: repo.ID}).(*Access)
  71. assert.EqualValues(t, AccessModeAdmin, access.Mode)
  72. assert.NoError(t, repo.ChangeCollaborationAccessMode(4, AccessModeAdmin))
  73. assert.NoError(t, repo.ChangeCollaborationAccessMode(NonexistentID, AccessModeAdmin))
  74. CheckConsistencyFor(t, &Repository{ID: repo.ID})
  75. }
  76. func TestRepository_DeleteCollaboration(t *testing.T) {
  77. assert.NoError(t, PrepareTestDatabase())
  78. repo := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
  79. assert.NoError(t, repo.GetOwner())
  80. assert.NoError(t, repo.DeleteCollaboration(4))
  81. AssertNotExistsBean(t, &Collaboration{RepoID: repo.ID, UserID: 4})
  82. assert.NoError(t, repo.DeleteCollaboration(4))
  83. AssertNotExistsBean(t, &Collaboration{RepoID: repo.ID, UserID: 4})
  84. CheckConsistencyFor(t, &Repository{ID: repo.ID})
  85. }