xo-server/tests/websocket.js

381 lines
7.4 KiB
JavaScript
Raw Normal View History

2013-07-15 15:24:31 +02:00
/* jshint loopfunc:false */
var assert = require('assert');
var sync = require('sync');
var WS = require('ws');
2013-07-24 11:19:07 +02:00
var _ = require('underscore');
2013-07-15 15:24:31 +02:00
//////////////////////////////////////////////////////////////////////
var tests = {
'Session management': {
'Password sign in': function () {
2013-07-17 10:41:52 +02:00
// Connects, signs in (with a password).
2013-07-17 15:19:36 +02:00
var conn = this.connect();
2013-07-15 15:24:31 +02:00
assert(conn('session.signInWithPassword', {
'email': 'bob@gmail.com',
'password': '123',
}));
},
2013-07-17 10:41:52 +02:00
'Password sign in with a inexistent user': function() {
// Connects
2013-07-17 15:19:36 +02:00
var conn = this.connect();
2013-07-17 10:41:52 +02:00
try
{
conn('session.signInWithPassword', {
2013-07-17 15:19:36 +02:00
'email': ' @gmail.com',
2013-07-17 10:41:52 +02:00
'password': '123',
});
}
catch (e)
{
2013-07-17 15:19:36 +02:00
// Check the error.
2013-07-17 10:41:52 +02:00
assert.strictEqual(e.code, 3);
return;
}
assert(false);
},
/* jshint maxlen:90 */
'Password sign in withan existing user and incorrect password': function () {
// Connects
2013-07-17 15:19:36 +02:00
var conn = this.connect();
2013-07-17 10:41:52 +02:00
try
{
// Try to sign in (with password).
conn('session.signInWithPassword', {
'email': 'bob@gmail.com',
'password': 'abc',
});
}
catch (e)
{
// Check if received invalid credential error.
assert.strictEqual(e.code, 3);
return;
}
assert(false);
},
'Password sign in with user already authenticated': function() {
// Connects, signs in (with a password).
2013-07-17 15:19:36 +02:00
var conn = this.connect();
2013-07-17 10:41:52 +02:00
conn('session.signInWithPassword', {
'email': 'bob@gmail.com',
'password': '123',
});
try
{
// Try to connect with other user account
conn('session.signInWithPassword', {
'email': 'toto@gmail.com',
'password': '123',
});
}
catch (e)
{
// Check if received already authenticated error.
assert.strictEqual(e.code, 4);
return;
}
assert(false);
},
},
///////////////////////////////////////
'Token management': {
2013-07-15 15:24:31 +02:00
'Token sign in': function () {
2013-07-17 15:19:36 +02:00
// Creates a token.
var token = this.master('token.create');
2013-07-17 10:41:52 +02:00
2013-07-17 15:19:36 +02:00
// Connects, signs in (with a token).
var conn = this.connect();
assert(conn('session.signInWithToken', {
2013-07-17 10:41:52 +02:00
'token': token
}));
2013-07-17 15:19:36 +02:00
// Check if connected with the same user.
2013-07-17 10:41:52 +02:00
assert.strictEqual(
2013-07-17 15:19:36 +02:00
conn('session.getUserId'),
this.master('session.getUserId')
2013-07-17 10:41:52 +02:00
);
},
'Token sign in with invalid parameter': function() {
// Connects.
2013-07-17 15:19:36 +02:00
var conn = this.connect();
2013-07-17 10:41:52 +02:00
try
{
// Try to sign in (with a token).
conn('session.signInWithToken', {
'token': ' ',
});
}
catch (e)
{
// Check if received invalid credential error.
assert.strictEqual(e.code, 3);
return;
}
2013-07-15 15:24:31 +02:00
assert(false);
},
'Connection close out when token removed': function () {
2013-07-17 15:19:36 +02:00
// Creates a token.
var token = this.master('token.create');
2013-07-15 15:24:31 +02:00
// Connects again and uses the token to sign in.
2013-07-17 15:19:36 +02:00
var conn = this.connect();
conn('session.signInWithToken', {'token': token});
2013-07-15 15:24:31 +02:00
// Delete the tokens.
2013-07-24 11:19:07 +02:00
this.master('token.delete', {'token': token});
2013-07-15 15:24:31 +02:00
2013-07-17 15:19:36 +02:00
// Checks the connection is closed.
2013-07-15 15:24:31 +02:00
assert.throws(function () {
2013-07-17 15:19:36 +02:00
conn('session.getUserId');
2013-07-15 15:24:31 +02:00
});
},
},
2013-07-17 10:41:52 +02:00
///////////////////////////////////////
'User management': {
'Create user': function() {
// Connects, sign in (with a password).
2013-07-17 15:19:36 +02:00
var conn = this.connect();
2013-07-17 10:41:52 +02:00
conn('session.signInWithPassword', {
'email': 'bob@gmail.com',
'password': '123',
});
// Create a user account.
assert(conn('user.create', {
'email': 'tintin@gmail.com',
'password': 'abc',
2013-07-24 11:19:07 +02:00
'permission': 'admin',
}));
},
'Delete user': function() {
// Connects, sign in (with a password).
var user_id = this.master('user.create', {
'email': 'fox@gmail.com',
'password': '123',
2013-07-17 10:41:52 +02:00
'permission': 'none',
2013-07-24 11:19:07 +02:00
});
// Delete user
assert(this.master('user.delete', {
'id': user_id,
2013-07-17 10:41:52 +02:00
}));
},
2013-07-24 11:19:07 +02:00
'Connection close out when user removed': function() {
// Connects, sign in (with a password).
var user_id = this.master('user.create', {
'email': 'fox@gmail.com',
'password': '123',
'permission': 'none',
});
// Connects, sign in (with a password)
var conn = this.connect();
conn('session.signInWithPassword', {
'email': 'fox@gmail.com',
'password': '123',
});
// Delete the user
this.master('user.delete', {
'id': user_id,
});
// Checks the connection is closed.
assert.throws(function () {
conn('session.getUserId');
});
},
'Change password': function() {
// Create new account.
this.master('user.create', {
'email': 'fox@gmail.com',
'password': '123',
'permission': 'none',
});
// Connects, sign in (with a password).
var conn = this.connect();
conn('session.signInWithPassword', {
'email': 'fox@gmail.com',
'password': '123',
});
// Change password.
conn('user.changePassword', {
'old': '123',
'new': 'abc',
});
// Check if password has changed
var conn2 = this.connect();
assert(conn2('session.signInWithPassword', {
'email': 'fox@gmail.com',
'password': 'abc',
}));
},
'Get all users': function() {
var users = this.master('user.getAll');
assert(_.isArray(users));
},
'Set user': function() {
var user_id = this.master('user.create', {
'email': 'link@gmail.com',
'password': 'abc',
'permission': 'none',
});
this.master('user.set', {
'id': user_id,
'email': 'mario@gmail.com',
'password': '123',
});
var conn = this.connect();
assert(conn('session.signInWithPassword', {
'email': 'mario@gmail.com',
'password': '123',
}));
},
},
2013-07-15 15:24:31 +02:00
};
//////////////////////////////////////////////////////////////////////
var next_id = 0;
function call(method, params)
{
return function (callback)
{
var request = {
'jsonrpc': '2.0',
'id': next_id++,
'method': method,
'params': params || {},
};
this.send(JSON.stringify(request), function (error) {
if (error)
{
callback(error);
}
});
this.once('message', function (response) {
try
{
response = JSON.parse(response.toString());
}
catch (e)
{
callback(e);
return;
}
if (response.error)
{
// To help find the problem, the request is included
// in the error.
var error = response.error;
error.request = request;
callback(error);
return;
}
callback(null, response.result);
});
}.sync(this);
}
function connect(url)
{
var socket;
(function (callback)
{
socket = new WS(url);
socket.on('open', function () {
callback(null, socket);
});
socket.on('error', function (error) {
callback(error);
});
}).sync();
var conn = function (method, params) {
return call.call(socket, method, params);
};
return conn;
}
//////////////////////////////////////////////////////////////////////
sync(function () {
2013-07-17 15:19:36 +02:00
// All tests have access to this master connection to create
// initial data.
var master = connect('ws://localhost:8080/');
master('session.signInWithPassword', {
'email': 'bob@gmail.com',
'password': '123',
});
var self = {
'connect': function () {
return connect('ws://localhost:8080/');
},
'master': master,
};
2013-07-15 15:24:31 +02:00
for (var category in tests)
{
2013-07-17 10:41:52 +02:00
console.log();
2013-07-15 15:24:31 +02:00
console.log(category);
console.log('====================');
for (var test in tests[category])
{
console.log('- '+ test);
var f = tests[category][test];
try
{
2013-07-17 15:19:36 +02:00
f.call(self);
2013-07-15 15:24:31 +02:00
}
catch (error)
{
console.error(error);
}
}
}
}, function () {
process.exit();
});
2013-07-17 10:41:52 +02:00