Merge pull request #776 from ethantkoenig/tests/org

Unit tests and bug fix for models/org
This commit is contained in:
Andrey Nering
2017-01-28 11:27:38 -02:00
committed by GitHub
10 changed files with 671 additions and 26 deletions

View File

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

View File

@ -5,3 +5,27 @@
is_public: true
is_owner: true
num_teams: 1
-
id: 2
uid: 4
org_id: 3
is_public: false
is_owner: false
num_teams: 0
-
id: 3
uid: 5
org_id: 6
is_public: true
is_owner: true
num_teams: 1
-
id: 4
uid: 5
org_id: 7
is_public: false
is_owner: true
num_teams: 1

View File

@ -41,3 +41,15 @@
num_closed_issues: 0
num_pulls: 0
num_closed_pulls: 0
-
id: 5
owner_id: 3
lower_name: repo5
name: repo5
is_private: true
num_issues: 0
num_closed_issues: 0
num_pulls: 0
num_closed_pulls: 0
is_mirror: true

View File

@ -1,7 +1,35 @@
-
id: 1
org_id: 3
lower_name: owners
name: owners
authorize: 4 # owner
num_repos: 1
num_members: 1
-
id: 2
org_id: 3
lower_name: team1
name: team1
authorize: 2 # write
num_repos: 1
num_members: 2
-
id: 3
org_id: 6
lower_name: owners
name: owners
authorize: 4 # owner
num_repos: 0
num_members: 1
-
id: 4
org_id: 7
lower_name: owners
name: owners
authorize: 4 # owner
num_repos: 0
num_members: 1

View File

@ -3,3 +3,15 @@
org_id: 3
team_id: 1
repo_id: 3
-
id: 2
org_id: 3
team_id: 2
repo_id: 3
-
id: 3
org_id: 3
team_id: 1
repo_id: 5

View File

@ -3,3 +3,27 @@
org_id: 3
team_id: 1
uid: 2
-
id: 2
org_id: 3
team_id: 2
uid: 2
-
id: 3
org_id: 3
team_id: 2
uid: 4
-
id: 4
org_id: 6
team_id: 3
uid: 5
-
id: 5
org_id: 7
team_id: 4
uid: 5

View File

@ -38,7 +38,8 @@
is_admin: false
avatar: avatar3
avatar_email: user3@example.com
num_repos: 1
num_repos: 2
num_members: 2
-
id: 4
@ -47,7 +48,7 @@
full_name: User Four
email: user4@example.com
passwd: password
type: 1 # individual
type: 0 # individual
salt: salt
is_admin: false
avatar: avatar4
@ -61,9 +62,40 @@
full_name: User Five
email: user5@example.com
passwd: password
type: 1 # individual
type: 0 # individual
salt: salt
is_admin: false
avatar: avatar5
avatar_email: user5@example.com
num_repos: 1
allow_create_organization: false
-
id: 6
lower_name: user6
name: user6
full_name: User Six
email: user6@example.com
passwd: password
type: 1 # organization
salt: salt
is_admin: false
avatar: avatar6
avatar_email: user6@example.com
num_repos: 0
num_members: 1
-
id: 7
lower_name: user7
name: user7
full_name: User Seven
email: user7@example.com
passwd: password
type: 1 # organization
salt: salt
is_admin: false
avatar: avatar7
avatar_email: user7@example.com
num_repos: 0
num_members: 1

View File

@ -124,6 +124,7 @@ func CreateOrganization(org, owner *User) (err error) {
org.MaxRepoCreation = -1
org.NumTeams = 1
org.NumMembers = 1
org.Type = UserTypeOrganization
sess := x.NewSession()
defer sessionRelease(sess)
@ -350,12 +351,6 @@ func GetOrgsByUserID(userID int64, showAll bool) ([]*User, error) {
return getOrgsByUserID(sess, userID, showAll)
}
// GetOrgsByUserIDDesc returns a list of organizations that the given user ID
// has joined, ordered descending by the given condition.
func GetOrgsByUserIDDesc(userID int64, desc string, showAll bool) ([]*User, error) {
return getOrgsByUserID(x.Desc(desc), userID, showAll)
}
func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) {
orgs := make([]*User, 0, 10)
return orgs, sess.
@ -464,10 +459,6 @@ func RemoveOrgUser(orgID, userID int64) error {
return nil
}
user, err := GetUserByID(userID)
if err != nil {
return fmt.Errorf("GetUserByID [%d]: %v", userID, err)
}
org, err := GetUserByID(orgID)
if err != nil {
return fmt.Errorf("GetUserByID [%d]: %v", orgID, err)
@ -497,23 +488,23 @@ func RemoveOrgUser(orgID, userID int64) error {
}
// Delete all repository accesses and unwatch them.
env, err := org.AccessibleReposEnv(user.ID)
env, err := org.AccessibleReposEnv(userID)
if err != nil {
return fmt.Errorf("AccessibleReposEnv: %v", err)
}
repoIDs, err := env.RepoIDs(1, org.NumRepos)
if err != nil {
return fmt.Errorf("GetUserRepositories [%d]: %v", user.ID, err)
return fmt.Errorf("GetUserRepositories [%d]: %v", userID, err)
}
for _, repoID := range repoIDs {
if err = watchRepo(sess, user.ID, repoID, false); err != nil {
if err = watchRepo(sess, userID, repoID, false); err != nil {
return err
}
}
if len(repoIDs) > 0 {
if _, err = sess.
Where("user_id = ?", user.ID).
Where("user_id = ?", userID).
In("repo_id", repoIDs).
Delete(new(Access)); err != nil {
return err
@ -521,12 +512,12 @@ func RemoveOrgUser(orgID, userID int64) error {
}
// Delete member in his/her teams.
teams, err := getUserTeams(sess, org.ID, user.ID)
teams, err := getUserTeams(sess, org.ID, userID)
if err != nil {
return err
}
for _, t := range teams {
if err = removeTeamMember(sess, org.ID, t.ID, user.ID); err != nil {
if err = removeTeamMember(sess, org.ID, t.ID, userID); err != nil {
return err
}
}
@ -542,11 +533,6 @@ func removeOrgRepo(e Engine, orgID, repoID int64) error {
return err
}
// RemoveOrgRepo removes all team-repository relations of given organization.
func RemoveOrgRepo(orgID, repoID int64) error {
return removeOrgRepo(x, orgID, repoID)
}
func (org *User) getUserTeams(e Engine, userID int64, cols ...string) ([]*Team, error) {
teams := make([]*Team, 0, org.NumTeams)
return teams, e.
@ -619,7 +605,7 @@ func (env *accessibleReposEnv) CountRepos() (int64, error) {
repoCount, err := x.
Join("INNER", "team_repo", "`team_repo`.repo_id=`repository`.id").
Where(env.cond()).
GroupBy("`repository`.id").
Distinct("`repository`.id").
Count(&Repository{})
if err != nil {
return 0, fmt.Errorf("count user repositories in organization: %v", err)

520
models/org_test.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -59,6 +59,13 @@ func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error)
return sess.Get(bean)
}
// BeanExists for testing, check if a bean exists
func BeanExists(t *testing.T, bean interface{}, conditions ...interface{}) bool {
exists, err := loadBeanIfExists(bean, conditions...)
assert.NoError(t, err)
return exists
}
// AssertExistsAndLoadBean assert that a bean exists and load it from the test
// database
func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) interface{} {