loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request 'test(avatar): deleting a user avatar is idempotent' (#4024) from earl-warren/forgejo:wip-delete-avatar into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4024
Reviewed-by: Victoria <efertone@noreply.codeberg.org>

+61 -28
+8 -8
modules/storage/helper.go
··· 10 10 "os" 11 11 ) 12 12 13 - var UninitializedStorage = discardStorage("uninitialized storage") 13 + var UninitializedStorage = DiscardStorage("uninitialized storage") 14 14 15 - type discardStorage string 15 + type DiscardStorage string 16 16 17 - func (s discardStorage) Open(_ string) (Object, error) { 17 + func (s DiscardStorage) Open(_ string) (Object, error) { 18 18 return nil, fmt.Errorf("%s", s) 19 19 } 20 20 21 - func (s discardStorage) Save(_ string, _ io.Reader, _ int64) (int64, error) { 21 + func (s DiscardStorage) Save(_ string, _ io.Reader, _ int64) (int64, error) { 22 22 return 0, fmt.Errorf("%s", s) 23 23 } 24 24 25 - func (s discardStorage) Stat(_ string) (os.FileInfo, error) { 25 + func (s DiscardStorage) Stat(_ string) (os.FileInfo, error) { 26 26 return nil, fmt.Errorf("%s", s) 27 27 } 28 28 29 - func (s discardStorage) Delete(_ string) error { 29 + func (s DiscardStorage) Delete(_ string) error { 30 30 return fmt.Errorf("%s", s) 31 31 } 32 32 33 - func (s discardStorage) URL(_, _ string) (*url.URL, error) { 33 + func (s DiscardStorage) URL(_, _ string) (*url.URL, error) { 34 34 return nil, fmt.Errorf("%s", s) 35 35 } 36 36 37 - func (s discardStorage) IterateObjects(_ string, _ func(string, Object) error) error { 37 + func (s DiscardStorage) IterateObjects(_ string, _ func(string, Object) error) error { 38 38 return fmt.Errorf("%s", s) 39 39 }
+2 -2
modules/storage/helper_test.go
··· 11 11 ) 12 12 13 13 func Test_discardStorage(t *testing.T) { 14 - tests := []discardStorage{ 14 + tests := []DiscardStorage{ 15 15 UninitializedStorage, 16 - discardStorage("empty"), 16 + DiscardStorage("empty"), 17 17 } 18 18 for _, tt := range tests { 19 19 t.Run(string(tt), func(t *testing.T) {
+5 -5
modules/storage/storage.go
··· 170 170 171 171 func initAttachments() (err error) { 172 172 if !setting.Attachment.Enabled { 173 - Attachments = discardStorage("Attachment isn't enabled") 173 + Attachments = DiscardStorage("Attachment isn't enabled") 174 174 return nil 175 175 } 176 176 log.Info("Initialising Attachment storage with type: %s", setting.Attachment.Storage.Type) ··· 180 180 181 181 func initLFS() (err error) { 182 182 if !setting.LFS.StartServer { 183 - LFS = discardStorage("LFS isn't enabled") 183 + LFS = DiscardStorage("LFS isn't enabled") 184 184 return nil 185 185 } 186 186 log.Info("Initialising LFS storage with type: %s", setting.LFS.Storage.Type) ··· 202 202 203 203 func initPackages() (err error) { 204 204 if !setting.Packages.Enabled { 205 - Packages = discardStorage("Packages isn't enabled") 205 + Packages = DiscardStorage("Packages isn't enabled") 206 206 return nil 207 207 } 208 208 log.Info("Initialising Packages storage with type: %s", setting.Packages.Storage.Type) ··· 212 212 213 213 func initActions() (err error) { 214 214 if !setting.Actions.Enabled { 215 - Actions = discardStorage("Actions isn't enabled") 216 - ActionsArtifacts = discardStorage("ActionsArtifacts isn't enabled") 215 + Actions = DiscardStorage("Actions isn't enabled") 216 + ActionsArtifacts = DiscardStorage("ActionsArtifacts isn't enabled") 217 217 return nil 218 218 } 219 219 log.Info("Initialising Actions storage with type: %s", setting.Actions.LogStorage.Type)
+46 -13
services/user/avatar_test.go
··· 7 7 "bytes" 8 8 "image" 9 9 "image/png" 10 + "os" 10 11 "testing" 11 12 12 13 "code.gitea.io/gitea/models/db" ··· 18 19 "github.com/stretchr/testify/assert" 19 20 ) 20 21 22 + type alreadyDeletedStorage struct { 23 + storage.DiscardStorage 24 + } 25 + 26 + func (s alreadyDeletedStorage) Delete(_ string) error { 27 + return os.ErrNotExist 28 + } 29 + 21 30 func TestUserDeleteAvatar(t *testing.T) { 22 31 myImage := image.NewRGBA(image.Rect(0, 0, 1, 1)) 23 32 var buff bytes.Buffer 24 33 png.Encode(&buff, myImage) 25 34 26 - assert.NoError(t, unittest.PrepareTestDatabase()) 27 - user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 35 + t.Run("AtomicStorageFailure", func(t *testing.T) { 36 + defer test.MockProtect[storage.ObjectStorage](&storage.Avatars)() 28 37 29 - err := UploadAvatar(db.DefaultContext, user, buff.Bytes()) 30 - assert.NoError(t, err) 31 - verification := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 32 - assert.NotEqual(t, "", verification.Avatar) 38 + assert.NoError(t, unittest.PrepareTestDatabase()) 39 + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 40 + 41 + err := UploadAvatar(db.DefaultContext, user, buff.Bytes()) 42 + assert.NoError(t, err) 43 + verification := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 44 + assert.NotEqual(t, "", verification.Avatar) 33 45 34 - t.Run("AtomicStorageFailure", func(t *testing.T) { 35 - defer test.MockVariableValue[storage.ObjectStorage](&storage.Avatars, storage.UninitializedStorage)() 46 + // fail to delete ... 47 + storage.Avatars = storage.UninitializedStorage 36 48 err = DeleteAvatar(db.DefaultContext, user) 37 49 assert.Error(t, err) 38 - verification := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 50 + 51 + // ... the avatar is not removed from the database 52 + verification = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 39 53 assert.True(t, verification.UseCustomAvatar) 54 + 55 + // already deleted ... 56 + storage.Avatars = alreadyDeletedStorage{} 57 + err = DeleteAvatar(db.DefaultContext, user) 58 + assert.NoError(t, err) 59 + 60 + // ... the avatar is removed from the database 61 + verification = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 62 + assert.Equal(t, "", verification.Avatar) 40 63 }) 41 64 42 - err = DeleteAvatar(db.DefaultContext, user) 43 - assert.NoError(t, err) 65 + t.Run("Success", func(t *testing.T) { 66 + assert.NoError(t, unittest.PrepareTestDatabase()) 67 + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 68 + 69 + err := UploadAvatar(db.DefaultContext, user, buff.Bytes()) 70 + assert.NoError(t, err) 71 + verification := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 72 + assert.NotEqual(t, "", verification.Avatar) 44 73 45 - verification = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 46 - assert.Equal(t, "", verification.Avatar) 74 + err = DeleteAvatar(db.DefaultContext, user) 75 + assert.NoError(t, err) 76 + 77 + verification = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 78 + assert.Equal(t, "", verification.Avatar) 79 + }) 47 80 }