From ac6a39728ad9f9288961abc671d08423ec48a565 Mon Sep 17 00:00:00 2001 From: blagoev Date: Mon, 18 Sep 2017 09:40:46 +0300 Subject: [PATCH 1/4] Fix admin-user-helper for ROS 2.0 --- tests/js/admin-user-helper.js | 88 +++++------------------------------ 1 file changed, 12 insertions(+), 76 deletions(-) diff --git a/tests/js/admin-user-helper.js b/tests/js/admin-user-helper.js index 0bd90141..85718b0f 100644 --- a/tests/js/admin-user-helper.js +++ b/tests/js/admin-user-helper.js @@ -2,92 +2,28 @@ function node_require(module) { return require(module); } -let fs = node_require("fs"); -let path = node_require("path"); + var Realm = node_require('realm'); -const DEFAULT_ADMIN_TOKEN_PATH = path.join(__dirname, "..", "..", "object-server-for-testing", "admin_token.base64"); -const ADMIN_TOKEN_PATH = process.env.ADMIN_TOKEN_PATH || DEFAULT_ADMIN_TOKEN_PATH; +const adminName = "realm-admin" +const password = ''; -function getAdminToken() { - if (fs.existsSync(ADMIN_TOKEN_PATH)) { - return fs.readFileSync(ADMIN_TOKEN_PATH, 'utf-8'); - } else { - throw new Error("Missing the file with an admin token: " + ADMIN_TOKEN_PATH); - } -} - -function random(min, max) { - min = Math.ceil(min); - max = Math.floor(max); - return Math.floor(Math.random() * (max - min + 1)) + min; -} - -const newAdminName = 'admin' + random(1, 100000); -const password = '123'; exports.createAdminUser = function () { - let nonTokenUser, userIdentity, admin_token_user return new Promise((resolve, reject) => { - Realm.Sync.User.register('http://localhost:9080', newAdminName, password, (error, user) => { + Realm.Sync.User.login('http://localhost:9080', adminName, password, (error, user) => { if (error) { reject(error); return; } - nonTokenUser = user - userIdentity = user.identity; - user.logout(); - - admin_token_user = Realm.Sync.User.adminUser(getAdminToken(), 'http://localhost:9080'); - - const config = { - sync: { - user: admin_token_user, - url: 'realm://localhost:9080/__admin', - error: err => { - reject(new Error('Error opening __admin realm error:' + err.user + ' url:' + err.url + ' state:' + err.state)); - } - } - }; - - resolve(Realm.open(config)); - }); - }).then(realm => { - let pendingAdminUser = realm.objectForPrimaryKey('User', userIdentity); - realm.write(() => { - pendingAdminUser.isAdmin = true; - }); - - admin_token_user.logout(); - }).then(() => { - return new Promise((resolve, reject) => { - let isAdminRetryCounter = 0; - let waitForServerToUpdateAdminUser = function () { - isAdminRetryCounter++; - if (isAdminRetryCounter > 10) { - reject("admin-user-helper: Create admin user timeout"); - return; - } - - Realm.Sync.User.login('http://localhost:9080', newAdminName, password, (error, newAdminUser) => { - if (error) { - reject(error); - return; - } - - let isAdmin = newAdminUser.isAdmin; - nonTokenUser.logout(); - if (!isAdmin) { - setTimeout(waitForServerToUpdateAdminUser, 500); - return; - } - - resolve({ - username: newAdminName, - password - }); - }); + + if (!user.isAdmin) { + reject(adminName + " user is not an admin user on this server"); } - waitForServerToUpdateAdminUser(); + + resolve({ + username: adminName, + password + }); }); }); } From 3ee26d5719d78bb7137c3f1af850f2148f9cea0b Mon Sep 17 00:00:00 2001 From: blagoev Date: Mon, 18 Sep 2017 09:44:11 +0300 Subject: [PATCH 2/4] fix error reporting --- tests/js/user-tests.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/js/user-tests.js b/tests/js/user-tests.js index d8b95708..968703bb 100644 --- a/tests/js/user-tests.js +++ b/tests/js/user-tests.js @@ -55,13 +55,13 @@ function assertIsError(error, message) { } } -function assertIsAuthError(error, code, type) { +function assertIsAuthError(error, code, title) { 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); + if (title) { + TestCase.assertEqual(error.title, title); } } From b7fef1b6e93ff51e8461e4c9aa80c244694b3626 Mon Sep 17 00:00:00 2001 From: blagoev Date: Mon, 18 Sep 2017 11:19:36 +0300 Subject: [PATCH 3/4] Fix tests --- tests/js/user-tests.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/js/user-tests.js b/tests/js/user-tests.js index 968703bb..a0fee2f5 100644 --- a/tests/js/user-tests.js +++ b/tests/js/user-tests.js @@ -128,7 +128,7 @@ module.exports = { assertIsUser(user); Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => { - assertIsAuthError(error, 611, 'https://realm.io/docs/object-server/problems/invalid-credentials'); + assertIsAuthError(error, 611, "The provided credentials are invalid or a user already exists."); TestCase.assertUndefined(user); }); }); @@ -202,7 +202,7 @@ module.exports = { testLoginNonExistingUser() { return callbackTest((callback) => Realm.Sync.User.login('http://localhost:9080', 'does_not', 'exist', callback), (error, user) => { - assertIsAuthError(error, 611, 'https://realm.io/docs/object-server/problems/invalid-credentials'); + assertIsAuthError(error, 611, "The provided credentials are invalid or a user already exists."); TestCase.assertUndefined(user); }); }, From 0f1d916874e6a4bdc339d0280d9dbf15ae7bf6ee Mon Sep 17 00:00:00 2001 From: blagoev Date: Mon, 18 Sep 2017 14:09:04 +0300 Subject: [PATCH 4/4] fix users tests (account tests still fail) --- tests/js/user-tests.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/tests/js/user-tests.js b/tests/js/user-tests.js index a0fee2f5..0cd6c703 100644 --- a/tests/js/user-tests.js +++ b/tests/js/user-tests.js @@ -123,13 +123,20 @@ module.exports = { testRegisterExistingUser() { var username = uuid(); - return callbackTest((callback) => Realm.Sync.User.register('http://localhost:9080', username, 'password', callback), (error, user) => { - failOnError(error); - assertIsUser(user); - + return new Promise((resolve, reject) => { Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => { - assertIsAuthError(error, 611, "The provided credentials are invalid or a user already exists."); - TestCase.assertUndefined(user); + failOnError(error); + assertIsUser(user); + + Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => { + try { + assertIsAuthError(error, 613, "The account cannot be registered as it exists already."); + TestCase.assertUndefined(user); + resolve(); + } catch(e) { + reject(e); + } + }); }); }); }, @@ -202,7 +209,7 @@ module.exports = { testLoginNonExistingUser() { return callbackTest((callback) => Realm.Sync.User.login('http://localhost:9080', 'does_not', 'exist', callback), (error, user) => { - assertIsAuthError(error, 611, "The provided credentials are invalid or a user already exists."); + assertIsAuthError(error, 611, "The provided credentials are invalid."); TestCase.assertUndefined(user); }); }, @@ -359,7 +366,12 @@ module.exports = { reject("Retrieving not existing account should fail"); }) .catch(e => { - TestCase.assertEqual(e.code, 404); + try { + TestCase.assertEqual(e.code, 404); + } + catch (e) { + reject(e); + } resolve() }); })