Add tests for retrieveAccount method

fix admin-user-helper wait for admin to correctly wait
This commit is contained in:
blagoev 2017-07-06 12:27:01 +03:00
parent 5cbfd540f1
commit e31529397f
5 changed files with 100 additions and 21 deletions

View File

@ -2,4 +2,4 @@ PACKAGE_NAME=realm-js
VERSION=1.8.3
REALM_CORE_VERSION=2.8.4
REALM_SYNC_VERSION=1.10.1
REALM_OBJECT_SERVER_VERSION=1.7.6
REALM_OBJECT_SERVER_VERSION=1.8.1

View File

@ -27,7 +27,7 @@ function node_require(module) {
function checkTypes(args, types) {
args = Array.prototype.slice.call(args);
for (var i = 0; i < types.length; ++i) {
if (typeof args[i] !== types[i]) {
if (typeof args[i] !== types[i]) {
throw new TypeError('param ' + i + ' must be of type ' + types[i]);
}
}
@ -37,13 +37,13 @@ const performFetch = typeof fetch === 'undefined' ? node_require('node-fetch') :
const url_parse = require('url-parse');
const postHeaders = {
const postHeaders = {
'content-type': 'application/json;charset=utf-8',
'accept': 'application/json'
};
function auth_url(server) {
if (server.charAt(server.length-1) != '/') {
if (server.charAt(server.length - 1) != '/') {
return server + '/auth';
}
return server + 'auth';
@ -122,7 +122,7 @@ function _authenticate(userConstructor, server, json, callback) {
open_timeout: 5000
};
performFetch(url, options)
.then((response) => {
.then((response) => {
if (response.status !== 200) {
return response.json().then((body) => callback(new AuthError(body)));
} else {
@ -132,7 +132,7 @@ function _authenticate(userConstructor, server, json, callback) {
const identity = body.refresh_token.token_data.identity;
const isAdmin = body.refresh_token.token_data.is_admin;
callback(undefined, userConstructor.createUser(server, identity, token, false, isAdmin));
})
})
}
})
.catch(callback);
@ -154,8 +154,8 @@ module.exports = {
adminUser(token) {
checkTypes(arguments, ['string']);
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
var uuid = '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);
});
var user = this.createUser('', uuid, token, true);
@ -164,18 +164,18 @@ module.exports = {
register(server, username, password, callback) {
checkTypes(arguments, ['string', 'string', 'string', 'function']);
_authenticate(this, server, {
provider: 'password',
user_info: { password: password, register: true },
_authenticate(this, server, {
provider: 'password',
user_info: { password: password, register: true },
data: username
}, callback);
},
login(server, username, password, callback) {
checkTypes(arguments, ['string', 'string', 'string', 'function']);
_authenticate(this, server, {
provider: 'password',
user_info: { password: password },
_authenticate(this, server, {
provider: 'password',
user_info: { password: password },
data: username
}, callback);
},
@ -190,7 +190,7 @@ module.exports = {
provider: arguments[1],
providerToken: arguments[2]
};
callback = arguments[3];
callback = arguments[3];
} else {
checkTypes(arguments, ['string', 'object', 'function']);
}
@ -232,11 +232,11 @@ module.exports = {
},
retrieveAccount(provider, provider_id) {
checkTypes(arguments, ['string', 'string']);
let url = url_parse(this.server);
url.set('pathname', `/api/providers/${provider}/accounts/${provider_id}`);
let headers = {
Authorization : this.token
Authorization: this.token
};
const options = {
@ -245,10 +245,14 @@ module.exports = {
open_timeout: 5000
};
return performFetch(url.href, options)
.then((response) => {
.then((response) => {
if (response.status !== 200) {
return response.json().then((body) => callback(new AuthError(body)));
return response.json()
.then(body => {
throw new AuthError(body);
});
} else {
return response.json();
}
});

View File

@ -47,6 +47,7 @@ exports.createAdminUser = function () {
setTimeout(_ => {
waitForServerToUpdateAdminUser();
}, 200);
return;
}
resolve({

View File

@ -41,7 +41,7 @@ if (Realm.Sync) {
TESTS.SessionTests = require('./session-tests');
}
function node_require(module) { return require(module); }
function node_require(module) { return require(module); }
// If on node, run the async tests
if (typeof process === 'object' && process + '' === '[object process]') {
@ -73,6 +73,18 @@ exports.registerTests = function(tests) {
}
};
exports.prepare = function(done) {
let helper = require('./admin-user-helper');
helper.createAdminUser().then(user => {
global.testAdminUserInfo = user;
done();
})
.catch(error => {
console.error("Error running admin-user-helper: " + error);
done();
});
};
exports.runTest = function(suiteName, testName) {
var testSuite = TESTS[suiteName];
var testMethod = testSuite && testSuite[testName];
@ -103,4 +115,4 @@ exports.runTest = function(suiteName, testName) {
} else if (!testSuite || !(testName in SPECIAL_METHODS)) {
throw new Error('Missing test: ' + suiteName + '.' + testName);
}
};
}

View File

@ -295,6 +295,68 @@ module.exports = {
});
},
testRetrieveAccount() {
return new Promise((resolve, reject) => {
if (!global.testAdminUserInfo) {
reject("Test requires an admin user");
}
Realm.Sync.User.login('http://localhost:9080', global.testAdminUserInfo.username, global.testAdminUserInfo.password, (error, user) => {
if (error) {
reject(error);
}
TestCase.assertTrue(user.isAdmin, "Test requires an admin user");
user.retrieveAccount('password', global.testAdminUserInfo.username)
.then(account => {
// {
// "provider_id": "admin",
// "provider": "password",
// "user": {
// "id": "07ac9a0a-a97a-4ee1-b53c-b05a6542035a",
// "isAdmin": true,
// }
// }
TestCase.assertEqual(account.provider_id, global.testAdminUserInfo.username);
TestCase.assertEqual(account.provider, 'password');
TestCase.assertTrue(account.user);
TestCase.assertTrue(account.user.isAdmin !== undefined);
TestCase.assertTrue(account.user.id);
resolve();
})
.catch(e => reject(e));
})
});
},
testRetrieveNotExistingAccount() {
return new Promise((resolve, reject) => {
if (!global.testAdminUserInfo) {
reject("Test requires an admin user");
}
Realm.Sync.User.login('http://localhost:9080', global.testAdminUserInfo.username, global.testAdminUserInfo.password, (error, user) => {
if (error) {
reject(error);
}
TestCase.assertTrue(user.isAdmin, "Test requires an admin user");
let notExistingUsername = uuid();
user.retrieveAccount('password', notExistingUsername)
.then(account => {
reject("Retrieving not existing account should fail");
})
.catch(e => {
TestCase.assertEqual(e.code, 404);
resolve()
});
})
});
},
/* This test fails because of realm-object-store #243 . We should use 2 users.
testSynchronizeChangesWithTwoClientsAndOneUser() {
// Test Schema