/
authLinks.spec.ts
150 lines (127 loc) · 4.73 KB
/
authLinks.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { handleBeforeAuth, handleAfterAuth } from '../../src/links/authLinks';
const getSdkAuth = (scope) => ({
anonymousFlow: jest.fn().mockImplementation(() => ({ scope, access_token: 'GUEST_TOKEN' })),
customerPasswordFlow: jest.fn().mockImplementation(() => ({ scope, access_token: 'LOGIN_TOKEN' }))
});
const getTokenProvider = (scope) => ({
setTokenInfo: jest.fn().mockImplementation(() => {}),
getTokenInfo: jest.fn().mockImplementation(() => ({ scope, access_token: 'ACCESS_TOKEN' })),
invalidateTokenInfo: jest.fn().mockImplementation(() => {})
});
describe('[commercetools-helpers] handleBeforeAuth', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('doesnt generate access token for guest on users related operations', async () => {
const scope = '';
const result = await handleBeforeAuth({
configuration: {},
sdkAuth: getSdkAuth(scope),
tokenProvider: getTokenProvider(scope),
apolloReq: { operationName: 'customerSignMeIn' }
});
expect(result).toMatchObject({ access_token: 'ACCESS_TOKEN' });
});
it('generates access token for guest on anonymous-session allowed operations', async () => {
const scope = '';
const result = await handleBeforeAuth({
configuration: {},
sdkAuth: getSdkAuth(scope),
tokenProvider: getTokenProvider(scope),
apolloReq: { operationName: 'createCart' }
});
expect(result).toMatchObject({ access_token: 'GUEST_TOKEN' });
});
it('returns current token for anonymous user', async () => {
const scope = 'anonymous_id';
const result = await handleBeforeAuth({
configuration: {},
sdkAuth: getSdkAuth(scope),
tokenProvider: getTokenProvider(scope),
apolloReq: { operationName: 'customerSignMeIn' }
});
expect(result).toMatchObject({ scope, access_token: 'ACCESS_TOKEN' });
});
it('returns current token for logged in user', async () => {
const scope = 'customer_id';
const result = await handleBeforeAuth({
configuration: {},
sdkAuth: getSdkAuth(scope),
tokenProvider: getTokenProvider(scope),
apolloReq: { operationName: 'customerSignMeIn' }
});
expect(result).toMatchObject({ scope, access_token: 'ACCESS_TOKEN' });
});
});
it('returns token from customToken handler', async () => {
const token = {
access_token: 'CUSTOM_TOKEN',
scope: 'CUSTOM_SCOPE'
};
const customToken = jest.fn().mockImplementation(() => token);
const result = await handleBeforeAuth({
configuration: {
customToken
},
sdkAuth: getSdkAuth(token.scope),
tokenProvider: getTokenProvider(token.scope),
apolloReq: { operationName: 'customerSignMeIn' }
});
expect(customToken).toBeCalled();
expect(result).toMatchObject(token);
});
describe('[commercetools-helpers] handleAfterAuth', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('doesnt fetch access token for non-user related operations', async () => {
const scope = '';
const result = await handleAfterAuth({
sdkAuth: getSdkAuth(scope),
tokenProvider: getTokenProvider(scope),
apolloReq: { operationName: 'createCart' },
response: { errors: [] }
});
expect(result).toMatchObject({ scope });
});
it('doesnt fetch access token for logged in user', async () => {
const scope = 'customer_id';
const result = await handleAfterAuth({
sdkAuth: getSdkAuth(scope),
tokenProvider: getTokenProvider(scope),
apolloReq: { operationName: 'customerSignMeIn' },
response: { errors: [] }
});
expect(result).toMatchObject({ scope });
});
it('fetches access token for anonymous session', async () => {
const scope = 'anonymous_id';
const sdkAuth = getSdkAuth(scope);
const result = await handleAfterAuth({
sdkAuth,
tokenProvider: getTokenProvider(scope),
apolloReq: {
operationName: 'customerSignMeIn',
variables: { draft: { email: 'EMAIL', password: 'PASSWORD' } }
},
response: { errors: [] }
});
expect(result).toMatchObject({ scope, access_token: 'LOGIN_TOKEN' });
expect(sdkAuth.customerPasswordFlow).toBeCalledWith({ username: 'EMAIL', password: 'PASSWORD' });
});
it('fetches access token for guest', async () => {
const scope = '';
const sdkAuth = getSdkAuth(scope);
const result = await handleAfterAuth({
sdkAuth,
tokenProvider: getTokenProvider(scope),
apolloReq: {
operationName: 'customerSignMeIn',
variables: { draft: { email: 'EMAIL', password: 'PASSWORD' } }
},
response: { errors: [] }
});
expect(result).toMatchObject({ scope, access_token: 'LOGIN_TOKEN' });
expect(sdkAuth.customerPasswordFlow).toBeCalledWith({ username: 'EMAIL', password: 'PASSWORD' });
});
});