Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Created March 7, 2019 13:36
Show Gist options
  • Save Noitidart/75527c0a1f2f936f35f79395cfd0191e to your computer and use it in GitHub Desktop.
Save Noitidart/75527c0a1f2f936f35f79395cfd0191e to your computer and use it in GitHub Desktop.
const SocketsMock = require('./utils/SocketsMock');
const Cloud = require('../assets/dependencies/cloud');
const ioClient = require('socket.io-client');
const sailsIO = require('../assets/dependencies/sails.io');
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,
// sockets: require('sails-hook-sockets')
},
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' }
},
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,
},
// sockets: {
// grant3rdPartyCookie: 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
};
// add a listener on connect, that should fire only on the first connect, it removes itself
io.socket.on('connect', function connectHandler() {
console.log('connectHandler FIRED');
io.socket.off('connect', connectHandler);
});
console.log('waiting for initial autoConnect');
await new Promise(resolve => io.socket.on('connect', resolve));
console.log('autoConnected, disconnect');
const dis = new Promise(resolve => io.socket.on('disconnect', resolve));
io.socket.disconnect();
console.log('waiting to get disconnected');
await dis;
console.log('reconnect');
io.socket.reconnect();
console.log('wait reconnect');
await new Promise(resolve => io.socket.on('connect', resolve));
throw new Error('debug');
// new SocketsMock();
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();
SocketsMock.restore();
sails.lower(done);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment