github.com/adacta-ru/mattermost-server/v6@v6.0.0/api4/openGraph_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package api4 5 6 import ( 7 "fmt" 8 "net/http" 9 "net/http/httptest" 10 "testing" 11 12 "github.com/stretchr/testify/require" 13 14 "github.com/adacta-ru/mattermost-server/v6/model" 15 ) 16 17 func TestGetOpenGraphMetadata(t *testing.T) { 18 th := Setup(t).InitBasic() 19 defer th.TearDown() 20 21 Client := th.Client 22 23 enableLinkPreviews := *th.App.Config().ServiceSettings.EnableLinkPreviews 24 allowedInternalConnections := *th.App.Config().ServiceSettings.AllowedUntrustedInternalConnections 25 defer func() { 26 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableLinkPreviews = enableLinkPreviews }) 27 th.App.UpdateConfig(func(cfg *model.Config) { 28 cfg.ServiceSettings.AllowedUntrustedInternalConnections = &allowedInternalConnections 29 }) 30 }() 31 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableLinkPreviews = true }) 32 th.App.UpdateConfig(func(cfg *model.Config) { 33 *cfg.ServiceSettings.AllowedUntrustedInternalConnections = "localhost,127.0.0.1" 34 }) 35 36 ogDataCacheMissCount := 0 37 38 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 39 ogDataCacheMissCount++ 40 41 if r.URL.Path == "/og-data/" { 42 fmt.Fprintln(w, ` 43 <html><head><meta property="og:type" content="article" /> 44 <meta property="og:title" content="Test Title" /> 45 <meta property="og:url" content="http://example.com/" /> 46 </head><body></body></html> 47 `) 48 } else if r.URL.Path == "/no-og-data/" { 49 fmt.Fprintln(w, `<html><head></head><body></body></html>`) 50 } 51 })) 52 53 for _, data := range [](map[string]interface{}){ 54 {"path": "/og-data/", "title": "Test Title", "cacheMissCount": 1}, 55 {"path": "/no-og-data/", "title": "", "cacheMissCount": 2}, 56 57 // Data should be cached for following 58 {"path": "/og-data/", "title": "Test Title", "cacheMissCount": 2}, 59 {"path": "/no-og-data/", "title": "", "cacheMissCount": 2}, 60 } { 61 62 openGraph, resp := Client.OpenGraph(ts.URL + data["path"].(string)) 63 CheckNoError(t, resp) 64 65 require.Equalf(t, openGraph["title"], data["title"].(string), 66 "OG data title mismatch for path \"%s\".") 67 68 require.Equal(t, ogDataCacheMissCount, data["cacheMissCount"].(int), 69 "Cache miss count didn't match.") 70 } 71 72 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableLinkPreviews = false }) 73 _, resp := Client.OpenGraph(ts.URL + "/og-data/") 74 CheckNotImplementedStatus(t, resp) 75 }