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

258 lines
7.8KB

  1. // Copyright 2018 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 TestRepoPermissionPublicNonOrgRepo(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. // public non-organization repo
  12. repo := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
  13. assert.NoError(t, repo.getUnits(x))
  14. // plain user
  15. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  16. perm, err := GetUserRepoPermission(repo, user)
  17. assert.NoError(t, err)
  18. for _, unit := range repo.Units {
  19. assert.True(t, perm.CanRead(unit.Type))
  20. assert.False(t, perm.CanWrite(unit.Type))
  21. }
  22. // change to collaborator
  23. assert.NoError(t, repo.AddCollaborator(user))
  24. perm, err = GetUserRepoPermission(repo, user)
  25. assert.NoError(t, err)
  26. for _, unit := range repo.Units {
  27. assert.True(t, perm.CanRead(unit.Type))
  28. assert.True(t, perm.CanWrite(unit.Type))
  29. }
  30. // collaborator
  31. collaborator := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
  32. perm, err = GetUserRepoPermission(repo, collaborator)
  33. assert.NoError(t, err)
  34. for _, unit := range repo.Units {
  35. assert.True(t, perm.CanRead(unit.Type))
  36. assert.True(t, perm.CanWrite(unit.Type))
  37. }
  38. // owner
  39. owner := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
  40. perm, err = GetUserRepoPermission(repo, owner)
  41. assert.NoError(t, err)
  42. for _, unit := range repo.Units {
  43. assert.True(t, perm.CanRead(unit.Type))
  44. assert.True(t, perm.CanWrite(unit.Type))
  45. }
  46. // admin
  47. admin := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  48. perm, err = GetUserRepoPermission(repo, admin)
  49. assert.NoError(t, err)
  50. for _, unit := range repo.Units {
  51. assert.True(t, perm.CanRead(unit.Type))
  52. assert.True(t, perm.CanWrite(unit.Type))
  53. }
  54. }
  55. func TestRepoPermissionPrivateNonOrgRepo(t *testing.T) {
  56. assert.NoError(t, PrepareTestDatabase())
  57. // private non-organization repo
  58. repo := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  59. assert.NoError(t, repo.getUnits(x))
  60. // plain user
  61. user := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
  62. perm, err := GetUserRepoPermission(repo, user)
  63. assert.NoError(t, err)
  64. for _, unit := range repo.Units {
  65. assert.False(t, perm.CanRead(unit.Type))
  66. assert.False(t, perm.CanWrite(unit.Type))
  67. }
  68. // change to collaborator to default write access
  69. assert.NoError(t, repo.AddCollaborator(user))
  70. perm, err = GetUserRepoPermission(repo, user)
  71. assert.NoError(t, err)
  72. for _, unit := range repo.Units {
  73. assert.True(t, perm.CanRead(unit.Type))
  74. assert.True(t, perm.CanWrite(unit.Type))
  75. }
  76. assert.NoError(t, repo.ChangeCollaborationAccessMode(user.ID, AccessModeRead))
  77. perm, err = GetUserRepoPermission(repo, user)
  78. assert.NoError(t, err)
  79. for _, unit := range repo.Units {
  80. assert.True(t, perm.CanRead(unit.Type))
  81. assert.False(t, perm.CanWrite(unit.Type))
  82. }
  83. // owner
  84. owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  85. perm, err = GetUserRepoPermission(repo, owner)
  86. assert.NoError(t, err)
  87. for _, unit := range repo.Units {
  88. assert.True(t, perm.CanRead(unit.Type))
  89. assert.True(t, perm.CanWrite(unit.Type))
  90. }
  91. // admin
  92. admin := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  93. perm, err = GetUserRepoPermission(repo, admin)
  94. assert.NoError(t, err)
  95. for _, unit := range repo.Units {
  96. assert.True(t, perm.CanRead(unit.Type))
  97. assert.True(t, perm.CanWrite(unit.Type))
  98. }
  99. }
  100. func TestRepoPermissionPublicOrgRepo(t *testing.T) {
  101. assert.NoError(t, PrepareTestDatabase())
  102. // public organization repo
  103. repo := AssertExistsAndLoadBean(t, &Repository{ID: 32}).(*Repository)
  104. assert.NoError(t, repo.getUnits(x))
  105. // plain user
  106. user := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
  107. perm, err := GetUserRepoPermission(repo, user)
  108. assert.NoError(t, err)
  109. for _, unit := range repo.Units {
  110. assert.True(t, perm.CanRead(unit.Type))
  111. assert.False(t, perm.CanWrite(unit.Type))
  112. }
  113. // change to collaborator to default write access
  114. assert.NoError(t, repo.AddCollaborator(user))
  115. perm, err = GetUserRepoPermission(repo, user)
  116. assert.NoError(t, err)
  117. for _, unit := range repo.Units {
  118. assert.True(t, perm.CanRead(unit.Type))
  119. assert.True(t, perm.CanWrite(unit.Type))
  120. }
  121. assert.NoError(t, repo.ChangeCollaborationAccessMode(user.ID, AccessModeRead))
  122. perm, err = GetUserRepoPermission(repo, user)
  123. assert.NoError(t, err)
  124. for _, unit := range repo.Units {
  125. assert.True(t, perm.CanRead(unit.Type))
  126. assert.False(t, perm.CanWrite(unit.Type))
  127. }
  128. // org member team owner
  129. owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  130. perm, err = GetUserRepoPermission(repo, owner)
  131. assert.NoError(t, err)
  132. for _, unit := range repo.Units {
  133. assert.True(t, perm.CanRead(unit.Type))
  134. assert.True(t, perm.CanWrite(unit.Type))
  135. }
  136. // org member team tester
  137. member := AssertExistsAndLoadBean(t, &User{ID: 15}).(*User)
  138. perm, err = GetUserRepoPermission(repo, member)
  139. assert.NoError(t, err)
  140. for _, unit := range repo.Units {
  141. assert.True(t, perm.CanRead(unit.Type))
  142. }
  143. assert.True(t, perm.CanWrite(UnitTypeIssues))
  144. assert.False(t, perm.CanWrite(UnitTypeCode))
  145. // admin
  146. admin := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  147. perm, err = GetUserRepoPermission(repo, admin)
  148. assert.NoError(t, err)
  149. for _, unit := range repo.Units {
  150. assert.True(t, perm.CanRead(unit.Type))
  151. assert.True(t, perm.CanWrite(unit.Type))
  152. }
  153. }
  154. func TestRepoPermissionPrivateOrgRepo(t *testing.T) {
  155. assert.NoError(t, PrepareTestDatabase())
  156. // private organization repo
  157. repo := AssertExistsAndLoadBean(t, &Repository{ID: 24}).(*Repository)
  158. assert.NoError(t, repo.getUnits(x))
  159. // plain user
  160. user := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
  161. perm, err := GetUserRepoPermission(repo, user)
  162. assert.NoError(t, err)
  163. for _, unit := range repo.Units {
  164. assert.False(t, perm.CanRead(unit.Type))
  165. assert.False(t, perm.CanWrite(unit.Type))
  166. }
  167. // change to collaborator to default write access
  168. assert.NoError(t, repo.AddCollaborator(user))
  169. perm, err = GetUserRepoPermission(repo, user)
  170. assert.NoError(t, err)
  171. for _, unit := range repo.Units {
  172. assert.True(t, perm.CanRead(unit.Type))
  173. assert.True(t, perm.CanWrite(unit.Type))
  174. }
  175. assert.NoError(t, repo.ChangeCollaborationAccessMode(user.ID, AccessModeRead))
  176. perm, err = GetUserRepoPermission(repo, user)
  177. assert.NoError(t, err)
  178. for _, unit := range repo.Units {
  179. assert.True(t, perm.CanRead(unit.Type))
  180. assert.False(t, perm.CanWrite(unit.Type))
  181. }
  182. // org member team owner
  183. owner := AssertExistsAndLoadBean(t, &User{ID: 15}).(*User)
  184. perm, err = GetUserRepoPermission(repo, owner)
  185. assert.NoError(t, err)
  186. for _, unit := range repo.Units {
  187. assert.True(t, perm.CanRead(unit.Type))
  188. assert.True(t, perm.CanWrite(unit.Type))
  189. }
  190. // update team information and then check permission
  191. team := AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team)
  192. err = UpdateTeamUnits(team, nil)
  193. assert.NoError(t, err)
  194. perm, err = GetUserRepoPermission(repo, owner)
  195. assert.NoError(t, err)
  196. for _, unit := range repo.Units {
  197. assert.True(t, perm.CanRead(unit.Type))
  198. assert.True(t, perm.CanWrite(unit.Type))
  199. }
  200. // org member team tester
  201. tester := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  202. perm, err = GetUserRepoPermission(repo, tester)
  203. assert.NoError(t, err)
  204. assert.True(t, perm.CanWrite(UnitTypeIssues))
  205. assert.False(t, perm.CanWrite(UnitTypeCode))
  206. assert.False(t, perm.CanRead(UnitTypeCode))
  207. // org member team reviewer
  208. reviewer := AssertExistsAndLoadBean(t, &User{ID: 20}).(*User)
  209. perm, err = GetUserRepoPermission(repo, reviewer)
  210. assert.NoError(t, err)
  211. assert.False(t, perm.CanRead(UnitTypeIssues))
  212. assert.False(t, perm.CanWrite(UnitTypeCode))
  213. assert.True(t, perm.CanRead(UnitTypeCode))
  214. // admin
  215. admin := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  216. perm, err = GetUserRepoPermission(repo, admin)
  217. assert.NoError(t, err)
  218. for _, unit := range repo.Units {
  219. assert.True(t, perm.CanRead(unit.Type))
  220. assert.True(t, perm.CanWrite(unit.Type))
  221. }
  222. }
上海开阖软件有限公司 沪ICP备12045867号-1