|
| 1 | +import fetchMock from "fetch-mock-jest"; |
| 2 | + |
| 3 | +import { ClientPrefix, MatrixClient } from "../../src"; |
1 | 4 | import { SSOAction } from "../../src/@types/auth"; |
2 | 5 | import { TestClient } from "../TestClient"; |
3 | 6 |
|
| 7 | +function createExampleMatrixClient(): MatrixClient { |
| 8 | + return new MatrixClient({ |
| 9 | + baseUrl: "https://example.com", |
| 10 | + }); |
| 11 | +} |
| 12 | + |
4 | 13 | describe("Login request", function () { |
5 | 14 | let client: TestClient; |
6 | 15 |
|
@@ -57,3 +66,84 @@ describe("SSO login URL", function () { |
57 | 66 | }); |
58 | 67 | }); |
59 | 68 | }); |
| 69 | + |
| 70 | +describe("refreshToken", () => { |
| 71 | + afterEach(() => { |
| 72 | + fetchMock.mockReset(); |
| 73 | + }); |
| 74 | + |
| 75 | + it("requests the correctly-prefixed /refresh endpoint when server correctly accepts /v3", async () => { |
| 76 | + const client = createExampleMatrixClient(); |
| 77 | + |
| 78 | + const response = { |
| 79 | + access_token: "access_token", |
| 80 | + refresh_token: "refresh_token", |
| 81 | + expires_in_ms: 30000, |
| 82 | + }; |
| 83 | + |
| 84 | + fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V3).toString(), response); |
| 85 | + fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V1).toString(), () => { |
| 86 | + throw new Error("/v1/refresh unexpectedly called"); |
| 87 | + }); |
| 88 | + |
| 89 | + const refreshResult = await client.refreshToken("initial_refresh_token"); |
| 90 | + expect(refreshResult).toEqual(response); |
| 91 | + }); |
| 92 | + |
| 93 | + it("falls back to /v1 when server does not recognized /v3 refresh", async () => { |
| 94 | + const client = createExampleMatrixClient(); |
| 95 | + |
| 96 | + const response = { |
| 97 | + access_token: "access_token", |
| 98 | + refresh_token: "refresh_token", |
| 99 | + expires_in_ms: 30000, |
| 100 | + }; |
| 101 | + |
| 102 | + fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V3).toString(), { |
| 103 | + status: 400, |
| 104 | + body: { errcode: "M_UNRECOGNIZED" }, |
| 105 | + }); |
| 106 | + fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V1).toString(), response); |
| 107 | + |
| 108 | + const refreshResult = await client.refreshToken("initial_refresh_token"); |
| 109 | + expect(refreshResult).toEqual(response); |
| 110 | + }); |
| 111 | + |
| 112 | + it("re-raises M_UNRECOGNIZED exceptions from /v1", async () => { |
| 113 | + const client = createExampleMatrixClient(); |
| 114 | + |
| 115 | + fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V3).toString(), { |
| 116 | + status: 400, |
| 117 | + body: { errcode: "M_UNRECOGNIZED" }, |
| 118 | + }); |
| 119 | + fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V1).toString(), { |
| 120 | + status: 400, |
| 121 | + body: { errcode: "M_UNRECOGNIZED" }, |
| 122 | + }); |
| 123 | + |
| 124 | + expect(client.refreshToken("initial_refresh_token")).rejects.toMatchObject({ errcode: "M_UNRECOGNIZED" }); |
| 125 | + }); |
| 126 | + |
| 127 | + it("re-raises non-M_UNRECOGNIZED exceptions from /v3", async () => { |
| 128 | + const client = createExampleMatrixClient(); |
| 129 | + |
| 130 | + fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V3).toString(), 429); |
| 131 | + fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V1).toString(), () => { |
| 132 | + throw new Error("/v1/refresh unexpectedly called"); |
| 133 | + }); |
| 134 | + |
| 135 | + expect(client.refreshToken("initial_refresh_token")).rejects.toMatchObject({ httpStatus: 429 }); |
| 136 | + }); |
| 137 | + |
| 138 | + it("re-raises non-M_UNRECOGNIZED exceptions from /v1", async () => { |
| 139 | + const client = createExampleMatrixClient(); |
| 140 | + |
| 141 | + fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V3).toString(), { |
| 142 | + status: 400, |
| 143 | + body: { errcode: "M_UNRECOGNIZED" }, |
| 144 | + }); |
| 145 | + fetchMock.postOnce(client.http.getUrl("/refresh", undefined, ClientPrefix.V1).toString(), 429); |
| 146 | + |
| 147 | + expect(client.refreshToken("initial_refresh_token")).rejects.toMatchObject({ httpStatus: 429 }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments