permissions_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Copyright 2020 The Gogs 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 database
  5. import (
  6. "context"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestPerms(t *testing.T) {
  12. if testing.Short() {
  13. t.Skip()
  14. }
  15. t.Parallel()
  16. ctx := context.Background()
  17. s := &PermissionsStore{
  18. db: newTestDB(t, "PermissionsStore"),
  19. }
  20. for _, tc := range []struct {
  21. name string
  22. test func(t *testing.T, ctx context.Context, s *PermissionsStore)
  23. }{
  24. {"AccessMode", permsAccessMode},
  25. {"Authorize", permsAuthorize},
  26. {"SetRepoPerms", permsSetRepoPerms},
  27. } {
  28. t.Run(tc.name, func(t *testing.T) {
  29. t.Cleanup(func() {
  30. err := clearTables(t, s.db)
  31. require.NoError(t, err)
  32. })
  33. tc.test(t, ctx, s)
  34. })
  35. if t.Failed() {
  36. break
  37. }
  38. }
  39. }
  40. func permsAccessMode(t *testing.T, ctx context.Context, s *PermissionsStore) {
  41. // Set up permissions
  42. err := s.SetRepoPerms(ctx, 1,
  43. map[int64]AccessMode{
  44. 2: AccessModeWrite,
  45. 3: AccessModeAdmin,
  46. },
  47. )
  48. require.NoError(t, err)
  49. err = s.SetRepoPerms(ctx, 2,
  50. map[int64]AccessMode{
  51. 1: AccessModeRead,
  52. },
  53. )
  54. require.NoError(t, err)
  55. publicRepoID := int64(1)
  56. publicRepoOpts := AccessModeOptions{
  57. OwnerID: 98,
  58. }
  59. privateRepoID := int64(2)
  60. privateRepoOpts := AccessModeOptions{
  61. OwnerID: 99,
  62. Private: true,
  63. }
  64. tests := []struct {
  65. name string
  66. userID int64
  67. repoID int64
  68. opts AccessModeOptions
  69. wantAccessMode AccessMode
  70. }{
  71. {
  72. name: "nil repository",
  73. wantAccessMode: AccessModeNone,
  74. },
  75. {
  76. name: "anonymous user has read access to public repository",
  77. repoID: publicRepoID,
  78. opts: publicRepoOpts,
  79. wantAccessMode: AccessModeRead,
  80. },
  81. {
  82. name: "anonymous user has no access to private repository",
  83. repoID: privateRepoID,
  84. opts: privateRepoOpts,
  85. wantAccessMode: AccessModeNone,
  86. },
  87. {
  88. name: "user is the owner",
  89. userID: 98,
  90. repoID: publicRepoID,
  91. opts: publicRepoOpts,
  92. wantAccessMode: AccessModeOwner,
  93. },
  94. {
  95. name: "user 1 has read access to public repo",
  96. userID: 1,
  97. repoID: publicRepoID,
  98. opts: publicRepoOpts,
  99. wantAccessMode: AccessModeRead,
  100. },
  101. {
  102. name: "user 2 has write access to public repo",
  103. userID: 2,
  104. repoID: publicRepoID,
  105. opts: publicRepoOpts,
  106. wantAccessMode: AccessModeWrite,
  107. },
  108. {
  109. name: "user 3 has admin access to public repo",
  110. userID: 3,
  111. repoID: publicRepoID,
  112. opts: publicRepoOpts,
  113. wantAccessMode: AccessModeAdmin,
  114. },
  115. {
  116. name: "user 1 has read access to private repo",
  117. userID: 1,
  118. repoID: privateRepoID,
  119. opts: privateRepoOpts,
  120. wantAccessMode: AccessModeRead,
  121. },
  122. {
  123. name: "user 2 has no access to private repo",
  124. userID: 2,
  125. repoID: privateRepoID,
  126. opts: privateRepoOpts,
  127. wantAccessMode: AccessModeNone,
  128. },
  129. {
  130. name: "user 3 has no access to private repo",
  131. userID: 3,
  132. repoID: privateRepoID,
  133. opts: privateRepoOpts,
  134. wantAccessMode: AccessModeNone,
  135. },
  136. }
  137. for _, test := range tests {
  138. t.Run(test.name, func(t *testing.T) {
  139. mode := s.AccessMode(ctx, test.userID, test.repoID, test.opts)
  140. assert.Equal(t, test.wantAccessMode, mode)
  141. })
  142. }
  143. }
  144. func permsAuthorize(t *testing.T, ctx context.Context, s *PermissionsStore) {
  145. // Set up permissions
  146. err := s.SetRepoPerms(ctx, 1,
  147. map[int64]AccessMode{
  148. 1: AccessModeRead,
  149. 2: AccessModeWrite,
  150. 3: AccessModeAdmin,
  151. },
  152. )
  153. require.NoError(t, err)
  154. repo := &Repository{
  155. ID: 1,
  156. OwnerID: 98,
  157. }
  158. tests := []struct {
  159. name string
  160. userID int64
  161. desired AccessMode
  162. wantAuthorized bool
  163. }{
  164. {
  165. name: "user 1 has read and wants read",
  166. userID: 1,
  167. desired: AccessModeRead,
  168. wantAuthorized: true,
  169. },
  170. {
  171. name: "user 1 has read and wants write",
  172. userID: 1,
  173. desired: AccessModeWrite,
  174. wantAuthorized: false,
  175. },
  176. {
  177. name: "user 2 has write and wants read",
  178. userID: 2,
  179. desired: AccessModeRead,
  180. wantAuthorized: true,
  181. },
  182. {
  183. name: "user 2 has write and wants write",
  184. userID: 2,
  185. desired: AccessModeWrite,
  186. wantAuthorized: true,
  187. },
  188. {
  189. name: "user 2 has write and wants admin",
  190. userID: 2,
  191. desired: AccessModeAdmin,
  192. wantAuthorized: false,
  193. },
  194. {
  195. name: "user 3 has admin and wants read",
  196. userID: 3,
  197. desired: AccessModeRead,
  198. wantAuthorized: true,
  199. },
  200. {
  201. name: "user 3 has admin and wants write",
  202. userID: 3,
  203. desired: AccessModeWrite,
  204. wantAuthorized: true,
  205. },
  206. {
  207. name: "user 3 has admin and wants admin",
  208. userID: 3,
  209. desired: AccessModeAdmin,
  210. wantAuthorized: true,
  211. },
  212. }
  213. for _, test := range tests {
  214. t.Run(test.name, func(t *testing.T) {
  215. authorized := s.Authorize(ctx, test.userID, repo.ID, test.desired,
  216. AccessModeOptions{
  217. OwnerID: repo.OwnerID,
  218. Private: repo.IsPrivate,
  219. },
  220. )
  221. assert.Equal(t, test.wantAuthorized, authorized)
  222. })
  223. }
  224. }
  225. func permsSetRepoPerms(t *testing.T, ctx context.Context, s *PermissionsStore) {
  226. for _, update := range []struct {
  227. repoID int64
  228. accessMap map[int64]AccessMode
  229. }{
  230. {
  231. repoID: 1,
  232. accessMap: map[int64]AccessMode{
  233. 1: AccessModeWrite,
  234. 2: AccessModeWrite,
  235. 3: AccessModeAdmin,
  236. 4: AccessModeWrite,
  237. },
  238. },
  239. {
  240. repoID: 2,
  241. accessMap: map[int64]AccessMode{
  242. 1: AccessModeWrite,
  243. 2: AccessModeRead,
  244. 4: AccessModeWrite,
  245. 5: AccessModeWrite,
  246. },
  247. },
  248. {
  249. repoID: 1,
  250. accessMap: map[int64]AccessMode{
  251. 2: AccessModeWrite,
  252. 3: AccessModeAdmin,
  253. },
  254. },
  255. {
  256. repoID: 2,
  257. accessMap: map[int64]AccessMode{
  258. 1: AccessModeWrite,
  259. 2: AccessModeRead,
  260. 5: AccessModeWrite,
  261. },
  262. },
  263. } {
  264. err := s.SetRepoPerms(ctx, update.repoID, update.accessMap)
  265. if err != nil {
  266. t.Fatal(err)
  267. }
  268. }
  269. var accesses []*Access
  270. err := s.db.Order("user_id, repo_id").Find(&accesses).Error
  271. require.NoError(t, err)
  272. // Ignore ID fields
  273. for _, a := range accesses {
  274. a.ID = 0
  275. }
  276. wantAccesses := []*Access{
  277. {UserID: 1, RepoID: 2, Mode: AccessModeWrite},
  278. {UserID: 2, RepoID: 1, Mode: AccessModeWrite},
  279. {UserID: 2, RepoID: 2, Mode: AccessModeRead},
  280. {UserID: 3, RepoID: 1, Mode: AccessModeAdmin},
  281. {UserID: 5, RepoID: 2, Mode: AccessModeWrite},
  282. }
  283. assert.Equal(t, wantAccesses, accesses)
  284. }