Skip to content

Commit dbe81c0

Browse files
Add unit tests for adding and deleting team from repository.
Signed-off-by: David Svantesson <davidsvantesson@gmail.com>
1 parent 8ff8824 commit dbe81c0

File tree

5 files changed

+235
-1
lines changed

5 files changed

+235
-1
lines changed

models/fixtures/repository.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,4 +508,15 @@
508508
num_stars: 0
509509
num_forks: 0
510510
num_issues: 0
511+
is_mirror: false
512+
513+
-
514+
id: 43
515+
owner_id: 26
516+
lower_name: repo26
517+
name: repo26
518+
is_private: true
519+
num_stars: 0
520+
num_forks: 0
521+
num_issues: 0
511522
is_mirror: false

models/fixtures/team.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,12 @@
8787
authorize: 1 # owner
8888
num_repos: 0
8989
num_members: 1
90+
91+
-
92+
id: 11
93+
org_id: 26
94+
lower_name: team11
95+
name: team11
96+
authorize: 1 # read
97+
num_repos: 0
98+
num_members: 0

models/fixtures/user.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,21 @@
410410
num_repos: 0
411411
num_members: 1
412412
num_teams: 1
413+
414+
-
415+
id: 26
416+
lower_name: org26
417+
name: org26
418+
full_name: "Org26"
419+
email: org26@example.com
420+
email_notifications_preference: onmention
421+
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password
422+
type: 1 # organization
423+
salt: ZogKvWdyEx
424+
is_admin: false
425+
avatar: avatar26
426+
avatar_email: org26@example.com
427+
num_repos: 1
428+
num_members: 0
429+
num_teams: 1
430+
repo_admin_change_team_access: true

models/user_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ func TestSearchUsers(t *testing.T) {
140140
testOrgSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 3, PageSize: 2},
141141
[]int64{19, 25})
142142

143-
testOrgSuccess(&SearchUserOptions{Page: 4, PageSize: 2},
143+
testOrgSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 4, PageSize: 2},
144+
[]int64{26})
145+
146+
testOrgSuccess(&SearchUserOptions{Page: 5, PageSize: 2},
144147
[]int64{})
145148

146149
// test users

