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.

128 lines
3.6 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. "github.com/stretchr/testify/assert"
  8. )
  9. var accessModes = []AccessMode{
  10. AccessModeRead,
  11. AccessModeWrite,
  12. AccessModeAdmin,
  13. AccessModeOwner,
  14. }
  15. func TestAccessLevel(t *testing.T) {
  16. assert.NoError(t, PrepareTestDatabase())
  17. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  18. user5 := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
  19. // A public repository owned by User 2
  20. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  21. assert.False(t, repo1.IsPrivate)
  22. // A private repository owned by Org 3
  23. repo3 := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  24. assert.True(t, repo3.IsPrivate)
  25. level, err := AccessLevel(user2, repo1)
  26. assert.NoError(t, err)
  27. assert.Equal(t, AccessModeOwner, level)
  28. level, err = AccessLevel(user2, repo3)
  29. assert.NoError(t, err)
  30. assert.Equal(t, AccessModeOwner, level)
  31. level, err = AccessLevel(user5, repo1)
  32. assert.NoError(t, err)
  33. assert.Equal(t, AccessModeRead, level)
  34. level, err = AccessLevel(user5, repo3)
  35. assert.NoError(t, err)
  36. assert.Equal(t, AccessModeNone, level)
  37. }
  38. func TestHasAccess(t *testing.T) {
  39. assert.NoError(t, PrepareTestDatabase())
  40. user1 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  41. user2 := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
  42. // A public repository owned by User 2
  43. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  44. assert.False(t, repo1.IsPrivate)
  45. // A private repository owned by Org 3
  46. repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  47. assert.True(t, repo2.IsPrivate)
  48. has, err := HasAccess(user1.ID, repo1)
  49. assert.NoError(t, err)
  50. assert.True(t, has)
  51. has, err = HasAccess(user1.ID, repo2)
  52. assert.NoError(t, err)
  53. has, err = HasAccess(user2.ID, repo1)
  54. assert.NoError(t, err)
  55. has, err = HasAccess(user2.ID, repo2)
  56. assert.NoError(t, err)
  57. }
  58. func TestUser_GetRepositoryAccesses(t *testing.T) {
  59. assert.NoError(t, PrepareTestDatabase())
  60. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  61. accesses, err := user1.GetRepositoryAccesses()
  62. assert.NoError(t, err)
  63. assert.Len(t, accesses, 0)
  64. }
  65. func TestUser_GetAccessibleRepositories(t *testing.T) {
  66. assert.NoError(t, PrepareTestDatabase())
  67. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  68. repos, err := user1.GetAccessibleRepositories(0)
  69. assert.NoError(t, err)
  70. assert.Len(t, repos, 0)
  71. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  72. repos, err = user2.GetAccessibleRepositories(0)
  73. assert.NoError(t, err)
  74. assert.Len(t, repos, 1)
  75. }
  76. func TestRepository_RecalculateAccesses(t *testing.T) {
  77. // test with organization repo
  78. assert.NoError(t, PrepareTestDatabase())
  79. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  80. assert.NoError(t, repo1.GetOwner())
  81. _, err := x.Delete(&Collaboration{UserID: 2, RepoID: 3})
  82. assert.NoError(t, err)
  83. assert.NoError(t, repo1.RecalculateAccesses())
  84. access := &Access{UserID: 2, RepoID: 3}
  85. has, err := x.Get(access)
  86. assert.NoError(t, err)
  87. assert.True(t, has)
  88. assert.Equal(t, AccessModeOwner, access.Mode)
  89. }
  90. func TestRepository_RecalculateAccesses2(t *testing.T) {
  91. // test with non-organization repo
  92. assert.NoError(t, PrepareTestDatabase())
  93. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
  94. assert.NoError(t, repo1.GetOwner())
  95. _, err := x.Delete(&Collaboration{UserID: 4, RepoID: 4})
  96. assert.NoError(t, err)
  97. assert.NoError(t, repo1.RecalculateAccesses())
  98. has, err := x.Get(&Access{UserID: 4, RepoID: 4})
  99. assert.NoError(t, err)
  100. assert.False(t, has)
  101. }