github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/storetest/upload_session_store.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package storetest 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/require" 11 12 "github.com/mattermost/mattermost-server/v5/model" 13 "github.com/mattermost/mattermost-server/v5/store" 14 ) 15 16 func TestUploadSessionStore(t *testing.T, ss store.Store) { 17 t.Run("UploadSessionStoreSaveGet", func(t *testing.T) { testUploadSessionStoreSaveGet(t, ss) }) 18 t.Run("UploadSessionStoreUpdate", func(t *testing.T) { testUploadSessionStoreUpdate(t, ss) }) 19 t.Run("UploadSessionStoreGetForUser", func(t *testing.T) { testUploadSessionStoreGetForUser(t, ss) }) 20 t.Run("UploadSessionStoreDelete", func(t *testing.T) { testUploadSessionStoreDelete(t, ss) }) 21 } 22 23 func testUploadSessionStoreSaveGet(t *testing.T, ss store.Store) { 24 var session *model.UploadSession 25 26 t.Run("saving nil session should fail", func(t *testing.T) { 27 us, err := ss.UploadSession().Save(nil) 28 require.Error(t, err) 29 require.Nil(t, us) 30 }) 31 32 t.Run("saving empty session should fail", func(t *testing.T) { 33 session = &model.UploadSession{} 34 us, err := ss.UploadSession().Save(session) 35 require.Error(t, err) 36 require.Nil(t, us) 37 }) 38 39 t.Run("saving valid session should succeed", func(t *testing.T) { 40 session = &model.UploadSession{ 41 Type: model.UploadTypeAttachment, 42 UserId: model.NewId(), 43 ChannelId: model.NewId(), 44 Filename: "test", 45 FileSize: 1024, 46 Path: "/tmp/test", 47 } 48 us, err := ss.UploadSession().Save(session) 49 require.NoError(t, err) 50 require.NotNil(t, us) 51 require.NotEmpty(t, us) 52 }) 53 54 t.Run("getting non-existing session should fail", func(t *testing.T) { 55 us, err := ss.UploadSession().Get("fake") 56 require.Error(t, err) 57 require.Nil(t, us) 58 }) 59 60 t.Run("getting existing session should succeed", func(t *testing.T) { 61 us, err := ss.UploadSession().Get(session.Id) 62 require.NoError(t, err) 63 require.NotNil(t, us) 64 require.Equal(t, session, us) 65 }) 66 } 67 68 func testUploadSessionStoreUpdate(t *testing.T, ss store.Store) { 69 session := &model.UploadSession{ 70 Type: model.UploadTypeAttachment, 71 UserId: model.NewId(), 72 ChannelId: model.NewId(), 73 Filename: "test", 74 FileSize: 1024, 75 Path: "/tmp/test", 76 } 77 78 t.Run("updating nil session should fail", func(t *testing.T) { 79 err := ss.UploadSession().Update(nil) 80 require.Error(t, err) 81 }) 82 83 t.Run("updating invalid session should fail", func(t *testing.T) { 84 err := ss.UploadSession().Update(&model.UploadSession{}) 85 require.Error(t, err) 86 }) 87 88 t.Run("updating non-existing session should fail", func(t *testing.T) { 89 err := ss.UploadSession().Update(&model.UploadSession{}) 90 require.Error(t, err) 91 }) 92 93 t.Run("updating existing session should succeed", func(t *testing.T) { 94 us, err := ss.UploadSession().Save(session) 95 require.NoError(t, err) 96 require.NotNil(t, us) 97 require.NotEmpty(t, us) 98 99 us.FileOffset = 512 100 err = ss.UploadSession().Update(us) 101 require.NoError(t, err) 102 103 updated, err := ss.UploadSession().Get(us.Id) 104 require.NoError(t, err) 105 require.NotNil(t, us) 106 require.Equal(t, us, updated) 107 }) 108 } 109 110 func testUploadSessionStoreGetForUser(t *testing.T, ss store.Store) { 111 userId := model.NewId() 112 113 sessions := []*model.UploadSession{ 114 { 115 Type: model.UploadTypeAttachment, 116 UserId: userId, 117 ChannelId: model.NewId(), 118 Filename: "test0", 119 FileSize: 1024, 120 Path: "/tmp/test0", 121 }, 122 { 123 Type: model.UploadTypeAttachment, 124 UserId: model.NewId(), 125 ChannelId: model.NewId(), 126 Filename: "test1", 127 FileSize: 1024, 128 Path: "/tmp/test1", 129 }, 130 { 131 Type: model.UploadTypeAttachment, 132 UserId: userId, 133 ChannelId: model.NewId(), 134 Filename: "test2", 135 FileSize: 1024, 136 Path: "/tmp/test2", 137 }, 138 { 139 Type: model.UploadTypeAttachment, 140 UserId: userId, 141 ChannelId: model.NewId(), 142 Filename: "test3", 143 FileSize: 1024, 144 Path: "/tmp/test3", 145 }, 146 } 147 148 t.Run("should return no sessions", func(t *testing.T) { 149 us, err := ss.UploadSession().GetForUser(userId) 150 require.NoError(t, err) 151 require.NotNil(t, us) 152 require.Empty(t, us) 153 }) 154 155 for i := 0; i < len(sessions); i++ { 156 us, err := ss.UploadSession().Save(sessions[i]) 157 require.NoError(t, err) 158 require.NotNil(t, us) 159 require.NotEmpty(t, us) 160 // We need this to make sure the ordering is consistent. 161 time.Sleep(1 * time.Millisecond) 162 } 163 164 t.Run("should return existing sessions", func(t *testing.T) { 165 us, err := ss.UploadSession().GetForUser(userId) 166 require.NoError(t, err) 167 require.NotNil(t, us) 168 require.NotEmpty(t, us) 169 require.Len(t, us, 3) 170 171 require.Equal(t, sessions[0], us[0]) 172 require.Equal(t, sessions[2], us[1]) 173 require.Equal(t, sessions[3], us[2]) 174 }) 175 } 176 177 func testUploadSessionStoreDelete(t *testing.T, ss store.Store) { 178 session := &model.UploadSession{ 179 Id: model.NewId(), 180 Type: model.UploadTypeAttachment, 181 UserId: model.NewId(), 182 ChannelId: model.NewId(), 183 Filename: "test", 184 FileSize: 1024, 185 Path: "/tmp/test", 186 } 187 188 t.Run("deleting invalid id should fail", func(t *testing.T) { 189 err := ss.UploadSession().Delete("invalidId") 190 require.Error(t, err) 191 }) 192 193 t.Run("deleting existing session should succeed", func(t *testing.T) { 194 us, err := ss.UploadSession().Save(session) 195 require.NoError(t, err) 196 require.NotNil(t, us) 197 require.NotEmpty(t, us) 198 199 err = ss.UploadSession().Delete(session.Id) 200 require.NoError(t, err) 201 202 us, err = ss.UploadSession().Get(us.Id) 203 require.Error(t, err) 204 require.Nil(t, us) 205 require.IsType(t, &store.ErrNotFound{}, err) 206 }) 207 }