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');
|
|
|
|
const schemas = require('./schemas');
|
|
|
|
|
|
|
|
function uuid() {
|
2016-11-10 18:45:43 +00:00
|
|
|
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);
|
|
|
|
});
|
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);
|
|
|
|
}
|
|
|
|
|
2016-11-10 18:34:39 +00:00
|
|
|
function assertIsError(error, code) {
|
2016-11-10 18:45:43 +00:00
|
|
|
TestCase.assertInstanceOf(error, Error, 'The API should return an Error');
|
|
|
|
if (code) {
|
|
|
|
TestCase.assertEqual(error.code, code);
|
|
|
|
}
|
2016-11-10 18:34:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function assertIsAuthError(error, code, type) {
|
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);
|
|
|
|
}
|
|
|
|
if (type) {
|
|
|
|
TestCase.assertEqual(error.type, type);
|
|
|
|
}
|
2016-11-10 18:34:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 16:59:30 +00:00
|
|
|
module.exports = {
|
2016-11-10 20:53:36 +00:00
|
|
|
|
2016-11-10 18:45:43 +00:00
|
|
|
testRegisterUser() {
|
|
|
|
var username = uuid();
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
assertIsUser(user);
|
|
|
|
|
|
|
|
// Can we open a realm with the registered user?
|
|
|
|
var realm = new Realm({sync: {user: user, url: 'realm://localhost:9080/~/test'}});
|
|
|
|
TestCase.assertInstanceOf(realm, Realm);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testRegisterExistingUser() {
|
|
|
|
var username = uuid();
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
assertIsUser(user);
|
|
|
|
|
|
|
|
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
|
|
|
|
assertIsAuthError(error, 613, 'https://realm.io/docs/object-server/problems/existing-account');
|
|
|
|
TestCase.assertUndefined(user);
|
|
|
|
resolve();
|
2016-11-10 18:34:39 +00:00
|
|
|
});
|
2016-11-10 18:45:43 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testRegisterMissingUsername() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Realm.Sync.User.register('http://localhost:9080', undefined, 'password', (error, user) => {
|
|
|
|
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
|
|
|
|
TestCase.assertUndefined(user);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testRegisterMissingPassword() {
|
|
|
|
var username = uuid();
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Realm.Sync.User.register('http://localhost:9080', username, undefined, (error, user) => {
|
|
|
|
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
|
|
|
|
TestCase.assertUndefined(user);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testRegisterServerOffline() {
|
|
|
|
var username = uuid();
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Because it waits for answer this takes some time..
|
|
|
|
Realm.Sync.User.register('http://fake_host.local', username, 'password', (error, user) => {
|
|
|
|
assertIsError(error, 'ECONNRESET');
|
|
|
|
TestCase.assertUndefined(user);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testLogin() {
|
|
|
|
var username = uuid();
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Create user, logout the new user, then login
|
|
|
|
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
|
|
|
|
user.logout();
|
|
|
|
Realm.Sync.User.login('http://localhost:9080', username, 'password', (error, user) => {
|
|
|
|
assertIsUser(user);
|
|
|
|
|
|
|
|
var realm = new Realm({sync: {user: user, url: 'realm://localhost:9080/~/test'}});
|
|
|
|
TestCase.assertInstanceOf(realm, Realm);
|
|
|
|
resolve();
|
2016-11-10 18:34:39 +00:00
|
|
|
});
|
2016-11-10 18:45:43 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testLoginMissingUsername() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Realm.Sync.User.login('http://localhost:9080', undefined, 'password', (error, user) => {
|
|
|
|
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
|
|
|
|
TestCase.assertUndefined(user);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testLoginMissingPassword() {
|
|
|
|
var username = uuid();
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Realm.Sync.User.login('http://localhost:9080', username, undefined, (error, user) => {
|
|
|
|
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
|
|
|
|
TestCase.assertUndefined(user);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testLoginNonExistingUser() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Realm.Sync.User.login('http://localhost:9080', 'does_not', 'exist', (error, user) => {
|
|
|
|
assertIsAuthError(error, 612, 'https://realm.io/docs/object-server/problems/unknown-account');
|
|
|
|
TestCase.assertUndefined(user);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testLoginServerOffline() {
|
|
|
|
var username = uuid();
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Because it waits for answer this takes some time..
|
|
|
|
Realm.Sync.User.register('http://fake_host.local', username, 'password', (error, user) => {
|
|
|
|
assertIsError(error, 'ECONNRESET');
|
|
|
|
TestCase.assertUndefined(user);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2016-11-10 18:34:39 +00:00
|
|
|
|
2016-11-10 20:53:36 +00:00
|
|
|
testAll() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let all;
|
|
|
|
all = Realm.Sync.User.all;
|
2016-11-10 21:55:22 +00:00
|
|
|
TestCase.assertArrayLength(Object.keys(all), 0);
|
2016-11-10 20:53:36 +00:00
|
|
|
|
|
|
|
Realm.Sync.User.register('http://localhost:9080', uuid(), 'password', (error, user1) => {
|
|
|
|
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
|
|
|
|
|
|
|
Realm.Sync.User.register('http://localhost:9080', uuid(), 'password', (error, user2) => {
|
|
|
|
all = Realm.Sync.User.all;
|
2016-11-10 21:55:22 +00:00
|
|
|
TestCase.assertArrayLength(Object.keys(all), 2);
|
2016-11-10 20:53:36 +00:00
|
|
|
// NOTE: the list of users is in latest-first order.
|
2016-11-10 21:55:22 +00:00
|
|
|
assertIsSameUser(all[user2.identity], user2);
|
|
|
|
assertIsSameUser(all[user1.identity], user1);
|
2016-11-10 20:53:36 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-11-08 16:59:30 +00:00
|
|
|
};
|
|
|
|
|