Skip to content

Commit

Permalink
Mock rest client instead of request due to library overlap
Browse files Browse the repository at this point in the history
  • Loading branch information
mllemango committed Apr 15, 2021
1 parent 77b01ec commit 3f60063
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions src/verify-request/tests/verify-request.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import '../../test/test_helper';

import {createMockContext} from '@shopify/jest-koa-mocks';
import {fetch} from '@shopify/jest-dom-mocks';
import {StatusCode} from '@shopify/network';
import Shopify from '@shopify/shopify-api';
import Shopify, { RequestReturn } from '@shopify/shopify-api';
import jwt from 'jsonwebtoken';

import verifyRequest from '../verify-request';
import {clearSession} from '../utilities';
import {TEST_COOKIE_NAME, TOP_LEVEL_OAUTH_COOKIE_NAME} from '../../index';
import {REAUTH_HEADER, REAUTH_URL_HEADER} from '../verify-token';
import { clear } from 'console';

const TEST_SHOP = 'testshop.myshopify.io';
const TEST_USER = '1';

describe('verifyRequest', () => {
afterEach(fetch.restore);

describe('when there is an accessToken and shop in session', () => {
let jwtToken: string;
Expand All @@ -43,6 +40,8 @@ describe('verifyRequest', () => {
session.accessToken = 'test_token';
session.scope = 'test_scope';
await Shopify.Utils.storeSession(session);

Shopify.Clients.Rest.prototype.get = jest.fn(() => Promise.resolve({ "body": "" } as RequestReturn));
});

it('calls next', async () => {
Expand All @@ -53,7 +52,6 @@ describe('verifyRequest', () => {
} as any);
const next = jest.fn();

fetch.mock(metaFieldsUrl(TEST_SHOP), StatusCode.Ok);
await verifyRequestMiddleware(ctx, next);

expect(next).toHaveBeenCalled();
Expand All @@ -75,7 +73,6 @@ describe('verifyRequest', () => {
} as any);
const next = jest.fn();

fetch.mock(metaFieldsUrl(TEST_SHOP), StatusCode.Ok);
await verifyRequestMiddleware(ctx, next);

expect(next).toHaveBeenCalled();
Expand All @@ -89,7 +86,6 @@ describe('verifyRequest', () => {
});
const next = jest.fn();

fetch.mock(metaFieldsUrl(TEST_SHOP), StatusCode.Ok);
await verifyRequestMiddleware(ctx, next);

expect(next).toHaveBeenCalled();
Expand All @@ -103,7 +99,6 @@ describe('verifyRequest', () => {
});
const next = jest.fn();

fetch.mock(metaFieldsUrl(TEST_SHOP), StatusCode.Ok);
await verifyRequestMiddleware(ctx, next);

expect(ctx.cookies.set).toHaveBeenCalledWith(TOP_LEVEL_OAUTH_COOKIE_NAME);
Expand Down Expand Up @@ -139,7 +134,6 @@ describe('verifyRequest', () => {
headers: { authorization: `Bearer ${jwtToken}` }
});

fetch.mock(metaFieldsUrl(TEST_SHOP), StatusCode.Ok);
await verifyRequestMiddleware(ctx, next);

expect(ctx.redirect).toHaveBeenCalledWith(
Expand All @@ -162,7 +156,6 @@ describe('verifyRequest', () => {
headers: { authorization: `Bearer ${jwtToken}` }
});

fetch.mock(metaFieldsUrl(TEST_SHOP), StatusCode.Ok);
await verifyRequestMiddleware(ctx, next);

expect(ctx.redirect).toHaveBeenCalledWith(
Expand Down Expand Up @@ -321,10 +314,6 @@ describe('verifyRequest', () => {
});
});

function metaFieldsUrl(shop: string) {
return `https://${shop}/admin/metafields.json`;
}

function appUrl(shop?: string) {
return shop == null ? '/foo' : `/foo?shop=${shop}`;
}
Expand Down

0 comments on commit 3f60063

Please sign in to comment.