Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Nov 19, 2021
1 parent 48c40c6 commit 61d7ece
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 15 deletions.
11 changes: 10 additions & 1 deletion models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ func (org *Organization) DisplayName() string {
return org.AsUser().DisplayName()
}

// CustomAvatarRelativePath returns user custom avatar relative path.
func (org *Organization) CustomAvatarRelativePath() string {
return org.Avatar
}

// CreateOrganization creates record of a new organization.
func CreateOrganization(org *Organization, owner *User) (err error) {
if !owner.CanCreateOrganization() {
Expand Down Expand Up @@ -314,7 +319,11 @@ func CountOrganizations() int64 {
}

// DeleteOrganization deletes models associated to an organization.
func DeleteOrganization(ctx context.Context, org *User) error {
func DeleteOrganization(ctx context.Context, org *Organization) error {
if org.Type != UserTypeOrganization {
return fmt.Errorf("%s is a user not an organization", org.Name)
}

e := db.GetEngine(ctx)

if err := deleteBeans(e,
Expand Down
4 changes: 2 additions & 2 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1803,8 +1803,8 @@ func getPrivateRepositoryCount(e db.Engine, u *User) (int64, error) {
}

// GetRepositoryCount returns the total number of repositories of user.
func GetRepositoryCount(ctx context.Context, u *User) (int64, error) {
return getRepositoryCount(db.GetEngine(ctx), u.ID)
func GetRepositoryCount(ctx context.Context, ownerID int64) (int64, error) {
return getRepositoryCount(db.GetEngine(ctx), ownerID)
}

// GetPublicRepositoryCount returns the total number of public repositories of user.
Expand Down
2 changes: 1 addition & 1 deletion models/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestMetas(t *testing.T) {
func TestGetRepositoryCount(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

count, err1 := GetRepositoryCount(db.DefaultContext, &User{ID: int64(10)})
count, err1 := GetRepositoryCount(db.DefaultContext, 10)
privateCount, err2 := GetPrivateRepositoryCount(&User{ID: int64(10)})
publicCount, err3 := GetPublicRepositoryCount(&User{ID: int64(10)})
assert.NoError(t, err1)
Expand Down
8 changes: 2 additions & 6 deletions services/org/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@ import (
)

// DeleteOrganization completely and permanently deletes everything of organization.
func DeleteOrganization(org *models.User) error {
if !org.IsOrganization() {
return fmt.Errorf("%s is a user not an organization", org.Name)
}

func DeleteOrganization(org *models.Organization) error {
ctx, commiter, err := db.TxContext()
if err != nil {
return err
}
defer commiter.Close()

// Check ownership of repository.
count, err := models.GetRepositoryCount(ctx, org)
count, err := models.GetRepositoryCount(ctx, org.ID)
if err != nil {
return fmt.Errorf("GetRepositoryCount: %v", err)
} else if count > 0 {
Expand Down
8 changes: 4 additions & 4 deletions services/org/org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ func TestMain(m *testing.M) {

func TestDeleteOrganization(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
org := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 6}).(*models.User)
org := unittest.AssertExistsAndLoadBean(t, &models.Organization{ID: 6}).(*models.Organization)
assert.NoError(t, DeleteOrganization(org))
unittest.AssertNotExistsBean(t, &models.User{ID: 6})
unittest.AssertNotExistsBean(t, &models.Organization{ID: 6})
unittest.AssertNotExistsBean(t, &models.OrgUser{OrgID: 6})
unittest.AssertNotExistsBean(t, &models.Team{OrgID: 6})

org = unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User)
org = unittest.AssertExistsAndLoadBean(t, &models.Organization{ID: 3}).(*models.Organization)
err := DeleteOrganization(org)
assert.Error(t, err)
assert.True(t, models.IsErrUserOwnRepos(err))

user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User)
user := unittest.AssertExistsAndLoadBean(t, &models.Organization{ID: 5}).(*models.Organization)
assert.Error(t, DeleteOrganization(user))
unittest.CheckConsistencyFor(t, &models.User{}, &models.Team{})
}
2 changes: 1 addition & 1 deletion services/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func DeleteUser(u *models.User) error {
// cannot perform delete operation.

// Check ownership of repository.
count, err := models.GetRepositoryCount(ctx, u)
count, err := models.GetRepositoryCount(ctx, u.ID)
if err != nil {
return fmt.Errorf("GetRepositoryCount: %v", err)
} else if count > 0 {
Expand Down

0 comments on commit 61d7ece

Please sign in to comment.