@@ -2,6 +2,7 @@ package tests
22
33import (
44 "bytes"
5+ "strings"
56 "testing"
67
78 "github.com/matrix-org/complement"
@@ -276,6 +277,42 @@ func TestFederationRestrictedMediaUnstable(t *testing.T) {
276277
277278 })
278279
280+ // Test the media copy endpoint can produce a byte identical copy of a piece of media while also changing it's mxc uri
281+ t .Run ("TestMediaCopy" , func (t * testing.T ) {
282+ // alice has an existing global profile avatar, it's mxc is available at aliceGlobalProfileAvatarMxcUri
283+ // it's []byte is available at aliceOriginalProfileBytes
284+ // We will reuse that and copy it
285+
286+ // There seem to be no existing utilities to split an mxc uri into it's components, which is needed for the copy endpoint.
287+ // Mxc uri's are formatted as "mxc://server_name/media_id"
288+
289+ // Cut the "mxc://" off the front
290+ existSplitMxc , found := strings .CutPrefix (aliceGlobalProfileAvatarMxcUri , "mxc://" )
291+ if ! found {
292+ t .Fatalf ("mxc was malformed %s" , aliceGlobalProfileAvatarMxcUri )
293+ }
294+ // Split the remaining into the server name and the media id
295+ mxcComponents := strings .Split (existSplitMxc , "/" )
296+
297+ // Use bob to make the copy. The media should be viewable as it's a global profile
298+ res := bob .MustDo (t , "POST" , []string {"_matrix" , "client" , "unstable" , "org.matrix.msc3911" , "media" , "copy" , mxcComponents [0 ], mxcComponents [1 ]}, client .WithJSONBody (t , map [string ]any {}))
299+
300+ body := client .ParseJSON (t , res )
301+ newMxcUri := client .GetJSONFieldStr (t , body , "content_uri" )
302+
303+ aliceNewAvatarBytes , _ := bob .DownloadContentAuthenticated (t , newMxcUri )
304+ if ! bytes .Equal (aliceOriginalProfileBytes , aliceNewAvatarBytes ) {
305+ t .Fatalf ("Media is differing and should be identical" )
306+ }
307+
308+ })
309+
310+ t .Run ("TestMediaCopyNonexistingFile" , func (t * testing.T ) {
311+ // cheat a little on making a media id, it should not exist after all
312+ response := bob .Do (t , "POST" , []string {"_matrix" , "client" , "unstable" , "org.matrix.msc3911" , "media" , "copy" , "hs1" , "fAkEYfaKeyMeDiAId" }, client .WithJSONBody (t , map [string ]any {}))
313+ must .MatchResponse (t , response , match.HTTPResponse {StatusCode : 404 })
314+ })
315+
279316}
280317
281318// Tank has one job, be part of the room so that federation works. No other interaction except running sync to make
0 commit comments