From fca7cd4566d5d16ebe5103a93797d7c12c0c4b6a Mon Sep 17 00:00:00 2001 From: Kenneth Geisshirt Date: Fri, 13 Oct 2017 14:39:28 +0200 Subject: [PATCH] wip --- docs/sync.js | 2 +- lib/user-methods.js | 20 ++++++++++---------- src/js_sync.hpp | 6 ++++-- src/object-store | 2 +- tests/js/index.js | 10 +++++----- tests/js/session-tests.js | 25 ------------------------- tests/js/user-tests.js | 36 +++++++++++++++++++++++++++++++++--- 7 files changed, 54 insertions(+), 47 deletions(-) diff --git a/docs/sync.js b/docs/sync.js index 37b5da37..9e93db4f 100644 --- a/docs/sync.js +++ b/docs/sync.js @@ -209,7 +209,7 @@ 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 token or server is invalid. + * @throws {Error} If admin token or server is invalid. */ static adminUser(adminToken, server) {} diff --git a/lib/user-methods.js b/lib/user-methods.js index ba21ddc4..64fcfee0 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); @@ -197,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); }, @@ -213,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); @@ -224,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']); @@ -250,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/src/js_sync.hpp b/src/js_sync.hpp index d6ae8e19..3fa83e46 100644 --- a/src/js_sync.hpp +++ b/src/js_sync.hpp @@ -150,10 +150,12 @@ void UserClass::admin_user(ContextType ctx, FunctionType, ObjectType this_obj Value::validated_to_string(ctx, arguments[0], "authServerUrl"), Value::validated_to_string(ctx, arguments[1], "refreshToken") )); - if (user) { + if ((*user)->is_admin()) { return_value.set(create_object>(ctx, user)); } - throw std::runtime_error("Invalid token or server."); + else { + throw std::runtime_error("Invalid admin token or server."); + } } template diff --git a/src/object-store b/src/object-store index 0b1a2ad7..001dc5f8 160000 --- a/src/object-store +++ b/src/object-store @@ -1 +1 @@ -Subproject commit 0b1a2ad7bc1269d4399785240a525e587cd9ddda +Subproject commit 001dc5f8e5f2bb6ebb95f511339c347410710b6a diff --git a/tests/js/index.js b/tests/js/index.js index 99979e38..0a8037a1 100644 --- a/tests/js/index.js +++ b/tests/js/index.js @@ -23,30 +23,30 @@ const Realm = require('realm'); global.enableSyncTests = Realm.Sync; var TESTS = { -/* ListTests: require('./list-tests'), + ListTests: require('./list-tests'), LinkingObjectsTests: require('./linkingobjects-tests'), ObjectTests: require('./object-tests'), RealmTests: require('./realm-tests'), ResultsTests: require('./results-tests'), QueryTests: require('./query-tests'), - MigrationTests: require('./migration-tests'),*/ + MigrationTests: require('./migration-tests'), // GarbageCollectionTests: require('./garbage-collection'), }; // encryption is not supported on windows if (!(typeof process === 'object' && process.platform === 'win32')) { -// TESTS.EncryptionTests = require('./encryption-tests'); + TESTS.EncryptionTests = require('./encryption-tests'); } // If sync is enabled, run the sync tests if (global.enableSyncTests) { - // TESTS.UserTests = require('./user-tests'); + TESTS.UserTests = require('./user-tests'); TESTS.SessionTests = require('./session-tests'); // 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/session-tests.js b/tests/js/session-tests.js index 241b3ef5..3b453b3f 100644 --- a/tests/js/session-tests.js +++ b/tests/js/session-tests.js @@ -249,31 +249,6 @@ module.exports = { }); }, - testRealmOpenAsyncInvalidToken() { - if (!isNodeProccess) { - return Promise.resolve(); - } - - const username = uuid(); - const realmName = uuid(); - - return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH) - .then(() => { - const invalidFeatureToken = 'THIS_IS_INVALID'; - const url = `http://localhost:9080/~/${realmName}`; - return new Promise((resolve, reject) => { - try { - const user = Realm.Sync.User.adminUser(invalidFeatureToken, url); - } - catch (e) { - TestCase.assertTrue(e == 'Error: Invalid token or server.'); - resolve(); - } - reject(); - }); - }); - }, - testRealmOpenAsyncNoSchema() { if (!isNodeProccess) { return Promise.resolve(); diff --git a/tests/js/user-tests.js b/tests/js/user-tests.js index 6c7727d8..29b73952 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,6 +371,37 @@ 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 === 'Error: Invalid admin token or server.'); + } + TestCase.assertTrue(didThrow); + + didThrow = false; + try { + Realm.Sync.User.adminUser('', 'http://foo.bar:9080'); + } + catch (e) { + didThrow = true; + TestCase.assertTrue(e === 'Error: Invalid admin token or server.'); + } + TestCase.assertTrue(didThrow); + resolve(); + }); + } + + /* This test fails because of realm-object-store #243 . We should use 2 users. testSynchronizeChangesWithTwoClientsAndOneUser() { // Test Schema @@ -432,4 +463,3 @@ module.exports = { }, */ }; -