Skip to content

Commit fe34c11

Browse files
committed
Add tests for media copying
1 parent b5424af commit fe34c11

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

tests/msc3911/cs_api_restricted_media_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tests
22

33
import (
44
"bytes"
5+
"strings"
56
"testing"
67

78
"github.com/matrix-org/complement"
@@ -234,4 +235,40 @@ func TestRestrictedMediaUnstable(t *testing.T) {
234235
}
235236

236237
})
238+
239+
// Test the media copy endpoint can produce a byte identical copy of a piece of media while also changing it's mxc uri
240+
t.Run("TestMediaCopy", func(t *testing.T) {
241+
// alice has an existing global profile avatar, it's mxc is available at aliceGlobalProfileAvatarMxcUri
242+
// it's []byte is available at aliceOriginalProfileBytes
243+
// We will reuse that and copy it
244+
245+
// There seem to be no existing utilities to split an mxc uri into it's components, which is needed for the copy endpoint.
246+
// Mxc uri's are formatted as "mxc://server_name/media_id"
247+
248+
// Cut the "mxc://" off the front
249+
existSplitMxc, found := strings.CutPrefix(aliceGlobalProfileAvatarMxcUri, "mxc://")
250+
if !found {
251+
t.Fatalf("mxc was malformed %s", aliceGlobalProfileAvatarMxcUri)
252+
}
253+
// Split the remaining into the server name and the media id
254+
mxcComponents := strings.Split(existSplitMxc, "/")
255+
256+
// Use bob to make the copy. The media should be viewable as it's a global profile
257+
res := bob.MustDo(t, "POST", []string{"_matrix", "client", "unstable", "org.matrix.msc3911", "media", "copy", mxcComponents[0], mxcComponents[1]}, client.WithJSONBody(t, map[string]any{}))
258+
259+
body := client.ParseJSON(t, res)
260+
newMxcUri := client.GetJSONFieldStr(t, body, "content_uri")
261+
262+
aliceNewAvatarBytes, _ := bob.DownloadContentAuthenticated(t, newMxcUri)
263+
if !bytes.Equal(aliceOriginalProfileBytes, aliceNewAvatarBytes) {
264+
t.Fatalf("Media is differing and should be identical")
265+
}
266+
267+
})
268+
269+
t.Run("TestMediaCopyNonexistingFile", func(t *testing.T) {
270+
// cheat a little on making a media id, it should not exist after all
271+
response := bob.Do(t, "POST", []string{"_matrix", "client", "unstable", "org.matrix.msc3911", "media", "copy", "hs1", "fAkEYfaKeyMeDiAId"}, client.WithJSONBody(t, map[string]any{}))
272+
must.MatchResponse(t, response, match.HTTPResponse{StatusCode: 404})
273+
})
237274
}

tests/msc3911/federation_restricted_media_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tests
22

33
import (
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

Comments
 (0)