-
Notifications
You must be signed in to change notification settings - Fork 67
Fix TestAsyncUpload tests not being independent
#838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MadLittleMods
merged 5 commits into
main
from
madlittlemods/fix-media-async-upload-tests-not-being-independent
Jan 26, 2026
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6e1d680
Make async upload tests independent
MadLittleMods abb5075
Add `asyncUploadMedia` helper
MadLittleMods 7fe8245
Future work to clean up duplicate test
MadLittleMods 57ea4e1
Typo and better comment on how to resolve
MadLittleMods c4dbefd
More `asyncUploadMedia` usage
MadLittleMods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,13 +8,16 @@ import ( | |
|
|
||
| "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 | ||
|
|
||
|
|
@@ -23,16 +26,16 @@ func TestAsyncUpload(t *testing.T) { | |
|
|
||
| alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{}) | ||
|
|
||
| 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] | ||
| alice.CreateMedia(t) | ||
| }) | ||
|
|
||
| origin, mediaID := client.SplitMxc(mxcURI) | ||
|
|
||
| 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{ | ||
|
|
@@ -44,13 +47,23 @@ func TestAsyncUpload(t *testing.T) { | |
| }) | ||
| }) | ||
|
|
||
| wantContentType := "image/png" | ||
|
|
||
| t.Run("Upload media", func(t *testing.T) { | ||
| alice.UploadMediaAsync(t, origin, mediaID, data.MatrixPng, "test.png", wantContentType) | ||
| 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, | ||
|
|
@@ -61,23 +74,48 @@ func TestAsyncUpload(t *testing.T) { | |
| }) | ||
| }) | ||
|
|
||
| // 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) | ||
|
Comment on lines
+72
to
+77
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a future TODO (something to do in another PR). See #746 (comment) |
||
| 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 != wantContentType { | ||
| t.Fatalf("expected contentType to be %s, got %s", wantContentType, contentType) | ||
| 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 != wantContentType { | ||
| t.Fatalf("expected contentType to be %s, got %s", wantContentType, contentType) | ||
| 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 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.