Skip to content

Commit 4bd7c5a

Browse files
author
Gordon Sun
committed
fix a test case
1. not found should be 404 2. do not need to create a new server, just use the one created by beforeEach 3. use async await
1 parent 7ccd6ce commit 4bd7c5a

File tree

3 files changed

+50
-74
lines changed

3 files changed

+50
-74
lines changed

spec/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"globals": {
66
"Parse": true,
77
"reconfigureServer": true,
8+
"parseServerFromBeforeEach": true,
89
"createTestUser": true,
910
"jfail": true,
1011
"ok": true,
Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,28 @@
1-
const ParseServer = require('../lib/index');
2-
const express = require('express');
31
const request = require('../lib/request');
42

53
describe('Enable express error handler', () => {
6-
it('should call the default handler in case of error, like updating a non existing object', done => {
7-
const serverUrl = 'http://localhost:12667/parse';
8-
const appId = 'anOtherTestApp';
9-
const masterKey = 'anOtherTestMasterKey';
10-
let server;
4+
it('should call the default handler in case of error, like updating a non existing object', async (done) => {
5+
parseServerFromBeforeEach.app.use(function (err, req, res, next) {
6+
expect(err.message).toBe('Object not found.');
7+
next(err);
8+
});
119

12-
const parseServer = ParseServer.ParseServer(
13-
Object.assign({}, defaultConfiguration, {
14-
appId: appId,
15-
masterKey: masterKey,
16-
serverURL: serverUrl,
17-
enableExpressErrorHandler: true,
18-
serverStartComplete: () => {
19-
expect(Parse.applicationId).toEqual('anOtherTestApp');
20-
const app = express();
21-
app.use('/parse', parseServer);
22-
23-
server = app.listen(12667);
24-
25-
app.use(function(err, req, res, next) {
26-
expect(err.message).toBe('Object not found.');
27-
next(err);
28-
});
29-
30-
request({
31-
method: 'PUT',
32-
url: serverUrl + '/classes/AnyClass/nonExistingId',
33-
headers: {
34-
'X-Parse-Application-Id': appId,
35-
'X-Parse-Master-Key': masterKey,
36-
'Content-Type': 'application/json',
37-
},
38-
body: { someField: 'blablabla' },
39-
})
40-
.then(() => {
41-
fail('Should throw error');
42-
})
43-
.catch(response => {
44-
expect(response).toBeDefined();
45-
expect(response.status).toEqual(500);
46-
})
47-
.then(() => {
48-
server.close(done);
49-
});
10+
try {
11+
await request({
12+
method: 'PUT',
13+
url: defaultConfiguration.serverURL + '/classes/AnyClass/nonExistingId',
14+
headers: {
15+
'X-Parse-Application-Id': defaultConfiguration.appId,
16+
'X-Parse-Master-Key': defaultConfiguration.masterKey,
17+
'Content-Type': 'application/json',
5018
},
51-
})
52-
);
19+
body: { someField: 'blablabla' },
20+
});
21+
fail('Should throw error');
22+
} catch (response) {
23+
expect(response).toBeDefined();
24+
expect(response.status).toEqual(404);
25+
parseServerFromBeforeEach.server.close(done);
26+
}
5327
});
5428
});

spec/helper.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const openConnections = {};
119119
let server;
120120

121121
// Allows testing specific configurations of Parse Server
122-
const reconfigureServer = changedConfiguration => {
122+
const reconfigureServer = (changedConfiguration) => {
123123
return new Promise((resolve, reject) => {
124124
if (server) {
125125
return server.close(() => {
@@ -134,7 +134,7 @@ const reconfigureServer = changedConfiguration => {
134134
defaultConfiguration,
135135
changedConfiguration,
136136
{
137-
serverStartComplete: error => {
137+
serverStartComplete: (error) => {
138138
if (error) {
139139
reject(error);
140140
} else {
@@ -148,12 +148,12 @@ const reconfigureServer = changedConfiguration => {
148148
cache.clear();
149149
parseServer = ParseServer.start(newConfiguration);
150150
parseServer.app.use(require('./testing-routes').router);
151-
parseServer.expressApp.use('/1', err => {
151+
parseServer.expressApp.use('/1', (err) => {
152152
console.error(err);
153153
fail('should not call next');
154154
});
155155
server = parseServer.server;
156-
server.on('connection', connection => {
156+
server.on('connection', (connection) => {
157157
const key = `${connection.remoteAddress}:${connection.remotePort}`;
158158
openConnections[key] = connection;
159159
connection.on('close', () => {
@@ -170,7 +170,7 @@ const reconfigureServer = changedConfiguration => {
170170
const Parse = require('parse/node');
171171
Parse.serverURL = 'http://localhost:' + port + '/1';
172172

173-
beforeEach(done => {
173+
beforeEach((done) => {
174174
try {
175175
Parse.User.enableUnsafeCurrentUser();
176176
} catch (error) {
@@ -179,7 +179,7 @@ beforeEach(done => {
179179
}
180180
}
181181
TestUtils.destroyAllDataPermanently(true)
182-
.catch(error => {
182+
.catch((error) => {
183183
// For tests that connect to their own mongo, there won't be any data to delete.
184184
if (
185185
error.message === 'ns not found' ||
@@ -192,15 +192,16 @@ beforeEach(done => {
192192
}
193193
})
194194
.then(reconfigureServer)
195-
.then(() => {
195+
.then((parseServer) => {
196+
global.parseServerFromBeforeEach = parseServer;
196197
Parse.initialize('test', 'test', 'test');
197198
Parse.serverURL = 'http://localhost:' + port + '/1';
198199
done();
199200
})
200201
.catch(done.fail);
201202
});
202203

203-
afterEach(function(done) {
204+
afterEach(function (done) {
204205
const afterLogOut = () => {
205206
if (Object.keys(openConnections).length > 0) {
206207
fail(
@@ -212,11 +213,11 @@ afterEach(function(done) {
212213
Parse.Cloud._removeAllHooks();
213214
databaseAdapter
214215
.getAllClasses()
215-
.then(allSchemas => {
216-
allSchemas.forEach(schema => {
216+
.then((allSchemas) => {
217+
allSchemas.forEach((schema) => {
217218
const className = schema.className;
218219
expect(className).toEqual({
219-
asymmetricMatch: className => {
220+
asymmetricMatch: (className) => {
220221
if (!className.startsWith('_')) {
221222
return true;
222223
} else {
@@ -244,7 +245,7 @@ afterEach(function(done) {
244245
) // swallow errors
245246
.then(() => {
246247
// Connection close events are not immediate on node 10+... wait a bit
247-
return new Promise(resolve => {
248+
return new Promise((resolve) => {
248249
setTimeout(resolve, 0);
249250
});
250251
})
@@ -326,13 +327,13 @@ function range(n) {
326327

327328
function mockCustomAuthenticator(id, password) {
328329
const custom = {};
329-
custom.validateAuthData = function(authData) {
330+
custom.validateAuthData = function (authData) {
330331
if (authData.id === id && authData.password.startsWith(password)) {
331332
return Promise.resolve();
332333
}
333334
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'not validated');
334335
};
335-
custom.validateAppId = function() {
336+
custom.validateAppId = function () {
336337
return Promise.resolve();
337338
};
338339
return custom;
@@ -344,14 +345,14 @@ function mockCustom() {
344345

345346
function mockFacebookAuthenticator(id, token) {
346347
const facebook = {};
347-
facebook.validateAuthData = function(authData) {
348+
facebook.validateAuthData = function (authData) {
348349
if (authData.id === id && authData.access_token.startsWith(token)) {
349350
return Promise.resolve();
350351
} else {
351352
throw undefined;
352353
}
353354
};
354-
facebook.validateAppId = function(appId, authData) {
355+
facebook.validateAppId = function (appId, authData) {
355356
if (authData.access_token.startsWith(token)) {
356357
return Promise.resolve();
357358
} else {
@@ -368,17 +369,17 @@ function mockFacebook() {
368369
function mockShortLivedAuth() {
369370
const auth = {};
370371
let accessToken;
371-
auth.setValidAccessToken = function(validAccessToken) {
372+
auth.setValidAccessToken = function (validAccessToken) {
372373
accessToken = validAccessToken;
373374
};
374-
auth.validateAuthData = function(authData) {
375+
auth.validateAuthData = function (authData) {
375376
if (authData.access_token == accessToken) {
376377
return Promise.resolve();
377378
} else {
378379
return Promise.reject('Invalid access token');
379380
}
380381
};
381-
auth.validateAppId = function() {
382+
auth.validateAppId = function () {
382383
return Promise.resolve();
383384
};
384385
return auth;
@@ -403,19 +404,19 @@ global.defaultConfiguration = defaultConfiguration;
403404
global.mockCustomAuthenticator = mockCustomAuthenticator;
404405
global.mockFacebookAuthenticator = mockFacebookAuthenticator;
405406
global.databaseAdapter = databaseAdapter;
406-
global.jfail = function(err) {
407+
global.jfail = function (err) {
407408
fail(JSON.stringify(err));
408409
};
409410

410-
global.it_exclude_dbs = excluded => {
411+
global.it_exclude_dbs = (excluded) => {
411412
if (excluded.indexOf(process.env.PARSE_SERVER_TEST_DB) >= 0) {
412413
return xit;
413414
} else {
414415
return it;
415416
}
416417
};
417418

418-
global.it_only_db = db => {
419+
global.it_only_db = (db) => {
419420
if (
420421
process.env.PARSE_SERVER_TEST_DB === db ||
421422
(!process.env.PARSE_SERVER_TEST_DB && db == 'mongo')
@@ -426,15 +427,15 @@ global.it_only_db = db => {
426427
}
427428
};
428429

429-
global.fit_exclude_dbs = excluded => {
430+
global.fit_exclude_dbs = (excluded) => {
430431
if (excluded.indexOf(process.env.PARSE_SERVER_TEST_DB) >= 0) {
431432
return xit;
432433
} else {
433434
return fit;
434435
}
435436
};
436437

437-
global.describe_only_db = db => {
438+
global.describe_only_db = (db) => {
438439
if (process.env.PARSE_SERVER_TEST_DB == db) {
439440
return describe;
440441
} else if (!process.env.PARSE_SERVER_TEST_DB && db == 'mongo') {
@@ -444,7 +445,7 @@ global.describe_only_db = db => {
444445
}
445446
};
446447

447-
global.describe_only = validator => {
448+
global.describe_only = (validator) => {
448449
if (validator()) {
449450
return describe;
450451
} else {
@@ -453,7 +454,7 @@ global.describe_only = validator => {
453454
};
454455

455456
const libraryCache = {};
456-
jasmine.mockLibrary = function(library, name, mock) {
457+
jasmine.mockLibrary = function (library, name, mock) {
457458
const original = require(library)[name];
458459
if (!libraryCache[library]) {
459460
libraryCache[library] = {};
@@ -462,7 +463,7 @@ jasmine.mockLibrary = function(library, name, mock) {
462463
libraryCache[library][name] = original;
463464
};
464465

465-
jasmine.restoreLibrary = function(library, name) {
466+
jasmine.restoreLibrary = function (library, name) {
466467
if (!libraryCache[library] || !libraryCache[library][name]) {
467468
throw 'Can not find library ' + library + ' ' + name;
468469
}

0 commit comments

Comments
 (0)