Merge pull request #498 from drew-gross/test-configurations

Add ability to test multiple server configurations
This commit is contained in:
Drew
2016-02-19 19:28:30 -08:00
5 changed files with 138 additions and 51 deletions

View File

@@ -7,14 +7,17 @@ var DatabaseAdapter = require('../src/DatabaseAdapter');
var express = require('express');
var facebook = require('../src/oauth/facebook');
var ParseServer = require('../src/index').ParseServer;
var DatabaseAdapter = require('../src/DatabaseAdapter');
var databaseURI = process.env.DATABASE_URI;
var cloudMain = process.env.CLOUD_CODE_MAIN || './cloud/main.js';
var port = 8378;
// Set up an API server for testing
var api = new ParseServer({
// Default server configuration for tests.
var defaultConfiguration = {
databaseURI: databaseURI,
cloud: cloudMain,
serverURL: 'http://localhost:' + port + '/1',
appId: 'test',
javascriptKey: 'test',
dotNetKey: 'windows',
@@ -29,13 +32,29 @@ var api = new ParseServer({
module: "../spec/myoauth" // relative path as it's run from src
}
}
});
};
// Set up a default API server for testing with default configuration.
var api = new ParseServer(defaultConfiguration);
var app = express();
app.use('/1', api);
var port = 8378;
var server = app.listen(port);
// Prevent reinitializing the server from clobbering Cloud Code
delete defaultConfiguration.cloud;
// Allows testing specific configurations of Parse Server
var setServerConfiguration = configuration => {
api = new ParseServer(configuration);
app = express();
app.use('/1', api);
cache.clearCache();
server.close();
server = app.listen(port);
}
var restoreServerConfiguration = () => setServerConfiguration(defaultConfiguration);
// Set up a Parse client to talk to our test API server
var Parse = require('parse/node');
Parse.serverURL = 'http://localhost:' + port + '/1';
@@ -51,9 +70,11 @@ beforeEach(function(done) {
});
afterEach(function(done) {
restoreServerConfiguration();
Parse.User.logOut().then(() => {
return clearData();
}).then(() => {
DatabaseAdapter.clearDatabaseURIs();
done();
}, (error) => {
console.log('error in clearData', error);
@@ -221,3 +242,4 @@ global.expectError = expectError;
global.arrayContains = arrayContains;
global.jequal = jequal;
global.range = range;
global.setServerConfiguration = setServerConfiguration;

39
spec/index.spec.js Normal file
View File

@@ -0,0 +1,39 @@
var request = require('request');
describe('server', () => {
it('requires a master key and app id', done => {
expect(setServerConfiguration.bind(undefined, { masterKey: 'mykey' })).toThrow('You must provide an appId and masterKey!');
expect(setServerConfiguration.bind(undefined, { appId: 'myId' })).toThrow('You must provide an appId and masterKey!');
done();
});
it('fails if database is unreachable', done => {
setServerConfiguration({
databaseURI: 'mongodb://fake:fake@ds043605.mongolab.com:43605/drew3',
serverURL: 'http://localhost:8378/1',
appId: 'test',
javascriptKey: 'test',
dotNetKey: 'windows',
clientKey: 'client',
restAPIKey: 'rest',
masterKey: 'test',
collectionPrefix: 'test_',
fileKey: 'test',
});
//Need to use rest api because saving via JS SDK results in fail() not getting called
request.post({
url: 'http://localhost:8378/1/classes/NewClass',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
},
body: {},
json: true,
}, (error, response, body) => {
expect(response.statusCode).toEqual(500);
expect(body.code).toEqual(1);
expect(body.message).toEqual('Internal server error.');
done();
});
});
});