From 61d7ece090aa35ec7197004ca5ca55a9c37551dd Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 19 Nov 2021 13:32:58 +0800 Subject: [PATCH] Fix test --- models/org.go | 11 ++++++++++- models/repo.go | 4 ++-- models/repo_test.go | 2 +- services/org/org.go | 8 ++------ services/org/org_test.go | 8 ++++---- services/user/user.go | 2 +- 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/models/org.go b/models/org.go index 0dc230994a6ef..c79af5c7bba23 100644 --- a/models/org.go +++ b/models/org.go @@ -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() { @@ -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, diff --git a/models/repo.go b/models/repo.go index cc211afec4dd8..38775029262b5 100644 --- a/models/repo.go +++ b/models/repo.go @@ -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. diff --git a/models/repo_test.go b/models/repo_test.go index e6f4ea1c3fbc2..ec1bcc04879ab 100644 --- a/models/repo_test.go +++ b/models/repo_test.go @@ -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) diff --git a/services/org/org.go b/services/org/org.go index cb1f1eca0385f..c2b21d10ac916 100644 --- a/services/org/org.go +++ b/services/org/org.go @@ -14,11 +14,7 @@ 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 @@ -26,7 +22,7 @@ func DeleteOrganization(org *models.User) error { 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 { diff --git a/services/org/org_test.go b/services/org/org_test.go index 5fec086d10d1a..3c620c055be5e 100644 --- a/services/org/org_test.go +++ b/services/org/org_test.go @@ -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{}) } diff --git a/services/user/user.go b/services/user/user.go index a08e40f86e95e..733cc4a36e88e 100644 --- a/services/user/user.go +++ b/services/user/user.go @@ -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 {