complement/tests/csapi/url_preview_test.go
kegsay a669c750c8
Remove Deployment.Client and change Deploy (#676)
* Remove Deployment.Client and change Deploy

- `Deployment.Client` was used to get pre-registered clients. Now we want tests to register new users for each test, for dirty runs. So swap for `Deployment.Register` everywhere.
- `Deploy` was used to deploy a blueprint. We don't want this to enable dirty runs. So replace it with the number of servers you need e.g `Deploy(t, 2)`.

* Fix up more broken refactoring

* unbreak tests; make user localpart look nicer

* Alice and bob must share a room for presence

* Fix user directory test

* Fix race condition caused by making the room later than before
2023-10-17 18:07:43 +01:00

97 lines
2.7 KiB
Go

package csapi_tests
import (
"fmt"
"net/http"
"net/url"
"strings"
"testing"
"github.com/gorilla/mux"
"github.com/tidwall/gjson"
"github.com/matrix-org/complement"
"github.com/matrix-org/complement/client"
"github.com/matrix-org/complement/helpers"
"github.com/matrix-org/complement/internal/data"
"github.com/matrix-org/complement/internal/web"
"github.com/matrix-org/complement/match"
"github.com/matrix-org/complement/must"
"github.com/matrix-org/complement/runtime"
)
const oGraphTitle = "The Rock"
const oGraphType = "video.movie"
const oGraphUrl = "http://www.imdb.com/title/tt0117500/"
const oGraphImage = "test.png"
var oGraphHtml = fmt.Sprintf(`
<html prefix="og: http://ogp.me/ns#">
<head>
<title>The Rock (1996)</title>
<meta property="og:title" content="%s" />
<meta property="og:type" content="%s" />
<meta property="og:url" content="%s" />
<meta property="og:image" content="%s" />
</head>
<body></body>
</html>
`, oGraphTitle, oGraphType, oGraphUrl, oGraphImage)
// sytest: Test URL preview
func TestUrlPreview(t *testing.T) {
runtime.SkipIf(t, runtime.Dendrite) // FIXME: https://github.com/matrix-org/dendrite/issues/621
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
webServer := web.NewServer(t, deployment.GetConfig(), func(router *mux.Router) {
router.HandleFunc("/test.png", func(w http.ResponseWriter, req *http.Request) {
t.Log("/test.png fetched")
w.Header().Set("Content-Type", "image/png")
w.WriteHeader(200)
w.Write(data.MatrixPng)
}).Methods("GET")
router.HandleFunc("/test.html", func(w http.ResponseWriter, req *http.Request) {
t.Log("/test.html fetched")
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(200)
w.Write([]byte(oGraphHtml))
}).Methods("GET")
})
defer webServer.Close()
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
res := alice.MustDo(t, "GET", []string{"_matrix", "media", "v3", "preview_url"},
client.WithQueries(url.Values{
"url": []string{webServer.URL + "/test.html"},
}),
)
var e = client.GjsonEscape
must.MatchResponse(t, res, match.HTTPResponse{
JSON: []match.JSON{
match.JSONKeyEqual(e("og:title"), oGraphTitle),
match.JSONKeyEqual(e("og:type"), oGraphType),
match.JSONKeyEqual(e("og:url"), oGraphUrl),
match.JSONKeyEqual(e("matrix:image:size"), 2239.0),
match.JSONKeyEqual(e("og:image:height"), 129.0),
match.JSONKeyEqual(e("og:image:width"), 279.0),
func(body gjson.Result) error {
res := body.Get(e("og:image"))
if !res.Exists() {
return fmt.Errorf("can not find key og:image")
}
if !strings.HasPrefix(res.Str, "mxc://") {
return fmt.Errorf("image is not mxc")
}
return nil
},
},
})
}