diff --git a/app/post_metadata.go b/app/post_metadata.go index b288720c6c..cb2eac7a05 100644 --- a/app/post_metadata.go +++ b/app/post_metadata.go @@ -127,7 +127,7 @@ func (a *App) getEmbedForPost(post *model.Post, firstLink string, isNewPost bool }, nil } - if firstLink == "" { + if firstLink == "" || !*a.Config().ServiceSettings.EnableLinkPreviews { return nil, nil } @@ -145,7 +145,7 @@ func (a *App) getEmbedForPost(post *model.Post, firstLink string, isNewPost bool } if image != nil { - // Note that we're not passing the image info here since they'll be part of the PostMetadata.Images field + // Note that we're not passing the image info here since it'll be part of the PostMetadata.Images field return &model.PostEmbed{ Type: model.POST_EMBED_IMAGE, URL: firstLink, diff --git a/app/post_metadata_test.go b/app/post_metadata_test.go index 6ec8beaca1..3af1539a7e 100644 --- a/app/post_metadata_test.go +++ b/app/post_metadata_test.go @@ -66,6 +66,7 @@ func TestPreparePostForClient(t *testing.T) { th := Setup(t).InitBasic() th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.ServiceSettings.EnableLinkPreviews = true *cfg.ImageProxySettings.Enable = false *cfg.ExperimentalSettings.DisablePostMetadata = false }) @@ -420,6 +421,7 @@ func TestPreparePostForClientWithImageProxy(t *testing.T) { th := Setup(t).InitBasic() th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.ServiceSettings.EnableLinkPreviews = true *cfg.ServiceSettings.SiteURL = "http://mymattermost.com" *cfg.ImageProxySettings.Enable = true *cfg.ImageProxySettings.ImageProxyType = "atmos/camo" @@ -498,6 +500,123 @@ func testProxyOpenGraphImage(t *testing.T, th *TestHelper, shouldProxy bool) { } } +func TestGetEmbedForPost(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/index.html" { + w.Header().Set("Content-Type", "text/html") + w.Write([]byte(` + +
+ + + `)) + } else if r.URL.Path == "/image.png" { + file, err := testutils.ReadTestFile("test.png") + require.Nil(t, err) + + w.Header().Set("Content-Type", "image/png") + w.Write(file) + } else { + t.Fatal("Invalid path", r.URL.Path) + } + })) + defer server.Close() + + ogURL := server.URL + "/index.html" + imageURL := server.URL + "/image.png" + + t.Run("with link previews enabled", func(t *testing.T) { + th := Setup(t) + defer th.TearDown() + + th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.ServiceSettings.AllowedUntrustedInternalConnections = "127.0.0.1" + *cfg.ServiceSettings.EnableLinkPreviews = true + }) + + t.Run("should return a message attachment when the post has one", func(t *testing.T) { + embed, err := th.App.getEmbedForPost(&model.Post{ + Props: model.StringInterface{ + "attachments": []*model.SlackAttachment{ + { + Text: "test", + }, + }, + }, + }, "", false) + + assert.Equal(t, &model.PostEmbed{ + Type: model.POST_EMBED_MESSAGE_ATTACHMENT, + }, embed) + assert.Nil(t, err) + }) + + t.Run("should return an image embed when the first link is an image", func(t *testing.T) { + embed, err := th.App.getEmbedForPost(&model.Post{}, imageURL, false) + + assert.Equal(t, &model.PostEmbed{ + Type: model.POST_EMBED_IMAGE, + URL: imageURL, + }, embed) + assert.Nil(t, err) + }) + + t.Run("should return an image embed when the first link is an image", func(t *testing.T) { + embed, err := th.App.getEmbedForPost(&model.Post{}, ogURL, false) + + assert.Equal(t, &model.PostEmbed{ + Type: model.POST_EMBED_OPENGRAPH, + URL: ogURL, + Data: &opengraph.OpenGraph{ + Title: "Title", + }, + }, embed) + assert.Nil(t, err) + }) + }) + + t.Run("with link previews disabled", func(t *testing.T) { + th := Setup(t) + defer th.TearDown() + + th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.ServiceSettings.AllowedUntrustedInternalConnections = "127.0.0.1" + *cfg.ServiceSettings.EnableLinkPreviews = false + }) + + t.Run("should return an embedded message attachment", func(t *testing.T) { + embed, err := th.App.getEmbedForPost(&model.Post{ + Props: model.StringInterface{ + "attachments": []*model.SlackAttachment{ + { + Text: "test", + }, + }, + }, + }, "", false) + + assert.Equal(t, &model.PostEmbed{ + Type: model.POST_EMBED_MESSAGE_ATTACHMENT, + }, embed) + assert.Nil(t, err) + }) + + t.Run("should not return an opengraph embed", func(t *testing.T) { + embed, err := th.App.getEmbedForPost(&model.Post{}, ogURL, false) + + assert.Nil(t, embed) + assert.Nil(t, err) + }) + + t.Run("should not return an image embed", func(t *testing.T) { + embed, err := th.App.getEmbedForPost(&model.Post{}, imageURL, false) + + assert.Nil(t, embed) + assert.Nil(t, err) + }) + }) +} + func TestGetImagesForPost(t *testing.T) { t.Run("with an image link", func(t *testing.T) { th := Setup(t)