Skip to content

Commit

Permalink
fixup! Wait for URL update on redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
NSeydoux committed Oct 2, 2023
1 parent 0302f79 commit ebf09a9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
49 changes: 33 additions & 16 deletions packages/browser/src/ClientAuthentication.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,12 @@ describe("ClientAuthentication", () => {
// TODO: add tests for events & errors

it("reverts back to un-authenticated fetch on logout", async () => {
// eslint-disable-next-line no-restricted-globals
history.replaceState = jest.fn();
window.history.replaceState = jest.fn();
window.addEventListener = jest
.fn()
.mockImplementation((_event: unknown, callback: unknown) => {
(callback as () => void)();
});
const clientAuthn = getClientAuthentication();

const unauthFetch = clientAuthn.fetch;
Expand Down Expand Up @@ -402,8 +406,12 @@ describe("ClientAuthentication", () => {
mockEmitter.emit = jest.fn<typeof mockEmitter.emit>();

it("calls handle redirect", async () => {
// eslint-disable-next-line no-restricted-globals
history.replaceState = jest.fn();
window.history.replaceState = jest.fn();
window.addEventListener = jest
.fn()
.mockImplementation((_event: unknown, callback: unknown) => {
(callback as () => void)();
});
const expectedResult = SessionCreatorCreateResponse;
const clientAuthn = getClientAuthentication();
const unauthFetch = clientAuthn.fetch;
Expand Down Expand Up @@ -456,8 +464,12 @@ describe("ClientAuthentication", () => {
});

it("clears the current IRI from OAuth query parameters even if auth flow fails", async () => {
// eslint-disable-next-line no-restricted-globals
history.replaceState = jest.fn();
window.history.replaceState = jest.fn();
window.addEventListener = jest
.fn()
.mockImplementation((_event: unknown, callback: unknown) => {
(callback as () => void)();
});

mockHandleIncomingRedirect.mockImplementationOnce(() =>
Promise.reject(new Error("Something went wrong")),
Expand All @@ -468,8 +480,7 @@ describe("ClientAuthentication", () => {
const url =
"https://coolapp.com/redirect?state=someState&code=someAuthCode";
await clientAuthn.handleIncomingRedirect(url, mockEmitter);
// eslint-disable-next-line no-restricted-globals
expect(history.replaceState).toHaveBeenCalledWith(
expect(window.history.replaceState).toHaveBeenCalledWith(
null,
"",
"https://coolapp.com/redirect",
Expand All @@ -483,14 +494,17 @@ describe("ClientAuthentication", () => {
});

it("clears the current IRI from OAuth query parameters in the implicit flow", async () => {
// eslint-disable-next-line no-restricted-globals
history.replaceState = jest.fn();
window.history.replaceState = jest.fn();
window.addEventListener = jest
.fn()
.mockImplementation((_event: unknown, callback: unknown) => {
(callback as () => void)();
});
const clientAuthn = getClientAuthentication();
const url =
"https://coolapp.com/redirect?state=someState&id_token=idToken&access_token=accessToken";
await clientAuthn.handleIncomingRedirect(url, mockEmitter);
// eslint-disable-next-line no-restricted-globals
expect(history.replaceState).toHaveBeenCalledWith(
expect(window.history.replaceState).toHaveBeenCalledWith(
null,
"",
"https://coolapp.com/redirect",
Expand All @@ -499,14 +513,17 @@ describe("ClientAuthentication", () => {
});

it("preserves non-OAuth query strings", async () => {
// eslint-disable-next-line no-restricted-globals
history.replaceState = jest.fn();
window.history.replaceState = jest.fn();
window.addEventListener = jest
.fn()
.mockImplementation((_event: unknown, callback: unknown) => {
(callback as () => void)();
});
const clientAuthn = getClientAuthentication();
const url =
"https://coolapp.com/redirect?state=someState&code=someAuthCode&someQuery=someValue";
await clientAuthn.handleIncomingRedirect(url, mockEmitter);
// eslint-disable-next-line no-restricted-globals
expect(history.replaceState).toHaveBeenCalledWith(
expect(window.history.replaceState).toHaveBeenCalledWith(
null,
"",
"https://coolapp.com/redirect?someQuery=someValue",
Expand Down
23 changes: 10 additions & 13 deletions packages/browser/src/Session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,9 @@ describe("Session", () => {
it("uses current window location as default redirect URL", async () => {
mockLocation("https://some.url");
const clientAuthentication = mockClientAuthentication();
const incomingRedirectHandler = jest.spyOn(
clientAuthentication,
"handleIncomingRedirect",
);
const incomingRedirectHandler = jest
.spyOn(clientAuthentication, "handleIncomingRedirect")
.mockResolvedValue(undefined);

const mySession = new Session({ clientAuthentication });
await mySession.handleIncomingRedirect();
Expand All @@ -259,13 +258,12 @@ describe("Session", () => {
);
});

it("wraps up ClientAuthentication handleIncomingRedirect", async () => {
it("wraps ClientAuthentication handleIncomingRedirect", async () => {
mockLocation("https://some.url");
const clientAuthentication = mockClientAuthentication();
const incomingRedirectHandler = jest.spyOn(
clientAuthentication,
"handleIncomingRedirect",
);
const incomingRedirectHandler = jest
.spyOn(clientAuthentication, "handleIncomingRedirect")
.mockResolvedValue(undefined);
const mySession = new Session({ clientAuthentication });
await mySession.handleIncomingRedirect("https://some.url");
expect(incomingRedirectHandler).toHaveBeenCalled();
Expand Down Expand Up @@ -401,10 +399,9 @@ describe("Session", () => {

it("preserves a binding to its Session instance", async () => {
const clientAuthentication = mockClientAuthentication();
const incomingRedirectHandler = jest.spyOn(
clientAuthentication,
"handleIncomingRedirect",
);
const incomingRedirectHandler = jest
.spyOn(clientAuthentication, "handleIncomingRedirect")
.mockResolvedValue(undefined);
const mySession = new Session({ clientAuthentication });
const objectWithHandleIncomingRedirect = {
handleIncomingRedirect: mySession.handleIncomingRedirect,
Expand Down

0 comments on commit ebf09a9

Please sign in to comment.