From 164ea8ec7918654c0e640da5b129b84f2a4d9e83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Vind?= Date: Thu, 10 Nov 2016 10:34:39 -0800 Subject: [PATCH] Port over remaining tests for Realm.Sync.User --- tests/js/asserts.js | 16 ++++- tests/js/user-tests.js | 139 +++++++++++++++++++++++++++++++-------- tests/spec/sync_tests.js | 1 + 3 files changed, 129 insertions(+), 27 deletions(-) diff --git a/tests/js/asserts.js b/tests/js/asserts.js index fae31ea6..59f374fb 100644 --- a/tests/js/asserts.js +++ b/tests/js/asserts.js @@ -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]'; diff --git a/tests/js/user-tests.js b/tests/js/user-tests.js index 91e94512..2c5763c0 100644 --- a/tests/js/user-tests.js +++ b/tests/js/user-tests.js @@ -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(); + }); + }); + }, + }; diff --git a/tests/spec/sync_tests.js b/tests/spec/sync_tests.js index ef7206e9..6c4a086b 100644 --- a/tests/spec/sync_tests.js +++ b/tests/spec/sync_tests.js @@ -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());