@@ -8,13 +8,16 @@ import (
88
99 "github.com/matrix-org/complement"
1010 "github.com/matrix-org/complement/client"
11+ "github.com/matrix-org/complement/ct"
1112 "github.com/matrix-org/complement/helpers"
1213 "github.com/matrix-org/complement/internal/data"
1314 "github.com/matrix-org/complement/match"
1415 "github.com/matrix-org/complement/must"
1516 "github.com/matrix-org/complement/runtime"
1617)
1718
19+ const pngContentType = "image/png"
20+
1821func TestAsyncUpload (t * testing.T ) {
1922 runtime .SkipIf (t , runtime .Dendrite ) // Dendrite doesn't support async uploads
2023
@@ -23,16 +26,16 @@ func TestAsyncUpload(t *testing.T) {
2326
2427 alice := deployment .Register (t , "hs1" , helpers.RegistrationOpts {})
2528
26- var mxcURI , mediaID string
2729 t .Run ("Create media" , func (t * testing.T ) {
28- mxcURI = alice .CreateMedia (t )
29- parts := strings .Split (mxcURI , "/" )
30- mediaID = parts [len (parts )- 1 ]
30+ alice .CreateMedia (t )
3131 })
3232
33- origin , mediaID := client .SplitMxc (mxcURI )
34-
3533 t .Run ("Not yet uploaded" , func (t * testing.T ) {
34+ mxcURI := alice .CreateMedia (t )
35+ parts := strings .Split (mxcURI , "/" )
36+ mediaID := parts [len (parts )- 1 ]
37+ origin , mediaID := client .SplitMxc (mxcURI )
38+
3639 // Check that the media is not yet uploaded
3740 res := alice .Do (t , "GET" , []string {"_matrix" , "media" , "v3" , "download" , origin , mediaID })
3841 must .MatchResponse (t , res , match.HTTPResponse {
@@ -44,13 +47,18 @@ func TestAsyncUpload(t *testing.T) {
4447 })
4548 })
4649
47- wantContentType := "image/png"
48-
4950 t .Run ("Upload media" , func (t * testing.T ) {
50- alice . UploadMediaAsync (t , origin , mediaID , data . MatrixPng , "test.png" , wantContentType )
51+ _ = asyncUploadMedia (t , alice )
5152 })
5253
5354 t .Run ("Cannot upload to a media ID that has already been uploaded to" , func (t * testing.T ) {
55+ // First upload some media that we can conflict with
56+ mxcURI := asyncUploadMedia (t , alice )
57+ parts := strings .Split (mxcURI , "/" )
58+ mediaID := parts [len (parts )- 1 ]
59+ origin , mediaID := client .SplitMxc (mxcURI )
60+
61+ // Then try upload again using the same `mediaID`
5462 res := alice .Do (t , "PUT" , []string {"_matrix" , "media" , "v3" , "upload" , origin , mediaID })
5563 must .MatchResponse (t , res , match.HTTPResponse {
5664 StatusCode : http .StatusConflict ,
@@ -61,23 +69,48 @@ func TestAsyncUpload(t *testing.T) {
6169 })
6270 })
6371
72+ // TODO: This is the same as the test below (both use authenticated media). Previously
73+ // this was testing unauthenticated vs authenticated media. We should resolve this by
74+ // removing one of these tests or ideally, keeping both authenticated and
75+ // unauthenticated tests and just gate it behind some homeserver check for
76+ // unauthenticated media support (see
77+ // https://github.com/matrix-org/complement/pull/746#discussion_r2718904066)
6478 t .Run ("Download media" , func (t * testing.T ) {
79+ mxcURI := asyncUploadMedia (t , alice )
80+
6581 content , contentType := alice .DownloadContentAuthenticated (t , mxcURI )
6682 if ! bytes .Equal (data .MatrixPng , content ) {
6783 t .Fatalf ("uploaded and downloaded content doesn't match: want %v\n got\n %v" , data .MatrixPng , content )
6884 }
69- if contentType != wantContentType {
70- t .Fatalf ("expected contentType to be %s, got %s" , wantContentType , contentType )
85+ if contentType != pngContentType {
86+ t .Fatalf ("expected contentType to be %s, got %s" , pngContentType , contentType )
7187 }
7288 })
7389
7490 t .Run ("Download media over _matrix/client/v1/media/download" , func (t * testing.T ) {
91+ mxcURI := asyncUploadMedia (t , alice )
92+
7593 content , contentType := alice .DownloadContentAuthenticated (t , mxcURI )
7694 if ! bytes .Equal (data .MatrixPng , content ) {
7795 t .Fatalf ("uploaded and downloaded content doesn't match: want %v\n got\n %v" , data .MatrixPng , content )
7896 }
79- if contentType != wantContentType {
80- t .Fatalf ("expected contentType to be %s, got %s" , wantContentType , contentType )
97+ if contentType != pngContentType {
98+ t .Fatalf ("expected contentType to be %s, got %s" , pngContentType , contentType )
8199 }
82100 })
83101}
102+
103+ func asyncUploadMedia (
104+ t ct.TestLike ,
105+ matrixClient * client.CSAPI ,
106+ ) string {
107+ t .Helper ()
108+
109+ mxcURI := matrixClient .CreateMedia (t )
110+ parts := strings .Split (mxcURI , "/" )
111+ mediaID := parts [len (parts )- 1 ]
112+ origin , mediaID := client .SplitMxc (mxcURI )
113+ matrixClient .UploadMediaAsync (t , origin , mediaID , data .MatrixPng , "test.png" , pngContentType )
114+
115+ return mxcURI
116+ }
0 commit comments