-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.js
326 lines (280 loc) · 10.6 KB
/
config.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import config from 'wnyc-web-client/config/environment';
import { Response, faker } from 'ember-cli-mirage';
import { get } from '@ember/object';
// Mirage is diabled by default when using --proxy
// In development (without --proxy) and test environments, these handlers will be used
// Note for future people: schema.modelName.create() doesn't generate attributes in mirage factories. Create the objects using server.create in default.js (for local dev), or in the test
export const getPropertyCaseInsensitive = function(obj, prop) {
prop = prop.toLowerCase();
for (let p in obj) {
if (obj.hasOwnProperty(p) && prop === p.toLowerCase()) {
return obj[p];
}
}
return undefined;
};
const hasAuthToken = function(request) {
let authToken = getPropertyCaseInsensitive(request.requestHeaders, 'Authorization');
return /bearer \w+/i.test(authToken);
};
const hasProvider = function(request) {
let providerHeader = getPropertyCaseInsensitive(request.requestHeaders, 'X-Provider');
return (providerHeader !== undefined);
};
export default function() {
this.logging = false;
/*------------------------------------------------------------
legacy (v1) endpoints
--------------------------------------------------------------*/
this.get(config.etagAPI, {success: true});
this.urlPrefix = config.publisherAPI;
this.get('/v1/story/:id');
this.get('/v1/list/comments/24/:storyId/', 'comment');
this.get('/v1/whats_on/');
this.get('/v1/whats_on/:slug', 'whats-on');
this.get('/v1/list/streams/');
this.get('/v1/list/streams/:slug', 'stream');
this.get('/v1/story/:slug', function(schema, request) { // backbone makes this ajax request to the audio
let results = schema.discoverStories.all().models.filter(function(d) {
return d.id === request.params.slug;
});
return {
count: 1,
expires: "2020-06-10T14:10:32",
results: results
};
});
this.get('/v1/discover/topics', function(schema) {
let links = schema.discoverTopics.all().models.map(function(t) {
return {
url: t.url,
title: t.title
};
});
return {
links: links
};
});
this.post('/v1/listenaction/create/:id/play/', {});
this.post('/v1/listenaction/create/:id/complete/', {});
this.post('/most/view/managed_item/:id/', {});
this.post('/most/listen/managed_item/:id/', {});
/*------------------------------------------------------------
transitional (v2) endpoints
--------------------------------------------------------------*/
this.get('/v2/related/:storyId/', 'story');
/*------------------------------------------------------------
JSON:API (v3) endpoints
--------------------------------------------------------------*/
this.get('/v3/shows', 'show');
this.get(`/v3/shows/:slug/`, ({ shows }, { params }) => {
let { slug } = params;
return shows.where({ slug }).models[0];
});
this.get('/v3/buckets/:slug/', 'bucket');
this.get('/v3/story-pk/:id/', 'story');
this.get('/v3/story/:slug/', ({ stories }, { params }) => {
let { slug } = params;
return stories.where({ slug }).models[0];
});
this.get('/v3/story/related/', 'story');
this.get('/v3/channel/*id', ({ apiResponses, listingPages }, { params }) => {
let { id } = params;
if (id.split('/').length === 2) {
// listing-page
return listingPages.find(id);
} else {
// api response object
return apiResponses.find(id);
}
});
this.get('/v3/chunks/:id/', 'chunk');
let discoverPath = 'make_playlist';
this.get(`/v3/${discoverPath}`, 'discover-story');
/*------------------------------------------------------------
identity management (account) endpoints
--------------------------------------------------------------*/
this.urlPrefix = config.adminRoot;
this.get('/comments/security_info/', {security_hash: 'foo', timestamp: Date.now()});
this.get('/api/v1/is_logged_in/', {});
this.post('/api/v1/accounts/logout/', {successful_logout: true});
this.post('/api/v1/accounts/login/', function(schema, request) {
let params = {};
request.requestBody.split('&').forEach(p => {
params[p.split('=')[0]] = p.split('=')[1];
});
if (params.username === 'foo' && params.password === 'bar') {
return {success: true};
} else {
let errors = {};
Object.keys(params).forEach(p => {
errors[p] = 'This field is required';
});
return { errors };
}
});
/*------------------------------------------------------------
gothamist
--------------------------------------------------------------*/
this.get(config.gothamistStories, {entries: []})
/*------------------------------------------------------------
passthroughs
--------------------------------------------------------------*/
this.urlPrefix = ''
// Let this one slip by, we've got a http-proxy for it
this.passthrough('/api/v1/dynamic-script-loader');
this.passthrough(`${config.publisherAPI}/api/v1/dynamic-script-loader`);
this.passthrough(`${config.publisherAPI}/api/v1/schedule/whats_on_today/*`);
this.passthrough('/datanewswidget/**');
/*------------------------------------------------------------
${webRoot}/* requests. Oddballs without the api namespace
--------------------------------------------------------------*/
this.get(`${config.webRoot}`, function(schema) {
let home = schema.djangoPages.find('/');
return home ? home.attrs.text : '';
});
this.get(`${config.webRoot}/*id`, function(schema, {queryParams, params}) {
let { id } = params;
let page = schema.djangoPages.find(id);
if (!page) {
// try with queryParams
id += '?' + Object.keys(queryParams)
.map(p => `${p}=${queryParams[p]}`).join('&');
page = schema.djangoPages.find(id);
}
return page || new Response(404);
});
/*-------------------------------------------------------------
auth microservice
---------------------------------------------------------------*/
this.urlPrefix = config.authAPI;
this.post('/v1/password', {});
this.get('/v1/session', ({users}, request) => {
if (!request.requestHeaders.Authorization && !request.requestHeaders.authorization) {
return new Response(401);
}
return users.first();
});
this.post('/v1/session', {access_token: 'secret', expires_in: 3600, token_type: 'bearer'});
this.put('/v1/session', {access_token: 'secret', expires_in: 3600, token_type: 'bearer'});
this.delete('/v1/session', {});
this.post('/v1/user', ({users}, request) => {
let body = JSON.parse(request.requestBody);
if (request.requestHeaders['X-Provider']) {
let fbUser = users.create({
email: body.email || faker.internet.email(),
given_name: body.given_name || faker.name.firstName(),
family_name: body.family_name || faker.name.lastName(),
preferred_username: body.preferred_username || faker.name.firstName() + faker.name.firstName(),
facebook_id: body.facebook_id,
picture: body.picture
});
return fbUser;
}
return users.first();
});
this.patch('/v1/user', (schema, request) => {
if (!request.requestHeaders.Authorization && !request.requestHeaders.authorization) {
return new Response(401);
}
let user = schema.users.first();
if (!user) {
return new Response(500, {error: {code: 'BadTest', message: 'No users found'}});
}
return user.update(JSON.parse(request.requestBody));
});
this.delete('/v1/user', () => new Response(204));
this.get('/v1/user/exists-by-attribute', {username: ''});
let userNotFoundException = {
"errors": {
"code": "UserNotFoundException",
"message": "Username/client id combination not found.",
"values": ["email"]
}
};
this.get('/v1/confirm/sign-up', (schema, request) => {
if (!request.queryParams.username || request.queryParams.username === "null") {
return new Response(400, {}, userNotFoundException);
} else {
return new Response(200);
}
});
let expiredCodeException = {
"errors": {
"code": "ExpiredCodeException",
"message": "Invalid code provided, please request a code again.",
"values": ["email"]
}
};
this.post('/v1/confirm/password-reset', (schema, request) => {
let params = JSON.parse(request.requestBody);
if (!params.confirmation || params.confirmation === "null") {
return new Response(400, {}, expiredCodeException);
} else {
return new Response(200);
}
});
let unauthorizedAccessException = {
"errors": {
"code": "UnauthorizedAccess",
"message": "User account has expired, it must be reset by an administrator."
}
};
this.post('/v1/password/change-temp', (schema, request) => {
let params = JSON.parse(request.requestBody);
if (!params.temp || params.temp === "expired") {
return new Response(401, {}, unauthorizedAccessException);
} else {
return new Response(200);
}
});
this.post('/v1/password/send-temp', (schema, request) => {
if (!request.requestHeaders.Authorization && !request.requestHeaders.authorization) {
return new Response(401);
}
return new Response(200);
});
this.get('/v1/confirm/resend-attr', (schema, request) => {
if (hasAuthToken(request) && !hasProvider(request)) {
return new Response(200);
} else {
return new Response(401);
}
});
/*-------------------------------------------------------------
analytics microservice
---------------------------------------------------------------*/
this.urlPrefix = config.platformEventsAPI;
this.post('/v1/events/viewed', {});
this.post('/v1/events/listened', {});
/*-------------------------------------------------------------
membership microservice
---------------------------------------------------------------*/
this.urlPrefix = config.membershipAPI;
this.get('/v1/orders/', 'orders');
this.get('/v1/emails/is-verified/', {data: {is_verified: true}});
this.patch('/v1/emails/:email_id/verify/', (schema, request) => {
let params = JSON.parse(request.requestBody);
if (params &&
get(params, 'data.attributes.verification_token') &&
get(params, 'data.attributes.verification_token') !== "null") {
return new Response(200, {}, {data: {success: true}});
} else {
return new Response(200, {}, {data: {success: false}});
}
});
this.post(`${config.optInAPI}/mailchimp`, function() {
return {
email_address: "email@example.com",
status: "subscribed",
list_id: "a1b2c3"
}
});
this.post(`${config.optInAPI}/mobile-commons`, function() {
return {
campaign_id: "1",
opt_in_path_id: "12345",
phone_number: "212-555-0101"
}
});
}