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

@ -43,7 +43,7 @@ const postHeaders = {
}; };
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';
@ -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);
@ -236,7 +236,7 @@ module.exports = {
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 = {
@ -247,8 +247,12 @@ module.exports = {
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