forked from nestjs-shopify/nestjs-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-shop-from-request.util.spec.ts
114 lines (93 loc) · 2.92 KB
/
get-shop-from-request.util.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { JwtPayload, Session } from '@shopify/shopify-api';
import { getShopFromRequest } from '../../src/utils/get-shop-from-request.util';
import * as decodeUtil from '../../src/utils/decode-session-token.util';
import { ExecutionContext } from '@nestjs/common';
import { ShopifyHttpAdapter } from '@nestjs-shopify/core';
jest.mock('../../src/utils/decode-session-token.util', () => ({
__esModule: true,
decodeSessionToken: jest.fn(),
}));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let req: Record<string, any> = {};
const mockCtx = {
switchToHttp: () => ({
getRequest: () => req,
}),
} as ExecutionContext;
const mockShopifyHttpAdapter = {
getHeaderFromExecutionContext: (_: unknown, header: string) =>
req['headers'][header],
getQueryParamFromExecutionContext: (_: unknown, query: string) =>
req['query'][query],
} as unknown as ShopifyHttpAdapter;
describe('getShopFromRequest', () => {
afterEach(() => {
jest.resetAllMocks();
});
describe('when standalone app', () => {
const session = undefined;
it('returns undefined when query param is missing', () => {
req = {
headers: {},
url: '/',
query: {},
};
expect(
getShopFromRequest(mockCtx, mockShopifyHttpAdapter, session),
).toBeUndefined();
});
it('returns shop from query param', () => {
req = {
headers: {},
url: '/?shop=test.myshopify.io',
query: {
shop: 'test.myshopify.io',
},
};
expect(
getShopFromRequest(mockCtx, mockShopifyHttpAdapter, session),
).toEqual('test.myshopify.io');
});
});
describe('when embedded app', () => {
beforeEach(() => {
req = {
headers: {
authorization: 'Bearer token',
},
query: {},
url: '/',
};
});
it('should return shop from session if given', () => {
const shop = getShopFromRequest(mockCtx, mockShopifyHttpAdapter, {
shop: 'test2.myshopify.io',
} as Session);
expect(shop).toEqual('test2.myshopify.io');
});
it('should return shop from auth header', () => {
jest.mocked(decodeUtil).decodeSessionToken.mockReturnValueOnce({
dest: 'https://test3.myshopify.io',
} as JwtPayload);
const shop = getShopFromRequest(
mockCtx,
mockShopifyHttpAdapter,
undefined,
);
expect(shop).toEqual('test3.myshopify.io');
expect(decodeUtil.decodeSessionToken).toHaveBeenCalledWith('token');
});
it('should return undefined if decoding fails', () => {
jest.mocked(decodeUtil).decodeSessionToken.mockImplementationOnce(() => {
throw new Error();
});
const shop = getShopFromRequest(
mockCtx,
mockShopifyHttpAdapter,
undefined,
);
expect(shop).toBeUndefined();
expect(decodeUtil.decodeSessionToken).toHaveBeenCalledWith('token');
});
});
});