2017-02-01 13:18:59 +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');
|
|
|
|
|
|
|
|
function uuid() {
|
|
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
|
|
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
|
|
|
return v.toString(16);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function promisifiedRegister(server, username, password) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Realm.Sync.User.register(server, username, password, (error, user) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve(user);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function wait(delay) {
|
|
|
|
return new Promise((resolve, reject) => setTimeout(resolve, delay));
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
testLocalRealmHasNoSession() {
|
|
|
|
let realm = new Realm();
|
|
|
|
TestCase.assertNull(realm.syncSession);
|
|
|
|
},
|
|
|
|
|
|
|
|
testProperties() {
|
|
|
|
return promisifiedRegister('http://localhost:9080', uuid(), 'password').then(user => {
|
|
|
|
let config = { sync: { user, url: 'realm://localhost:9080/~/myrealm' } };
|
|
|
|
let realm = new Realm(config);
|
|
|
|
let session = realm.syncSession;
|
|
|
|
|
|
|
|
TestCase.assertInstanceOf(session, Realm.Sync.Session);
|
|
|
|
TestCase.assertEqual(session.user.identity, user.identity);
|
|
|
|
TestCase.assertEqual(session.config.url, config.sync.url);
|
|
|
|
TestCase.assertEqual(session.config.user.identity, config.sync.user.identity);
|
|
|
|
TestCase.assertUndefined(session.url);
|
|
|
|
TestCase.assertEqual(session.state, 'active');
|
|
|
|
|
2017-03-07 16:41:19 +00:00
|
|
|
/* disable until the sporadic failures are resolved
|
2017-02-01 13:18:59 +00:00
|
|
|
// give the session enough time to refresh its access token and bind itself
|
2017-02-08 15:35:36 +00:00
|
|
|
// TODO: Use an event to discover when the session is bound
|
2017-02-09 11:11:31 +00:00
|
|
|
let timeout = 4000;
|
2017-02-08 15:35:36 +00:00
|
|
|
|
2017-02-01 22:44:56 +00:00
|
|
|
return wait(timeout).then(() => {
|
2017-02-01 13:18:59 +00:00
|
|
|
TestCase.assertEqual(session.url, `realm://localhost:9080/${user.identity}/myrealm`);
|
|
|
|
});
|
2017-03-07 16:41:19 +00:00
|
|
|
*/
|
2017-02-01 13:18:59 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testErrorHandling() {
|
|
|
|
return promisifiedRegister('http://localhost:9080', uuid(), 'password').then(user => {
|
|
|
|
let errors = [];
|
|
|
|
let config = { sync: { user,
|
|
|
|
url: 'realm://localhost:9080/~/myrealm',
|
|
|
|
error: (sender, error) => errors.push([sender, error])
|
|
|
|
} };
|
|
|
|
let realm = new Realm(config);
|
|
|
|
let session = realm.syncSession;
|
|
|
|
|
2017-02-07 10:01:26 +00:00
|
|
|
TestCase.assertEqual(session.config.error, config.sync.error);
|
2017-02-01 13:18:59 +00:00
|
|
|
session._simulateError(123, 'simulated error');
|
|
|
|
|
2017-02-09 11:11:31 +00:00
|
|
|
return wait(1000).then(() => {
|
2017-02-01 13:18:59 +00:00
|
|
|
TestCase.assertArrayLength(errors, 1);
|
|
|
|
TestCase.assertEqual(errors[0][0].config.url, session.config.url);
|
|
|
|
TestCase.assertEqual(errors[0][1].message, 'simulated error');
|
|
|
|
TestCase.assertEqual(errors[0][1].code, 123);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-03-07 16:41:19 +00:00
|
|
|
}
|