Skip to content

Commit

Permalink
async uploads: add initial set of tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <[email protected]>
  • Loading branch information
sumnerevans committed Aug 14, 2023
1 parent b986a30 commit f58d695
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ type CSAPI struct {
txnID int64
}

// CreateMedia creates an MXC URI for asynchronous media uploads.
func (c *CSAPI) CreateMedia(t *testing.T) string {
t.Helper()
res := c.MustDoFunc(t, "POST", []string{"_matrix", "media", "v1", "create"})
body := ParseJSON(t, res)
return GetJSONFieldStr(t, body, "content_uri")
}

// UploadMediaAsync uploads the provided content to the given server and media ID. Fails the test on error.
func (c *CSAPI) UploadMediaAsync(t *testing.T, serverName, mediaID string, fileBody []byte, fileName string, contentType string) {
t.Helper()
c.MustDoFunc(t, "PUT", []string{"_matrix", "media", "v3", "upload", serverName, mediaID})
}

// UploadContent uploads the provided content with an optional file name. Fails the test on error. Returns the MXC URI.
func (c *CSAPI) UploadContent(t *testing.T, fileBody []byte, fileName string, contentType string) string {
t.Helper()
Expand Down
72 changes: 72 additions & 0 deletions tests/csapi/media_async_uploads_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package csapi_tests

import (
"bytes"
"net/http"
"strings"
"testing"

"github.com/matrix-org/complement/internal/b"
"github.com/matrix-org/complement/internal/client"
"github.com/matrix-org/complement/internal/data"
"github.com/matrix-org/complement/internal/match"
"github.com/matrix-org/complement/internal/must"
"github.com/matrix-org/complement/runtime"
)

func TestAsyncUpload(t *testing.T) {
runtime.SkipIf(t, runtime.Dendrite) // Dendrite doesn't support async uploads

deployment := Deploy(t, b.BlueprintAlice)
defer deployment.Destroy(t)

alice := deployment.Client(t, "hs1", "@alice:hs1")

var mxcURI, mediaID string
t.Run("Create media", func(t *testing.T) {
mxcURI = alice.CreateMedia(t)
parts := strings.Split(mxcURI, "/")
mediaID = parts[len(parts)-1]
})

origin, mediaID := client.SplitMxc(mxcURI)

t.Run("Not yet uploaded", func(t *testing.T) {
// Check that the media is not yet uploaded
res := alice.DoFunc(t, "GET", []string{"_matrix", "media", "v3", "download", origin, mediaID})
must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: http.StatusGatewayTimeout,
JSON: []match.JSON{
match.JSONKeyEqual("errcode", "M_NOT_YET_UPLOADED"),
match.JSONKeyEqual("error", "Media has not been uploaded yet"),
},
})
})

wantContentType := "image/png"

t.Run("Upload media", func(t *testing.T) {
alice.UploadMediaAsync(t, origin, mediaID, data.MatrixPng, "test.png", wantContentType)
})

t.Run("Cannot upload to a media ID that has already been uploaded to", func(t *testing.T) {
res := alice.DoFunc(t, "PUT", []string{"_matrix", "media", "v3", "upload", origin, mediaID})
must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: http.StatusConflict,
JSON: []match.JSON{
match.JSONKeyEqual("errcode", "M_INVALID_USERNAME"),
match.JSONKeyEqual("error", "Media ID already has content"),
},
})
})

t.Run("Download media", func(t *testing.T) {
content, contentType := alice.DownloadContent(t, mxcURI)
if !bytes.Equal(data.MatrixPng, content) {
t.Fatalf("uploaded and downloaded content doesn't match: want %v\ngot\n%v", data.MatrixPng, content)
}
if contentType != wantContentType {
t.Fatalf("expected contentType to be %s, got %s", wantContentType, contentType)
}
})
}

0 comments on commit f58d695

Please sign in to comment.