2019-11-29 12:59:40 +01:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See LICENSE.txt for license information.
|
2017-05-10 12:44:18 +02:00
|
|
|
|
|
|
|
|
package api4
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
2017-12-22 06:09:33 -06:00
|
|
|
|
2019-10-31 20:19:19 +09:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2019-11-28 14:39:38 +01:00
|
|
|
"github.com/mattermost/mattermost-server/v5/model"
|
2017-05-10 12:44:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestGetOpenGraphMetadata(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic()
|
2017-10-02 03:50:56 -05:00
|
|
|
defer th.TearDown()
|
|
|
|
|
|
2017-05-10 12:44:18 +02:00
|
|
|
Client := th.Client
|
|
|
|
|
|
2017-10-18 15:36:43 -07:00
|
|
|
enableLinkPreviews := *th.App.Config().ServiceSettings.EnableLinkPreviews
|
|
|
|
|
allowedInternalConnections := *th.App.Config().ServiceSettings.AllowedUntrustedInternalConnections
|
2017-05-10 12:44:18 +02:00
|
|
|
defer func() {
|
2017-10-18 15:36:43 -07:00
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableLinkPreviews = enableLinkPreviews })
|
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
|
|
|
cfg.ServiceSettings.AllowedUntrustedInternalConnections = &allowedInternalConnections
|
|
|
|
|
})
|
2017-05-10 12:44:18 +02:00
|
|
|
}()
|
2017-10-18 15:36:43 -07:00
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableLinkPreviews = true })
|
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
2019-07-17 16:04:09 +02:00
|
|
|
*cfg.ServiceSettings.AllowedUntrustedInternalConnections = "localhost,127.0.0.1"
|
2017-10-18 15:36:43 -07:00
|
|
|
})
|
2017-05-10 12:44:18 +02:00
|
|
|
|
|
|
|
|
ogDataCacheMissCount := 0
|
|
|
|
|
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ogDataCacheMissCount++
|
|
|
|
|
|
|
|
|
|
if r.URL.Path == "/og-data/" {
|
|
|
|
|
fmt.Fprintln(w, `
|
|
|
|
|
<html><head><meta property="og:type" content="article" />
|
|
|
|
|
<meta property="og:title" content="Test Title" />
|
|
|
|
|
<meta property="og:url" content="http://example.com/" />
|
|
|
|
|
</head><body></body></html>
|
|
|
|
|
`)
|
|
|
|
|
} else if r.URL.Path == "/no-og-data/" {
|
|
|
|
|
fmt.Fprintln(w, `<html><head></head><body></body></html>`)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
for _, data := range [](map[string]interface{}){
|
|
|
|
|
{"path": "/og-data/", "title": "Test Title", "cacheMissCount": 1},
|
|
|
|
|
{"path": "/no-og-data/", "title": "", "cacheMissCount": 2},
|
|
|
|
|
|
|
|
|
|
// Data should be cached for following
|
|
|
|
|
{"path": "/og-data/", "title": "Test Title", "cacheMissCount": 2},
|
|
|
|
|
{"path": "/no-og-data/", "title": "", "cacheMissCount": 2},
|
|
|
|
|
} {
|
|
|
|
|
|
|
|
|
|
openGraph, resp := Client.OpenGraph(ts.URL + data["path"].(string))
|
|
|
|
|
CheckNoError(t, resp)
|
|
|
|
|
|
2019-10-31 20:19:19 +09:00
|
|
|
require.Equalf(t, openGraph["title"], data["title"].(string),
|
|
|
|
|
"OG data title mismatch for path \"%s\".")
|
|
|
|
|
|
|
|
|
|
require.Equal(t, ogDataCacheMissCount, data["cacheMissCount"].(int),
|
|
|
|
|
"Cache miss count didn't match.")
|
2017-05-10 12:44:18 +02:00
|
|
|
}
|
|
|
|
|
|
2017-10-18 15:36:43 -07:00
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableLinkPreviews = false })
|
2017-05-10 12:44:18 +02:00
|
|
|
_, resp := Client.OpenGraph(ts.URL + "/og-data/")
|
|
|
|
|
CheckNotImplementedStatus(t, resp)
|
|
|
|
|
}
|