perms_test.go 6.4 KB

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