diff --git a/src/js_sync.hpp b/src/js_sync.hpp index 83f00903..21e0967f 100644 --- a/src/js_sync.hpp +++ b/src/js_sync.hpp @@ -127,19 +127,33 @@ void UserClass::create_user(ContextType ctx, ObjectType this_object, size_t a template void UserClass::all_users(ContextType ctx, ObjectType object, ReturnValue &return_value) { std::vector user_vector; + // TODO: This method should return a dictionary of shape {userid->user} for (auto user : SyncManager::shared().all_users()) { - user_vector.push_back(create_object>(ctx, new SharedUser(user))); + if (user->state() == SyncUser::State::Active) { + user_vector.push_back(create_object>(ctx, new SharedUser(user))); + } } return_value.set(Object::create_array(ctx, user_vector)); } template void UserClass::current_user(ContextType ctx, ObjectType object, ReturnValue &return_value) { - auto users = SyncManager::shared().all_users(); - if (users.size() != 1) { - throw std::runtime_error("No current user"); + SharedUser *current = nullptr; + for (auto user : SyncManager::shared().all_users()) { + if (user->state() == SyncUser::State::Active) { + if (current != nullptr) { + throw std::runtime_error("More than one user logged in currently."); + } + current = new SharedUser(user); + } + } + + if (current != nullptr) { + return_value.set(create_object>(ctx, current)); + } + else { + return_value.set_undefined(); } - return_value.set(create_object>(ctx, new SharedUser(users[0]))); } template diff --git a/tests/js/asserts.js b/tests/js/asserts.js index 59f374fb..acc9a7e5 100644 --- a/tests/js/asserts.js +++ b/tests/js/asserts.js @@ -49,6 +49,19 @@ module.exports = { } }, + assertArray: function(value, length, errorMessage) { + if (!Array.isArray(value)) { + throw new TestFailureError(errorMessage || `Value ${value} is not an array`); + } + }, + + assertArrayLength: function(value, length, errorMessage) { + this.assertArray(value); + if (value.length !== length) { + throw new TestFailureError(errorMessage || `Value ${value} is not an array of length ${length}`); + } + }, + assertArraysEqual: function(val1, val2, errorMessage) { var len1 = val1.length; var len2 = val2.length; diff --git a/tests/js/user-tests.js b/tests/js/user-tests.js index f1f62a12..def89e43 100644 --- a/tests/js/user-tests.js +++ b/tests/js/user-tests.js @@ -41,6 +41,13 @@ function assertIsUser(user, isAdmin) { } } +function assertIsSameUser(value, user) { + assertIsUser(value); + TestCase.assertEqual(value.token, user.token); + TestCase.assertEqual(value.identity, user.identity); + TestCase.assertEqual(value.isAdmin, user.isAdmin); +} + function assertIsError(error, code) { TestCase.assertInstanceOf(error, Error, 'The API should return an Error'); if (code) { @@ -59,6 +66,7 @@ function assertIsAuthError(error, code, type) { } module.exports = { + testRegisterUser() { var username = uuid(); return new Promise((resolve, reject) => { @@ -187,5 +195,46 @@ module.exports = { }); }, + testAll() { + return new Promise((resolve, reject) => { + let all; + all = Realm.Sync.User.all; + TestCase.assertArrayLength(all, 0); + + Realm.Sync.User.register('http://localhost:9080', uuid(), 'password', (error, user1) => { + all = Realm.Sync.User.all; + TestCase.assertArrayLength(all, 1); + assertIsSameUser(all[0], user1); + + Realm.Sync.User.register('http://localhost:9080', uuid(), 'password', (error, user2) => { + all = Realm.Sync.User.all; + TestCase.assertArrayLength(all, 2); + // NOTE: the list of users is in latest-first order. + assertIsSameUser(all[0], user2); + assertIsSameUser(all[1], user1); + resolve(); + }); + }); + }); + }, + + testCurrent() { + return new Promise((resolve, reject) => { + let current; + current = Realm.Sync.User.current; + TestCase.assertUndefined(current); + + Realm.Sync.User.register('http://localhost:9080', uuid(), 'password', (error, user1) => { + current = Realm.Sync.User.current; + assertIsSameUser(current, user1); + + Realm.Sync.User.register('http://localhost:9080', uuid(), 'password', (error, user2) => { + TestCase.assertThrows(() => Realm.Sync.User.current, 'We expect Realm.Sync.User.current to throw if > 1 user.'); + resolve(); + }); + }); + }); + }, + }; diff --git a/tests/spec/sync_tests.js b/tests/spec/sync_tests.js index 6c4a086b..bad62e00 100644 --- a/tests/spec/sync_tests.js +++ b/tests/spec/sync_tests.js @@ -27,7 +27,12 @@ const userTests = require('../js/user-tests'); describe('SyncTests', () => { jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000; beforeEach(() => Realm.clearTestState()); - afterEach(() => Realm.clearTestState()); + afterEach(() => { + Realm.clearTestState(); + Realm.Sync.User.all.forEach((user) => { + user.logout(); + }); + }); for (const testName in userTests) { it(testName, (done) => userTests[testName]().catch((e) => fail(e)).then(done));