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

access_test.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. user1 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  18. user2 := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
  19. repo1 := AssertExistsAndLoadBean(t, &Repository{OwnerID: 2, IsPrivate: false}).(*Repository)
  20. repo2 := AssertExistsAndLoadBean(t, &Repository{OwnerID: 3, IsPrivate: true}).(*Repository)
  21. level, err := AccessLevel(user1.ID, repo1)
  22. assert.NoError(t, err)
  23. assert.Equal(t, AccessModeOwner, level)
  24. level, err = AccessLevel(user1.ID, repo2)
  25. assert.NoError(t, err)
  26. assert.Equal(t, AccessModeWrite, level)
  27. level, err = AccessLevel(user2.ID, repo1)
  28. assert.NoError(t, err)
  29. assert.Equal(t, AccessModeRead, level)
  30. level, err = AccessLevel(user2.ID, repo2)
  31. assert.NoError(t, err)
  32. assert.Equal(t, AccessModeNone, level)
  33. }
  34. func TestHasAccess(t *testing.T) {
  35. assert.NoError(t, PrepareTestDatabase())
  36. user1 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  37. user2 := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
  38. repo1 := AssertExistsAndLoadBean(t, &Repository{OwnerID: 2, IsPrivate: false}).(*Repository)
  39. repo2 := AssertExistsAndLoadBean(t, &Repository{OwnerID: 3, IsPrivate: true}).(*Repository)
  40. for _, accessMode := range accessModes {
  41. has, err := HasAccess(user1.ID, repo1, accessMode)
  42. assert.NoError(t, err)
  43. assert.True(t, has)
  44. has, err = HasAccess(user1.ID, repo2, accessMode)
  45. assert.NoError(t, err)
  46. assert.Equal(t, accessMode <= AccessModeWrite, has)
  47. has, err = HasAccess(user2.ID, repo1, accessMode)
  48. assert.NoError(t, err)
  49. assert.Equal(t, accessMode <= AccessModeRead, has)
  50. has, err = HasAccess(user2.ID, repo2, accessMode)
  51. assert.NoError(t, err)
  52. assert.Equal(t, accessMode <= AccessModeNone, has)
  53. }
  54. }
  55. func TestUser_GetRepositoryAccesses(t *testing.T) {
  56. assert.NoError(t, PrepareTestDatabase())
  57. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  58. accesses, err := user1.GetRepositoryAccesses()
  59. assert.NoError(t, err)
  60. assert.Len(t, accesses, 0)
  61. }
  62. func TestUser_GetAccessibleRepositories(t *testing.T) {
  63. assert.NoError(t, PrepareTestDatabase())
  64. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  65. repos, err := user1.GetAccessibleRepositories(0)
  66. assert.NoError(t, err)
  67. assert.Len(t, repos, 0)
  68. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  69. repos, err = user2.GetAccessibleRepositories(0)
  70. assert.NoError(t, err)
  71. assert.Len(t, repos, 1)
  72. }
  73. func TestRepository_RecalculateAccesses(t *testing.T) {
  74. // test with organization repo
  75. assert.NoError(t, PrepareTestDatabase())
  76. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  77. assert.NoError(t, repo1.GetOwner())
  78. _, err := x.Delete(&Collaboration{UserID: 2, RepoID: 3})
  79. assert.NoError(t, err)
  80. assert.NoError(t, repo1.RecalculateAccesses())
  81. access := &Access{UserID: 2, RepoID: 3}
  82. has, err := x.Get(access)
  83. assert.NoError(t, err)
  84. assert.True(t, has)
  85. assert.Equal(t, AccessModeOwner, access.Mode)
  86. }
  87. func TestRepository_RecalculateAccesses2(t *testing.T) {
  88. // test with non-organization repo
  89. assert.NoError(t, PrepareTestDatabase())
  90. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
  91. assert.NoError(t, repo1.GetOwner())
  92. _, err := x.Delete(&Collaboration{UserID: 4, RepoID: 4})
  93. assert.NoError(t, err)
  94. assert.NoError(t, repo1.RecalculateAccesses())
  95. has, err := x.Get(&Access{UserID: 4, RepoID: 4})
  96. assert.NoError(t, err)
  97. assert.False(t, has)
  98. }