github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/storetest/link_metadata_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 "net/http" 8 "testing" 9 "time" 10 11 "github.com/dyatlov/go-opengraph/opengraph" 12 "github.com/mattermost/mattermost-server/model" 13 "github.com/mattermost/mattermost-server/store" 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/require" 16 ) 17 18 // These tests are ran on the same store instance, so this provides easier unique, valid timestamps 19 var linkMetadataTimestamp int64 = 1546300800000 20 21 func getNextLinkMetadataTimestamp() int64 { 22 linkMetadataTimestamp += int64(time.Hour) / 1000 23 return linkMetadataTimestamp 24 } 25 26 func TestLinkMetadataStore(t *testing.T, ss store.Store) { 27 t.Run("Save", func(t *testing.T) { testLinkMetadataStoreSave(t, ss) }) 28 t.Run("Get", func(t *testing.T) { testLinkMetadataStoreGet(t, ss) }) 29 t.Run("Types", func(t *testing.T) { testLinkMetadataStoreTypes(t, ss) }) 30 } 31 32 func testLinkMetadataStoreSave(t *testing.T, ss store.Store) { 33 t.Run("should save item", func(t *testing.T) { 34 metadata := &model.LinkMetadata{ 35 URL: "http://example.com", 36 Timestamp: getNextLinkMetadataTimestamp(), 37 Type: model.LINK_METADATA_TYPE_IMAGE, 38 Data: &model.PostImage{}, 39 } 40 41 result := <-ss.LinkMetadata().Save(metadata) 42 43 require.Nil(t, result.Err) 44 require.IsType(t, metadata, result.Data) 45 assert.Equal(t, *metadata, *result.Data.(*model.LinkMetadata)) 46 }) 47 48 t.Run("should fail to save invalid item", func(t *testing.T) { 49 metadata := &model.LinkMetadata{ 50 URL: "", 51 Timestamp: 0, 52 Type: "garbage", 53 Data: nil, 54 } 55 56 result := <-ss.LinkMetadata().Save(metadata) 57 58 assert.NotNil(t, result.Err) 59 }) 60 61 t.Run("should save with duplicate URL and different timestamp", func(t *testing.T) { 62 metadata := &model.LinkMetadata{ 63 URL: "http://example.com", 64 Timestamp: getNextLinkMetadataTimestamp(), 65 Type: model.LINK_METADATA_TYPE_IMAGE, 66 Data: &model.PostImage{}, 67 } 68 69 result := <-ss.LinkMetadata().Save(metadata) 70 require.Nil(t, result.Err) 71 72 metadata.Timestamp = getNextLinkMetadataTimestamp() 73 74 result = <-ss.LinkMetadata().Save(metadata) 75 76 require.Nil(t, result.Err) 77 require.IsType(t, metadata, result.Data) 78 assert.Equal(t, *metadata, *result.Data.(*model.LinkMetadata)) 79 }) 80 81 t.Run("should save with duplicate timestamp and different URL", func(t *testing.T) { 82 metadata := &model.LinkMetadata{ 83 URL: "http://example.com", 84 Timestamp: getNextLinkMetadataTimestamp(), 85 Type: model.LINK_METADATA_TYPE_IMAGE, 86 Data: &model.PostImage{}, 87 } 88 89 result := <-ss.LinkMetadata().Save(metadata) 90 require.Nil(t, result.Err) 91 92 metadata.URL = "http://example.com/another/page" 93 94 result = <-ss.LinkMetadata().Save(metadata) 95 96 require.Nil(t, result.Err) 97 require.IsType(t, metadata, result.Data) 98 assert.Equal(t, *metadata, *result.Data.(*model.LinkMetadata)) 99 }) 100 101 t.Run("should not save with duplicate URL and timestamp, but should not return an error", func(t *testing.T) { 102 metadata := &model.LinkMetadata{ 103 URL: "http://example.com", 104 Timestamp: getNextLinkMetadataTimestamp(), 105 Type: model.LINK_METADATA_TYPE_IMAGE, 106 Data: &model.PostImage{}, 107 } 108 109 result := <-ss.LinkMetadata().Save(metadata) 110 require.Nil(t, result.Err) 111 assert.Equal(t, &model.PostImage{}, result.Data.(*model.LinkMetadata).Data) 112 113 metadata.Data = &model.PostImage{Height: 10, Width: 20} 114 115 result = <-ss.LinkMetadata().Save(metadata) 116 require.Nil(t, result.Err) 117 assert.Equal(t, result.Data.(*model.LinkMetadata).Data, &model.PostImage{Height: 10, Width: 20}) 118 119 // Should return the original result, not the duplicate one 120 result = <-ss.LinkMetadata().Get(metadata.URL, metadata.Timestamp) 121 require.Nil(t, result.Err) 122 assert.Equal(t, &model.PostImage{}, result.Data.(*model.LinkMetadata).Data) 123 }) 124 } 125 126 func testLinkMetadataStoreGet(t *testing.T, ss store.Store) { 127 t.Run("should get value", func(t *testing.T) { 128 metadata := &model.LinkMetadata{ 129 URL: "http://example.com", 130 Timestamp: getNextLinkMetadataTimestamp(), 131 Type: model.LINK_METADATA_TYPE_IMAGE, 132 Data: &model.PostImage{}, 133 } 134 135 result := <-ss.LinkMetadata().Save(metadata) 136 require.Nil(t, result.Err) 137 138 result = <-ss.LinkMetadata().Get(metadata.URL, metadata.Timestamp) 139 140 require.Nil(t, result.Err) 141 require.IsType(t, metadata, result.Data) 142 assert.Equal(t, *metadata, *result.Data.(*model.LinkMetadata)) 143 }) 144 145 t.Run("should return not found with incorrect URL", func(t *testing.T) { 146 metadata := &model.LinkMetadata{ 147 URL: "http://example.com", 148 Timestamp: getNextLinkMetadataTimestamp(), 149 Type: model.LINK_METADATA_TYPE_IMAGE, 150 Data: &model.PostImage{}, 151 } 152 153 result := <-ss.LinkMetadata().Save(metadata) 154 require.Nil(t, result.Err) 155 156 result = <-ss.LinkMetadata().Get("http://example.com/another_page", metadata.Timestamp) 157 158 require.NotNil(t, result.Err) 159 assert.Equal(t, http.StatusNotFound, result.Err.StatusCode) 160 }) 161 162 t.Run("should return not found with incorrect timestamp", func(t *testing.T) { 163 metadata := &model.LinkMetadata{ 164 URL: "http://example.com", 165 Timestamp: getNextLinkMetadataTimestamp(), 166 Type: model.LINK_METADATA_TYPE_IMAGE, 167 Data: &model.PostImage{}, 168 } 169 170 result := <-ss.LinkMetadata().Save(metadata) 171 require.Nil(t, result.Err) 172 173 result = <-ss.LinkMetadata().Get(metadata.URL, getNextLinkMetadataTimestamp()) 174 175 require.NotNil(t, result.Err) 176 assert.Equal(t, http.StatusNotFound, result.Err.StatusCode) 177 }) 178 } 179 180 func testLinkMetadataStoreTypes(t *testing.T, ss store.Store) { 181 t.Run("should save and get image metadata", func(t *testing.T) { 182 metadata := &model.LinkMetadata{ 183 URL: "http://example.com", 184 Timestamp: getNextLinkMetadataTimestamp(), 185 Type: model.LINK_METADATA_TYPE_IMAGE, 186 Data: &model.PostImage{ 187 Width: 123, 188 Height: 456, 189 }, 190 } 191 192 result := <-ss.LinkMetadata().Save(metadata) 193 require.Nil(t, result.Err) 194 195 received := result.Data.(*model.LinkMetadata) 196 require.IsType(t, &model.PostImage{}, received.Data) 197 assert.Equal(t, *(metadata.Data.(*model.PostImage)), *(received.Data.(*model.PostImage))) 198 199 result = <-ss.LinkMetadata().Get(metadata.URL, metadata.Timestamp) 200 require.Nil(t, result.Err) 201 202 received = result.Data.(*model.LinkMetadata) 203 require.IsType(t, &model.PostImage{}, received.Data) 204 assert.Equal(t, *(metadata.Data.(*model.PostImage)), *(received.Data.(*model.PostImage))) 205 }) 206 207 t.Run("should save and get opengraph data", func(t *testing.T) { 208 og := &opengraph.OpenGraph{ 209 URL: "http://example.com", 210 Images: []*opengraph.Image{ 211 { 212 URL: "http://example.com/image.png", 213 }, 214 }, 215 } 216 217 metadata := &model.LinkMetadata{ 218 URL: "http://example.com", 219 Timestamp: getNextLinkMetadataTimestamp(), 220 Type: model.LINK_METADATA_TYPE_OPENGRAPH, 221 Data: og, 222 } 223 224 result := <-ss.LinkMetadata().Save(metadata) 225 require.Nil(t, result.Err) 226 227 received := result.Data.(*model.LinkMetadata) 228 require.IsType(t, &opengraph.OpenGraph{}, received.Data) 229 assert.Equal(t, *(metadata.Data.(*opengraph.OpenGraph)), *(received.Data.(*opengraph.OpenGraph))) 230 231 result = <-ss.LinkMetadata().Get(metadata.URL, metadata.Timestamp) 232 require.Nil(t, result.Err) 233 234 received = result.Data.(*model.LinkMetadata) 235 require.IsType(t, &opengraph.OpenGraph{}, received.Data) 236 assert.Equal(t, *(metadata.Data.(*opengraph.OpenGraph)), *(received.Data.(*opengraph.OpenGraph))) 237 }) 238 239 t.Run("should save and get nil", func(t *testing.T) { 240 metadata := &model.LinkMetadata{ 241 URL: "http://example.com", 242 Timestamp: getNextLinkMetadataTimestamp(), 243 Type: model.LINK_METADATA_TYPE_NONE, 244 Data: nil, 245 } 246 247 result := <-ss.LinkMetadata().Save(metadata) 248 require.Nil(t, result.Err) 249 250 received := result.Data.(*model.LinkMetadata) 251 assert.Nil(t, received.Data) 252 253 result = <-ss.LinkMetadata().Get(metadata.URL, metadata.Timestamp) 254 require.Nil(t, result.Err) 255 256 received = result.Data.(*model.LinkMetadata) 257 require.Nil(t, received.Data) 258 }) 259 }