routers/repo/settings_test.go

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,196 @@ func TestCollaborationPost_NonExistentUser(t *testing.T) {
185185
assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
186186
assert.NotEmpty(t, ctx.Flash.ErrorMsg)
187187
}
188+
189+
func TestAddTeamPost(t *testing.T) {
190+
models.PrepareTestEnv(t)
191+
ctx := test.MockContext(t, "org26/repo43")
192+
193+
ctx.Req.Form.Set("team", "team11")
194+
195+
org := &models.User{
196+
LowerName: "org26",
197+
Type: models.UserTypeOrganization,
198+
}
199+
200+
team := &models.Team{
201+
ID: 11,
202+
OrgID: 26,
203+
}
204+
205+
re := &models.Repository{
206+
ID: 43,
207+
Owner: org,
208+
OwnerID: 26,
209+
}
210+
211+
repo := &context.Repository{
212+
Owner: &models.User{
213+
ID: 26,
214+
LowerName: "org26",
215+
RepoAdminChangeTeamAccess: true,
216+
},
217+
Repository: re,
218+
}
219+
220+
ctx.Repo = repo
221+
222+
AddTeamPost(ctx)
223+
224+
assert.True(t, team.HasRepository(re.ID))
225+
assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
226+
assert.Empty(t, ctx.Flash.ErrorMsg)
227+
}
228+
229+
func TestAddTeamPost_NotAllowed(t *testing.T) {
230+
models.PrepareTestEnv(t)
231+
ctx := test.MockContext(t, "org26/repo43")
232+
233+
ctx.Req.Form.Set("team", "team11")
234+
235+
org := &models.User{
236+
LowerName: "org26",
237+
Type: models.UserTypeOrganization,
238+
}
239+
240+
team := &models.Team{
241+
ID: 11,
242+
OrgID: 26,
243+
}
244+
245+
re := &models.Repository{
246+
ID: 43,
247+
Owner: org,
248+
OwnerID: 26,
249+
}
250+
251+
repo := &context.Repository{
252+
Owner: &models.User{
253+
ID: 26,
254+
LowerName: "org26",
255+
RepoAdminChangeTeamAccess: false,
256+
},
257+
Repository: re,
258+
}
259+
260+
ctx.Repo = repo
261+
262+
AddTeamPost(ctx)
263+
264+
assert.False(t, team.HasRepository(re.ID))
265+
assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
266+
assert.NotEmpty(t, ctx.Flash.ErrorMsg)
267+
268+
}
269+
270+
func TestAddTeamPost_AddTeamTwice(t *testing.T) {
271+
models.PrepareTestEnv(t)
272+
ctx := test.MockContext(t, "org26/repo43")
273+
274+
ctx.Req.Form.Set("team", "team11")
275+
276+
org := &models.User{
277+
LowerName: "org26",
278+
Type: models.UserTypeOrganization,
279+
}
280+
281+
team := &models.Team{
282+
ID: 11,
283+
OrgID: 26,
284+
}
285+
286+
re := &models.Repository{
287+
ID: 43,
288+
Owner: org,
289+
OwnerID: 26,
290+
}
291+
292+
repo := &context.Repository{
293+
Owner: &models.User{
294+
ID: 26,
295+
LowerName: "org26",
296+
RepoAdminChangeTeamAccess: true,
297+
},
298+
Repository: re,
299+
}
300+
301+
ctx.Repo = repo
302+
303+
AddTeamPost(ctx)
304+
305+
AddTeamPost(ctx)
306+
assert.True(t, team.HasRepository(re.ID))
307+
assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
308+
assert.NotEmpty(t, ctx.Flash.ErrorMsg)
309+
}
310+
311+
func TestAddTeamPost_NonExistentTeam(t *testing.T) {
312+
models.PrepareTestEnv(t)
313+
ctx := test.MockContext(t, "org26/repo43")
314+
315+
ctx.Req.Form.Set("team", "team-non-existent")
316+
317+
org := &models.User{
318+
LowerName: "org26",
319+
Type: models.UserTypeOrganization,
320+
}
321+
322+
re := &models.Repository{
323+
ID: 43,
324+
Owner: org,
325+
OwnerID: 26,
326+
}
327+
328+
repo := &context.Repository{
329+
Owner: &models.User{
330+
ID: 26,
331+
LowerName: "org26",
332+
RepoAdminChangeTeamAccess: true,
333+
},
334+
Repository: re,
335+
}
336+
337+
ctx.Repo = repo
338+
339+
AddTeamPost(ctx)
340+
assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
341+
assert.NotEmpty(t, ctx.Flash.ErrorMsg)
342+
}
343+
344+
func TestDeleteTeam(t *testing.T) {
345+
models.PrepareTestEnv(t)
346+
ctx := test.MockContext(t, "org3/team1/repo3")
347+
348+
ctx.Req.Form.Set("id", "2")
349+
350+
org := &models.User{
351+
LowerName: "org3",
352+
Type: models.UserTypeOrganization,
353+
}
354+
355+
team := &models.Team{
356+
ID: 2,
357+
OrgID: 3,
358+
}
359+
360+
re := &models.Repository{
361+
ID: 3,
362+
Owner: org,
363+
OwnerID: 3,
364+
}
365+
366+
repo := &context.Repository{
367+
Owner: &models.User{
368+
ID: 3,
369+
LowerName: "org3",
370+
RepoAdminChangeTeamAccess: true,
371+
},
372+
Repository: re,
373+
}
374+
375+
ctx.Repo = repo
376+
377+
DeleteTeam(ctx)
378+
379+
assert.False(t, team.HasRepository(re.ID))
380+
}

0 commit comments

Comments
 (0)