This repository has been archived by the owner on Jul 14, 2022. It is now read-only.
/
shopifyAuth.test.js
99 lines (79 loc) · 2.52 KB
/
shopifyAuth.test.js
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
const findFreePort = require('find-free-port')
const fetch = require('node-fetch');
const http = require('http');
const express = require('express');
const { MemoryStrategy } = require('../../strategies');
const createShopifyAuthRoutes = require('../shopifyAuth');
const PORT = 3000;
const BASE_URL = `http://localhost:${PORT}`
let server;
let afterAuth;
describe('shopifyAuth', async () => {
beforeEach(async () => {
afterAuth = jest.fn();
server = await createServer({afterAuth});
});
afterEach(() => {
server.close();
});
describe('/', () => {
it('responds to get requests by returning a redirect page', async () => {
const response = await fetch(`${BASE_URL}/auth?shop=shop1`);
const data = await response.text();
expect(response.status).toBe(200);
expect(data).toMatchSnapshot();
});
it('redirect page includes per-user grant for accessMode: online', async () => {
await server.close();
server = await createServer({accessMode: 'online'});
const response = await fetch(`${BASE_URL}/auth?shop=shop1`);
const data = await response.text();
expect(response.status).toBe(200);
expect(data).toContain('grant_options%5B%5D=per-user');
});
it('responds with a 400 when no shop query parameter is given', async () => {
const response = await fetch(`${BASE_URL}/auth`);
const data = await response.text();
expect(response.status).toBe(400);
expect(data).toMatchSnapshot();
});
});
describe('/callback', () => {
it('errors when hmac validation fails', () => {
pending();
});
it('does not error when hmac validation succeds', () => {
pending();
});
it('requests access token', () => {
pending();
});
it('console warns when no session is present on request context', () => {
pending();
});
});
});
function createServer(userConfig = {}) {
const app = express();
const serverConfig = {
host: 'http://myshop.myshopify.com',
apiKey: 'key',
secret: 'secret',
scope: ['scope'],
shopStore: new MemoryStrategy(),
accessMode: 'offline',
afterAuth: jest.fn(),
};
const {auth, callback} = createShopifyAuthRoutes(Object.assign({}, serverConfig, userConfig));
app.use('/auth', auth);
app.use('/auth/callback', callback);
server = http.createServer(app);
return new Promise((resolve, reject) => {
findFreePort(PORT, (err, freePort) => {
if (err) {
throw err;
}
server.listen(PORT, resolve(server));
})
});
}