[MM-18628] Fix flaky OpenGraph test (#12365)
* use local httptest server instead of github.com * use switch statements
Этот коммит содержится в:
коммит произвёл
Harrison Healey
родитель
af3ffeed1a
Коммит
1a4d7869cb
@@ -58,12 +58,54 @@ func TestPreparePostListForClient(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPreparePostForClient(t *testing.T) {
|
func TestPreparePostForClient(t *testing.T) {
|
||||||
|
var serverURL string
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/":
|
||||||
|
w.Header().Set("Content-Type", "text/html")
|
||||||
|
w.Write([]byte(`
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta property="og:image" content="` + serverURL + `/test-image3.png" />
|
||||||
|
<meta property="og:site_name" content="GitHub" />
|
||||||
|
<meta property="og:type" content="object" />
|
||||||
|
<meta property="og:title" content="hmhealey/test-files" />
|
||||||
|
<meta property="og:url" content="https://github.com/hmhealey/test-files" />
|
||||||
|
<meta property="og:description" content="Contribute to hmhealey/test-files development by creating an account on GitHub." />
|
||||||
|
</head>
|
||||||
|
</html>`))
|
||||||
|
case "/test-image1.png":
|
||||||
|
file, err := testutils.ReadTestFile("test.png")
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "image/png")
|
||||||
|
w.Write(file)
|
||||||
|
case "/test-image2.png":
|
||||||
|
file, err := testutils.ReadTestFile("test-data-graph.png")
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "image/png")
|
||||||
|
w.Write(file)
|
||||||
|
case "/test-image3.png":
|
||||||
|
file, err := testutils.ReadTestFile("qa-data-graph.png")
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "image/png")
|
||||||
|
w.Write(file)
|
||||||
|
default:
|
||||||
|
require.Fail(t, "Invalid path", r.URL.Path)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
serverURL = server.URL
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
setup := func() *TestHelper {
|
setup := func() *TestHelper {
|
||||||
th := Setup(t).InitBasic()
|
th := Setup(t).InitBasic()
|
||||||
|
|
||||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||||
*cfg.ServiceSettings.EnableLinkPreviews = true
|
*cfg.ServiceSettings.EnableLinkPreviews = true
|
||||||
*cfg.ImageProxySettings.Enable = false
|
*cfg.ImageProxySettings.Enable = false
|
||||||
|
*cfg.ServiceSettings.AllowedUntrustedInternalConnections = "localhost,127.0.0.1"
|
||||||
})
|
})
|
||||||
|
|
||||||
return th
|
return th
|
||||||
@@ -289,7 +331,7 @@ func TestPreparePostForClient(t *testing.T) {
|
|||||||
post, err := th.App.CreatePost(&model.Post{
|
post, err := th.App.CreatePost(&model.Post{
|
||||||
UserId: th.BasicUser.Id,
|
UserId: th.BasicUser.Id,
|
||||||
ChannelId: th.BasicChannel.Id,
|
ChannelId: th.BasicChannel.Id,
|
||||||
Message: "This is  and ",
|
Message: fmt.Sprintf("This is  and ", server.URL, server.URL),
|
||||||
}, th.BasicChannel, false)
|
}, th.BasicChannel, false)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
|
||||||
@@ -300,14 +342,14 @@ func TestPreparePostForClient(t *testing.T) {
|
|||||||
require.Len(t, imageDimensions, 2)
|
require.Len(t, imageDimensions, 2)
|
||||||
assert.Equal(t, &model.PostImage{
|
assert.Equal(t, &model.PostImage{
|
||||||
Format: "png",
|
Format: "png",
|
||||||
Width: 1068,
|
Width: 1280,
|
||||||
Height: 552,
|
Height: 1780,
|
||||||
}, imageDimensions["https://github.com/hmhealey/test-files/raw/master/logoVertical.png"])
|
}, imageDimensions[server.URL+"/test-image2.png"])
|
||||||
assert.Equal(t, &model.PostImage{
|
assert.Equal(t, &model.PostImage{
|
||||||
Format: "png",
|
Format: "png",
|
||||||
Width: 501,
|
Width: 408,
|
||||||
Height: 501,
|
Height: 336,
|
||||||
}, imageDimensions["https://github.com/hmhealey/test-files/raw/master/icon.png"])
|
}, imageDimensions[server.URL+"/test-image1.png"])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -332,8 +374,8 @@ func TestPreparePostForClient(t *testing.T) {
|
|||||||
post, err := th.App.CreatePost(&model.Post{
|
post, err := th.App.CreatePost(&model.Post{
|
||||||
UserId: th.BasicUser.Id,
|
UserId: th.BasicUser.Id,
|
||||||
ChannelId: th.BasicChannel.Id,
|
ChannelId: th.BasicChannel.Id,
|
||||||
Message: `This is our logo: https://github.com/hmhealey/test-files/raw/master/logoVertical.png
|
Message: `This is our logo: ` + server.URL + `/test-image2.png
|
||||||
And this is our icon: https://github.com/hmhealey/test-files/raw/master/icon.png`,
|
And this is our icon: ` + server.URL + `/test-image1.png`,
|
||||||
}, th.BasicChannel, false)
|
}, th.BasicChannel, false)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
|
||||||
@@ -345,7 +387,7 @@ func TestPreparePostForClient(t *testing.T) {
|
|||||||
assert.ElementsMatch(t, []*model.PostEmbed{
|
assert.ElementsMatch(t, []*model.PostEmbed{
|
||||||
{
|
{
|
||||||
Type: model.POST_EMBED_IMAGE,
|
Type: model.POST_EMBED_IMAGE,
|
||||||
URL: "https://github.com/hmhealey/test-files/raw/master/logoVertical.png",
|
URL: server.URL + "/test-image2.png",
|
||||||
},
|
},
|
||||||
}, clientPost.Metadata.Embeds)
|
}, clientPost.Metadata.Embeds)
|
||||||
})
|
})
|
||||||
@@ -355,9 +397,9 @@ func TestPreparePostForClient(t *testing.T) {
|
|||||||
require.Len(t, imageDimensions, 1)
|
require.Len(t, imageDimensions, 1)
|
||||||
assert.Equal(t, &model.PostImage{
|
assert.Equal(t, &model.PostImage{
|
||||||
Format: "png",
|
Format: "png",
|
||||||
Width: 1068,
|
Width: 1280,
|
||||||
Height: 552,
|
Height: 1780,
|
||||||
}, imageDimensions["https://github.com/hmhealey/test-files/raw/master/logoVertical.png"])
|
}, imageDimensions[server.URL+"/test-image2.png"])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -368,7 +410,7 @@ func TestPreparePostForClient(t *testing.T) {
|
|||||||
post, err := th.App.CreatePost(&model.Post{
|
post, err := th.App.CreatePost(&model.Post{
|
||||||
UserId: th.BasicUser.Id,
|
UserId: th.BasicUser.Id,
|
||||||
ChannelId: th.BasicChannel.Id,
|
ChannelId: th.BasicChannel.Id,
|
||||||
Message: `This is our web page: https://github.com/hmhealey/test-files`,
|
Message: `This is our web page: ` + server.URL,
|
||||||
}, th.BasicChannel, false)
|
}, th.BasicChannel, false)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
|
||||||
@@ -378,13 +420,13 @@ func TestPreparePostForClient(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("populates embeds", func(t *testing.T) {
|
t.Run("populates embeds", func(t *testing.T) {
|
||||||
assert.Equal(t, firstEmbed.Type, model.POST_EMBED_OPENGRAPH)
|
assert.Equal(t, firstEmbed.Type, model.POST_EMBED_OPENGRAPH)
|
||||||
assert.Equal(t, firstEmbed.URL, "https://github.com/hmhealey/test-files")
|
assert.Equal(t, firstEmbed.URL, server.URL)
|
||||||
assert.Equal(t, ogData.Description, "Contribute to hmhealey/test-files development by creating an account on GitHub.")
|
assert.Equal(t, ogData.Description, "Contribute to hmhealey/test-files development by creating an account on GitHub.")
|
||||||
assert.Equal(t, ogData.SiteName, "GitHub")
|
assert.Equal(t, ogData.SiteName, "GitHub")
|
||||||
assert.Equal(t, ogData.Title, "hmhealey/test-files")
|
assert.Equal(t, ogData.Title, "hmhealey/test-files")
|
||||||
assert.Equal(t, ogData.Type, "object")
|
assert.Equal(t, ogData.Type, "object")
|
||||||
assert.Equal(t, ogData.URL, "https://github.com/hmhealey/test-files")
|
assert.Equal(t, ogData.URL, server.URL)
|
||||||
assert.Equal(t, ogData.Images[0].URL, "https://avatars1.githubusercontent.com/u/3277310?s=400&v=4")
|
assert.Equal(t, ogData.Images[0].URL, server.URL+"/test-image3.png")
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("populates image dimensions", func(t *testing.T) {
|
t.Run("populates image dimensions", func(t *testing.T) {
|
||||||
@@ -392,9 +434,9 @@ func TestPreparePostForClient(t *testing.T) {
|
|||||||
require.Len(t, imageDimensions, 1)
|
require.Len(t, imageDimensions, 1)
|
||||||
assert.Equal(t, &model.PostImage{
|
assert.Equal(t, &model.PostImage{
|
||||||
Format: "png",
|
Format: "png",
|
||||||
Width: 420,
|
Width: 1790,
|
||||||
Height: 420,
|
Height: 1340,
|
||||||
}, imageDimensions["https://avatars1.githubusercontent.com/u/3277310?s=400&v=4"])
|
}, imageDimensions[server.URL+"/test-image3.png"])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -408,7 +450,7 @@ func TestPreparePostForClient(t *testing.T) {
|
|||||||
Props: map[string]interface{}{
|
Props: map[string]interface{}{
|
||||||
"attachments": []interface{}{
|
"attachments": []interface{}{
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"text": "",
|
"text": "",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -430,9 +472,9 @@ func TestPreparePostForClient(t *testing.T) {
|
|||||||
require.Len(t, imageDimensions, 1)
|
require.Len(t, imageDimensions, 1)
|
||||||
assert.Equal(t, &model.PostImage{
|
assert.Equal(t, &model.PostImage{
|
||||||
Format: "png",
|
Format: "png",
|
||||||
Width: 501,
|
Width: 408,
|
||||||
Height: 501,
|
Height: 336,
|
||||||
}, imageDimensions["https://github.com/hmhealey/test-files/raw/master/icon.png"])
|
}, imageDimensions[server.URL+"/test-image1.png"])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -444,6 +486,7 @@ func TestPreparePostForClientWithImageProxy(t *testing.T) {
|
|||||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||||
*cfg.ServiceSettings.EnableLinkPreviews = true
|
*cfg.ServiceSettings.EnableLinkPreviews = true
|
||||||
*cfg.ServiceSettings.SiteURL = "http://mymattermost.com"
|
*cfg.ServiceSettings.SiteURL = "http://mymattermost.com"
|
||||||
|
*cfg.ServiceSettings.AllowedUntrustedInternalConnections = "localhost,127.0.0.1"
|
||||||
*cfg.ImageProxySettings.Enable = true
|
*cfg.ImageProxySettings.Enable = true
|
||||||
*cfg.ImageProxySettings.ImageProxyType = "atmos/camo"
|
*cfg.ImageProxySettings.ImageProxyType = "atmos/camo"
|
||||||
*cfg.ImageProxySettings.RemoteImageProxyURL = "https://127.0.0.1"
|
*cfg.ImageProxySettings.RemoteImageProxyURL = "https://127.0.0.1"
|
||||||
@@ -490,10 +533,39 @@ func testProxyLinkedImage(t *testing.T, th *TestHelper, shouldProxy bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testProxyOpenGraphImage(t *testing.T, th *TestHelper, shouldProxy bool) {
|
func testProxyOpenGraphImage(t *testing.T, th *TestHelper, shouldProxy bool) {
|
||||||
|
var serverURL string
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/":
|
||||||
|
w.Header().Set("Content-Type", "text/html")
|
||||||
|
w.Write([]byte(`
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta property="og:image" content="` + serverURL + `/test-image3.png" />
|
||||||
|
<meta property="og:site_name" content="GitHub" />
|
||||||
|
<meta property="og:type" content="object" />
|
||||||
|
<meta property="og:title" content="hmhealey/test-files" />
|
||||||
|
<meta property="og:url" content="https://github.com/hmhealey/test-files" />
|
||||||
|
<meta property="og:description" content="Contribute to hmhealey/test-files development by creating an account on GitHub." />
|
||||||
|
</head>
|
||||||
|
</html>`))
|
||||||
|
case "/test-image3.png":
|
||||||
|
file, err := testutils.ReadTestFile("qa-data-graph.png")
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "image/png")
|
||||||
|
w.Write(file)
|
||||||
|
default:
|
||||||
|
require.Fail(t, "Invalid path", r.URL.Path)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
serverURL = server.URL
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
post, err := th.App.CreatePost(&model.Post{
|
post, err := th.App.CreatePost(&model.Post{
|
||||||
UserId: th.BasicUser.Id,
|
UserId: th.BasicUser.Id,
|
||||||
ChannelId: th.BasicChannel.Id,
|
ChannelId: th.BasicChannel.Id,
|
||||||
Message: `This is our web page: https://github.com/hmhealey/test-files`,
|
Message: `This is our web page: ` + server.URL,
|
||||||
}, th.BasicChannel, false)
|
}, th.BasicChannel, false)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
|
||||||
@@ -502,10 +574,11 @@ func testProxyOpenGraphImage(t *testing.T, th *TestHelper, shouldProxy bool) {
|
|||||||
|
|
||||||
embed := embeds[0]
|
embed := embeds[0]
|
||||||
assert.Equal(t, model.POST_EMBED_OPENGRAPH, embed.Type, "embed type should be OpenGraph")
|
assert.Equal(t, model.POST_EMBED_OPENGRAPH, embed.Type, "embed type should be OpenGraph")
|
||||||
assert.Equal(t, "https://github.com/hmhealey/test-files", embed.URL, "embed URL should be correct")
|
assert.Equal(t, server.URL, embed.URL, "embed URL should be correct")
|
||||||
|
|
||||||
og, ok := embed.Data.(*opengraph.OpenGraph)
|
og, ok := embed.Data.(*opengraph.OpenGraph)
|
||||||
assert.Equal(t, true, ok, "data should be non-nil OpenGraph data")
|
assert.True(t, ok, "data should be non-nil OpenGraph data")
|
||||||
|
assert.NotNil(t, og, "data should be non-nil OpenGraph data")
|
||||||
assert.Equal(t, "GitHub", og.SiteName, "OpenGraph data should be correctly populated")
|
assert.Equal(t, "GitHub", og.SiteName, "OpenGraph data should be correctly populated")
|
||||||
|
|
||||||
require.Len(t, og.Images, 1, "OpenGraph data should have one image")
|
require.Len(t, og.Images, 1, "OpenGraph data should have one image")
|
||||||
@@ -513,9 +586,9 @@ func testProxyOpenGraphImage(t *testing.T, th *TestHelper, shouldProxy bool) {
|
|||||||
image := og.Images[0]
|
image := og.Images[0]
|
||||||
if shouldProxy {
|
if shouldProxy {
|
||||||
assert.Equal(t, "", image.URL, "image URL should not be set with proxy")
|
assert.Equal(t, "", image.URL, "image URL should not be set with proxy")
|
||||||
assert.Equal(t, "http://mymattermost.com/api/v4/image?url=https%3A%2F%2Favatars1.githubusercontent.com%2Fu%2F3277310%3Fs%3D400%26v%3D4", image.SecureURL, "secure image URL should be sent through proxy")
|
assert.Equal(t, "http://mymattermost.com/api/v4/image?url="+url.QueryEscape(server.URL+"/test-image3.png"), image.SecureURL, "secure image URL should be sent through proxy")
|
||||||
} else {
|
} else {
|
||||||
assert.Equal(t, "https://avatars1.githubusercontent.com/u/3277310?s=400&v=4", image.URL, "image URL should be set")
|
assert.Equal(t, server.URL+"/test-image3.png", image.URL, "image URL should be set")
|
||||||
assert.Equal(t, "", image.SecureURL, "secure image URL should not be set")
|
assert.Equal(t, "", image.SecureURL, "secure image URL should not be set")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user