Skip to content

Commit

Permalink
Add a basic test fetching thumbnail from `_matrix/federation/v1/media…
Browse files Browse the repository at this point in the history
…/thumbnail` (#732)

* add a basic test fetching thumbnail from federation endpoint

* test federation thumbnail body against thumbnail body

* verify we we actually got an image
  • Loading branch information
H-Shay committed Aug 22, 2024
1 parent 6e4426a commit 39733c1
Showing 1 changed file with 97 additions and 5 deletions.
102 changes: 97 additions & 5 deletions tests/media_thumbnail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ package tests

import (
"bytes"
"context"
"github.com/matrix-org/complement"
"github.com/matrix-org/complement/client"
"github.com/matrix-org/complement/federation"
"github.com/matrix-org/complement/helpers"
"github.com/matrix-org/complement/internal/data"
"github.com/matrix-org/complement/runtime"
"github.com/matrix-org/gomatrixserverlib/fclient"
"github.com/matrix-org/gomatrixserverlib/spec"
"image/jpeg"
"image/png"
"io"
"mime"
"mime/multipart"
"net/http"
"net/url"
"strings"
"testing"

"github.com/matrix-org/complement"
"github.com/matrix-org/complement/client"
"github.com/matrix-org/complement/helpers"
"github.com/matrix-org/complement/internal/data"
)

// TODO: add JPEG testing
Expand Down Expand Up @@ -68,6 +73,93 @@ func TestRemotePngThumbnail(t *testing.T) {
}
}

func TestFederationThumbnail(t *testing.T) {
runtime.SkipIf(t, runtime.Dendrite)

deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)

alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})

srv := federation.NewServer(t, deployment,
federation.HandleKeyRequests(),
)
cancel := srv.Listen()
defer cancel()
origin := spec.ServerName(srv.ServerName())

fileName := "test.png"
contentType := "image/png"

uri := alice.UploadContent(t, data.LargePng, fileName, contentType)
mediaOrigin, mediaId := client.SplitMxc(uri)

path := []string{"_matrix", "media", "v3", "thumbnail", mediaOrigin, mediaId}
res := alice.MustDo(t, "GET", path, client.WithQueries(url.Values{
"width": []string{"32"},
"height": []string{"32"},
"method": []string{"scale"},
}))

if res.StatusCode != 200 {
t.Fatalf("thumbnail request for uploaded file failed")
}

body, err := io.ReadAll(res.Body)
if err != nil {
t.Fatalf("thumbnail request for uploaded file failed: %s", err)
}

fedReq := fclient.NewFederationRequest(
"GET",
origin,
"hs1",
"/_matrix/federation/v1/media/thumbnail/"+mediaId+"?method=scale&width=32&height=32",
)

resp, err := srv.DoFederationRequest(context.Background(), t, deployment, fedReq)
if err != nil {
t.Fatalf("federation thumbnail request for uploaded file failed: %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("federation thumbnail request for uploaded file failed with status code: %v", resp.StatusCode)
}

resContentType := resp.Header.Get("Content-Type")
_, params, err := mime.ParseMediaType(resContentType)

if err != nil {
t.Fatalf("failed to parse multipart response: %s", err)
}

foundImage := false
reader := multipart.NewReader(resp.Body, params["boundary"])
for {
p, err := reader.NextPart()
if err == io.EOF { // End of the multipart content
break
}
if err != nil {
t.Fatalf("failed to read multipart response: %s", err)
}

partContentType := p.Header.Get("Content-Type")
if partContentType == contentType {
imageBody, err := io.ReadAll(p)
if err != nil {
t.Fatalf("failed to read multipart part %s: %s", partContentType, err)
}
if !bytes.Equal(imageBody, body) {
t.Fatalf("body does not match uploaded file")
}
foundImage = true
}
}
if !foundImage {
t.Fatalf("No image was found in response.")
}
}

func fetchAndValidateThumbnail(t *testing.T, c *client.CSAPI, mxcUri string, authenticated bool) {
t.Helper()

Expand Down

0 comments on commit 39733c1

Please sign in to comment.