Browse Source

Unit tests and bug fix for models/org

Ethan Koenig 3 years ago
parent
commit
a2412492da

+ 1 - 1
models/access_test.go

@@ -106,7 +106,7 @@ func TestRepository_RecalculateAccesses(t *testing.T) {
106 106
 	has, err := x.Get(access)
107 107
 	assert.NoError(t, err)
108 108
 	assert.True(t, has)
109
-	assert.Equal(t, AccessModeWrite, access.Mode)
109
+	assert.Equal(t, AccessModeOwner, access.Mode)
110 110
 }
111 111
 
112 112
 func TestRepository_RecalculateAccesses2(t *testing.T) {

+ 24 - 0
models/fixtures/org_user.yml

@@ -5,3 +5,27 @@
5 5
   is_public: true
6 6
   is_owner: true
7 7
   num_teams: 1
8
+
9
+-
10
+  id: 2
11
+  uid: 4
12
+  org_id: 3
13
+  is_public: false
14
+  is_owner: false
15
+  num_teams: 0
16
+
17
+-
18
+  id: 3
19
+  uid: 5
20
+  org_id: 6
21
+  is_public: true
22
+  is_owner: true
23
+  num_teams: 1
24
+
25
+-
26
+  id: 4
27
+  uid: 5
28
+  org_id: 7
29
+  is_public: false
30
+  is_owner: true
31
+  num_teams: 1

+ 12 - 0
models/fixtures/repository.yml

@@ -41,3 +41,15 @@
41 41
   num_closed_issues: 0
42 42
   num_pulls: 0
43 43
   num_closed_pulls: 0
44
+
45
+-
46
+  id: 5
47
+  owner_id: 3
48
+  lower_name: repo5
49
+  name: repo5
50
+  is_private: true
51
+  num_issues: 0
52
+  num_closed_issues: 0
53
+  num_pulls: 0
54
+  num_closed_pulls: 0
55
+  is_mirror: true

+ 28 - 0
models/fixtures/team.yml

@@ -1,7 +1,35 @@
1 1
 -
2 2
   id: 1
3 3
   org_id: 3
4
+  lower_name: owners
5
+  name: owners
6
+  authorize: 4 # owner
7
+  num_repos: 1
8
+  num_members: 1
9
+
10
+-
11
+  id: 2
12
+  org_id: 3
4 13
   lower_name: team1
5 14
   name: team1
6 15
   authorize: 2 # write
7 16
   num_repos: 1
17
+  num_members: 2
18
+
19
+-
20
+  id: 3
21
+  org_id: 6
22
+  lower_name: owners
23
+  name: owners
24
+  authorize: 4 # owner
25
+  num_repos: 0
26
+  num_members: 1
27
+
28
+-
29
+  id: 4
30
+  org_id: 7
31
+  lower_name: owners
32
+  name: owners
33
+  authorize: 4 # owner
34
+  num_repos: 0
35
+  num_members: 1

+ 12 - 0
models/fixtures/team_repo.yml

@@ -3,3 +3,15 @@
3 3
   org_id: 3
4 4
   team_id: 1
5 5
   repo_id: 3
6
+
7
+-
8
+  id: 2
9
+  org_id: 3
10
+  team_id: 2
11
+  repo_id: 3
12
+
13
+-
14
+  id: 3
15
+  org_id: 3
16
+  team_id: 1
17
+  repo_id: 5

+ 24 - 0
models/fixtures/team_user.yml

@@ -3,3 +3,27 @@
3 3
   org_id: 3
4 4
   team_id: 1
5 5
   uid: 2
6
+
7
+-
8
+  id: 2
9
+  org_id: 3
10
+  team_id: 2
11
+  uid: 2
12
+
13
+-
14
+  id: 3
15
+  org_id: 3
16
+  team_id: 2
17
+  uid: 4
18
+
19
+-
20
+  id: 4
21
+  org_id: 6
22
+  team_id: 3
23
+  uid: 5
24
+
25
+-
26
+  id: 5
27
+  org_id: 7
28
+  team_id: 4
29
+  uid: 5

+ 35 - 3
models/fixtures/user.yml

@@ -38,7 +38,8 @@
38 38
   is_admin: false
39 39
   avatar: avatar3
40 40
   avatar_email: user3@example.com
41
-  num_repos: 1
41
+  num_repos: 2
42
+  num_members: 2
42 43
 
43 44
 -
44 45
   id: 4
@@ -47,7 +48,7 @@
47 48
   full_name: User Four
48 49
   email: user4@example.com
49 50
   passwd: password
50
-  type: 1 # individual
51
+  type: 0 # individual
51 52
   salt: salt
52 53
   is_admin: false
53 54
   avatar: avatar4
@@ -61,9 +62,40 @@
61 62
   full_name: User Five
62 63
   email: user5@example.com
63 64
   passwd: password
64
-  type: 1 # individual
65
+  type: 0 # individual
65 66
   salt: salt
66 67
   is_admin: false
67 68
   avatar: avatar5
68 69
   avatar_email: user5@example.com
69 70
   num_repos: 1
71
+  allow_create_organization: false
72
+
73
+-
74
+  id: 6
75
+  lower_name: user6
76
+  name: user6
77
+  full_name: User Six
78
+  email: user6@example.com
79
+  passwd: password
80
+  type: 1 # organization
81
+  salt: salt
82
+  is_admin: false
83
+  avatar: avatar6
84
+  avatar_email: user6@example.com
85
+  num_repos: 0
86
+  num_members: 1
87
+
88
+-
89
+  id: 7
90
+  lower_name: user7
91
+  name: user7
92
+  full_name: User Seven
93
+  email: user7@example.com
94
+  passwd: password
95
+  type: 1 # organization
96
+  salt: salt
97
+  is_admin: false
98
+  avatar: avatar7
99
+  avatar_email: user7@example.com
100
+  num_repos: 0
101
+  num_members: 1

+ 8 - 22
models/org.go

@@ -124,6 +124,7 @@ func CreateOrganization(org, owner *User) (err error) {
124 124
 	org.MaxRepoCreation = -1
125 125
 	org.NumTeams = 1
126 126
 	org.NumMembers = 1
127
+	org.Type = UserTypeOrganization
127 128
 
128 129
 	sess := x.NewSession()
129 130
 	defer sessionRelease(sess)
@@ -350,12 +351,6 @@ func GetOrgsByUserID(userID int64, showAll bool) ([]*User, error) {
350 351
 	return getOrgsByUserID(sess, userID, showAll)
351 352
 }
352 353
 
353
-// GetOrgsByUserIDDesc returns a list of organizations that the given user ID
354
-// has joined, ordered descending by the given condition.
355
-func GetOrgsByUserIDDesc(userID int64, desc string, showAll bool) ([]*User, error) {
356
-	return getOrgsByUserID(x.Desc(desc), userID, showAll)
357
-}
358
-
359 354
 func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) {
360 355
 	orgs := make([]*User, 0, 10)
361 356
 	return orgs, sess.
@@ -464,10 +459,6 @@ func RemoveOrgUser(orgID, userID int64) error {
464 459
 		return nil
465 460
 	}
466 461
 
467
-	user, err := GetUserByID(userID)
468
-	if err != nil {
469
-		return fmt.Errorf("GetUserByID [%d]: %v", userID, err)
470
-	}
471 462
 	org, err := GetUserByID(orgID)
472 463
 	if err != nil {
473 464
 		return fmt.Errorf("GetUserByID [%d]: %v", orgID, err)
@@ -497,23 +488,23 @@ func RemoveOrgUser(orgID, userID int64) error {
497 488
 	}
498 489
 
499 490
 	// Delete all repository accesses and unwatch them.
500
-	env, err := org.AccessibleReposEnv(user.ID)
491
+	env, err := org.AccessibleReposEnv(userID)
501 492
 	if err != nil {
502 493
 		return fmt.Errorf("AccessibleReposEnv: %v", err)
503 494
 	}
504 495
 	repoIDs, err := env.RepoIDs(1, org.NumRepos)
505 496
 	if err != nil {
506
-		return fmt.Errorf("GetUserRepositories [%d]: %v", user.ID, err)
497
+		return fmt.Errorf("GetUserRepositories [%d]: %v", userID, err)
507 498
 	}
508 499
 	for _, repoID := range repoIDs {
509
-		if err = watchRepo(sess, user.ID, repoID, false); err != nil {
500
+		if err = watchRepo(sess, userID, repoID, false); err != nil {
510 501
 			return err
511 502
 		}
512 503
 	}
513 504
 
514 505
 	if len(repoIDs) > 0 {
515 506
 		if _, err = sess.
516
-			Where("user_id = ?", user.ID).
507
+			Where("user_id = ?", userID).
517 508
 			In("repo_id", repoIDs).
518 509
 			Delete(new(Access)); err != nil {
519 510
 			return err
@@ -521,12 +512,12 @@ func RemoveOrgUser(orgID, userID int64) error {
521 512
 	}
522 513
 
523 514
 	// Delete member in his/her teams.
524
-	teams, err := getUserTeams(sess, org.ID, user.ID)
515
+	teams, err := getUserTeams(sess, org.ID, userID)
525 516
 	if err != nil {
526 517
 		return err
527 518
 	}
528 519
 	for _, t := range teams {
529
-		if err = removeTeamMember(sess, org.ID, t.ID, user.ID); err != nil {
520
+		if err = removeTeamMember(sess, org.ID, t.ID, userID); err != nil {
530 521
 			return err
531 522
 		}
532 523
 	}
@@ -542,11 +533,6 @@ func removeOrgRepo(e Engine, orgID, repoID int64) error {
542 533
 	return err
543 534
 }
544 535
 
545
-// RemoveOrgRepo removes all team-repository relations of given organization.
546
-func RemoveOrgRepo(orgID, repoID int64) error {
547
-	return removeOrgRepo(x, orgID, repoID)
548
-}
549
-
550 536
 func (org *User) getUserTeams(e Engine, userID int64, cols ...string) ([]*Team, error) {
551 537
 	teams := make([]*Team, 0, org.NumTeams)
552 538
 	return teams, e.
@@ -619,7 +605,7 @@ func (env *accessibleReposEnv) CountRepos() (int64, error) {
619 605
 	repoCount, err := x.
620 606
 		Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
621 607
 		Where(env.cond()).
622
-		GroupBy("`repository`.id").
608
+		Distinct("`repository`.id").
623 609
 		Count(&Repository{})
624 610
 	if err != nil {
625 611
 		return 0, fmt.Errorf("count user repositories in organization: %v", err)

+ 520 - 0
models/org_test.go

@@ -0,0 +1,520 @@
1
+// Copyright 2017 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
+
5
+package models
6
+
7
+import (
8
+	"testing"
9
+
10
+	"github.com/stretchr/testify/assert"
11
+)
12
+
13
+func TestUser_IsOwnedBy(t *testing.T) {
14
+	assert.NoError(t, PrepareTestDatabase())
15
+	org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
16
+	assert.True(t, org.IsOwnedBy(2))
17
+	assert.False(t, org.IsOwnedBy(1))
18
+	assert.False(t, org.IsOwnedBy(3))
19
+	assert.False(t, org.IsOwnedBy(4))
20
+
21
+	nonOrg := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
22
+	assert.False(t, nonOrg.IsOwnedBy(2))
23
+	assert.False(t, nonOrg.IsOwnedBy(3))
24
+}
25
+
26
+func TestUser_IsOrgMember(t *testing.T) {
27
+	assert.NoError(t, PrepareTestDatabase())
28
+	org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
29
+	assert.True(t, org.IsOrgMember(2))
30
+	assert.True(t, org.IsOrgMember(4))
31
+	assert.False(t, org.IsOrgMember(1))
32
+	assert.False(t, org.IsOrgMember(3))
33
+
34
+	nonOrg := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
35
+	assert.False(t, nonOrg.IsOrgMember(2))
36
+	assert.False(t, nonOrg.IsOrgMember(3))
37
+}
38
+
39
+func TestUser_GetTeam(t *testing.T) {
40
+	assert.NoError(t, PrepareTestDatabase())
41
+	org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
42
+	team, err := org.GetTeam("team1")
43
+	assert.NoError(t, err)
44
+	assert.Equal(t, org.ID, team.OrgID)
45
+	assert.Equal(t, "team1", team.LowerName)
46
+
47
+	_, err = org.GetTeam("does not exist")
48
+	assert.Equal(t, ErrTeamNotExist, err)
49
+
50
+	nonOrg := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
51
+	_, err = nonOrg.GetTeam("team")
52
+	assert.Equal(t, ErrTeamNotExist, err)
53
+}
54
+
55
+func TestUser_GetOwnerTeam(t *testing.T) {
56
+	assert.NoError(t, PrepareTestDatabase())
57
+	org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
58
+	team, err := org.GetOwnerTeam()
59
+	assert.NoError(t, err)
60
+	assert.Equal(t, org.ID, team.OrgID)
61
+
62
+	nonOrg := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
63
+	_, err = nonOrg.GetOwnerTeam()
64
+	assert.Equal(t, ErrTeamNotExist, err)
65
+}
66
+
67
+func TestUser_GetTeams(t *testing.T) {
68
+	assert.NoError(t, PrepareTestDatabase())
69
+	org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
70
+	assert.NoError(t, org.GetTeams())
71
+	assert.Len(t, org.Teams, 2)
72
+	assert.Equal(t, int64(1), org.Teams[0].ID)
73
+	assert.Equal(t, int64(2), org.Teams[1].ID)
74
+}
75
+
76
+func TestUser_GetMembers(t *testing.T) {
77
+	assert.NoError(t, PrepareTestDatabase())
78
+	org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
79
+	assert.NoError(t, org.GetMembers())
80
+	assert.Len(t, org.Members, 2)
81
+	assert.Equal(t, int64(2), org.Members[0].ID)
82
+	assert.Equal(t, int64(4), org.Members[1].ID)
83
+}
84
+
85
+func TestUser_AddMember(t *testing.T) {
86
+	assert.NoError(t, PrepareTestDatabase())
87
+	org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
88
+
89
+	// add a user that is not a member
90
+	AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3})
91
+	prevNumMembers := org.NumMembers
92
+	assert.NoError(t, org.AddMember(5))
93
+	AssertExistsAndLoadBean(t, &OrgUser{UID: 5, OrgID: 3})
94
+	org = AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
95
+	assert.Equal(t, prevNumMembers+1, org.NumMembers)
96
+
97
+	// add a user that is already a member
98
+	AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3})
99
+	prevNumMembers = org.NumMembers
100
+	assert.NoError(t, org.AddMember(4))
101
+	AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3})
102
+	org = AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
103
+	assert.Equal(t, prevNumMembers, org.NumMembers)
104
+}
105
+
106
+func TestUser_RemoveMember(t *testing.T) {
107
+	assert.NoError(t, PrepareTestDatabase())
108
+	org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
109
+
110
+	// remove a user that is a member
111
+	AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3})
112
+	prevNumMembers := org.NumMembers
113
+	assert.NoError(t, org.RemoveMember(4))
114
+	AssertNotExistsBean(t, &OrgUser{UID: 4, OrgID: 3})
115
+	org = AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
116
+	assert.Equal(t, prevNumMembers-1, org.NumMembers)
117
+
118
+	// remove a user that is not a member
119
+	AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3})
120
+	prevNumMembers = org.NumMembers
121
+	assert.NoError(t, org.RemoveMember(5))
122
+	AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3})
123
+	org = AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
124
+	assert.Equal(t, prevNumMembers, org.NumMembers)
125
+}
126
+
127
+func TestUser_RemoveOrgRepo(t *testing.T) {
128
+	assert.NoError(t, PrepareTestDatabase())
129
+	org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
130
+	repo := AssertExistsAndLoadBean(t, &Repository{OwnerID: org.ID}).(*Repository)
131
+
132
+	// remove a repo that does belong to org
133
+	AssertExistsAndLoadBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID})
134
+	assert.NoError(t, org.RemoveOrgRepo(repo.ID))
135
+	AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID})
136
+	AssertExistsAndLoadBean(t, &Repository{ID: repo.ID}) // repo should still exist
137
+
138
+	// remove a repo that does not belong to org
139
+	assert.NoError(t, org.RemoveOrgRepo(repo.ID))
140
+	AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID})
141
+
142
+	assert.NoError(t, org.RemoveOrgRepo(NonexistentID))
143
+}
144
+
145
+func TestCreateOrganization(t *testing.T) {
146
+	// successful creation of org
147
+	assert.NoError(t, PrepareTestDatabase())
148
+
149
+	owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
150
+	const newOrgName = "neworg"
151
+	org := &User{
152
+		Name: newOrgName,
153
+	}
154
+
155
+	AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization})
156
+	assert.NoError(t, CreateOrganization(org, owner))
157
+	org = AssertExistsAndLoadBean(t,
158
+		&User{Name: newOrgName, Type: UserTypeOrganization}).(*User)
159
+	ownerTeam := AssertExistsAndLoadBean(t,
160
+		&Team{Name: ownerTeamName, OrgID: org.ID}).(*Team)
161
+	AssertExistsAndLoadBean(t, &TeamUser{UID: owner.ID, TeamID: ownerTeam.ID})
162
+}
163
+
164
+func TestCreateOrganization2(t *testing.T) {
165
+	// unauthorized creation of org
166
+	assert.NoError(t, PrepareTestDatabase())
167
+
168
+	owner := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
169
+	const newOrgName = "neworg"
170
+	org := &User{
171
+		Name: newOrgName,
172
+	}
173
+
174
+	AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization})
175
+	err := CreateOrganization(org, owner)
176
+	assert.Error(t, err)
177
+	assert.True(t, IsErrUserNotAllowedCreateOrg(err))
178
+	AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization})
179
+}
180
+
181
+func TestCreateOrganization3(t *testing.T) {
182
+	// create org with same name as existent org
183
+	assert.NoError(t, PrepareTestDatabase())
184
+
185
+	owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
186
+	org := &User{Name: "user3"}                       // should already exist
187
+	AssertExistsAndLoadBean(t, &User{Name: org.Name}) // sanity check
188
+	err := CreateOrganization(org, owner)
189
+	assert.Error(t, err)
190
+	assert.True(t, IsErrUserAlreadyExist(err))
191
+}
192
+
193
+func TestCreateOrganization4(t *testing.T) {
194
+	// create org with unusable name
195
+	assert.NoError(t, PrepareTestDatabase())
196
+
197
+	owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
198
+	err := CreateOrganization(&User{Name: "assets"}, owner)
199
+	assert.Error(t, err)
200
+	assert.True(t, IsErrNameReserved(err))
201
+}
202
+
203
+func TestGetOrgByName(t *testing.T) {
204
+	assert.NoError(t, PrepareTestDatabase())
205
+
206
+	org, err := GetOrgByName("user3")
207
+	assert.NoError(t, err)
208
+	assert.EqualValues(t, 3, org.ID)
209
+	assert.Equal(t, "user3", org.Name)
210
+
211
+	org, err = GetOrgByName("user2") // user2 is an individual
212
+	assert.Equal(t, ErrOrgNotExist, err)
213
+
214
+	org, err = GetOrgByName("") // corner case
215
+	assert.Equal(t, ErrOrgNotExist, err)
216
+}
217
+
218
+func TestCountOrganizations(t *testing.T) {
219
+	assert.NoError(t, PrepareTestDatabase())
220
+	expected, err := x.Where("type=?", UserTypeOrganization).Count(&User{})
221
+	assert.NoError(t, err)
222
+	assert.Equal(t, expected, CountOrganizations())
223
+}
224
+
225
+func TestOrganizations(t *testing.T) {
226
+	assert.NoError(t, PrepareTestDatabase())
227
+	testSuccess := func(opts *SearchUserOptions, expectedOrgIDs []int64) {
228
+		orgs, err := Organizations(opts)
229
+		assert.NoError(t, err)
230
+		assert.Len(t, orgs, len(expectedOrgIDs))
231
+		for i, expectedOrgID := range expectedOrgIDs {
232
+			assert.EqualValues(t, expectedOrgID, orgs[i].ID)
233
+		}
234
+	}
235
+	testSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 1, PageSize: 2},
236
+		[]int64{3, 6})
237
+
238
+	testSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 2, PageSize: 2},
239
+		[]int64{7})
240
+
241
+	testSuccess(&SearchUserOptions{Page: 3, PageSize: 2},
242
+		[]int64{})
243
+}
244
+
245
+func TestDeleteOrganization(t *testing.T) {
246
+	assert.NoError(t, PrepareTestDatabase())
247
+	org := AssertExistsAndLoadBean(t, &User{ID: 6}).(*User)
248
+	assert.NoError(t, DeleteOrganization(org))
249
+	AssertNotExistsBean(t, &User{ID: 6})
250
+	AssertNotExistsBean(t, &OrgUser{OrgID: 6})
251
+	AssertNotExistsBean(t, &Team{OrgID: 6})
252
+
253
+	org = AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
254
+	err := DeleteOrganization(org)
255
+	assert.Error(t, err)
256
+	assert.True(t, IsErrUserOwnRepos(err))
257
+
258
+	nonOrg := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
259
+	assert.Error(t, DeleteOrganization(nonOrg))
260
+}
261
+
262
+func TestIsOrganizationOwner(t *testing.T) {
263
+	assert.NoError(t, PrepareTestDatabase())
264
+	assert.True(t, IsOrganizationOwner(3, 2))
265
+	assert.False(t, IsOrganizationOwner(3, 3))
266
+	assert.True(t, IsOrganizationOwner(6, 5))
267
+	assert.False(t, IsOrganizationOwner(6, 4))
268
+	assert.False(t, IsOrganizationOwner(NonexistentID, NonexistentID))
269
+}
270
+
271
+func TestIsOrganizationMember(t *testing.T) {
272
+	assert.NoError(t, PrepareTestDatabase())
273
+	assert.True(t, IsOrganizationMember(3, 2))
274
+	assert.False(t, IsOrganizationMember(3, 3))
275
+	assert.True(t, IsOrganizationMember(3, 4))
276
+	assert.True(t, IsOrganizationMember(6, 5))
277
+	assert.False(t, IsOrganizationMember(6, 4))
278
+	assert.False(t, IsOrganizationMember(NonexistentID, NonexistentID))
279
+}
280
+
281
+func TestIsPublicMembership(t *testing.T) {
282
+	assert.NoError(t, PrepareTestDatabase())
283
+	assert.True(t, IsPublicMembership(3, 2))
284
+	assert.False(t, IsPublicMembership(3, 3))
285
+	assert.False(t, IsPublicMembership(3, 4))
286
+	assert.True(t, IsPublicMembership(6, 5))
287
+	assert.False(t, IsPublicMembership(6, 4))
288
+	assert.False(t, IsPublicMembership(NonexistentID, NonexistentID))
289
+}
290
+
291
+func TestGetOrgsByUserID(t *testing.T) {
292
+	assert.NoError(t, PrepareTestDatabase())
293
+
294
+	orgs, err := GetOrgsByUserID(4, true)
295
+	assert.NoError(t, err)
296
+	assert.Len(t, orgs, 1)
297
+	assert.EqualValues(t, 3, orgs[0].ID)
298
+
299
+	orgs, err = GetOrgsByUserID(4, false)
300
+	assert.NoError(t, err)
301
+	assert.Len(t, orgs, 0)
302
+}
303
+
304
+func TestGetOwnedOrgsByUserID(t *testing.T) {
305
+	assert.NoError(t, PrepareTestDatabase())
306
+
307
+	orgs, err := GetOwnedOrgsByUserID(2)
308
+	assert.NoError(t, err)
309
+	assert.Len(t, orgs, 1)
310
+	assert.EqualValues(t, 3, orgs[0].ID)
311
+
312
+	orgs, err = GetOwnedOrgsByUserID(4)
313
+	assert.NoError(t, err)
314
+	assert.Len(t, orgs, 0)
315
+}
316
+
317
+func TestGetOwnedOrgsByUserIDDesc(t *testing.T) {
318
+	assert.NoError(t, PrepareTestDatabase())
319
+
320
+	orgs, err := GetOwnedOrgsByUserIDDesc(5, "id")
321
+	assert.NoError(t, err)
322
+	assert.Len(t, orgs, 2)
323
+	assert.EqualValues(t, 7, orgs[0].ID)
324
+	assert.EqualValues(t, 6, orgs[1].ID)
325
+
326
+	orgs, err = GetOwnedOrgsByUserIDDesc(4, "id")
327
+	assert.NoError(t, err)
328
+	assert.Len(t, orgs, 0)
329
+}
330
+
331
+func TestGetOrgUsersByUserID(t *testing.T) {
332
+	assert.NoError(t, PrepareTestDatabase())
333
+
334
+	orgUsers, err := GetOrgUsersByUserID(5, true)
335
+	assert.NoError(t, err)
336
+	assert.Len(t, orgUsers, 2)
337
+	assert.Equal(t, OrgUser{
338
+		ID:       orgUsers[0].ID,
339
+		OrgID:    6,
340
+		UID:      5,
341
+		IsOwner:  true,
342
+		IsPublic: true,
343
+		NumTeams: 1}, *orgUsers[0])
344
+	assert.Equal(t, OrgUser{
345
+		ID:       orgUsers[1].ID,
346
+		OrgID:    7,
347
+		UID:      5,
348
+		IsOwner:  true,
349
+		IsPublic: false,
350
+		NumTeams: 1}, *orgUsers[1])
351
+
352
+	publicOrgUsers, err := GetOrgUsersByUserID(5, false)
353
+	assert.NoError(t, err)
354
+	assert.Len(t, publicOrgUsers, 1)
355
+	assert.Equal(t, *orgUsers[0], *publicOrgUsers[0])
356
+
357
+	orgUsers, err = GetOrgUsersByUserID(1, true)
358
+	assert.NoError(t, err)
359
+	assert.Len(t, orgUsers, 0)
360
+}
361
+
362
+func TestGetOrgUsersByOrgID(t *testing.T) {
363
+	assert.NoError(t, PrepareTestDatabase())
364
+
365
+	orgUsers, err := GetOrgUsersByOrgID(3)
366
+	assert.NoError(t, err)
367
+	assert.Len(t, orgUsers, 2)
368
+	assert.Equal(t, OrgUser{
369
+		ID:       orgUsers[0].ID,
370
+		OrgID:    3,
371
+		UID:      2,
372
+		IsOwner:  true,
373
+		IsPublic: true,
374
+		NumTeams: 1}, *orgUsers[0])
375
+	assert.Equal(t, OrgUser{
376
+		ID:       orgUsers[1].ID,
377
+		OrgID:    3,
378
+		UID:      4,
379
+		IsOwner:  false,
380
+		IsPublic: false,
381
+		NumTeams: 0}, *orgUsers[1])
382
+
383
+	orgUsers, err = GetOrgUsersByOrgID(NonexistentID)
384
+	assert.NoError(t, err)
385
+	assert.Len(t, orgUsers, 0)
386
+}
387
+
388
+func TestChangeOrgUserStatus(t *testing.T) {
389
+	assert.NoError(t, PrepareTestDatabase())
390
+
391
+	testSuccess := func(orgID, userID int64, public bool) {
392
+		assert.NoError(t, ChangeOrgUserStatus(orgID, userID, public))
393
+		orgUser := AssertExistsAndLoadBean(t, &OrgUser{OrgID: orgID, UID: userID}).(*OrgUser)
394
+		assert.Equal(t, public, orgUser.IsPublic)
395
+	}
396
+
397
+	testSuccess(3, 2, false)
398
+	testSuccess(3, 2, false)
399
+	testSuccess(3, 4, true)
400
+	assert.NoError(t, ChangeOrgUserStatus(NonexistentID, NonexistentID, true))
401
+}
402
+
403
+func TestAddOrgUser(t *testing.T) {
404
+	assert.NoError(t, PrepareTestDatabase())
405
+	testSuccess := func(orgID, userID int64) {
406
+		org := AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
407
+		expectedNumMembers := org.NumMembers
408
+		if !BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) {
409
+			expectedNumMembers++
410
+		}
411
+		assert.NoError(t, AddOrgUser(orgID, userID))
412
+		AssertExistsAndLoadBean(t, &OrgUser{OrgID: orgID, UID: userID})
413
+		org = AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
414
+		assert.EqualValues(t, expectedNumMembers, org.NumMembers)
415
+	}
416
+	testSuccess(3, 5)
417
+	testSuccess(3, 5)
418
+	testSuccess(6, 2)
419
+}
420
+
421
+func TestRemoveOrgUser(t *testing.T) {
422
+	assert.NoError(t, PrepareTestDatabase())
423
+	testSuccess := func(orgID, userID int64) {
424
+		org := AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
425
+		expectedNumMembers := org.NumMembers
426
+		if BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) {
427
+			expectedNumMembers--
428
+		}
429
+		assert.NoError(t, RemoveOrgUser(orgID, userID))
430
+		AssertNotExistsBean(t, &OrgUser{OrgID: orgID, UID: userID})
431
+		org = AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
432
+		assert.EqualValues(t, expectedNumMembers, org.NumMembers)
433
+	}
434
+	testSuccess(3, 4)
435
+	testSuccess(3, 4)
436
+
437
+	err := RemoveOrgUser(7, 5)
438
+	assert.Error(t, err)
439
+	assert.True(t, IsErrLastOrgOwner(err))
440
+	AssertExistsAndLoadBean(t, &OrgUser{OrgID: 7, UID: 5})
441
+}
442
+
443
+func TestUser_GetUserTeamIDs(t *testing.T) {
444
+	assert.NoError(t, PrepareTestDatabase())
445
+	org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
446
+	testSuccess := func(userID int64, expected []int64) {
447
+		teamIDs, err := org.GetUserTeamIDs(userID)
448
+		assert.NoError(t, err)
449
+		assert.Equal(t, expected, teamIDs)
450
+	}
451
+	testSuccess(2, []int64{1, 2})
452
+	testSuccess(4, []int64{2})
453
+	testSuccess(NonexistentID, []int64{})
454
+}
455
+
456
+func TestAccessibleReposEnv_CountRepos(t *testing.T) {
457
+	assert.NoError(t, PrepareTestDatabase())
458
+	org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
459
+	testSuccess := func(userID, expectedCount int64) {
460
+		env, err := org.AccessibleReposEnv(userID)
461
+		assert.NoError(t, err)
462
+		count, err := env.CountRepos()
463
+		assert.NoError(t, err)
464
+		assert.EqualValues(t, expectedCount, count)
465
+	}
466
+	testSuccess(2, 2)
467
+	testSuccess(4, 1)
468
+}
469
+
470
+func TestAccessibleReposEnv_RepoIDs(t *testing.T) {
471
+	assert.NoError(t, PrepareTestDatabase())
472
+	org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
473
+	testSuccess := func(userID, page, pageSize int64, expectedRepoIDs []int64) {
474
+		env, err := org.AccessibleReposEnv(userID)
475
+		assert.NoError(t, err)
476
+		repoIDs, err := env.RepoIDs(1, 100)
477
+		assert.NoError(t, err)
478
+		assert.Equal(t, expectedRepoIDs, repoIDs)
479
+	}
480
+	testSuccess(2, 1, 100, []int64{3, 5})
481
+	testSuccess(4, 0, 100, []int64{3})
482
+}
483
+
484
+func TestAccessibleReposEnv_Repos(t *testing.T) {
485
+	assert.NoError(t, PrepareTestDatabase())
486
+	org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
487
+	testSuccess := func(userID int64, expectedRepoIDs []int64) {
488
+		env, err := org.AccessibleReposEnv(userID)
489
+		assert.NoError(t, err)
490
+		repos, err := env.Repos(1, 100)
491
+		assert.NoError(t, err)
492
+		expectedRepos := make([]*Repository, len(expectedRepoIDs))
493
+		for i, repoID := range expectedRepoIDs {
494
+			expectedRepos[i] = AssertExistsAndLoadBean(t,
495
+				&Repository{ID: repoID}).(*Repository)
496
+		}
497
+		assert.Equal(t, expectedRepos, repos)
498
+	}
499
+	testSuccess(2, []int64{3, 5})
500
+	testSuccess(4, []int64{3})
501
+}
502
+
503
+func TestAccessibleReposEnv_MirrorRepos(t *testing.T) {
504
+	assert.NoError(t, PrepareTestDatabase())
505
+	org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
506
+	testSuccess := func(userID int64, expectedRepoIDs []int64) {
507
+		env, err := org.AccessibleReposEnv(userID)
508
+		assert.NoError(t, err)
509
+		repos, err := env.MirrorRepos()
510
+		assert.NoError(t, err)
511
+		expectedRepos := make([]*Repository, len(expectedRepoIDs))
512
+		for i, repoID := range expectedRepoIDs {
513
+			expectedRepos[i] = AssertExistsAndLoadBean(t,
514
+				&Repository{ID: repoID}).(*Repository)
515
+		}
516
+		assert.Equal(t, expectedRepos, repos)
517
+	}
518
+	testSuccess(2, []int64{5})
519
+	testSuccess(4, []int64{})
520
+}

+ 7 - 0
models/setup_for_test.go

@@ -59,6 +59,13 @@ func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error)
59 59
 	return sess.Get(bean)
60 60
 }
61 61
 
62
+// BeanExists for testing, check if a bean exists
63
+func BeanExists(t *testing.T, bean interface{}, conditions ...interface{}) bool {
64
+	exists, err := loadBeanIfExists(bean, conditions...)
65
+	assert.NoError(t, err)
66
+	return exists
67
+}
68
+
62 69
 // AssertExistsAndLoadBean assert that a bean exists and load it from the test
63 70
 // database
64 71
 func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) interface{} {