2016-11-08 16:59:30 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2016 Realm Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/* eslint-env es6, node */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const Realm = require('realm');
|
|
|
|
const TestCase = require('./asserts');
|
2017-07-07 13:38:13 +00:00
|
|
|
const isNodeProcess = typeof process === 'object' && process + '' === '[object process]';
|
2016-11-08 16:59:30 +00:00
|
|
|
|
2018-09-21 12:24:37 +00:00
|
|
|
const require_method = require;
|
|
|
|
function node_require(module) {
|
|
|
|
return require_method(module);
|
|
|
|
}
|
|
|
|
|
2018-09-24 10:43:44 +00:00
|
|
|
let fs;
|
2018-09-21 12:24:37 +00:00
|
|
|
if (isNodeProcess) {
|
|
|
|
fs = node_require('fs');
|
|
|
|
}
|
|
|
|
|
2016-11-08 16:59:30 +00:00
|
|
|
function uuid() {
|
2016-11-10 18:45:43 +00:00
|
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
2017-11-10 20:05:35 +00:00
|
|
|
const r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
2016-11-10 18:45:43 +00:00
|
|
|
return v.toString(16);
|
|
|
|
});
|
2016-11-08 16:59:30 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 18:34:39 +00:00
|
|
|
function assertIsUser(user, isAdmin) {
|
2016-11-10 18:45:43 +00:00
|
|
|
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);
|
|
|
|
}
|
2016-11-10 18:34:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 20:53:36 +00:00
|
|
|
function assertIsSameUser(value, user) {
|
|
|
|
assertIsUser(value);
|
|
|
|
TestCase.assertEqual(value.token, user.token);
|
|
|
|
TestCase.assertEqual(value.identity, user.identity);
|
|
|
|
TestCase.assertEqual(value.isAdmin, user.isAdmin);
|
|
|
|
}
|
|
|
|
|
2017-01-31 13:07:29 +00:00
|
|
|
function assertIsError(error, message) {
|
2016-11-10 18:45:43 +00:00
|
|
|
TestCase.assertInstanceOf(error, Error, 'The API should return an Error');
|
2017-01-31 13:07:29 +00:00
|
|
|
if (message) {
|
|
|
|
TestCase.assertEqual(error.message, message);
|
2016-11-10 18:45:43 +00:00
|
|
|
}
|
2016-11-10 18:34:39 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 06:44:11 +00:00
|
|
|
function assertIsAuthError(error, code, title) {
|
2016-11-10 18:45:43 +00:00
|
|
|
TestCase.assertInstanceOf(error, Realm.Sync.AuthError, 'The API should return an AuthError');
|
|
|
|
if (code) {
|
|
|
|
TestCase.assertEqual(error.code, code);
|
|
|
|
}
|
2017-09-18 06:44:11 +00:00
|
|
|
if (title) {
|
|
|
|
TestCase.assertEqual(error.title, title);
|
2016-11-10 18:45:43 +00:00
|
|
|
}
|
2016-11-10 18:34:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 16:59:30 +00:00
|
|
|
module.exports = {
|
2017-08-31 22:28:37 +00:00
|
|
|
|
2016-11-11 02:06:06 +00:00
|
|
|
testLogout() {
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.anonymous()).then((user) => {
|
2016-11-11 02:06:06 +00:00
|
|
|
assertIsUser(user);
|
|
|
|
|
|
|
|
assertIsSameUser(user, Realm.Sync.User.current);
|
|
|
|
user.logout();
|
|
|
|
|
|
|
|
// Is now logged out.
|
|
|
|
TestCase.assertUndefined(Realm.Sync.User.current);
|
|
|
|
|
|
|
|
// Can we open a realm with the registered user?
|
2017-11-10 20:05:35 +00:00
|
|
|
TestCase.assertThrows(() => new Realm({sync: {user: user, url: 'realm://localhost:9080/~/test'}}));
|
|
|
|
});
|
2016-11-11 02:06:06 +00:00
|
|
|
},
|
2016-11-10 20:53:36 +00:00
|
|
|
|
2016-11-10 18:45:43 +00:00
|
|
|
testRegisterUser() {
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.anonymous()).then((user) => {
|
2016-11-11 02:06:06 +00:00
|
|
|
// Can we open a realm with the registered user?
|
2017-11-10 20:05:35 +00:00
|
|
|
const realm = new Realm({sync: {user: user, url: 'realm://localhost:9080/~/test'}});
|
2016-11-11 02:06:06 +00:00
|
|
|
TestCase.assertInstanceOf(realm, Realm);
|
2016-11-10 18:45:43 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testRegisterExistingUser() {
|
2017-11-10 20:05:35 +00:00
|
|
|
const username = uuid();
|
2018-09-13 06:45:06 +00:00
|
|
|
const credentials = Realm.Sync.Credentials.usernamePassword(username, 'password', true);
|
|
|
|
return Realm.Sync.User.login('http://localhost:9080', credentials).then((user) => {
|
2017-11-10 20:05:35 +00:00
|
|
|
assertIsUser(user);
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', credentials)
|
2017-11-10 20:05:35 +00:00
|
|
|
.then((user) => { throw new Error(user); })
|
|
|
|
.catch((e) => {
|
|
|
|
assertIsAuthError(e, 611, "The provided credentials are invalid or the user does not exist.");
|
|
|
|
})
|
2016-11-10 18:45:43 +00:00
|
|
|
});
|
2017-08-31 22:28:37 +00:00
|
|
|
},
|
2016-11-10 18:45:43 +00:00
|
|
|
|
|
|
|
testRegisterMissingUsername() {
|
2018-09-13 06:45:06 +00:00
|
|
|
TestCase.assertThrows(() => Realm.Sync.Credentials.usernamePassword(undefined, 'password'));
|
2016-11-10 18:45:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testRegisterMissingPassword() {
|
2018-09-13 06:45:06 +00:00
|
|
|
TestCase.assertThrows(() => Realm.Sync.Credentials.usernamePassword(uuid(), undefined));
|
2016-11-10 18:45:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testRegisterServerOffline() {
|
2016-11-11 02:06:06 +00:00
|
|
|
// Because it waits for answer this takes some time..
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://fake_host.local', Realm.Sync.Credentials.anonymous())
|
2017-11-10 20:05:35 +00:00
|
|
|
.catch((e) => {})
|
|
|
|
.then((user) => { if (user) { throw new Error('should not have been able to register'); }})
|
2016-11-10 18:45:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testLogin() {
|
2018-09-13 06:45:06 +00:00
|
|
|
const username = uuid();
|
|
|
|
const registerCredentials = Realm.Sync.Credentials.usernamePassword(username, 'password', true);
|
|
|
|
// Create user, logout the new user, then login
|
|
|
|
return Realm.Sync.User.login('http://localhost:9080', registerCredentials).then((user) => {
|
|
|
|
user.logout();
|
|
|
|
const loginCredentials = Realm.Sync.Credentials.usernamePassword(username, 'password', false);
|
|
|
|
return Realm.Sync.User.login('http://localhost:9080', loginCredentials);
|
|
|
|
}).then((user => {
|
|
|
|
assertIsUser(user);
|
|
|
|
// Can we open a realm with the logged-in user?
|
|
|
|
const config = user.createConfiguration({ sync: { url: 'realm://localhost:9080/~/test' }});
|
|
|
|
const realm = new Realm(config);
|
|
|
|
TestCase.assertInstanceOf(realm, Realm);
|
|
|
|
realm.close();
|
|
|
|
}))
|
2016-11-10 18:45:43 +00:00
|
|
|
},
|
|
|
|
|
2018-01-05 08:38:53 +00:00
|
|
|
testAuthenticateWithPassword() {
|
|
|
|
const username = uuid();
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.usernamePassword(username, 'password', true)).then((user) => {
|
2018-01-05 08:38:53 +00:00
|
|
|
user.logout();
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.usernamePassword(username, 'password'));
|
2018-01-05 08:38:53 +00:00
|
|
|
}).then((user => {
|
|
|
|
assertIsUser(user);
|
2018-05-30 10:54:51 +00:00
|
|
|
const realm = new Realm(user.createConfiguration({ sync: { url: 'realm://localhost:9080/~/test' } }));
|
2018-01-05 08:38:53 +00:00
|
|
|
TestCase.assertInstanceOf(realm, Realm);
|
|
|
|
realm.close();
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
|
2016-11-10 18:45:43 +00:00
|
|
|
testLoginMissingUsername() {
|
2017-11-10 20:05:35 +00:00
|
|
|
TestCase.assertThrows(() => Realm.Sync.User.login('http://localhost:9080', undefined, 'password'));
|
2016-11-10 18:45:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testLoginMissingPassword() {
|
2017-11-10 20:05:35 +00:00
|
|
|
const username = uuid();
|
|
|
|
TestCase.assertThrows(() => Realm.Sync.User.login('http://localhost:9080', username, undefined));
|
2016-11-10 18:45:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testLoginNonExistingUser() {
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.usernamePassword('foo', 'pass', false))
|
2017-11-10 20:05:35 +00:00
|
|
|
.then((user) => { throw new Error(user); })
|
|
|
|
.catch((e) => assertIsAuthError(e, 611, "The provided credentials are invalid or the user does not exist."))
|
2016-11-10 18:45:43 +00:00
|
|
|
},
|
|
|
|
|
2017-12-07 09:36:24 +00:00
|
|
|
testLoginTowardsMisbehavingServer() {
|
|
|
|
// Try authenticating towards a server thats clearly not ROS
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('https://github.com/realm/realm-js', Realm.Sync.Credentials.anonymous())
|
2017-12-07 09:36:24 +00:00
|
|
|
.catch((e) => {
|
|
|
|
assertIsError(e);
|
|
|
|
TestCase.assertEqual(
|
|
|
|
e.message,
|
|
|
|
"Could not authenticate: Realm Object Server didn't respond with valid JSON"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-01-05 08:38:53 +00:00
|
|
|
testAuthenticateJWT() {
|
|
|
|
let token = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJhdXN0aW5femhlbmciLCJpc0FkbWluIjp0cnVlLCJpYXQiOjE1MTI2OTI3NDl9.klca-3wYLe5mGdVk7N7dE9YRIlB1el1Dv6BxZNAKMsJ3Ms4vBTweu4-65kVJftiMrYhmSGY6QtTzqQ-xlLH4XzPd3jYIXlPQ45lxO7PW7EkJNs9m83VdcsJmHRHQ3PRP8V_mx0f2Ks4ga3xZ9IycAQB4q5NXLei_HJk8tRRJccZ6qB5nnAoD48Qu8JOEfhO596Mdoi-QCbH51iJZjgXo4gSRZ4KKK8jU0S6twLj_lf9jehENTqHDdtsRHdyCnICcPcz4AjFrNHEvUrsPkGxXSZ2BCGgDcvsSTVgGNV7rWU4IjH4FaDssenumi50R1QcZh8kiO35s9H6MngQsEm-zApRgd0V9_L3A6Ys47_crmKbunYRsATfMNBn2fKm5tS6RXvM2RN2G_Y9AkGgh2boY42CRy7HOcHby2vQ8IoQ-fZfE5xn_YYktNlKeNiCv3_-i86lANFbmB3tcdScrbjsgO6Tfg3u71VmJ_ZW1_vyMi5vCTEysLXfHG-OA85c3o8-25vcfuX5gIpbU-nMLgPagyn5w7Uazd27uhFfwepP9OMc8jz2JTlQICInLCUdESu8aG5d1F_IPUA5NU_ryPmebqUmyaRVDS8cGChxp0gZDNSiIvaggw8N2JCDGvk-s_PSG2pFGq0f4veYyWGBTHD_iX4a0UrhB471QZplRpMwvu7o'
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.jwt(token))
|
2018-01-24 07:07:19 +00:00
|
|
|
.then((user) => {
|
2018-01-05 08:38:53 +00:00
|
|
|
TestCase.assertEqual(user.identity, 'austin_zheng')
|
2018-01-24 07:07:19 +00:00
|
|
|
Promise.resolve()
|
2018-01-05 08:38:53 +00:00
|
|
|
})
|
|
|
|
.catch((e) => { Promise.reject(e) } )
|
|
|
|
},
|
|
|
|
|
2018-10-08 13:01:12 +00:00
|
|
|
testAuthenticateCustom() {
|
|
|
|
// Assert that we can create custom credentials without specifying userInfo
|
|
|
|
Realm.Sync.Credentials.custom("foo", "bar");
|
|
|
|
},
|
|
|
|
|
2018-09-21 12:24:37 +00:00
|
|
|
testAuthenticateAdminToken() {
|
|
|
|
if (!isNodeProcess) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// read admin token from ROS
|
|
|
|
|
|
|
|
let obj = JSON.parse(fs.readFileSync('../realm-object-server-data/keys/admin.json', 'utf8'));
|
|
|
|
let token = obj['ADMIN_TOKEN'];
|
|
|
|
|
|
|
|
let credentials = Realm.Sync.Credentials.adminToken(token);
|
2018-09-24 10:43:44 +00:00
|
|
|
let user = Realm.Sync.User.login('http://localhost:9080', credentials);
|
|
|
|
TestCase.assertTrue(user.isAdmin);
|
2018-09-21 12:24:37 +00:00
|
|
|
},
|
|
|
|
|
2016-11-10 20:53:36 +00:00
|
|
|
testAll() {
|
2017-11-10 20:05:35 +00:00
|
|
|
const all = Realm.Sync.User.all;
|
|
|
|
TestCase.assertArrayLength(Object.keys(all), 0);
|
|
|
|
|
|
|
|
let user1;
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.anonymous()).then((user) => {
|
2017-11-10 20:05:35 +00:00
|
|
|
const all = Realm.Sync.User.all;
|
|
|
|
TestCase.assertArrayLength(Object.keys(all), 1);
|
|
|
|
assertIsSameUser(all[user.identity], user);
|
|
|
|
user1 = user;
|
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.anonymous());
|
2017-11-10 20:05:35 +00:00
|
|
|
}).then((user2) => {
|
|
|
|
let all = Realm.Sync.User.all;
|
|
|
|
TestCase.assertArrayLength(Object.keys(all), 2);
|
|
|
|
// NOTE: the list of users is in latest-first order.
|
|
|
|
assertIsSameUser(all[user2.identity], user2);
|
|
|
|
assertIsSameUser(all[user1.identity], user1);
|
2016-11-11 02:06:06 +00:00
|
|
|
|
2017-11-10 20:05:35 +00:00
|
|
|
user2.logout();
|
2016-11-10 20:53:36 +00:00
|
|
|
all = Realm.Sync.User.all;
|
2016-11-10 21:55:22 +00:00
|
|
|
TestCase.assertArrayLength(Object.keys(all), 1);
|
|
|
|
assertIsSameUser(all[user1.identity], user1);
|
2016-11-10 20:53:36 +00:00
|
|
|
|
2017-11-10 20:05:35 +00:00
|
|
|
user1.logout();
|
|
|
|
all = Realm.Sync.User.all;
|
|
|
|
TestCase.assertArrayLength(Object.keys(all), 0);
|
|
|
|
});
|
2016-11-10 20:53:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testCurrent() {
|
2017-11-10 20:05:35 +00:00
|
|
|
TestCase.assertUndefined(Realm.Sync.User.current);
|
2016-11-10 20:53:36 +00:00
|
|
|
|
2017-11-10 20:05:35 +00:00
|
|
|
let user1;
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.anonymous()).then((user) => {
|
2017-11-10 20:05:35 +00:00
|
|
|
user1 = user;
|
|
|
|
assertIsSameUser(Realm.Sync.User.current, user1);
|
2016-11-11 02:06:06 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.anonymous());
|
2017-11-10 20:05:35 +00:00
|
|
|
}).then((user2) => {
|
|
|
|
TestCase.assertThrows(() => Realm.Sync.User.current, 'We expect Realm.Sync.User.current to throw if > 1 user.');
|
|
|
|
user2.logout();
|
2016-11-11 02:06:06 +00:00
|
|
|
|
2017-11-10 20:05:35 +00:00
|
|
|
assertIsSameUser(Realm.Sync.User.current, user1);
|
2017-04-06 11:25:40 +00:00
|
|
|
|
2017-11-10 20:05:35 +00:00
|
|
|
user1.logout();
|
|
|
|
TestCase.assertUndefined(Realm.Sync.User.current);
|
|
|
|
});
|
2017-01-31 21:56:09 +00:00
|
|
|
},
|
2016-11-10 20:53:36 +00:00
|
|
|
|
2018-01-24 07:07:19 +00:00
|
|
|
testGetExistingUser() {
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.anonymous()).then((user) => {
|
2018-01-24 07:07:19 +00:00
|
|
|
let identity = user.identity;
|
|
|
|
let user1 = Realm.Sync.User._getExistingUser('http://localhost:9080', identity);
|
|
|
|
assertIsSameUser(user1, user);
|
|
|
|
user.logout();
|
|
|
|
let user2 = Realm.Sync.User._getExistingUser('http://localhost:9080', identity);
|
|
|
|
TestCase.assertUndefined(user2);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-01-31 21:56:09 +00:00
|
|
|
testManagementRealm() {
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.anonymous()).then((user) => {
|
2017-01-31 21:56:09 +00:00
|
|
|
let realm = user.openManagementRealm();
|
|
|
|
TestCase.assertInstanceOf(realm, Realm);
|
|
|
|
|
2017-11-10 20:05:35 +00:00
|
|
|
TestCase.assertArraysEqual(realm.schema.map(o => o.name),
|
|
|
|
['PermissionChange', 'PermissionOffer', 'PermissionOfferResponse']);
|
2017-01-31 21:56:09 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-09-20 23:38:22 +00:00
|
|
|
testRetrieveAccount() {
|
2017-11-10 20:05:35 +00:00
|
|
|
if (!isNodeProcess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!global.testAdminUserInfo) {
|
|
|
|
throw new Error("Test requires an admin user");
|
|
|
|
}
|
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
const credentials = Realm.Sync.Credentials.usernamePassword(global.testAdminUserInfo.username, global.testAdminUserInfo.password);
|
|
|
|
return Realm.Sync.User.login('http://localhost:9080', credentials).then((user) => {
|
2017-11-10 20:05:35 +00:00
|
|
|
TestCase.assertTrue(user.isAdmin, "Test requires an admin user");
|
|
|
|
|
|
|
|
return user.retrieveAccount('password', global.testAdminUserInfo.username)
|
|
|
|
}).then((account) => {
|
|
|
|
TestCase.assertEqual(account.accounts[0].provider_id, global.testAdminUserInfo.username);
|
|
|
|
TestCase.assertEqual(account.accounts[0].provider, 'password');
|
|
|
|
TestCase.assertTrue(account.is_admin);
|
|
|
|
TestCase.assertTrue(account.user_id);
|
2017-09-20 23:38:22 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testRetrieveNotExistingAccount() {
|
2017-11-10 20:05:35 +00:00
|
|
|
if (!isNodeProcess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!global.testAdminUserInfo) {
|
|
|
|
throw new Error("Test requires an admin user");
|
|
|
|
}
|
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
const credentials = Realm.Sync.Credentials.usernamePassword(global.testAdminUserInfo.username, global.testAdminUserInfo.password);
|
|
|
|
return Realm.Sync.User.login('http://localhost:9080', credentials).then((user) => {
|
2017-11-10 20:05:35 +00:00
|
|
|
TestCase.assertTrue(user.isAdmin, "Test requires an admin user");
|
|
|
|
|
|
|
|
let notExistingUsername = uuid();
|
|
|
|
return user.retrieveAccount('password', notExistingUsername)
|
|
|
|
}).catch(e => {
|
|
|
|
TestCase.assertEqual(e.status, 404);
|
|
|
|
TestCase.assertEqual(e.code, 612);
|
|
|
|
TestCase.assertEqual(e.message, "The account does not exist.");
|
|
|
|
TestCase.assertEqual(e.type, "https://realm.io/docs/object-server/problems/unknown-account");
|
|
|
|
}).then(account => { if (account) { throw new Error("Retrieving nonexistent account should fail"); }});
|
2017-09-20 23:38:22 +00:00
|
|
|
},
|
2017-07-06 09:27:01 +00:00
|
|
|
|
2018-05-30 10:54:51 +00:00
|
|
|
testCreateConfiguration_defaultConfig() {
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.anonymous()).then((user) => {
|
2018-05-30 10:54:51 +00:00
|
|
|
let config = user.createConfiguration();
|
|
|
|
TestCase.assertEqual(config.sync.url, "realm://localhost:9080/default");
|
|
|
|
TestCase.assertUndefined(config.sync.partial);
|
|
|
|
TestCase.assertFalse(config.sync.fullSynchronization);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testCreateConfiguration_useOldConfiguration() {
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.anonymous()).then((user) => {
|
2018-05-30 10:54:51 +00:00
|
|
|
let config = user.createConfiguration({ sync: { url: 'http://localhost:9080/other_realm', partial: true }});
|
|
|
|
TestCase.assertEqual(config.sync.url, 'http://localhost:9080/other_realm');
|
|
|
|
TestCase.assertUndefined(config.sync.fullSynchronization);
|
|
|
|
TestCase.assertTrue(config.sync.partial);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testCreateConfiguration_settingPartialAndFullSynchronizationThrows() {
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.anonymous()).then((user) => {
|
2018-05-30 10:54:51 +00:00
|
|
|
TestCase.assertThrowsContaining(() => {
|
2018-08-28 13:01:32 +00:00
|
|
|
let config = {
|
|
|
|
sync: {
|
2018-05-30 10:54:51 +00:00
|
|
|
url: 'http://localhost:9080/~/default',
|
2018-08-28 13:01:32 +00:00
|
|
|
partial: true,
|
|
|
|
fullSynchronization: false
|
|
|
|
}
|
2018-05-30 10:54:51 +00:00
|
|
|
};
|
|
|
|
user.createConfiguration(config);
|
|
|
|
}, "'partial' and 'fullSynchronization' were both set. 'partial' has been deprecated, use only 'fullSynchronization'");
|
|
|
|
});
|
|
|
|
},
|
2018-08-28 13:01:32 +00:00
|
|
|
|
2018-05-30 10:54:51 +00:00
|
|
|
testOpen_partialAndFullSynchronizationSetThrows() {
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.anonymous()).then((user) => {
|
2018-05-30 10:54:51 +00:00
|
|
|
TestCase.assertThrowsContaining(() => {
|
|
|
|
new Realm({
|
|
|
|
sync: {
|
|
|
|
user: user,
|
|
|
|
url: 'http://localhost:9080/~/default',
|
|
|
|
partial: false,
|
|
|
|
fullSynchronization: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}, "'partial' and 'fullSynchronization' were both set. 'partial' has been deprecated, use only 'fullSynchronization'");
|
|
|
|
});
|
2018-08-28 13:01:32 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testSerialize() {
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.anonymous()).then((user) => {
|
2018-08-28 13:01:32 +00:00
|
|
|
const serialized = user.serialize();
|
|
|
|
TestCase.assertFalse(serialized.isAdmin);
|
|
|
|
TestCase.assertEqual(serialized.identity, user.identity);
|
|
|
|
TestCase.assertEqual(serialized.server, 'http://localhost:9080');
|
|
|
|
TestCase.assertEqual(serialized.refreshToken, user.token);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testDeserialize() {
|
2018-09-13 06:45:06 +00:00
|
|
|
return Realm.Sync.User.login('http://localhost:9080', Realm.Sync.Credentials.anonymous())
|
2018-08-28 13:01:32 +00:00
|
|
|
.then((user) => {
|
|
|
|
const userConfig = user.createConfiguration({
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
sync: {
|
|
|
|
url: 'realm://localhost:9080/~/foo',
|
|
|
|
fullSynchronization: true,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const realm = new Realm(userConfig);
|
|
|
|
realm.write(() => {
|
|
|
|
realm.create('Dog', {
|
|
|
|
name: 'Doggo'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const session = realm.syncSession;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let callback = (transferred, total) => {
|
|
|
|
if (transferred >= total) {
|
|
|
|
session.removeProgressNotification(callback);
|
|
|
|
realm.close();
|
|
|
|
Realm.deleteFile(userConfig);
|
|
|
|
resolve(user.serialize());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
session.addProgressNotification('upload', 'forCurrentlyOutstandingWork', callback);
|
|
|
|
});
|
|
|
|
}).then((serialized) => {
|
|
|
|
const deserialized = Realm.Sync.User.deserialize(serialized);
|
|
|
|
const config = deserialized.createConfiguration({
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
sync: {
|
|
|
|
url: 'realm://localhost:9080/~/foo',
|
|
|
|
fullSynchronization: true,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return Realm.open(config);
|
|
|
|
}).then((realm) => {
|
|
|
|
const dogs = realm.objects('Dog');
|
|
|
|
TestCase.assertEqual(dogs.length, 1);
|
|
|
|
TestCase.assertEqual(dogs[0].name, 'Doggo');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testDeserializeInvalidInput() {
|
|
|
|
const dummy = {
|
|
|
|
server: '123',
|
|
|
|
identity: '123',
|
|
|
|
refreshToken: '123',
|
|
|
|
isAdmin: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const name of Object.getOwnPropertyNames(dummy)) {
|
|
|
|
const clone = Object.assign({}, dummy);
|
|
|
|
// Set to invalid type
|
|
|
|
clone[name] = 123;
|
|
|
|
|
|
|
|
TestCase.assertThrowsContaining(() => Realm.Sync.User.deserialize(clone), `${name} must be of type '${typeof dummy[name]}'`);
|
|
|
|
|
|
|
|
// Set to undefined
|
|
|
|
clone[name] = undefined;
|
|
|
|
TestCase.assertThrowsContaining(() => Realm.Sync.User.deserialize(clone), `${name} is required, but a value was not provided.`);
|
|
|
|
}
|
2018-05-30 10:54:51 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 21:56:09 +00:00
|
|
|
/* This test fails because of realm-object-store #243 . We should use 2 users.
|
2016-11-11 17:16:40 +00:00
|
|
|
testSynchronizeChangesWithTwoClientsAndOneUser() {
|
|
|
|
// Test Schema
|
|
|
|
class Foo {}
|
|
|
|
Foo.schema = {
|
|
|
|
name: 'Foo',
|
|
|
|
properties: {
|
|
|
|
string: 'string',
|
|
|
|
bars: { type: 'list', objectType: 'Bar' },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
class Bar {}
|
|
|
|
Bar.schema = {
|
|
|
|
name: 'Bar',
|
|
|
|
properties: { integer: 'int' },
|
|
|
|
};
|
|
|
|
|
|
|
|
const schema = [Foo.schema, Bar.schema];
|
2016-11-29 09:51:26 +00:00
|
|
|
|
2016-11-11 17:16:40 +00:00
|
|
|
// Create a user, open two clients at different local paths, synchronize changes
|
|
|
|
const username = uuid();
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error ,user) => {
|
|
|
|
failOnError(error);
|
2016-11-29 09:51:26 +00:00
|
|
|
|
2016-11-11 17:16:40 +00:00
|
|
|
const clientA = new Realm({
|
|
|
|
path: 'testSynchronizeChangesWithTwoClientsAndOneUser_clientA.realm',
|
|
|
|
schema: schema,
|
|
|
|
sync: {
|
|
|
|
user: user,
|
|
|
|
url: 'http://localhost:9080/~/test',
|
|
|
|
},
|
|
|
|
});
|
2016-11-29 09:51:26 +00:00
|
|
|
|
2016-11-11 17:16:40 +00:00
|
|
|
const clientB = new Realm({
|
|
|
|
path: 'testSynchronizeChangesWithTwoClientsAndOneUser_clientB.realm',
|
|
|
|
schema: schema,
|
|
|
|
sync: {
|
|
|
|
user: user,
|
|
|
|
url: 'http://localhost:9080/~/test',
|
|
|
|
},
|
|
|
|
});
|
2016-11-29 09:51:26 +00:00
|
|
|
|
2016-11-11 17:16:40 +00:00
|
|
|
clientB.addListener('change', () => {
|
|
|
|
const foos = clientB.objects('Foo');
|
|
|
|
if (foos.length > 0) {
|
|
|
|
TestCase.assertEqual(foos.length, 1);
|
|
|
|
TestCase.assertEqual(foos[0].string, 'Hello, World!');
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
2016-11-29 09:51:26 +00:00
|
|
|
|
2016-11-11 17:16:40 +00:00
|
|
|
clientA.write(() => {
|
|
|
|
clientA.create('Foo', { string: 'Hello, World!' });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-11-29 09:51:26 +00:00
|
|
|
}, */
|
2016-11-11 17:16:40 +00:00
|
|
|
|
2016-11-08 16:59:30 +00:00
|
|
|
};
|