Created
March 12, 2019 03:09
-
-
Save Noitidart/63df651dc76812da6cb3bfe1ce4e9e0e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Cloud = require('../assets/dependencies/cloud'); | |
const ioClient = require('socket.io-client'); | |
const sailsIO = require('../assets/dependencies/sails.io'); | |
const mockSailsSockets = require('./utils/mockSailsSockets'); | |
let sails; | |
// Before running any tests... | |
before(function(done) { | |
sails = new require('sails').Sails(); | |
// Increase the Mocha timeout so that Sails has enough time to lift, even if you have a bunch of assets. | |
this.timeout(10000); | |
const port = 1313; | |
sails.lift( | |
{ | |
port, | |
custom: { | |
baseUrl: 'http://localhost:' + port | |
}, | |
datastores: { | |
default: { | |
adapter: 'sails-mongo', | |
url: 'mongodb://localhost:27017/homie-test', | |
}, | |
}, | |
hooks: { | |
grunt: false, | |
apianalytics: false | |
}, | |
log: { | |
// Disable all logs except errors and warnings | |
level: 'warn' | |
}, | |
models: { | |
migrate: 'drop' | |
}, | |
routes: { | |
// Provide a way to get a CSRF token: | |
'GET /.temporary/csrf/token/for/tests': { action: 'security/grant-csrf-token' }, | |
'GET /.temporary/socket/id/for/tests': { | |
isSocket: true, | |
fn: function(req, res) { | |
return res.send(sails.sockets.getId(req)); | |
} | |
} | |
}, | |
policies: { | |
// Poke a hole in any global policies to ensure the test runner can | |
// actually get access to a CSRF token. | |
'security/grant-csrf-token': true, | |
} | |
}, | |
async function(err) { | |
if (err) return done(err); | |
// here you can load fixtures, etc. | |
// (for example, you might want to create some records in the database) | |
// Do request for getting CSRF material (token and cookie) for Cloud and supertest | |
const res = await sails.helpers.http.sendHttpRequest.with({ | |
method: 'GET', | |
url: '.temporary/csrf/token/for/tests', | |
baseUrl: sails.config.custom.baseUrl | |
}); | |
if (global.CSRF_TOKEN) throw new Error('Test runner cannot expose `CSRF_TOKEN` -- that global already exists!'); | |
try { | |
global.CSRF_TOKEN = sails.config.security.csrf ? JSON.parse(res.body)._csrf : undefined; | |
} catch(err) { | |
throw new Error('Test runner could not parse CSRF token from the Sails server.\nDetails:\n'+err.stack); | |
} | |
if (global.SAILS_SID_COOKIE) throw new Error('Test runner cannot expose `SAILS_SID_COOKIE` -- that global already exists!'); | |
try { | |
global.SAILS_SID_COOKIE = res.headers['set-cookie'][0].split(';')[0].trim(); | |
} catch(err) { | |
throw new Error('Test runner could not parse the `set-cookie` response header from the Sails server.\nDetails:\n'+err.stack); | |
} | |
// Hook up Cloud with CSRF | |
if (global.Cloud) throw new Error('Test runner cannot expose `Cloud` -- that global already exists!'); | |
global.Cloud = Cloud; | |
Cloud.setup({ | |
apiBaseUrl: sails.config.custom.baseUrl, | |
headers: { | |
'x-csrf-token': CSRF_TOKEN, | |
'cookie': SAILS_SID_COOKIE | |
}, | |
protocol: sails.helpers.http, | |
methods: require('./private/CLOUD_SDK_METHODS') | |
}); | |
// Instantiate socket client. | |
if (global.io) throw new Error('Test runner cannot expose `io` -- that global already exists!'); | |
global.io = sailsIO(ioClient); | |
io.sails.url = sails.config.custom.baseUrl; | |
io.sails.environment = 'production'; // Disable the sails.io.js client's logger | |
io.sails.reconnection = false; // Don't automatically reconnect after being disconnected | |
io.sails.initialConnectionHeaders = { | |
'x-csrf-token': CSRF_TOKEN, | |
'cookie': SAILS_SID_COOKIE | |
}; | |
// Wait till socket connects | |
await new Promise(resolve => io.socket.on('connect', function resolver() { | |
resolve(); | |
// If i do it in setTimeout, mockSailsSockets would have already mocked `io.socket.off` | |
const removeResolver = () => io.socket.off('connect', resolver); | |
setTimeout(removeResolver, 0); | |
})); | |
await mockSailsSockets(); | |
return done(); | |
} | |
); | |
}); | |
// After all tests have finished... | |
after(done => { | |
// here you can clear fixtures, etc. | |
// (e.g. you might want to destroy the records you created above) | |
io.socket.disconnect(); | |
sails.sockets.unmock(); | |
sails.lower(done); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment