MM-15302 Don't get post embeds with link previews disabled (#10587)
* MM-15302 Don't get post embeds with link previews disabled * Enable link previews for other metadata tests
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
41d117c37b
Коммит
a5501cf3f8
@@ -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,
|
||||
|
||||
@@ -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(`
|
||||
<html>
|
||||
<head>
|
||||
<meta property="og:title" content="Title" />
|
||||
</head>
|
||||
</html>`))
|
||||
} 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)
|
||||
|
||||
Ссылка в новой задаче
Block a user