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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
testLocalRealmHasNoSession() {
|
|
|
|
let realm = new Realm();
|
|
|
|
TestCase.assertNull(realm.syncSession);
|
|
|
|
},
|
|
|
|
|
|
|
|
testProperties() {
|
|
|
|
return promisifiedRegister('http://localhost:9080', uuid(), 'password').then(user => {
|
2017-05-06 23:26:14 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
2017-04-04 12:00:00 +00:00
|
|
|
const accessTokenRefreshed = this;
|
2017-05-06 23:26:14 +00:00
|
|
|
let successCounter = 0;
|
|
|
|
function checkSuccess() {
|
|
|
|
successCounter++;
|
|
|
|
if (successCounter == 2) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}
|
2017-03-24 11:49:28 +00:00
|
|
|
|
2017-04-04 12:00:00 +00:00
|
|
|
function postTokenRefreshChecks(sender, error) {
|
2017-04-20 14:02:18 +00:00
|
|
|
try {
|
|
|
|
TestCase.assertEqual(error, accessTokenRefreshed);
|
|
|
|
TestCase.assertEqual(sender.url, `realm://localhost:9080/${user.identity}/myrealm`);
|
2017-05-06 23:26:14 +00:00
|
|
|
checkSuccess();
|
2017-04-20 14:02:18 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
2017-05-06 23:26:14 +00:00
|
|
|
reject(e)
|
2017-04-20 14:02:18 +00:00
|
|
|
}
|
2017-03-24 11:49:28 +00:00
|
|
|
};
|
2017-02-01 13:18:59 +00:00
|
|
|
|
2017-03-24 11:49:28 +00:00
|
|
|
// Let the error handler trigger our checks when the access token was refreshed.
|
|
|
|
postTokenRefreshChecks._notifyOnAccessTokenRefreshed = accessTokenRefreshed;
|
2017-02-01 13:18:59 +00:00
|
|
|
|
2017-03-24 11:49:28 +00:00
|
|
|
const config = { sync: { user, url: 'realm://localhost:9080/~/myrealm', error: postTokenRefreshChecks } };
|
|
|
|
const realm = new Realm(config);
|
2017-04-04 12:00:00 +00:00
|
|
|
const session = realm.syncSession;
|
2017-03-24 11:49:28 +00:00
|
|
|
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-05-06 23:26:14 +00:00
|
|
|
checkSuccess();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testPropertiesWithOpen() {
|
|
|
|
return promisifiedRegister('http://localhost:9080', uuid(), 'password').then(user => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
const accessTokenRefreshed = this;
|
|
|
|
let successCounter = 0;
|
|
|
|
|
|
|
|
function checkSuccess() {
|
|
|
|
successCounter++;
|
|
|
|
if (successCounter == 2) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function postTokenRefreshChecks(sender, error) {
|
|
|
|
try {
|
|
|
|
TestCase.assertEqual(error, accessTokenRefreshed);
|
|
|
|
TestCase.assertEqual(sender.url, `realm://localhost:9080/${user.identity}/myrealm`);
|
|
|
|
checkSuccess();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
reject(e)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Let the error handler trigger our checks when the access token was refreshed.
|
|
|
|
postTokenRefreshChecks._notifyOnAccessTokenRefreshed = accessTokenRefreshed;
|
|
|
|
|
|
|
|
const config = { sync: { user, url: 'realm://localhost:9080/~/myrealm', error: postTokenRefreshChecks } };
|
|
|
|
Realm.open(config)
|
|
|
|
.then(realm => {
|
|
|
|
const 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.assertEqual(session.state, 'active');
|
|
|
|
checkSuccess();
|
|
|
|
})
|
|
|
|
.catch(e => reject(e));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
testPropertiesWithOpenAsync() {
|
|
|
|
return promisifiedRegister('http://localhost:9080', uuid(), 'password').then(user => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
const accessTokenRefreshed = this;
|
|
|
|
let successCounter = 0;
|
|
|
|
|
|
|
|
function checkSuccess() {
|
|
|
|
successCounter++;
|
|
|
|
if (successCounter == 2) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function postTokenRefreshChecks(sender, error) {
|
|
|
|
try {
|
|
|
|
TestCase.assertEqual(error, accessTokenRefreshed);
|
|
|
|
TestCase.assertEqual(sender.url, `realm://localhost:9080/${user.identity}/myrealm`);
|
|
|
|
checkSuccess();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
reject(e)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Let the error handler trigger our checks when the access token was refreshed.
|
|
|
|
postTokenRefreshChecks._notifyOnAccessTokenRefreshed = accessTokenRefreshed;
|
|
|
|
|
|
|
|
const config = { sync: { user, url: 'realm://localhost:9080/~/myrealm', error: postTokenRefreshChecks } };
|
|
|
|
Realm.openAsync(config, (error, realm) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const 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.assertEqual(session.state, 'active');
|
|
|
|
|
|
|
|
checkSuccess();
|
|
|
|
});
|
2017-02-01 13:18:59 +00:00
|
|
|
});
|
2017-04-20 14:03:15 +00:00
|
|
|
});
|
2017-02-01 13:18:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testErrorHandling() {
|
|
|
|
return promisifiedRegister('http://localhost:9080', uuid(), 'password').then(user => {
|
2017-04-20 14:02:18 +00:00
|
|
|
return new Promise((resolve, _reject) => {
|
2017-04-04 12:00:00 +00:00
|
|
|
const config = { sync: { user, url: 'realm://localhost:9080/~/myrealm' } };
|
|
|
|
config.sync.error = (sender, error) => {
|
2017-04-20 14:02:18 +00:00
|
|
|
try {
|
|
|
|
TestCase.assertEqual(error.message, 'simulated error');
|
|
|
|
TestCase.assertEqual(error.code, 123);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
_reject(e);
|
|
|
|
}
|
2017-04-04 12:00:00 +00:00
|
|
|
};
|
|
|
|
const realm = new Realm(config);
|
|
|
|
const session = realm.syncSession;
|
2017-02-01 13:18:59 +00:00
|
|
|
|
2017-04-04 12:00:00 +00:00
|
|
|
TestCase.assertEqual(session.config.error, config.sync.error);
|
|
|
|
session._simulateError(123, 'simulated error');
|
2017-02-01 13:18:59 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-03-07 16:41:19 +00:00
|
|
|
}
|