0 package db
1
2 import (
3 "testing"
4 "gemigit/test"
5 )
6
7 func TestAddUserAccess(t *testing.T) {
8
9 initDB(t)
10
11 user, signature := createUserAndSession(t)
12 user2, signature2 := createUserAndSession(t)
13
14 test.IsNil(t, user.CreateRepo(validRepoName, signature))
15 repo, err := user.GetRepo(validRepoName)
16 test.IsNil(t, err)
17
18 test.IsNil(t, user2.CreateRepo(validRepoName, signature2))
19 repo2, err := user2.GetRepo(validRepoName)
20 test.IsNil(t, err)
21
22 test.IsNil(t, user.AddUserAccess(repo, user2.Name))
23 test.IsNotNil(t, user.AddUserAccess(repo, user2.Name + "a"),
24 "invalid user")
25 test.IsNotNil(t, user.AddUserAccess(repo2, user2.Name),
26 "not the owner of the repository")
27 test.IsNotNil(t, user.AddUserAccess(repo, user.Name),
28 "the owner already has access")
29 test.IsNotNil(t, user.AddUserAccess(repo, user2.Name),
30 "user already has access")
31
32 }
33
34 func TestAddGroupAccess(t *testing.T) {
35
36 initDB(t)
37
38 user, signature := createUserAndSession(t)
39 user2, signature2 := createUserAndSession(t)
40
41 test.IsNil(t, user.CreateRepo(validRepoName, signature))
42 repo, err := user.GetRepo(validRepoName)
43 test.IsNil(t, err)
44
45 test.IsNil(t, user2.CreateRepo(validRepoName, signature2))
46 repo2, err := user2.GetRepo(validRepoName)
47 test.IsNil(t, err)
48
49 group := test.FuncName(t)
50 group2 := group + "2"
51 test.IsNil(t, user.CreateGroup(group, signature))
52 test.IsNil(t, user2.CreateGroup(group2, signature2))
53
54 test.IsNil(t, user.AddGroupAccess(repo, group))
55 test.IsNotNil(t, user.AddGroupAccess(repo, group + "a"),
56 "invalid group")
57 test.IsNotNil(t, user.AddGroupAccess(repo2, group2),
58 "not the owner of the repository")
59 test.IsNotNil(t, user.AddGroupAccess(repo, group),
60 "group already has access")
61
62 }
63
64 func TestRemoveUserAccess(t *testing.T) {
65
66 initDB(t)
67
68 user, signature := createUserAndSession(t)
69 user2, _ := createUserAndSession(t)
70
71 test.IsNil(t, user.CreateRepo(validRepoName, signature))
72 repo, err := user.GetRepo(validRepoName)
73 test.IsNil(t, err)
74
75 test.IsNil(t, user.AddUserAccess(repo, user2.Name))
76 test.IsNotNil(t, user2.RemoveUserAccess(repo, user2.ID),
77 "not the repository owner")
78 test.IsNil(t, user.RemoveUserAccess(repo, user2.ID))
79 test.IsNotNil(t, user.RemoveUserAccess(repo, user2.ID),
80 "access already revoked")
81
82 }
83
84 func TestRemoveGroupAccess(t *testing.T) {
85
86 initDB(t)
87
88 user, signature := createUserAndSession(t)
89 user2, _ := createUserAndSession(t)
90
91 test.IsNil(t, user.CreateRepo(validRepoName, signature))
92 repo, err := user.GetRepo(validRepoName)
93 test.IsNil(t, err)
94
95 group := test.FuncName(t)
96 test.IsNil(t, user.CreateGroup(group, signature))
97 id, err := GetGroupID(group)
98 test.IsNil(t, err)
99
100 test.IsNil(t, user.AddGroupAccess(repo, group))
101 test.IsNotNil(t, user2.RemoveGroupAccess(repo, id),
102 "not the repository owner")
103 test.IsNil(t, user.RemoveGroupAccess(repo, id))
104 test.IsNotNil(t, user.RemoveGroupAccess(repo, id),
105 "access already revoked")
106
107 }
108
109 func TestSetUserAccess(t *testing.T) {
110
111 initDB(t)
112
113 user, signature := createUserAndSession(t)
114 user2, _ := createUserAndSession(t)
115
116 test.IsNil(t, user.CreateRepo(validRepoName, signature))
117 repo, err := user.GetRepo(validRepoName)
118 test.IsNil(t, err)
119
120 test.IsNil(t, user.AddUserAccess(repo, user2.Name))
121
122 test.IsNil(t, user.SetUserAccess(repo, user2.ID, 2))
123 test.IsNotNil(t, user.SetUserAccess(repo, user.ID, 2),
124 "cannot change owner access")
125 test.IsNotNil(t, user2.SetUserAccess(repo, user2.ID, 2),
126 "only the owner can manage access")
127
128 }
129
130 func TestSetGroupAccess(t *testing.T) {
131
132 initDB(t)
133
134 user, signature := createUserAndSession(t)
135 user2, _ := createUserAndSession(t)
136
137 test.IsNil(t, user.CreateRepo(validRepoName, signature))
138 repo, err := user.GetRepo(validRepoName)
139 test.IsNil(t, err)
140
141 group := test.FuncName(t)
142 test.IsNil(t, user.CreateGroup(group, signature))
143
144 test.IsNil(t, user.AddGroupAccess(repo, group))
145 id, err := GetGroupID(group)
146 test.IsNil(t, err)
147
148 test.IsNil(t, user.SetGroupAccess(repo, id, 2))
149 test.IsNotNil(t, user2.SetGroupAccess(repo, id, 2),
150 "only the owner can manage access")
151
152 }
153
154 func TestGetUserAccess(t *testing.T) {
155
156 initDB(t)
157
158 user, signature := createUserAndSession(t)
159 user2, _ := createUserAndSession(t)
160
161 test.IsNil(t, user.CreateRepo(validRepoName, signature))
162 repo, err := user.GetRepo(validRepoName)
163 test.IsNil(t, err)
164
165 access, err := GetUserAccess(repo, user2)
166 test.IsNil(t, err)
167 test.IsEqual(t, access, accessNone)
168
169 access, err = GetUserAccess(repo, user)
170 test.IsNil(t, err)
171 test.IsEqual(t, access, accessMax)
172
173 test.IsNil(t, user.AddUserAccess(repo, user2.Name))
174
175 test.IsNil(t, user.SetUserAccess(repo, user2.ID, accessRead))
176 access, err = GetUserAccess(repo, user2)
177 test.IsNil(t, err)
178 test.IsEqual(t, access, accessRead)
179
180 test.IsNil(t, user.SetUserAccess(repo, user2.ID, accessReadWrite))
181 access, err = GetUserAccess(repo, user2)
182 test.IsNil(t, err)
183 test.IsEqual(t, access, accessReadWrite)
184
185 }
186
187 func TestGetGroupAccess(t *testing.T) {
188
189 initDB(t)
190
191 user, signature := createUserAndSession(t)
192
193 test.IsNil(t, user.CreateRepo(validRepoName, signature))
194 repo, err := user.GetRepo(validRepoName)
195 test.IsNil(t, err)
196
197 group := test.FuncName(t)
198 test.IsNil(t, user.CreateGroup(group, signature))
199 id, err := GetGroupID(group)
200 test.IsNil(t, err)
201
202 _, err = GetGroupAccess(repo, id)
203 test.IsNotNil(t, err, "the group does not have any access")
204
205 test.IsNil(t, user.AddGroupAccess(repo, group))
206
207 test.IsNil(t, user.SetGroupAccess(repo, id, 1))
208 access, err := GetGroupAccess(repo, id)
209 test.IsNil(t, err)
210 test.IsEqual(t, access, 1)
211
212 test.IsNil(t, user.SetGroupAccess(repo, id, 0))
213 access, err = GetGroupAccess(repo, id)
214 test.IsNil(t, err)
215 test.IsEqual(t, access, 0)
216
217 }
218
219 func TestGetUserGroupAccess(t *testing.T) {
220
221 initDB(t)
222
223 user, signature := createUserAndSession(t)
224 user2, _ := createUserAndSession(t)
225
226 test.IsNil(t, user.CreateRepo(validRepoName, signature))
227 repo, err := user.GetRepo(validRepoName)
228 test.IsNil(t, err)
229
230 group := test.FuncName(t)
231 test.IsNil(t, user.CreateGroup(group, signature))
232 id, err := GetGroupID(group)
233 test.IsNil(t, err)
234
235 test.IsNil(t, user.AddGroupAccess(repo, group))
236
237 access, err := GetUserGroupAccess(repo, user2)
238 test.IsNil(t, err)
239 test.IsEqual(t, access, -1)
240
241 test.IsNil(t, user.AddUserToGroup(group, user2.Name))
242
243 access, err = GetUserGroupAccess(repo, user2)
244 test.IsNil(t, err)
245 test.IsEqual(t, access, accessRead)
246
247 test.IsNil(t, user.SetGroupAccess(repo, id, accessReadWrite))
248
249 access, err = GetUserGroupAccess(repo, user2)
250 test.IsNil(t, err)
251 test.IsEqual(t, access, accessReadWrite)
252
253 }
254
255 func TestGetAccess(t *testing.T) {
256
257 initDB(t)
258
259 user, signature := createUserAndSession(t)
260 user2, _ := createUserAndSession(t)
261
262 test.IsNil(t, user.CreateRepo(validRepoName, signature))
263 repo, err := user.GetRepo(validRepoName)
264 test.IsNil(t, err)
265
266 group := test.FuncName(t)
267 test.IsNil(t, user.CreateGroup(group, signature))
268 id, err := GetGroupID(group)
269 test.IsNil(t, err)
270
271 test.IsNil(t, user.AddGroupAccess(repo, group))
272
273 access, err := GetAccess(user, repo)
274 test.IsNil(t, err)
275 test.IsEqual(t, access, accessMax)
276
277 access, err = GetAccess(user2, repo)
278 test.IsNil(t, err)
279 test.IsEqual(t, access, accessNone)
280
281 test.IsNil(t, user.AddUserToGroup(group, user2.Name))
282
283 access, err = GetAccess(user2, repo)
284 test.IsNil(t, err)
285 test.IsEqual(t, access, accessRead)
286
287 test.IsNil(t, user.SetGroupAccess(repo, id, 2))
288
289 access, err = GetAccess(user2, repo)
290 test.IsNil(t, err)
291 test.IsEqual(t, access, 2)
292
293 }
294
295 func TestGetRepoUserAccess(t *testing.T) {
296
297 initDB(t)
298
299 user, signature := createUserAndSession(t)
300 user2, _ := createUserAndSession(t)
301
302 test.IsNil(t, user.CreateRepo(validRepoName, signature))
303 repo, err := user.GetRepo(validRepoName)
304 test.IsNil(t, err)
305
306 access, err := GetRepoUserAccess(repo.ID)
307 test.IsNil(t, err)
308 test.IsEqual(t, len(access), 0)
309
310 test.IsNil(t, user.AddUserAccess(repo, user2.Name))
311
312 access, err = GetRepoUserAccess(repo.ID)
313 test.IsNil(t, err)
314 test.IsEqual(t, len(access), 1)
315 test.IsEqual(t, access[0].UserID, user2.ID)
316 test.IsEqual(t, access[0].Privilege, accessDefault)
317
318 test.IsNil(t, user.SetUserAccess(repo, user2.ID, accessReadWrite))
319
320 access, err = GetRepoUserAccess(repo.ID)
321 test.IsNil(t, err)
322 test.IsEqual(t, len(access), 1)
323 test.IsEqual(t, access[0].UserID, user2.ID)
324 test.IsEqual(t, access[0].Privilege, accessReadWrite)
325
326 }
327
328 func TestGetRepoGroupAccess(t *testing.T) {
329
330 initDB(t)
331
332 user, signature := createUserAndSession(t)
333
334 group := test.FuncName(t)
335 test.IsNil(t, user.CreateGroup(group, signature))
336 id, err := GetGroupID(group)
337 test.IsNil(t, err)
338
339 test.IsNil(t, user.CreateRepo(validRepoName, signature))
340 repo, err := user.GetRepo(validRepoName)
341 test.IsNil(t, err)
342
343 access, err := GetRepoGroupAccess(repo.ID)
344 test.IsNil(t, err)
345 test.IsEqual(t, len(access), 0)
346
347 test.IsNil(t, user.AddGroupAccess(repo, group))
348
349 access, err = GetRepoGroupAccess(repo.ID)
350 test.IsNil(t, err)
351 test.IsEqual(t, len(access), 1)
352 test.IsEqual(t, access[0].GroupID, id)
353 test.IsEqual(t, access[0].Privilege, accessDefault)
354
355 test.IsNil(t, user.SetGroupAccess(repo, id, accessReadWrite))
356
357 access, err = GetRepoGroupAccess(repo.ID)
358 test.IsNil(t, err)
359 test.IsEqual(t, len(access), 1)
360 test.IsEqual(t, access[0].GroupID, id)
361 test.IsEqual(t, access[0].Privilege, accessReadWrite)
362
363 }
364
365 func TestHasReadAccessTo(t *testing.T) {
366
367 initDB(t)
368
369 user, signature := createUserAndSession(t)
370 user2, signature2 := createUserAndSession(t)
371
372 repos, err := user.HasReadAccessTo()
373 test.IsNil(t, err)
374 test.IsEqual(t, len(repos), 0)
375
376 test.IsNil(t, user2.CreateRepo(validRepoName, signature2))
377 repo, err := user2.GetRepo(validRepoName)
378 test.IsNil(t, err)
379
380 test.IsNil(t, user2.CreateRepo(validRepoName + "2", signature2))
381 repo2, err := user2.GetRepo(validRepoName + "2")
382 test.IsNil(t, err)
383
384 group := test.FuncName(t)
385 test.IsNil(t, user.CreateGroup(group, signature))
386
387 user2.AddUserAccess(repo, user.Name)
388
389 repos, err = user.HasReadAccessTo()
390 test.IsNil(t, err)
391 test.IsEqual(t, len(repos), 1)
392
393 user2.AddGroupAccess(repo2, group)
394
395 repos, err = user.HasReadAccessTo()
396 test.IsNil(t, err)
397 test.IsEqual(t, len(repos), 2)
398 }
399