From 0b5aa68c62423cfd05fd11253f0a68cba26ec349 Mon Sep 17 00:00:00 2001 From: blagoev Date: Tue, 17 Oct 2017 00:51:47 +0300 Subject: [PATCH] Revert "Merge pull request #1409 from realm/kneth/bugfix/invalid-token" This reverts commit 4055c9e047b93053eb35402dfc56f2e57e260b92, reversing changes made to d6b3fcb8b14e316756da90d712cb7767e1cfb3a7. --- docs/sync.js | 7 +++---- lib/user-methods.js | 34 +++++++++++----------------------- tests/js/index.js | 2 +- tests/js/user-tests.js | 37 +++---------------------------------- 4 files changed, 18 insertions(+), 62 deletions(-) diff --git a/docs/sync.js b/docs/sync.js index f546e89a..e33e2f93 100644 --- a/docs/sync.js +++ b/docs/sync.js @@ -21,9 +21,9 @@ * migrated to the v2.x format. In case this migration * is not possible, an exception is thrown. The exception´s `message` property will be equal * to `IncompatibleSyncedRealmException`. The Realm is backed up, and the property `configuration` - * is a {Realm~Configuration} which refers to it. You can open it as a local, read-only Realm, and + * is a {Realm~Configuration} which refers to it. You can open it as a local, read-only Realm, and * copy objects to a new synced Realm. - * + * * @memberof Realm */ class Sync { @@ -153,7 +153,7 @@ class IncompatibleSyncedRealmError { * The name of the error is 'IncompatibleSyncedRealmError' */ get name() {} - + /** * The {Realm~Configuration} of the backed up Realm. * @type {Realm~Configuration} @@ -209,7 +209,6 @@ class User { * @param {string} adminToken - existing admin token * @param {string} server - authentication server * @return {User} - admin user populated with the given token and server - * @throws {Error} If adminToken or server is invalid. */ static adminUser(adminToken, server) {} diff --git a/lib/user-methods.js b/lib/user-methods.js index 34059715..ba21ddc4 100644 --- a/lib/user-methods.js +++ b/lib/user-methods.js @@ -129,10 +129,10 @@ function refreshAccessToken(user, localRealmPath, realmUrl) { /** * The base authentication method. It fires a JSON POST to the server parameter plus the auth url * For example, if the server parameter is `http://myapp.com`, this url will post to `http://myapp.com/auth` - * @param {object} userConstructor - * @param {string} server the http or https server url + * @param {object} userConstructor + * @param {string} server the http or https server url * @param {object} json the json to post to the auth endpoint - * @param {Function} callback an optional callback with an error and user parameter + * @param {Function} callback an optional callback with an error and user parameter * @returns {Promise} only returns a promise if the callback parameter was omitted */ function _authenticate(userConstructor, server, json, callback) { @@ -161,8 +161,8 @@ function _authenticate(userConstructor, server, json, callback) { }); if (callback) { - promise.then(user => { - callback(null, user); + promise.then(user => { + callback(null, user); }) .catch(err => { callback(err); @@ -187,19 +187,7 @@ const staticMethods = { adminUser(token, server) { checkTypes(arguments, ['string', 'string']); - const user = this._adminUser(server, token); - // FIXME: find a better way to detect that token or server is invalid - // check if object is empty - var isEmpty = true; - for(var prop in user) { - if (user.hasOwnProperty(prop)) { - isEmpty = false; - } - } - if (isEmpty) { - throw new Error('Invalid adminToken or server.'); - } - return user; + return this._adminUser(server, token); }, register(server, username, password, callback) { @@ -209,12 +197,12 @@ const staticMethods = { user_info: { password: password, register: true }, data: username }; - + if (callback) { const message = "register(..., callback) is now deprecated in favor of register(): Promise. This function argument will be removed in future versions."; (console.warn || console.log).call(console, message); } - + return _authenticate(this, server, json, callback); }, @@ -225,7 +213,7 @@ const staticMethods = { user_info: { password: password }, data: username }; - + if (callback) { const message = "login(..., callback) is now deprecated in favor of login(): Promise. This function argument will be removed in future versions."; (console.warn || console.log).call(console, message); @@ -236,7 +224,7 @@ const staticMethods = { registerWithProvider(server, options, callback) { - // Compatibility with previous signature: + // Compatibility with previous signature: // registerWithProvider(server, provider, providerToken, callback) if (arguments.length === 4) { checkTypes(arguments, ['string', 'string', 'string', 'function']); @@ -262,7 +250,7 @@ const staticMethods = { const message = "registerWithProvider(..., callback) is now deprecated in favor of registerWithProvider(): Promise. This function argument will be removed in future versions."; (console.warn || console.log).call(console, message); } - + return _authenticate(this, server, json, callback); }, diff --git a/tests/js/index.js b/tests/js/index.js index b0f57f5c..37524737 100644 --- a/tests/js/index.js +++ b/tests/js/index.js @@ -53,7 +53,7 @@ if (global.enableSyncTests) { // FIXME: Permission tests currently fail in chrome debugging mode. if (typeof navigator === 'undefined' || !/Chrome/.test(navigator.userAgent)) { // eslint-disable-line no-undef - TESTS.PermissionTests = require('./permission-tests'); + TESTS.PermissionTests = require('./permission-tests'); } } diff --git a/tests/js/user-tests.js b/tests/js/user-tests.js index d4a7123c..6c7727d8 100644 --- a/tests/js/user-tests.js +++ b/tests/js/user-tests.js @@ -129,11 +129,11 @@ module.exports = { assertIsUser(user); Realm.Sync.User.register('http://localhost:9080', username, 'password', (error, user) => { - try { + try { assertIsAuthError(error, 611, "The provided credentials are invalid or the user does not exist."); TestCase.assertUndefined(user); resolve(); - } catch(e) { + } catch(e) { reject(e); } }); @@ -371,38 +371,6 @@ module.exports = { }); }, - testAdminUser() { - return new Promise((resolve, reject) => { - if (!isNodeProcess) { - resolve(); - } - - // FIXME: check if adminUser() returns user iff valid token/server - let didThrow = false; - try { - let user = Realm.Sync.User.adminUser('THIS_IS_INVALID', 'http://localhost:9080'); - } - catch (e) { - didThrow = true; - TestCase.assertTrue(e.message === 'Invalid adminToken or server.'); - } - TestCase.assertTrue(didThrow); - - // FIXME: find a way to set up a proper admin token - didThrow = false; - try { - Realm.Sync.User.adminUser('THIS_IS_VALID', 'http://foo.bar:9080'); - } - catch (e) { - didThrow = true; - TestCase.assertTrue(e.message === 'Invalid adminToken or server.'); - } - TestCase.assertTrue(didThrow); - resolve(); - }); - } - - /* This test fails because of realm-object-store #243 . We should use 2 users. testSynchronizeChangesWithTwoClientsAndOneUser() { // Test Schema @@ -464,3 +432,4 @@ module.exports = { }, */ }; +