Add tests of Realm.Sync.User.all and Realm.Sync.User.current. Fix both to only return active (logged-in) users. Fix current return values: Throws if >1 user logged in, returns undefined if no logged in.
This commit is contained in:
parent
448f68a787
commit
87b30fb4ec
|
@ -127,19 +127,33 @@ void UserClass<T>::create_user(ContextType ctx, ObjectType this_object, size_t a
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void UserClass<T>::all_users(ContextType ctx, ObjectType object, ReturnValue &return_value) {
|
void UserClass<T>::all_users(ContextType ctx, ObjectType object, ReturnValue &return_value) {
|
||||||
std::vector<ValueType> user_vector;
|
std::vector<ValueType> user_vector;
|
||||||
|
// TODO: This method should return a dictionary of shape {userid->user}
|
||||||
for (auto user : SyncManager::shared().all_users()) {
|
for (auto user : SyncManager::shared().all_users()) {
|
||||||
|
if (user->state() == SyncUser::State::Active) {
|
||||||
user_vector.push_back(create_object<T, UserClass<T>>(ctx, new SharedUser(user)));
|
user_vector.push_back(create_object<T, UserClass<T>>(ctx, new SharedUser(user)));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return_value.set(Object::create_array(ctx, user_vector));
|
return_value.set(Object::create_array(ctx, user_vector));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void UserClass<T>::current_user(ContextType ctx, ObjectType object, ReturnValue &return_value) {
|
void UserClass<T>::current_user(ContextType ctx, ObjectType object, ReturnValue &return_value) {
|
||||||
auto users = SyncManager::shared().all_users();
|
SharedUser *current = nullptr;
|
||||||
if (users.size() != 1) {
|
for (auto user : SyncManager::shared().all_users()) {
|
||||||
throw std::runtime_error("No current user");
|
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<T, UserClass<T>>(ctx, current));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return_value.set_undefined();
|
||||||
}
|
}
|
||||||
return_value.set(create_object<T, UserClass<T>>(ctx, new SharedUser(users[0])));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
|
@ -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) {
|
assertArraysEqual: function(val1, val2, errorMessage) {
|
||||||
var len1 = val1.length;
|
var len1 = val1.length;
|
||||||
var len2 = val2.length;
|
var len2 = val2.length;
|
||||||
|
|
|
@ -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) {
|
function assertIsError(error, code) {
|
||||||
TestCase.assertInstanceOf(error, Error, 'The API should return an Error');
|
TestCase.assertInstanceOf(error, Error, 'The API should return an Error');
|
||||||
if (code) {
|
if (code) {
|
||||||
|
@ -59,6 +66,7 @@ function assertIsAuthError(error, code, type) {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
testRegisterUser() {
|
testRegisterUser() {
|
||||||
var username = uuid();
|
var username = uuid();
|
||||||
return new Promise((resolve, reject) => {
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,12 @@ const userTests = require('../js/user-tests');
|
||||||
describe('SyncTests', () => {
|
describe('SyncTests', () => {
|
||||||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
|
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
|
||||||
beforeEach(() => Realm.clearTestState());
|
beforeEach(() => Realm.clearTestState());
|
||||||
afterEach(() => Realm.clearTestState());
|
afterEach(() => {
|
||||||
|
Realm.clearTestState();
|
||||||
|
Realm.Sync.User.all.forEach((user) => {
|
||||||
|
user.logout();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
for (const testName in userTests) {
|
for (const testName in userTests) {
|
||||||
it(testName, (done) => userTests[testName]().catch((e) => fail(e)).then(done));
|
it(testName, (done) => userTests[testName]().catch((e) => fail(e)).then(done));
|
||||||
|
|
Loading…
Reference in New Issue