-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): auth refresh tokens (#4499)
* feat(auth): refresh for relay * add more tests
- Loading branch information
1 parent
fe5a1e3
commit 6b98a2d
Showing
11 changed files
with
583 additions
and
46 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,59 @@ | ||
import { authFetch } from "@phoenix/authFetch"; | ||
jest.mock("@phoenix/config"); | ||
|
||
describe("authFetch", () => { | ||
const _fetch = global.fetch; | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterEach(() => { | ||
global.fetch = _fetch; | ||
}); | ||
it("should call fetch with the provided input and init", async () => { | ||
// @ts-expect-error mock global fetch | ||
global.fetch = jest.fn(() => | ||
Promise.resolve({ | ||
json: () => Promise.resolve({ data: "12345" }), | ||
}) | ||
); | ||
|
||
const response = await authFetch("/test", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ test: "test" }), | ||
}).then((res) => res.json()); | ||
|
||
expect(response).toEqual({ data: "12345" }); | ||
}); | ||
it("should try to refresh the tokens if it gets a 401", async () => { | ||
let count = 0; | ||
// @ts-expect-error mock global fetch | ||
global.fetch = jest.fn(() => { | ||
count += 1; | ||
return Promise.resolve({ | ||
status: count === 1 ? 401 : 200, | ||
ok: count === 1 ? false : true, | ||
json: () => Promise.resolve(), | ||
}); | ||
}); | ||
|
||
await authFetch("/test", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ test: "test" }), | ||
}); | ||
|
||
expect(count).toBe(3); | ||
// @ts-expect-error mock global fetch | ||
expect(global.fetch.mock.calls[0][0]).toBe("/test"); | ||
// @ts-expect-error mock global fetch | ||
expect(global.fetch.mock.calls[1][0]).toBe("http://localhost/auth/refresh"); | ||
// @ts-expect-error mock global fetch | ||
expect(global.fetch.mock.calls[2][0]).toBe("/test"); | ||
}); | ||
}); |
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
Oops, something went wrong.