-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
test-local-subscriptions.js
295 lines (270 loc) · 11 KB
/
test-local-subscriptions.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Dialog} from '../dialog';
import {Entitlement, GrantReason} from '../entitlement';
import {LocalSubscriptionPlatform} from '../local-subscription-platform';
import {PageConfig} from '../../../../third_party/subscriptions-project/config';
import {ServiceAdapter} from '../service-adapter';
import {SubscriptionAnalytics} from '../analytics';
describes.fakeWin('LocalSubscriptionsPlatform', {amp: true}, env => {
let ampdoc;
let localSubscriptionPlatform;
let serviceAdapter;
const actionMap = {
'subscribe': 'https://lipsum.com/subscribe',
'login': 'https://lipsum.com/login',
};
const service = 'sample-service';
const source = 'sample-source';
const json = {
service,
source,
granted: true,
grantReason: GrantReason.SUBSCRIBER,
};
const readerId = 'reader1';
const entitlement = Entitlement.parseFromJson(json);
const configAuthUrl = 'https://lipsum.com/login/authorize?rid=READER_ID';
const configPingbackUrl = 'https://lipsum.com/login/pingback?rid=READER_ID';
const serviceConfig = {
'services': [
{
'serviceId': 'local',
'authorizationUrl': configAuthUrl,
'pingbackUrl': configPingbackUrl,
'actions': actionMap,
'baseScore': 99,
},
],
};
const authUrl = configAuthUrl.replace('READER_ID', readerId);
const pingbackUrl = configPingbackUrl.replace('READER_ID', readerId);
beforeEach(() => {
ampdoc = env.ampdoc;
serviceAdapter = new ServiceAdapter(null);
sandbox.stub(serviceAdapter, 'getPageConfig')
.callsFake(() => new PageConfig('example.org:basic', true));
sandbox.stub(serviceAdapter, 'getDialog')
.callsFake(() => new Dialog(ampdoc));
sandbox.stub(serviceAdapter, 'getReaderId')
.callsFake(() => Promise.resolve('reader1'));
localSubscriptionPlatform = new LocalSubscriptionPlatform(ampdoc,
serviceConfig.services[0], serviceAdapter,
new SubscriptionAnalytics(ampdoc.getRootNode()));
});
it('initializeListeners_ should listen to clicks on rootNode', () => {
const domStub = sandbox.stub(localSubscriptionPlatform.rootNode_,
'addEventListener');
localSubscriptionPlatform.initializeListeners_();
expect(domStub).calledOnce;
expect(domStub.getCall(0).args[0])
.to.be.equals('click');
});
it('should return baseScore', () => {
expect(localSubscriptionPlatform.getBaseScore()).to.be.equal(99);
});
it('should fetch the entitlements on getEntitlements', () => {
const fetchStub = sandbox.stub(localSubscriptionPlatform.xhr_, 'fetchJson')
.callsFake(() => Promise.resolve({json: () => Promise.resolve(json)}));
return localSubscriptionPlatform.getEntitlements().then(() => {
expect(fetchStub).to.be.calledOnce;
expect(fetchStub.getCall(0).args[0]).to.be.equals(authUrl);
expect(fetchStub.getCall(0).args[1].credentials)
.to.be.equals('include');
});
});
it('should buildUrl before fetchingAuth', () => {
const builtUrl = 'builtUrl';
const urlBuildingStub = sandbox.stub(localSubscriptionPlatform.urlBuilder_,
'buildUrl').callsFake(() => Promise.resolve(builtUrl));
const fetchStub = sandbox.stub(localSubscriptionPlatform.xhr_,'fetchJson')
.callsFake(() => Promise.resolve({json: () => Promise.resolve(json)}));
return localSubscriptionPlatform.getEntitlements().then(() => {
expect(urlBuildingStub).to.be.calledWith(configAuthUrl, false);
expect(fetchStub).to.be.calledWith(builtUrl, {credentials: 'include'});
});
});
describe('validateActionMap', () => {
let actionMap;
beforeEach(() => {
actionMap = {
'subscribe': 'https://lipsum.com/subscribe',
'login': 'https://lipsum.com/login',
'other': 'https://lipsum.com/other',
};
});
it('should check that login action is present', () => {
delete (actionMap['login']);
expect(localSubscriptionPlatform.validateActionMap, actionMap).to.throw;
});
it('should check that subscribe action is present', () => {
delete (actionMap['subscribe']);
expect(localSubscriptionPlatform.validateActionMap, actionMap).to.throw;
});
it('should return actionMap as is if login and subscribe actions'
+ ' are present', () => {
const returnedMap =
localSubscriptionPlatform.validateActionMap(actionMap);
expect(JSON.stringify(returnedMap)).to.be
.equal(JSON.stringify((actionMap)));
});
});
describe('handleClick_', () => {
let element;
beforeEach(() => {
element = document.createElement('div');
element.setAttribute('subscriptions-action', 'subscribe');
element.setAttribute('subscriptions-service', 'local');
});
// This must be a sync call to avoid Safari popup blocker issues.
it('should call executeAction synchronosly when service is "auto"', () => {
const executeStub = sandbox.stub(localSubscriptionPlatform,
'executeAction');
element.setAttribute('subscriptions-service', 'auto');
localSubscriptionPlatform.handleClick_(element);
expect(executeStub).to.be.calledWith(
element.getAttribute('subscriptions-action'));
});
it('should call executeAction with subscriptions-action value', () => {
const executeStub = sandbox.stub(localSubscriptionPlatform,
'executeAction');
localSubscriptionPlatform.handleClick_(element);
expect(executeStub).to.be.calledWith(
element.getAttribute('subscriptions-action'));
});
it('should delegate action to service specified in '
+ 'subscriptions-service', () => {
const executeStub = sandbox.stub(localSubscriptionPlatform,
'executeAction');
const delegateStub = sandbox.stub(
localSubscriptionPlatform.serviceAdapter_,
'delegateActionToService'
);
element.setAttribute('subscriptions-service', 'swg.google.com');
localSubscriptionPlatform.handleClick_(element);
expect(executeStub).to.not.be.called;
expect(delegateStub).to.be.called;
});
it('should delegate service selection to scoreBasedLogin if no service '
+ 'name is specified for login', () => {
element.setAttribute('subscriptions-action', 'login');
element.removeAttribute('subscriptions-service');
const platform = {};
const serviceId = 'serviceId';
platform.getServiceId = sandbox.stub().callsFake(() => serviceId);
const loginStub = sandbox.stub(
localSubscriptionPlatform.serviceAdapter_,
'selectPlatformForLogin'
).callsFake(() => platform);
const delegateStub = sandbox.stub(
localSubscriptionPlatform.serviceAdapter_,
'delegateActionToService'
);
localSubscriptionPlatform.handleClick_(element);
expect(loginStub).to.be.called;
expect(delegateStub).to.be.calledWith(
'login',
serviceId,
);
});
it('should delegate service selection to scoreBasedLogin '
+ 'service specified is auto for login', () => {
element.setAttribute('subscriptions-action', 'login');
element.setAttribute('subscriptions-service', 'auto');
const loginStub = sandbox.stub(
localSubscriptionPlatform.serviceAdapter_,
'selectPlatformForLogin'
).callsFake(() => platform);
const delegateStub = sandbox.stub(
localSubscriptionPlatform.serviceAdapter_,
'delegateActionToService'
);
const platform = {};
const serviceId = 'serviceId';
platform.getServiceId = sandbox.stub().callsFake(() => serviceId);
localSubscriptionPlatform.handleClick_(element);
expect(loginStub).to.be.called;
expect(delegateStub).to.be.calledWith(
'login',
serviceId,
);
});
it('should NOT delegate for scoreBasedLogin for non-login action', () => {
element.setAttribute('subscriptions-action', 'subscribe');
element.setAttribute('subscriptions-service', 'auto');
const loginStub = sandbox.stub(
localSubscriptionPlatform.serviceAdapter_,
'selectPlatformForLogin');
const executeStub = sandbox.stub(localSubscriptionPlatform,
'executeAction');
const delegateStub = sandbox.stub(
localSubscriptionPlatform.serviceAdapter_,
'delegateActionToService');
const platform = {};
const serviceId = 'serviceId';
platform.getServiceId = sandbox.stub().callsFake(() => serviceId);
localSubscriptionPlatform.handleClick_(element);
expect(loginStub).to.not.be.called;
expect(delegateStub).to.not.be.called;
expect(executeStub).to.be.calledOnce.calledWith('subscribe');
});
});
describe('executeAction', () => {
it('should call executeAction on actions_', () => {
const actionString = 'action';
const executeStub =
sandbox.stub(localSubscriptionPlatform.actions_, 'execute')
.callsFake(() => Promise.resolve(true));
const entitlementsStub = sandbox.stub(
localSubscriptionPlatform.serviceAdapter_,
'reAuthorizePlatform');
localSubscriptionPlatform.executeAction(actionString);
expect(executeStub).to.be.calledWith(actionString);
return executeStub().then(() => {
expect(entitlementsStub).to.be.calledOnce;
});
});
});
describe('render', () => {
it('should call renderer\'s render method', () => {
const renderStub =
sandbox.stub(localSubscriptionPlatform.renderer_, 'render');
localSubscriptionPlatform.activate(entitlement);
return localSubscriptionPlatform.actions_.build().then(() => {
expect(renderStub).to.be.calledOnce;
});
});
it('should reset renderer\'s on reset', () => {
const resetStub =
sandbox.stub(localSubscriptionPlatform.renderer_, 'reset');
localSubscriptionPlatform.reset();
expect(resetStub).to.be.calledOnce;
});
});
describe('pingback', () => {
it('should call `sendSignal` to the pingback signal', () => {
const sendSignalStub =
sandbox.stub(localSubscriptionPlatform.xhr_, 'sendSignal');
return localSubscriptionPlatform.pingback(entitlement).then(() => {
expect(sendSignalStub).to.be.calledOnce;
expect(sendSignalStub.getCall(0).args[0]).to.be.equal(
pingbackUrl);
expect(sendSignalStub.getCall(0).args[1].body).to.equal(
JSON.stringify(entitlement.jsonForPingback()));
});
});
});
});