Port over remaining tests for Realm.Sync.User

This commit is contained in:
Søren Vind 2016-11-10 10:34:39 -08:00
parent 5b3825092d
commit 164ea8ec79
3 changed files with 129 additions and 27 deletions

View File

@ -106,10 +106,24 @@ module.exports = {
assertTrue: function(condition, errorMessage) {
if (!condition) {
throw new TestFailureError(errorMessage || 'Condition expected to be true');
throw new TestFailureError(errorMessage || `Condition ${condition} expected to be true`);
}
},
assertInstanceOf: function(object, type, errorMessage) {
if (!(object instanceof type)) {
throw new TestFailureError(errorMessage || `Object ${object} expected to be of type ${type}`);
}
},
assertType: function(value, type) {
this.assertEqual(typeof value, type, `Value ${value} expected to be of type ${type}`);
},
assertUndefined: function(value) {
this.assertEqual(value, undefined, `Value ${value} expected to be undefined`);
},
isNode: function() {
// eslint-disable-next-line no-undef
return typeof process == 'object' && Object.prototype.toString.call(process) == '[object process]';

View File

@ -31,18 +31,46 @@ function uuid() {
});
}
function assertIsUser(user, isAdmin) {
TestCase.assertType(user, 'object');
TestCase.assertType(user.token, 'string');
TestCase.assertType(user.identity, 'string');
TestCase.assertInstanceOf(user, Realm.Sync.User);
if (isAdmin !== undefined) {
TestCase.assertEqual(user.isAdmin, isAdmin);
}
}
function assertIsError(error, code) {
TestCase.assertInstanceOf(error, Error, 'The API should return an Error');
if (code) {
TestCase.assertEqual(error.code, code);
}
}
function assertIsAuthError(error, code, type) {
TestCase.assertInstanceOf(error, Realm.Sync.AuthError, 'The API should return an AuthError');
if (code) {
TestCase.assertEqual(error.code, code);
}
if (type) {
TestCase.assertEqual(error.type, type);
}
}
module.exports = {
testRegisterUser() {
var username = uuid();
return new Promise((resolve, reject) => {
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
TestCase.assertEqual(typeof user, 'object');
TestCase.assertEqual(typeof user.token, 'string');
TestCase.assertEqual(typeof user.identity, 'string');
TestCase.assertEqual(user.isAdmin, false);
if (error) {
reject(error);
}
assertIsUser(user);
// Can we open a realm with the registered user?
var realm = new Realm({sync: {user: user, url: 'realm://localhost:9080/~/test'}});
TestCase.assertNotEqual(realm instanceof Realm);
TestCase.assertInstanceOf(realm, Realm);
resolve();
});
});
@ -52,26 +80,48 @@ module.exports = {
var username = uuid();
return new Promise((resolve, reject) => {
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
TestCase.assertEqual(typeof user, 'object');
if (error) {
reject(error);
}
assertIsUser(user);
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
TestCase.assertTrue(error instanceof Realm.Sync.AuthError);
TestCase.assertEqual(error.code, 613);
TestCase.assertEqual(error.type, 'https://realm.io/docs/object-server/problems/existing-account');
TestCase.assertEqual(user, undefined);
assertIsAuthError(error, 613, 'https://realm.io/docs/object-server/problems/existing-account');
TestCase.assertUndefined(user);
resolve();
});
});
});
},
testRegisterInvalidUsername() {
var username = uuid();
testRegisterMissingUsername() {
return new Promise((resolve, reject) => {
Realm.Sync.User.register('http://localhost:9080', undefined, 'password', (error, user) => {
TestCase.assertTrue(error instanceof Realm.Sync.AuthError);
TestCase.assertEqual(error.code, 602);
TestCase.assertEqual(error.type, 'https://realm.io/docs/object-server/problems/missing-parameters');
TestCase.assertEqual(user, undefined);
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
TestCase.assertUndefined(user);
resolve();
});
});
},
testRegisterMissingPassword() {
var username = uuid();
return new Promise((resolve, reject) => {
Realm.Sync.User.register('http://localhost:9080', username, undefined, (error, user) => {
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
TestCase.assertUndefined(user);
resolve();
});
});
},
testRegisterServerOffline() {
var username = uuid();
return new Promise((resolve, reject) => {
// Because it waits for answer this takes some time..
Realm.Sync.User.register('http://fake_host.local', username, 'password', (error, user) => {
assertIsError(error, 'ECONNRESET');
TestCase.assertUndefined(user);
resolve();
});
});
@ -80,25 +130,62 @@ module.exports = {
testLogin() {
var username = uuid();
return new Promise((resolve, reject) => {
// Create user, logout the new user, then login
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
user.logout();
//TestCase.assertEqual(Realm.Sync.User.all.length, 0);
Realm.Sync.User.login('http://localhost:9080', username, 'password', (error, user) => {
TestCase.assertEqual(typeof user, 'object');
TestCase.assertEqual(typeof user.token, 'string');
TestCase.assertEqual(typeof user.identity, 'string');
TestCase.assertEqual(user.isAdmin, false);
assertIsUser(user);
var realm = new Realm({sync: {user: user, url: 'realm://localhost:9080/~/test'}});
TestCase.assertNotEqual(realm instanceof Realm);
//TestCase.assertEqual(Realm.Sync.User.all.length, 1);
TestCase.assertInstanceOf(realm, Realm);
resolve();
});
});
});
},
testLoginMissingUsername() {
return new Promise((resolve, reject) => {
Realm.Sync.User.login('http://localhost:9080', undefined, 'password', (error, user) => {
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
TestCase.assertUndefined(user);
resolve();
});
});
},
testLoginMissingPassword() {
var username = uuid();
return new Promise((resolve, reject) => {
Realm.Sync.User.login('http://localhost:9080', username, undefined, (error, user) => {
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
TestCase.assertUndefined(user);
resolve();
});
});
},
testLoginNonExistingUser() {
return new Promise((resolve, reject) => {
Realm.Sync.User.login('http://localhost:9080', 'does_not', 'exist', (error, user) => {
assertIsAuthError(error, 612, 'https://realm.io/docs/object-server/problems/unknown-account');
TestCase.assertUndefined(user);
resolve();
});
});
},
testLoginServerOffline() {
var username = uuid();
return new Promise((resolve, reject) => {
// Because it waits for answer this takes some time..
Realm.Sync.User.register('http://fake_host.local', username, 'password', (error, user) => {
assertIsError(error, 'ECONNRESET');
TestCase.assertUndefined(user);
resolve();
});
});
},
};

View File

@ -25,6 +25,7 @@ const Realm = require('realm');
const userTests = require('../js/user-tests');
describe('SyncTests', () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
beforeEach(() => Realm.clearTestState());
afterEach(() => Realm.clearTestState());