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

View File

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

View File

@ -41,7 +41,7 @@ if (Realm.Sync) {
TESTS.SessionTests = require('./session-tests'); 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 on node, run the async tests
if (typeof process === 'object' && process + '' === '[object process]') { 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) { exports.runTest = function(suiteName, testName) {
var testSuite = TESTS[suiteName]; var testSuite = TESTS[suiteName];
var testMethod = testSuite && testSuite[testName]; var testMethod = testSuite && testSuite[testName];
@ -103,4 +115,4 @@ exports.runTest = function(suiteName, testName) {
} else if (!testSuite || !(testName in SPECIAL_METHODS)) { } else if (!testSuite || !(testName in SPECIAL_METHODS)) {
throw new Error('Missing test: ' + suiteName + '.' + testName); 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. /* This test fails because of realm-object-store #243 . We should use 2 users.
testSynchronizeChangesWithTwoClientsAndOneUser() { testSynchronizeChangesWithTwoClientsAndOneUser() {
// Test Schema // Test Schema