-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Authenticate media requests when loading avatars (#2856)
* Load authenicated media * lint * Add tests * Add widget behaviour and test. * Update src/Avatar.test.tsx Co-authored-by: Hugh Nimmo-Smith <[email protected]> --------- Co-authored-by: Hugh Nimmo-Smith <[email protected]>
- Loading branch information
Showing
4 changed files
with
236 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
Copyright 2024 New Vector Ltd. | ||
SPDX-License-Identifier: AGPL-3.0-only | ||
Please see LICENSE in the repository root for full details. | ||
*/ | ||
|
||
import { afterEach, expect, test, vi } from "vitest"; | ||
import { render, screen } from "@testing-library/react"; | ||
import { MatrixClient } from "matrix-js-sdk/src/client"; | ||
import { FC, PropsWithChildren } from "react"; | ||
|
||
import { ClientContextProvider } from "./ClientContext"; | ||
import { Avatar } from "./Avatar"; | ||
import { mockMatrixRoomMember, mockRtcMembership } from "./utils/test"; | ||
|
||
const TestComponent: FC< | ||
PropsWithChildren<{ client: MatrixClient; supportsThumbnails?: boolean }> | ||
> = ({ client, children, supportsThumbnails }) => { | ||
return ( | ||
<ClientContextProvider | ||
value={{ | ||
state: "valid", | ||
disconnected: false, | ||
supportedFeatures: { | ||
reactions: true, | ||
thumbnails: supportsThumbnails ?? true, | ||
}, | ||
setClient: vi.fn(), | ||
authenticated: { | ||
client, | ||
isPasswordlessUser: true, | ||
changePassword: vi.fn(), | ||
logout: vi.fn(), | ||
}, | ||
}} | ||
> | ||
{children} | ||
</ClientContextProvider> | ||
); | ||
}; | ||
|
||
afterEach(() => { | ||
vi.unstubAllGlobals(); | ||
}); | ||
|
||
test("should just render a placeholder when the user has no avatar", () => { | ||
const client = vi.mocked<MatrixClient>({ | ||
getAccessToken: () => "my-access-token", | ||
mxcUrlToHttp: () => vi.fn(), | ||
} as unknown as MatrixClient); | ||
|
||
vi.spyOn(client, "mxcUrlToHttp"); | ||
const member = mockMatrixRoomMember( | ||
mockRtcMembership("@alice:example.org", "AAAA"), | ||
{ | ||
getMxcAvatarUrl: () => undefined, | ||
}, | ||
); | ||
const displayName = "Alice"; | ||
render( | ||
<TestComponent client={client}> | ||
<Avatar | ||
id={member.userId} | ||
name={displayName} | ||
size={96} | ||
src={member.getMxcAvatarUrl()} | ||
/> | ||
</TestComponent>, | ||
); | ||
const element = screen.getByRole("img", { name: "@alice:example.org" }); | ||
expect(element.tagName).toEqual("SPAN"); | ||
expect(client.mxcUrlToHttp).toBeCalledTimes(0); | ||
}); | ||
|
||
test("should just render a placeholder when thumbnails are not supported", () => { | ||
const client = vi.mocked<MatrixClient>({ | ||
getAccessToken: () => "my-access-token", | ||
mxcUrlToHttp: () => vi.fn(), | ||
} as unknown as MatrixClient); | ||
|
||
vi.spyOn(client, "mxcUrlToHttp"); | ||
const member = mockMatrixRoomMember( | ||
mockRtcMembership("@alice:example.org", "AAAA"), | ||
{ | ||
getMxcAvatarUrl: () => "mxc://example.org/alice-avatar", | ||
}, | ||
); | ||
const displayName = "Alice"; | ||
render( | ||
<TestComponent client={client} supportsThumbnails={false}> | ||
<Avatar | ||
id={member.userId} | ||
name={displayName} | ||
size={96} | ||
src={member.getMxcAvatarUrl()} | ||
/> | ||
</TestComponent>, | ||
); | ||
const element = screen.getByRole("img", { name: "@alice:example.org" }); | ||
expect(element.tagName).toEqual("SPAN"); | ||
expect(client.mxcUrlToHttp).toBeCalledTimes(0); | ||
}); | ||
|
||
test("should attempt to fetch authenticated media", async () => { | ||
const expectedAuthUrl = "http://example.org/media/alice-avatar"; | ||
const expectedObjectURL = "my-object-url"; | ||
const accessToken = "my-access-token"; | ||
const theBlob = new Blob([]); | ||
|
||
// vitest doesn't have a implementation of create/revokeObjectURL, so we need | ||
// to delete the property. It's a bit odd, but it works. | ||
Reflect.deleteProperty(global.window.URL, "createObjectURL"); | ||
globalThis.URL.createObjectURL = vi.fn().mockReturnValue(expectedObjectURL); | ||
Reflect.deleteProperty(global.window.URL, "revokeObjectURL"); | ||
globalThis.URL.revokeObjectURL = vi.fn(); | ||
|
||
const fetchFn = vi.fn().mockResolvedValue({ | ||
blob: async () => Promise.resolve(theBlob), | ||
}); | ||
vi.stubGlobal("fetch", fetchFn); | ||
|
||
const client = vi.mocked<MatrixClient>({ | ||
getAccessToken: () => accessToken, | ||
mxcUrlToHttp: () => vi.fn(), | ||
} as unknown as MatrixClient); | ||
|
||
vi.spyOn(client, "mxcUrlToHttp").mockReturnValue(expectedAuthUrl); | ||
const member = mockMatrixRoomMember( | ||
mockRtcMembership("@alice:example.org", "AAAA"), | ||
{ | ||
getMxcAvatarUrl: () => "mxc://example.org/alice-avatar", | ||
}, | ||
); | ||
const displayName = "Alice"; | ||
render( | ||
<TestComponent client={client}> | ||
<Avatar | ||
id={member.userId} | ||
name={displayName} | ||
size={96} | ||
src={member.getMxcAvatarUrl()} | ||
/> | ||
</TestComponent>, | ||
); | ||
|
||
// Fetch is asynchronous, so wait for this to resolve. | ||
await vi.waitUntil(() => | ||
document.querySelector(`img[src='${expectedObjectURL}']`), | ||
); | ||
|
||
expect(client.mxcUrlToHttp).toBeCalledTimes(1); | ||
expect(globalThis.fetch).toBeCalledWith(expectedAuthUrl, { | ||
headers: { Authorization: `Bearer ${accessToken}` }, | ||
}); | ||
}); |
This file contains 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
This file contains 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
This file contains 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