-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathmedia_async_uploads_test.go
More file actions
121 lines (101 loc) · 3.98 KB
/
media_async_uploads_test.go
File metadata and controls
121 lines (101 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package csapi_tests
import (
"bytes"
"net/http"
"strings"
"testing"
"github.com/matrix-org/complement"
"github.com/matrix-org/complement/client"
"github.com/matrix-org/complement/ct"
"github.com/matrix-org/complement/helpers"
"github.com/matrix-org/complement/internal/data"
"github.com/matrix-org/complement/match"
"github.com/matrix-org/complement/must"
"github.com/matrix-org/complement/runtime"
)
const pngContentType = "image/png"
func TestAsyncUpload(t *testing.T) {
runtime.SkipIf(t, runtime.Dendrite) // Dendrite doesn't support async uploads
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
t.Run("Create media", func(t *testing.T) {
alice.CreateMedia(t)
})
t.Run("Not yet uploaded", func(t *testing.T) {
mxcURI := alice.CreateMedia(t)
parts := strings.Split(mxcURI, "/")
mediaID := parts[len(parts)-1]
origin, mediaID := client.SplitMxc(mxcURI)
// Check that the media is not yet uploaded
res := alice.Do(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"),
},
})
})
t.Run("Upload media", func(t *testing.T) {
mxcURI := alice.CreateMedia(t)
parts := strings.Split(mxcURI, "/")
mediaID := parts[len(parts)-1]
origin, mediaID := client.SplitMxc(mxcURI)
alice.UploadMediaAsync(t, origin, mediaID, data.MatrixPng, "test.png", pngContentType)
})
t.Run("Cannot upload to a media ID that has already been uploaded to", func(t *testing.T) {
// First upload some media that we can conflict with
mxcURI := asyncUploadMedia(t, alice)
parts := strings.Split(mxcURI, "/")
mediaID := parts[len(parts)-1]
origin, mediaID := client.SplitMxc(mxcURI)
// Then try upload again using the same `mediaID`
res := alice.Do(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_CANNOT_OVERWRITE_MEDIA"),
match.JSONKeyEqual("error", "Media ID already has content"),
},
})
})
// TODO: This is the same as the test below (both use authenticated media). Previously
// this was testing unauthenticated vs authenticated media. We should resolve this by
// removing one of these tests or ideally, keeping both authenticated and
// unauthenticated tests and just gate it behind some homeserver check for
// unauthenticated media support (see
// https://github.com/matrix-org/complement/pull/746#discussion_r2718904066)
t.Run("Download media", func(t *testing.T) {
mxcURI := asyncUploadMedia(t, alice)
content, contentType := alice.DownloadContentAuthenticated(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 != pngContentType {
t.Fatalf("expected contentType to be %s, got %s", pngContentType, contentType)
}
})
t.Run("Download media over _matrix/client/v1/media/download", func(t *testing.T) {
mxcURI := asyncUploadMedia(t, alice)
content, contentType := alice.DownloadContentAuthenticated(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 != pngContentType {
t.Fatalf("expected contentType to be %s, got %s", pngContentType, contentType)
}
})
}
func asyncUploadMedia(
t ct.TestLike,
matrixClient *client.CSAPI,
) string {
t.Helper()
mxcURI := matrixClient.CreateMedia(t)
parts := strings.Split(mxcURI, "/")
mediaID := parts[len(parts)-1]
origin, mediaID := client.SplitMxc(mxcURI)
matrixClient.UploadMediaAsync(t, origin, mediaID, data.MatrixPng, "test.png", pngContentType)
return mxcURI
}