Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
typeofweb committed Nov 16, 2023
1 parent 3153ca0 commit fd7d39e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
12 changes: 0 additions & 12 deletions src/SaleorAuthClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,16 @@ export class SaleorAuthClient {

// the refresh already finished, proceed as normal
if (accessToken && !isExpiredToken(accessToken, this.tokenGracePeriod)) {
console.log("Not expired, proceed");
return this.fetchWithAuth(input, init, additionalParams);
}

this.onAuthRefresh?.(true);

// if the promise is already there, use it
if (this.tokenRefreshPromise) {
console.log("Token refresh promise already exists, wait for it");
const response = await this.tokenRefreshPromise;

const res = (await response.clone().json()) as TokenRefreshResponse;
console.log(res);

const {
errors: graphqlErrors,
Expand All @@ -168,15 +165,11 @@ export class SaleorAuthClient {
const a = this.acessTokenStorage.getAccessToken();

Check failure on line 165 in src/SaleorAuthClient.ts

View workflow job for this annotation

GitHub Actions / build

'a' is assigned a value but never used
this.acessTokenStorage.setAccessToken(token);
const b = this.acessTokenStorage.getAccessToken();

Check failure on line 167 in src/SaleorAuthClient.ts

View workflow job for this annotation

GitHub Actions / build

'b' is assigned a value but never used
console.log(a === b);
console.log(a === token);
console.log(b === token);
this.tokenRefreshPromise = null;
return this.runAuthorizedRequest(input, init, additionalParams);
}

// this is the first failed request, initialize refresh
console.log("Initialize token refresh");
this.tokenRefreshPromise = fetch(this.saleorApiUrl, getRequestData(TOKEN_REFRESH, { refreshToken }));
return this.fetchWithAuth(input, init, additionalParams);
};
Expand Down Expand Up @@ -218,10 +211,8 @@ export class SaleorAuthClient {
*/
fetchWithAuth: FetchWithAdditionalParams = async (input, init, additionalParams) => {
const refreshToken = this.refreshTokenStorage?.getRefreshToken();
console.log({ refreshToken });

let accessToken = this.acessTokenStorage.getAccessToken();
console.log({ accessToken });
if (!accessToken) {
if (typeof document !== "undefined") {
const tokenFromCookie = cookie.parse(document.cookie).token ?? null;
Expand All @@ -235,18 +226,15 @@ export class SaleorAuthClient {

// access token is fine, add it to the request and proceed
if (accessToken && !isExpiredToken(accessToken, this.tokenGracePeriod)) {
console.log("Not expired, proceed");
return this.runAuthorizedRequest(input, init, additionalParams);
}

// refresh token exists, try to authenticate if possible
if (refreshToken) {
console.log("Refresh token exists, try to authenticate");
return this.handleRequestWithTokenRefresh(input, init, additionalParams);
}

// any regular mutation, no previous sign in, proceed
console.log("No refresh token, proceed");
return fetch(input, init);
};

Expand Down
21 changes: 11 additions & 10 deletions test/SaleorAuthClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { it, describe, vi, expect } from "vitest";
import { SaleorAuthClient } from "../src/SaleorAuthClient";
import { getRefreshTokenKey } from "../src/SaleorRefreshTokenStorageHandler";
import type { StorageRepository } from "../src";

describe("SaleorAuthClient", () => {
const mockStorage = {
getItem: vi.fn(),
setItem: vi.fn(),
};
const storage = mockStorage as unknown as Storage;
removeItem: vi.fn(),
} satisfies StorageRepository;
const masterStagingUrl = "https://master.staging.saleor.cloud/graphql/";
const otherApiUrl = "https://some-other-domain-auth-sdk.saleor.cloud/graphql/";

it(`should fetch without authentication token`, async () => {
const onAuthRefresh = vi.fn();
const saleorAuthClient = new SaleorAuthClient({
saleorApiUrl: masterStagingUrl,
storage,
refreshTokenStorage: mockStorage,
onAuthRefresh,
});

Expand All @@ -34,7 +35,7 @@ describe("SaleorAuthClient", () => {
const onAuthRefresh = vi.fn();
const saleorAuthClient = new SaleorAuthClient({
saleorApiUrl: masterStagingUrl,
storage,
refreshTokenStorage: mockStorage,
onAuthRefresh,
});

Expand All @@ -43,7 +44,7 @@ describe("SaleorAuthClient", () => {
if (key === getRefreshTokenKey(masterStagingUrl)) {
return refreshToken;
}
return undefined;
return null;
});

fetchMock.mockResponse(async (req) => {
Expand Down Expand Up @@ -74,7 +75,7 @@ describe("SaleorAuthClient", () => {
const onAuthRefresh = vi.fn();
const saleorAuthClient = new SaleorAuthClient({
saleorApiUrl: masterStagingUrl,
storage,
refreshTokenStorage: mockStorage,
onAuthRefresh,
});

Expand All @@ -83,7 +84,7 @@ describe("SaleorAuthClient", () => {
if (key === getRefreshTokenKey(masterStagingUrl)) {
return refreshToken;
}
return undefined;
return null;
});

fetchMock.mockResponse(async (req) => {
Expand Down Expand Up @@ -114,7 +115,7 @@ describe("SaleorAuthClient", () => {
const onAuthRefresh = vi.fn();
const saleorAuthClient = new SaleorAuthClient({
saleorApiUrl: otherApiUrl,
storage,
refreshTokenStorage: mockStorage,
onAuthRefresh,
});

Expand All @@ -131,7 +132,7 @@ describe("SaleorAuthClient", () => {
const onAuthRefresh = vi.fn();
const saleorAuthClient = new SaleorAuthClient({
saleorApiUrl: masterStagingUrl,
storage,
refreshTokenStorage: mockStorage,
onAuthRefresh,
});

Expand All @@ -140,7 +141,7 @@ describe("SaleorAuthClient", () => {
if (key === getRefreshTokenKey(masterStagingUrl)) {
return refreshToken;
}
return undefined;
return null;
});

fetchMock.mockResponse(async (req) => {
Expand Down

0 comments on commit fd7d39e

Please sign in to comment.