Reformat code, 2 spaces
This commit is contained in:
parent
fb47fb10cc
commit
448f68a787
|
@ -25,167 +25,167 @@ const TestCase = require('./asserts');
|
||||||
const schemas = require('./schemas');
|
const schemas = require('./schemas');
|
||||||
|
|
||||||
function uuid() {
|
function uuid() {
|
||||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
||||||
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
|
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
||||||
return v.toString(16);
|
return v.toString(16);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function assertIsUser(user, isAdmin) {
|
function assertIsUser(user, isAdmin) {
|
||||||
TestCase.assertType(user, 'object');
|
TestCase.assertType(user, 'object');
|
||||||
TestCase.assertType(user.token, 'string');
|
TestCase.assertType(user.token, 'string');
|
||||||
TestCase.assertType(user.identity, 'string');
|
TestCase.assertType(user.identity, 'string');
|
||||||
TestCase.assertInstanceOf(user, Realm.Sync.User);
|
TestCase.assertInstanceOf(user, Realm.Sync.User);
|
||||||
if (isAdmin !== undefined) {
|
if (isAdmin !== undefined) {
|
||||||
TestCase.assertEqual(user.isAdmin, isAdmin);
|
TestCase.assertEqual(user.isAdmin, 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) {
|
||||||
TestCase.assertEqual(error.code, code);
|
TestCase.assertEqual(error.code, code);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function assertIsAuthError(error, code, type) {
|
function assertIsAuthError(error, code, type) {
|
||||||
TestCase.assertInstanceOf(error, Realm.Sync.AuthError, 'The API should return an AuthError');
|
TestCase.assertInstanceOf(error, Realm.Sync.AuthError, 'The API should return an AuthError');
|
||||||
if (code) {
|
if (code) {
|
||||||
TestCase.assertEqual(error.code, code);
|
TestCase.assertEqual(error.code, code);
|
||||||
}
|
}
|
||||||
if (type) {
|
if (type) {
|
||||||
TestCase.assertEqual(error.type, type);
|
TestCase.assertEqual(error.type, type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
testRegisterUser() {
|
testRegisterUser() {
|
||||||
var username = uuid();
|
var username = uuid();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
|
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(error);
|
reject(error);
|
||||||
}
|
}
|
||||||
assertIsUser(user);
|
assertIsUser(user);
|
||||||
|
|
||||||
// Can we open a realm with the registered user?
|
// Can we open a realm with the registered user?
|
||||||
var realm = new Realm({sync: {user: user, url: 'realm://localhost:9080/~/test'}});
|
var realm = new Realm({sync: {user: user, url: 'realm://localhost:9080/~/test'}});
|
||||||
TestCase.assertInstanceOf(realm, Realm);
|
TestCase.assertInstanceOf(realm, Realm);
|
||||||
resolve();
|
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();
|
||||||
});
|
});
|
||||||
},
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
testRegisterExistingUser() {
|
testRegisterMissingUsername() {
|
||||||
var username = uuid();
|
return new Promise((resolve, reject) => {
|
||||||
return new Promise((resolve, reject) => {
|
Realm.Sync.User.register('http://localhost:9080', undefined, 'password', (error, user) => {
|
||||||
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
|
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
|
||||||
if (error) {
|
TestCase.assertUndefined(user);
|
||||||
reject(error);
|
resolve();
|
||||||
}
|
});
|
||||||
assertIsUser(user);
|
});
|
||||||
|
},
|
||||||
|
|
||||||
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
|
testRegisterMissingPassword() {
|
||||||
assertIsAuthError(error, 613, 'https://realm.io/docs/object-server/problems/existing-account');
|
var username = uuid();
|
||||||
TestCase.assertUndefined(user);
|
return new Promise((resolve, reject) => {
|
||||||
resolve();
|
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();
|
||||||
});
|
});
|
||||||
},
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
testRegisterMissingUsername() {
|
testLoginMissingUsername() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
Realm.Sync.User.register('http://localhost:9080', undefined, 'password', (error, user) => {
|
Realm.Sync.User.login('http://localhost:9080', undefined, 'password', (error, user) => {
|
||||||
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
|
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
|
||||||
TestCase.assertUndefined(user);
|
TestCase.assertUndefined(user);
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
testRegisterMissingPassword() {
|
testLoginMissingPassword() {
|
||||||
var username = uuid();
|
var username = uuid();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
Realm.Sync.User.register('http://localhost:9080', username, undefined, (error, user) => {
|
Realm.Sync.User.login('http://localhost:9080', username, undefined, (error, user) => {
|
||||||
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
|
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
|
||||||
TestCase.assertUndefined(user);
|
TestCase.assertUndefined(user);
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
testRegisterServerOffline() {
|
testLoginNonExistingUser() {
|
||||||
var username = uuid();
|
return new Promise((resolve, reject) => {
|
||||||
return new Promise((resolve, reject) => {
|
Realm.Sync.User.login('http://localhost:9080', 'does_not', 'exist', (error, user) => {
|
||||||
// Because it waits for answer this takes some time..
|
assertIsAuthError(error, 612, 'https://realm.io/docs/object-server/problems/unknown-account');
|
||||||
Realm.Sync.User.register('http://fake_host.local', username, 'password', (error, user) => {
|
TestCase.assertUndefined(user);
|
||||||
assertIsError(error, 'ECONNRESET');
|
resolve();
|
||||||
TestCase.assertUndefined(user);
|
});
|
||||||
resolve();
|
});
|
||||||
});
|
},
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
testLogin() {
|
testLoginServerOffline() {
|
||||||
var username = uuid();
|
var username = uuid();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// Create user, logout the new user, then login
|
// Because it waits for answer this takes some time..
|
||||||
Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => {
|
Realm.Sync.User.register('http://fake_host.local', username, 'password', (error, user) => {
|
||||||
user.logout();
|
assertIsError(error, 'ECONNRESET');
|
||||||
Realm.Sync.User.login('http://localhost:9080', username, 'password', (error, user) => {
|
TestCase.assertUndefined(user);
|
||||||
assertIsUser(user);
|
resolve();
|
||||||
|
});
|
||||||
var realm = new Realm({sync: {user: user, url: 'realm://localhost:9080/~/test'}});
|
});
|
||||||
TestCase.assertInstanceOf(realm, Realm);
|
},
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
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();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue