From 2520e7b6c620242deff651b0df50b9c2fbf610e3 Mon Sep 17 00:00:00 2001 From: Mike McDonald Date: Sat, 27 Feb 2016 16:33:34 -0800 Subject: [PATCH 01/29] Initial commit of Google Cloud Storage File Adapter --- package.json | 1 + src/Adapters/Files/GCSAdapter.js | 96 ++++++++++++++++++++++++++++++++ src/index.js | 14 +++-- 3 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 src/Adapters/Files/GCSAdapter.js diff --git a/package.json b/package.json index 9837376d..22b11e1c 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "commander": "^2.9.0", "deepcopy": "^0.6.1", "express": "^4.13.4", + "gcloud": "^0.28.0", "mime": "^1.3.4", "mongodb": "~2.1.0", "multer": "^1.1.0", diff --git a/src/Adapters/Files/GCSAdapter.js b/src/Adapters/Files/GCSAdapter.js new file mode 100644 index 00000000..a5ee4096 --- /dev/null +++ b/src/Adapters/Files/GCSAdapter.js @@ -0,0 +1,96 @@ +// GCSAdapter +// Store Parse Files in Google Cloud Storage: https://cloud.google.com/storage +import * as gcloud from 'gcloud'; +import { FilesAdapter } from './FilesAdapter'; + +export class GCSAdapter extends FilesAdapter { + // GCS Project ID and the name of a corresponding Keyfile are required. + // See https://googlecloudplatform.github.io/gcloud-node/#/docs/master/guides/authentication + // for more details. + constructor( + projectId, + keyFilename, + bucket, + { bucketPrefix = '', + directAccess = false } = {} + ) { + super(); + + this._bucket = bucket; + this._bucketPrefix = bucketPrefix; + this._directAccess = directAccess; + + let gcsOptions = { + projectId: projectId, + keyFilename: keyFilename + }; + + this._gcsClient = new gcloud.storage(gcsOptions); + } + + // For a given config object, filename, and data, store a file in GCS. + // Resolves the promise or fails with an error. + createFile(config, filename, data) { + return new Promise((resolve, reject) => { + let file = this._gcsClient.bucket(this._bucket).file(this._bucketPrefix + filename); + // gcloud supports upload(file) not upload(bytes), so we need to stream. + var uploadStream = file.createWriteStream(options); + uploadStream.on('error', (err) => { + return reject(err); + }).on('finish', () => { + // Second call to set public read ACL after object is uploaded. + if (this._directAccess) { + file.makePublic((err, res) => { + if (err !== null) { + return reject(err); + } + resolve(); + }); + } + resolve(); + }); + uploadStream.write(data); + uploadStream.end(); + }); + } + + // Deletes a file with the given file name. + // Returns a promise that succeeds with the delete response, or fails with an error. + deleteFile(config, filename) { + return new Promise((resolve, reject) => { + let file = this._gcsClient.bucket(this._bucket).file(this._bucketPrefix + filename); + file.delete((err, res) => { + if(err !== null) { + return reject(err); + } + resolve(res); + }); + }); + } + + // Search for and return a file if found by filename. + // Returns a promise that succeeds with the buffer result from GCS, or fails with an error. + getFileData(config, filename) { + return new Promise((resolve, reject) => { + let file = this._gcsClient.bucket(this._bucket).file(this._bucketPrefix + filename); + file.download((err, data) => { + if (err !== null) { + return reject(err); + } + resolve(data); + }); + }); + } + + // Generates and returns the location of a file stored in GCS for the given request and filename. + // The location is the direct GCS link if the option is set, + // otherwise we serve the file through parse-server. + getFileLocation(config, filename) { + if (this._directAccess) { + return `https://${this._bucket}.storage.googleapis.com/${this._bucketPrefix + filename}`; + } + return (config.mount + '/files/' + config.applicationId + '/' + encodeURIComponent(filename)); + } +} + +export default GCSAdapter; diff --git a/src/index.js b/src/index.js index ad715902..6112a3ea 100644 --- a/src/index.js +++ b/src/index.js @@ -14,6 +14,7 @@ import cache from './cache'; import PromiseRouter from './PromiseRouter'; import { GridStoreAdapter } from './Adapters/Files/GridStoreAdapter'; import { S3Adapter } from './Adapters/Files/S3Adapter'; +import { GCSAdapter } from './Adapters/Files/GCSAdapter'; import { FilesController } from './Controllers/FilesController'; import ParsePushAdapter from './Adapters/Push/ParsePushAdapter'; @@ -91,11 +92,11 @@ function ParseServer({ serverURL = requiredParameter('You must provide a serverURL!'), maxUploadSize = '20mb' }) { - + // Initialize the node client SDK automatically Parse.initialize(appId, javascriptKey || 'unused', masterKey); Parse.serverURL = serverURL; - + if (databaseAdapter) { DatabaseAdapter.setAdapter(databaseAdapter); } @@ -103,7 +104,7 @@ function ParseServer({ if (databaseURI) { DatabaseAdapter.setAppDatabaseURI(appId, databaseURI); } - + if (cloud) { addParseCloud(); if (typeof cloud === 'function') { @@ -125,7 +126,7 @@ function ParseServer({ const pushController = new PushController(pushControllerAdapter); const loggerController = new LoggerController(loggerControllerAdapter); const hooksController = new HooksController(appId, collectionPrefix); - + cache.apps[appId] = { masterKey: masterKey, collectionPrefix: collectionPrefix, @@ -185,7 +186,7 @@ function ParseServer({ if (process.env.PARSE_EXPERIMENTAL_CONFIG_ENABLED || process.env.TESTING) { routers.push(require('./global_config')); } - + if (process.env.PARSE_EXPERIMENTAL_HOOKS_ENABLED || process.env.TESTING) { routers.push(new HooksRouter()); } @@ -229,5 +230,6 @@ function getClassName(parseClass) { module.exports = { ParseServer: ParseServer, - S3Adapter: S3Adapter + S3Adapter: S3Adapter, + GCSAdapter: GCSAdapter }; From deafb680aef5befa52ba636c989d556167ec9144 Mon Sep 17 00:00:00 2001 From: Mike McDonald Date: Sat, 27 Feb 2016 16:47:26 -0800 Subject: [PATCH 02/29] Removed orphaned "options" --- src/Adapters/Files/GCSAdapter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapters/Files/GCSAdapter.js b/src/Adapters/Files/GCSAdapter.js index a5ee4096..23f9ef02 100644 --- a/src/Adapters/Files/GCSAdapter.js +++ b/src/Adapters/Files/GCSAdapter.js @@ -34,7 +34,7 @@ export class GCSAdapter extends FilesAdapter { return new Promise((resolve, reject) => { let file = this._gcsClient.bucket(this._bucket).file(this._bucketPrefix + filename); // gcloud supports upload(file) not upload(bytes), so we need to stream. - var uploadStream = file.createWriteStream(options); + var uploadStream = file.createWriteStream(); uploadStream.on('error', (err) => { return reject(err); }).on('finish', () => { From 360cc3461d242c669d2ba6f315a4a5a930c2de9a Mon Sep 17 00:00:00 2001 From: Mike McDonald Date: Sat, 27 Feb 2016 21:27:32 -0800 Subject: [PATCH 03/29] Added tests! Note that tests won't run without a GCP Project, key, and GCS bucket --- spec/ParseFile+GCS.spec.js | 398 +++++++++++++++++++++++++++++++ src/Adapters/Files/GCSAdapter.js | 3 +- 2 files changed, 400 insertions(+), 1 deletion(-) create mode 100644 spec/ParseFile+GCS.spec.js diff --git a/spec/ParseFile+GCS.spec.js b/spec/ParseFile+GCS.spec.js new file mode 100644 index 00000000..850704a3 --- /dev/null +++ b/spec/ParseFile+GCS.spec.js @@ -0,0 +1,398 @@ +// This is a port of the test suite: +// hungry/js/test/parse_file_test.js + +"use strict"; + +var request = require('request'); +var GCSAdapter = require('../src/index').GCSAdapter; + +var str = "Hello World!"; +var data = []; +for (var i = 0; i < str.length; i++) { + data.push(str.charCodeAt(i)); +} + +// Make sure that you fill these in, otherwise the tests won't run!!! +var GCP_PROJECT_ID = ""; +var GCP_KEYFILE_PATH = ""; +var GCS_BUCKET_NAME = ""; + +describe('Parse.File testing', () => { + describe('GCS directAccess: false', () => { + beforeEach(function(done){ + var port = 8378; + var GCSConfiguration = { + databaseURI: process.env.DATABASE_URI, + serverURL: 'http://localhost:' + port + '/1', + appId: 'test', + javascriptKey: 'test', + restAPIKey: 'rest', + masterKey: 'test', + fileKey: 'test', + filesAdapter: new GCSAdapter( + GCP_PROJECT_ID, + GCP_KEYFILE_PATH, + GCS_BUCKET_NAME, + { + bucketPrefix: 'private/', + directAccess: false + } + ) + }; + setServerConfiguration(GCSConfiguration); + done(); + }); + + it('works with Content-Type', done => { + var headers = { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/file.txt', + body: 'argle bargle', + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + expect(b.name).toMatch(/_file.txt$/); + expect(b.url).toMatch(/^http:\/\/localhost:8378\/1\/files\/test\/.*file.txt$/); + request.get(b.url, (error, response, body) => { + expect(error).toBe(null); + expect(body).toEqual('argle bargle'); + done(); + }); + }); + }); + + it('works without Content-Type', done => { + var headers = { + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/file.txt', + body: 'argle bargle', + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + expect(b.name).toMatch(/_file.txt$/); + expect(b.url).toMatch(/^http:\/\/localhost:8378\/1\/files\/test\/.*file.txt$/); + request.get(b.url, (error, response, body) => { + expect(error).toBe(null); + expect(body).toEqual('argle bargle'); + done(); + }); + }); + }); + + it('supports REST end-to-end file create, read, delete, read', done => { + var headers = { + 'Content-Type': 'image/jpeg', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/testfile.txt', + body: 'check one two', + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + expect(b.name).toMatch(/_testfile.txt$/); + expect(b.url).toMatch(/^http:\/\/localhost:8378\/1\/files\/test\/.*testfile.txt$/); + request.get(b.url, (error, response, body) => { + expect(error).toBe(null); + expect(body).toEqual('check one two'); + request.del({ + headers: { + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest', + 'X-Parse-Master-Key': 'test' + }, + url: 'http://localhost:8378/1/files/' + b.name + }, (error, response, body) => { + expect(error).toBe(null); + expect(response.statusCode).toEqual(200); + request.get({ + headers: { + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }, + url: b.url + }, (error, response, body) => { + expect(error).toBe(null); + expect(response.statusCode).toEqual(404); + done(); + }); + }); + }); + }); + }); + + it('blocks file deletions with missing or incorrect master-key header', done => { + var headers = { + 'Content-Type': 'image/jpeg', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/thefile.jpg', + body: 'the file body' + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + expect(b.url).toMatch(/^http:\/\/localhost:8378\/1\/files\/test\/.*thefile.jpg$/); + // missing X-Parse-Master-Key header + request.del({ + headers: { + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }, + url: 'http://localhost:8378/1/files/' + b.name + }, (error, response, body) => { + expect(error).toBe(null); + var del_b = JSON.parse(body); + expect(response.statusCode).toEqual(403); + expect(del_b.error).toMatch(/unauthorized/); + // incorrect X-Parse-Master-Key header + request.del({ + headers: { + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest', + 'X-Parse-Master-Key': 'tryagain' + }, + url: 'http://localhost:8378/1/files/' + b.name + }, (error, response, body) => { + expect(error).toBe(null); + var del_b2 = JSON.parse(body); + expect(response.statusCode).toEqual(403); + expect(del_b2.error).toMatch(/unauthorized/); + done(); + }); + }); + }); + }); + + it('handles other filetypes', done => { + var headers = { + 'Content-Type': 'image/jpeg', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/file.jpg', + body: 'argle bargle', + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + expect(b.name).toMatch(/_file.jpg$/); + expect(b.url).toMatch(/^http:\/\/localhost:8378\/1\/files\/.*file.jpg$/); + request.get(b.url, (error, response, body) => { + expect(error).toBe(null); + expect(body).toEqual('argle bargle'); + done(); + }); + }); + }); + }); + + describe('GCS directAccess: true', () => { + beforeEach(function(done){ + var port = 8378; + var GCSConfiguration = { + databaseURI: process.env.DATABASE_URI, + serverURL: 'http://localhost:' + port + '/1', + appId: 'test', + javascriptKey: 'test', + restAPIKey: 'rest', + masterKey: 'test', + fileKey: 'test', + filesAdapter: new GCSAdapter( + GCP_PROJECT_ID, + GCP_KEYFILE_PATH, + GCS_BUCKET_NAME, + { + bucketPrefix: 'public/', + directAccess: true + } + ) + }; + setServerConfiguration(GCSConfiguration); + done(); + }); + + it('works with Content-Type', done => { + var headers = { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/file.txt', + body: 'argle bargle', + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + expect(b.name).toMatch(/_file.txt$/); + var gcsRegex = new RegExp("https:\/\/" + GCS_BUCKET_NAME + ".storage.googleapis.com\/public\/.*file.txt") + expect(b.url).toMatch(gcsRegex); + request.get(b.url, (error, response, body) => { + expect(error).toBe(null); + expect(body).toEqual('argle bargle'); + done(); + }); + }); + }); + + it('works without Content-Type', done => { + var headers = { + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/file.txt', + body: 'argle bargle', + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + expect(b.name).toMatch(/_file.txt$/); + var gcsRegex = new RegExp("https:\/\/" + GCS_BUCKET_NAME + ".storage.googleapis.com\/public\/.*file.txt") + expect(b.url).toMatch(gcsRegex); + request.get(b.url, (error, response, body) => { + expect(error).toBe(null); + expect(body).toEqual('argle bargle'); + done(); + }); + }); + }); + + it('supports REST end-to-end file create, read, delete, read', done => { + var headers = { + 'Content-Type': 'image/jpeg', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + // Create the file + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/testfile.txt', + body: 'check one two', + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + expect(b.name).toMatch(/_testfile.txt$/); + var gcsRegex = new RegExp("https:\/\/" + GCS_BUCKET_NAME + ".storage.googleapis.com\/public\/.*testfile.txt") + expect(b.url).toMatch(gcsRegex); + // Read the file the first time + request.get(b.url, (error, response, body) => { + expect(error).toBe(null); + expect(body).toEqual('check one two'); + // Delete the file + request.del({ + headers: { + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest', + 'X-Parse-Master-Key': 'test' + }, + url: 'http://localhost:8378/1/files/' + b.name + }, (error, response, body) => { + expect(error).toBe(null); + expect(response.statusCode).toEqual(200); + // Read the file the second time--expect it to be gone + // Note that we're reading from the public cloud storage URL + // This is different from the above test since it's assumed + // users are reading from the public URL + request.get({ + headers: { + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }, + url: "https://" + GCS_BUCKET_NAME + ".storage.googleapis.com/public/.*testfile.txt" + }, (error, response, body) => { + expect(error).toBe(null); + expect(response.statusCode).toEqual(404); + done(); + }); + }); + }); + }); + }); + + it('blocks file deletions with missing or incorrect master-key header', done => { + var headers = { + 'Content-Type': 'image/jpeg', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/thefile.jpg', + body: 'the file body' + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + var gcsRegex = new RegExp("https:\/\/" + GCS_BUCKET_NAME + ".storage.googleapis.com\/public\/.*thefile.jpg") + expect(b.url).toMatch(gcsRegex); + // missing X-Parse-Master-Key header + request.del({ + headers: { + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }, + url: 'http://localhost:8378/1/files/' + b.name + }, (error, response, body) => { + expect(error).toBe(null); + var del_b = JSON.parse(body); + expect(response.statusCode).toEqual(403); + expect(del_b.error).toMatch(/unauthorized/); + // incorrect X-Parse-Master-Key header + request.del({ + headers: { + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest', + 'X-Parse-Master-Key': 'tryagain' + }, + url: 'http://localhost:8378/1/files/' + b.name + }, (error, response, body) => { + expect(error).toBe(null); + var del_b2 = JSON.parse(body); + expect(response.statusCode).toEqual(403); + expect(del_b2.error).toMatch(/unauthorized/); + done(); + }); + }); + }); + }); + + it('handles other filetypes', done => { + var headers = { + 'Content-Type': 'image/jpeg', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/file.jpg', + body: 'argle bargle', + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + expect(b.name).toMatch(/_file.jpg$/); + var gcsRegex = new RegExp("https:\/\/" + GCS_BUCKET_NAME + ".storage.googleapis.com\/public\/.*file.jpg") + expect(b.url).toMatch(gcsRegex); + request.get(b.url, (error, response, body) => { + expect(error).toBe(null); + expect(body).toEqual('argle bargle'); + done(); + }); + }); + }); + }); +}); diff --git a/src/Adapters/Files/GCSAdapter.js b/src/Adapters/Files/GCSAdapter.js index 23f9ef02..b0f841e0 100644 --- a/src/Adapters/Files/GCSAdapter.js +++ b/src/Adapters/Files/GCSAdapter.js @@ -46,8 +46,9 @@ export class GCSAdapter extends FilesAdapter { } resolve(); }); + } else { + resolve(); } - resolve(); }); uploadStream.write(data); uploadStream.end(); From 3e41a21e27348ed737be3c46b06f17f44aedbc3d Mon Sep 17 00:00:00 2001 From: Mike McDonald Date: Sat, 27 Feb 2016 22:13:20 -0800 Subject: [PATCH 04/29] x-ing out the test suite for CI buildability. --- spec/ParseFile+GCS.spec.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/ParseFile+GCS.spec.js b/spec/ParseFile+GCS.spec.js index 850704a3..ae6543e2 100644 --- a/spec/ParseFile+GCS.spec.js +++ b/spec/ParseFile+GCS.spec.js @@ -17,7 +17,9 @@ var GCP_PROJECT_ID = ""; var GCP_KEYFILE_PATH = ""; var GCS_BUCKET_NAME = ""; -describe('Parse.File testing', () => { +// Note the 'xdescribe', make sure to delete the 'x' once the above vars +// are filled in to run the test suite +xdescribe('Parse.File GCS testing', () => { describe('GCS directAccess: false', () => { beforeEach(function(done){ var port = 8378; From 5b8ad9d4c6c727475155a53928aaa403cc24e0d8 Mon Sep 17 00:00:00 2001 From: Mike McDonald Date: Thu, 3 Mar 2016 22:50:19 -0800 Subject: [PATCH 05/29] No more .DS_Store --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index de88257b..8ea322c4 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,6 @@ lib/ # cache folder .cache + +# Mac DS_Store files +.DS_Store From 172da3aaa31f4eaf50a618f9159a595ba54ed0ef Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Fri, 4 Mar 2016 18:43:19 -0800 Subject: [PATCH 06/29] Move HooksController to use MongoCollection instead of direct Mongo access. --- src/Adapters/Storage/Mongo/MongoCollection.js | 15 +- src/Controllers/HooksController.js | 170 ++++++++---------- 2 files changed, 86 insertions(+), 99 deletions(-) diff --git a/src/Adapters/Storage/Mongo/MongoCollection.js b/src/Adapters/Storage/Mongo/MongoCollection.js index 66113c3d..84f47899 100644 --- a/src/Adapters/Storage/Mongo/MongoCollection.js +++ b/src/Adapters/Storage/Mongo/MongoCollection.js @@ -1,4 +1,3 @@ - let mongodb = require('mongodb'); let Collection = mongodb.Collection; @@ -18,8 +17,7 @@ export default class MongoCollection { return this._rawFind(query, { skip, limit, sort }) .catch(error => { // Check for "no geoindex" error - if (error.code != 17007 || - !error.message.match(/unable to find index for .geoNear/)) { + if (error.code != 17007 || !error.message.match(/unable to find index for .geoNear/)) { throw error; } // Figure out what key needs an index @@ -59,6 +57,13 @@ export default class MongoCollection { }) } + // Atomically updates data in the database for a single (first) object that matched the query + // If there is nothing that matches the query - does insert + // Postgres Note: `INSERT ... ON CONFLICT UPDATE` that is available since 9.5. + upsertOne(query, update) { + return this._mongoCollection.update(query, update, { upsert: true }); + } + // Atomically find and delete an object based on query. // The result is the promise with an object that was in the database before deleting. // Postgres Note: Translates directly to `DELETE * FROM ... RETURNING *`, which will return data after delete is done. @@ -70,6 +75,10 @@ export default class MongoCollection { }); } + remove(query) { + return this._mongoCollection.remove(query); + } + drop() { return this._mongoCollection.drop(); } diff --git a/src/Controllers/HooksController.js b/src/Controllers/HooksController.js index 1e47acd7..fbc7e920 100644 --- a/src/Controllers/HooksController.js +++ b/src/Controllers/HooksController.js @@ -8,104 +8,91 @@ import * as request from "request"; const DefaultHooksCollectionName = "_Hooks"; export class HooksController { - _applicationId: string; - _collectionPrefix: string; + _applicationId:string; + _collectionPrefix:string; _collection; - constructor(applicationId: string, collectionPrefix: string = '') { + constructor(applicationId:string, collectionPrefix:string = '') { this._applicationId = applicationId; this._collectionPrefix = collectionPrefix; } - - database() { - return DatabaseAdapter.getDatabaseConnection(this._applicationId, this._collectionPrefix); + + load() { + return this._getHooks().then(hooks => { + hooks = hooks || []; + hooks.forEach((hook) => { + this.addHookToTriggers(hook); + }); + }); } - - collection() { + + getCollection() { if (this._collection) { return Promise.resolve(this._collection) } - return this.database().rawCollection(DefaultHooksCollectionName).then((collection) => { + + let database = DatabaseAdapter.getDatabaseConnection(this._applicationId, this._collectionPrefix); + return database.adaptiveCollection(DefaultHooksCollectionName).then(collection => { this._collection = collection; return collection; }); } - + getFunction(functionName) { - return this.getOne({functionName: functionName}) + return this._getHooks({ functionName: functionName }, 1).then(results => results[0]); } - + getFunctions() { - return this.get({functionName: { $exists: true }}) + return this._getHooks({ functionName: { $exists: true } }); } - + getTrigger(className, triggerName) { - return this.getOne({className: className, triggerName: triggerName }) + return this._getHooks({ className: className, triggerName: triggerName }, 1).then(results => results[0]); } - + getTriggers() { - return this.get({className: { $exists: true }, triggerName: { $exists: true }}) + return this._getHooks({ className: { $exists: true }, triggerName: { $exists: true } }); } - + deleteFunction(functionName) { triggers.removeFunction(functionName, this._applicationId); - return this.delete({functionName: functionName}); + return this._removeHooks({ functionName: functionName }); } - + deleteTrigger(className, triggerName) { triggers.removeTrigger(triggerName, className, this._applicationId); - return this.delete({className: className, triggerName: triggerName}); + return this._removeHooks({ className: className, triggerName: triggerName }); } - - delete(query) { - return this.collection().then((collection) => { - return collection.remove(query) - }).then( (res) => { + + _getHooks(query, limit) { + let options = limit ? { limit: limit } : undefined; + return this.getCollection().then(collection => collection.find(query, options)); + } + + _removeHooks(query) { + return this.getCollection().then(collection => { + return collection.remove(query); + }).then(() => { return {}; - }, 1); - } - - getOne(query) { - return this.collection() - .then(coll => coll.findOne(query, {_id: 0})) - .then(hook => { - return hook; }); } - - get(query) { - return this.collection() - .then(coll => coll.find(query, {_id: 0}).toArray()) - .then(hooks => { - return hooks; - }); - } - - getHooks() { - return this.collection() - .then(coll => coll.find({}, {_id: 0}).toArray()) - .then(hooks => { - return hooks; - }, () => ([])) - } - + saveHook(hook) { - var query; if (hook.functionName && hook.url) { - query = {functionName: hook.functionName } + query = { functionName: hook.functionName } } else if (hook.triggerName && hook.className && hook.url) { query = { className: hook.className, triggerName: hook.triggerName } } else { throw new Parse.Error(143, "invalid hook declaration"); } - return this.collection().then((collection) => { - return collection.update(query, hook, {upsert: true}) - }).then(function(res){ - return hook; - }) + return this.getCollection() + .then(collection => collection.upsertOne(query, hook)) + .then(() => { + return hook; + }); } - + addHookToTriggers(hook) { var wrappedFunction = wrapToHTTPRequest(hook); wrappedFunction.url = hook.url; @@ -114,13 +101,13 @@ export class HooksController { } else { triggers.addFunction(hook.functionName, wrappedFunction, null, this._applicationId); } - } - + } + addHook(hook) { this.addHookToTriggers(hook); return this.saveHook(hook); } - + createOrUpdateHook(aHook) { var hook; if (aHook && aHook.functionName && aHook.url) { @@ -132,19 +119,19 @@ export class HooksController { hook.className = aHook.className; hook.url = aHook.url; hook.triggerName = aHook.triggerName; - + } else { throw new Parse.Error(143, "invalid hook declaration"); - } - + } + return this.addHook(hook); }; - + createHook(aHook) { if (aHook.functionName) { return this.getFunction(aHook.functionName).then((result) => { if (result) { - throw new Parse.Error(143,`function name: ${aHook.functionName} already exits`); + throw new Parse.Error(143, `function name: ${aHook.functionName} already exits`); } else { return this.createOrUpdateHook(aHook); } @@ -152,49 +139,39 @@ export class HooksController { } else if (aHook.className && aHook.triggerName) { return this.getTrigger(aHook.className, aHook.triggerName).then((result) => { if (result) { - throw new Parse.Error(143,`class ${aHook.className} already has trigger ${aHook.triggerName}`); + throw new Parse.Error(143, `class ${aHook.className} already has trigger ${aHook.triggerName}`); } return this.createOrUpdateHook(aHook); }); } - + throw new Parse.Error(143, "invalid hook declaration"); }; - + updateHook(aHook) { if (aHook.functionName) { return this.getFunction(aHook.functionName).then((result) => { if (result) { return this.createOrUpdateHook(aHook); } - throw new Parse.Error(143,`no function named: ${aHook.functionName} is defined`); + throw new Parse.Error(143, `no function named: ${aHook.functionName} is defined`); }); } else if (aHook.className && aHook.triggerName) { return this.getTrigger(aHook.className, aHook.triggerName).then((result) => { if (result) { return this.createOrUpdateHook(aHook); } - throw new Parse.Error(143,`class ${aHook.className} does not exist`); + throw new Parse.Error(143, `class ${aHook.className} does not exist`); }); } throw new Parse.Error(143, "invalid hook declaration"); }; - - load() { - return this.getHooks().then((hooks) => { - hooks = hooks || []; - hooks.forEach((hook) => { - this.addHookToTriggers(hook); - }); - }); - } - } function wrapToHTTPRequest(hook) { - return function(req, res) { - var jsonBody = {}; - for(var i in req) { + return (req, res) => { + let jsonBody = {}; + for (var i in req) { jsonBody[i] = req[i]; } if (req.object) { @@ -205,30 +182,31 @@ function wrapToHTTPRequest(hook) { jsonBody.original = req.original.toJSON(); jsonBody.original.className = req.original.className; } - var jsonRequest = {}; - jsonRequest.headers = { - 'Content-Type': 'application/json' - } - jsonRequest.body = JSON.stringify(jsonBody); - - request.post(hook.url, jsonRequest, function(err, httpResponse, body){ + let jsonRequest = { + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(jsonBody) + }; + + request.post(hook.url, jsonRequest, function (err, httpResponse, body) { var result; if (body) { if (typeof body == "string") { try { body = JSON.parse(body); - } catch(e) { - err = {error: "Malformed response", code: -1}; + } catch (e) { + err = { error: "Malformed response", code: -1 }; } } if (!err) { result = body.success; - err = body.error; + err = body.error; } } if (err) { return res.error(err); - } else { + } else { return res.success(result); } }); From 3a06117fa13a09aabaf01129efb4eb5f680d4023 Mon Sep 17 00:00:00 2001 From: Fosco Marotto Date: Sun, 6 Mar 2016 18:26:10 -0800 Subject: [PATCH 07/29] Adding a role scenario test for issue 827 --- spec/ParseRole.spec.js | 79 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/spec/ParseRole.spec.js b/spec/ParseRole.spec.js index 1b7fbcca..f48fbf7f 100644 --- a/spec/ParseRole.spec.js +++ b/spec/ParseRole.spec.js @@ -197,5 +197,84 @@ describe('Parse Role testing', () => { }); }); + // Based on various scenarios described in issues #827 and #683, + it('should properly handle role permissions on objects', (done) => { + var user, user2, user3; + var role, role2, role3; + var obj, obj2; + + var prACL = new Parse.ACL(); + prACL.setPublicReadAccess(true); + var adminACL, superACL, customerACL; + + createTestUser().then((x) => { + user = x; + user2 = new Parse.User(); + return user2.save({ username: 'user2', password: 'omgbbq' }); + }).then((x) => { + user3 = new Parse.User(); + return user3.save({ username: 'user3', password: 'omgbbq' }); + }).then((x) => { + role = new Parse.Role('Admin', prACL); + role.getUsers().add(user); + return role.save({}, { useMasterKey: true }); + }).then(() => { + adminACL = new Parse.ACL(); + adminACL.setRoleReadAccess("Admin", true); + adminACL.setRoleWriteAccess("Admin", true); + + role2 = new Parse.Role('Super', prACL); + role2.getUsers().add(user2); + return role2.save({}, { useMasterKey: true }); + }).then(() => { + superACL = new Parse.ACL(); + superACL.setRoleReadAccess("Super", true); + superACL.setRoleWriteAccess("Super", true); + + role.getRoles().add(role2); + return role.save({}, { useMasterKey: true }); + }).then(() => { + role3 = new Parse.Role('Customer', prACL); + role3.getUsers().add(user3); + role3.getRoles().add(role); + return role3.save({}, { useMasterKey: true }); + }).then(() => { + customerACL = new Parse.ACL(); + customerACL.setRoleReadAccess("Customer", true); + customerACL.setRoleWriteAccess("Customer", true); + + var query = new Parse.Query('_Role'); + return query.find({ useMasterKey: true }); + }).then((x) => { + expect(x.length).toEqual(3); + + obj = new Parse.Object('TestObjectRoles'); + obj.set('ACL', customerACL); + return obj.save(null, { useMasterKey: true }); + }).then(() => { + // Above, the Admin role was added to the Customer role. + // An object secured by the Customer ACL should be able to be edited by the Admin user. + obj.set('changedByAdmin', true); + return obj.save(null, { sessionToken: user.getSessionToken() }); + }).then(() => { + obj2 = new Parse.Object('TestObjectRoles'); + obj2.set('ACL', adminACL); + return obj2.save(null, { useMasterKey: true }); + }, (e) => { + fail('Admin user should have been able to save.'); + done(); + }).then(() => { + // An object secured by the Admin ACL should not be able to be edited by a Customer role user. + obj2.set('changedByCustomer', true); + return obj2.save(null, { sessionToken: user3.getSessionToken() }); + }).then(() => { + fail('Customer user should not have been able to save.'); + done(); + }, (e) => { + expect(e.code).toEqual(101); + done(); + }) + }); + }); From ce35b81cc6d6ab0b774f59b71ea0c235ebd73441 Mon Sep 17 00:00:00 2001 From: Mike McDonald Date: Mon, 7 Mar 2016 00:30:21 -0800 Subject: [PATCH 08/29] New things for GCS Adapter --- spec/FilesControllerTestFactory.js | 25 ++++++++++++------------ src/Adapters/AdapterLoader.js | 7 +++---- src/Adapters/Files/GCSAdapter.js | 27 ++++++++++++++++++++------ src/Controllers/AdaptableController.js | 18 ++++++++--------- src/Controllers/FilesController.js | 8 +++++--- 5 files changed, 50 insertions(+), 35 deletions(-) diff --git a/spec/FilesControllerTestFactory.js b/spec/FilesControllerTestFactory.js index 217a383a..b467d031 100644 --- a/spec/FilesControllerTestFactory.js +++ b/spec/FilesControllerTestFactory.js @@ -1,35 +1,34 @@ - var FilesController = require('../src/Controllers/FilesController').FilesController; var Config = require("../src/Config"); var testAdapter = function(name, adapter) { // Small additional tests to improve overall coverage - + var config = new Config(Parse.applicationId); var filesController = new FilesController(adapter); describe("FilesController with "+name,()=>{ - + it("should properly expand objects", (done) => { - + var result = filesController.expandFilesInObject(config, function(){}); - + expect(result).toBeUndefined(); - + var fullFile = { type: '__type', url: "http://an.url" } - + var anObject = { aFile: fullFile } filesController.expandFilesInObject(config, anObject); expect(anObject.aFile.url).toEqual("http://an.url"); - + done(); - }) - + }) + it("should properly create, read, delete files", (done) => { var filename; filesController.createFile(config, "file.txt", "hello world").then( (result) => { @@ -51,14 +50,14 @@ var testAdapter = function(name, adapter) { console.error(err); done(); }).then((result) => { - + filesController.getFileData(config, filename).then((res) => { fail("the file should be deleted"); done(); }, (err) => { - done(); + done(); }); - + }, (err) => { fail("The adapter should delete the file"); console.error(err); diff --git a/src/Adapters/AdapterLoader.js b/src/Adapters/AdapterLoader.js index a9521f0b..654948e9 100644 --- a/src/Adapters/AdapterLoader.js +++ b/src/Adapters/AdapterLoader.js @@ -1,6 +1,5 @@ export function loadAdapter(adapter, defaultAdapter, options) { - - if (!adapter) + if (!adapter) { if (!defaultAdapter) { return options; @@ -20,7 +19,7 @@ export function loadAdapter(adapter, defaultAdapter, options) { if (adapter.default) { adapter = adapter.default; } - + return loadAdapter(adapter, undefined, options); } else if (adapter.module) { return loadAdapter(adapter.module, undefined, adapter.options); @@ -30,7 +29,7 @@ export function loadAdapter(adapter, defaultAdapter, options) { return loadAdapter(adapter.adapter, undefined, adapter.options); } // return the adapter as provided - return adapter; + return adapter; } export default loadAdapter; diff --git a/src/Adapters/Files/GCSAdapter.js b/src/Adapters/Files/GCSAdapter.js index e749502d..065592eb 100644 --- a/src/Adapters/Files/GCSAdapter.js +++ b/src/Adapters/Files/GCSAdapter.js @@ -4,18 +4,33 @@ import { storage } from 'gcloud'; import { FilesAdapter } from './FilesAdapter'; import requiredParameter from '../../requiredParameter'; +function requiredOrFromEnvironment(env, name) { + let environmentVariable = process.env[env]; + if (!environmentVariable) { + requiredParameter(`GCSAdapter requires an ${name}`); + } + return environmentVariable; +} + +function fromEnvironmentOrDefault(env, defaultValue) { + let environmentVariable = process.env[env]; + if (environmentVariable) { + return environmentVariable; + } + return defaultValue; +} + export class GCSAdapter extends FilesAdapter { // GCS Project ID and the name of a corresponding Keyfile are required. // Unlike the S3 adapter, you must create a new Cloud Storage bucket, as this is not created automatically. // See https://googlecloudplatform.github.io/gcloud-node/#/docs/master/guides/authentication // for more details. constructor( - projectId = requiredParameter('GCSAdapter requires a GCP Project ID'), - keyFilename = requiredParameter('GCSAdapter requires a GCP keyfile'), - bucket = requiredParameter('GCSAdapter requires a GCS bucket name'), - { bucketPrefix = '', - directAccess = false } = {} - ) { + projectId = requiredOrFromEnvironment('GCP_PROJECT_ID', 'projectId'), + keyFilename = requiredOrFromEnvironment('GCP_KEYFILE_PATH', 'keyfile path'), + bucket = requiredOrFromEnvironment('GCS_BUCKET_NAME', 'bucket name'), + { bucketPrefix = fromEnvironmentOrDefault('GCS_BUCKET_PREFIX', ''), + directAccess = fromEnvironmentOrDefault('GCS_DIRECT_ACCESS', false) } = {}) { super(); this._bucket = bucket; diff --git a/src/Controllers/AdaptableController.js b/src/Controllers/AdaptableController.js index ab7d7156..7ff8ce29 100644 --- a/src/Controllers/AdaptableController.js +++ b/src/Controllers/AdaptableController.js @@ -2,7 +2,7 @@ AdaptableController.js AdaptableController is the base class for all controllers -that support adapter, +that support adapter, The super class takes care of creating the right instance for the adapter based on the parameters passed @@ -28,30 +28,30 @@ export class AdaptableController { this.validateAdapter(adapter); this[_adapter] = adapter; } - + get adapter() { return this[_adapter]; } - + get config() { return new Config(this.appId); } - + expectedAdapterType() { throw new Error("Subclasses should implement expectedAdapterType()"); } - + validateAdapter(adapter) { if (!adapter) { throw new Error(this.constructor.name+" requires an adapter"); } - + let Type = this.expectedAdapterType(); // Allow skipping for testing - if (!Type) { + if (!Type) { return; } - + // Makes sure the prototype matches let mismatches = Object.getOwnPropertyNames(Type.prototype).reduce( (obj, key) => { const adapterType = typeof adapter[key]; @@ -64,7 +64,7 @@ export class AdaptableController { } return obj; }, {}); - + if (Object.keys(mismatches).length > 0) { throw new Error("Adapter prototype don't match expected prototype", adapter, mismatches); } diff --git a/src/Controllers/FilesController.js b/src/Controllers/FilesController.js index 712e326c..9abd87ff 100644 --- a/src/Controllers/FilesController.js +++ b/src/Controllers/FilesController.js @@ -13,11 +13,11 @@ export class FilesController extends AdaptableController { } createFile(config, filename, data, contentType) { - + let extname = path.extname(filename); - + const hasExtension = extname.length > 0; - + if (!hasExtension && contentType && mime.extension(contentType)) { filename = filename + '.' + mime.extension(contentType); } else if (hasExtension && !contentType) { @@ -27,6 +27,8 @@ export class FilesController extends AdaptableController { filename = randomHexString(32) + '_' + filename; var location = this.adapter.getFileLocation(config, filename); + console.log(this.adapter); + console.log(location); return this.adapter.createFile(config, filename, data, contentType).then(() => { return Promise.resolve({ url: location, From 0f00d659cb95cbcf4baa683e3c4205f42632dae6 Mon Sep 17 00:00:00 2001 From: Mike McDonald Date: Mon, 7 Mar 2016 00:34:41 -0800 Subject: [PATCH 09/29] Removed extraneous console.log() --- src/Adapters/Files/GCSAdapter.js | 2 -- src/Controllers/FilesController.js | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/Adapters/Files/GCSAdapter.js b/src/Adapters/Files/GCSAdapter.js index 065592eb..8bd19447 100644 --- a/src/Adapters/Files/GCSAdapter.js +++ b/src/Adapters/Files/GCSAdapter.js @@ -82,7 +82,6 @@ export class GCSAdapter extends FilesAdapter { return new Promise((resolve, reject) => { let file = this._gcsClient.bucket(this._bucket).file(this._bucketPrefix + filename); file.delete((err, res) => { - console.log("delete: ", filename, err, res); if(err !== null) { return reject(err); } @@ -100,7 +99,6 @@ export class GCSAdapter extends FilesAdapter { file.exists((err, exists) => { if (exists) { file.download((err, data) => { - console.log("get: ", filename, err, data); if (err !== null) { return reject(err); } diff --git a/src/Controllers/FilesController.js b/src/Controllers/FilesController.js index 9abd87ff..335dfdf2 100644 --- a/src/Controllers/FilesController.js +++ b/src/Controllers/FilesController.js @@ -27,8 +27,6 @@ export class FilesController extends AdaptableController { filename = randomHexString(32) + '_' + filename; var location = this.adapter.getFileLocation(config, filename); - console.log(this.adapter); - console.log(location); return this.adapter.createFile(config, filename, data, contentType).then(() => { return Promise.resolve({ url: location, From 2c5144028bf5c641224a0fbd5accc63569dc2e6b Mon Sep 17 00:00:00 2001 From: Mike McDonald Date: Mon, 7 Mar 2016 00:47:08 -0800 Subject: [PATCH 10/29] Added tests to adapter loader, cleaned up README, renamed to GCS_BUCKET from GCS_BUCKET_NAME --- README.md | 22 ++++++++++++++++-- spec/AdapterLoader.spec.js | 40 ++++++++++++++++++++------------ spec/FilesController.spec.js | 8 +++---- src/Adapters/Files/GCSAdapter.js | 2 +- 4 files changed, 50 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index b0a8d015..c4a86d10 100644 --- a/README.md +++ b/README.md @@ -135,9 +135,14 @@ PARSE_SERVER_MAX_UPLOAD_SIZE ``` -##### Configuring S3 Adapter +##### Configuring File Adapters +Parse Server allows developers to choose from several options when hosting files: the `GridStoreAdapter`, which backed by MongoDB; the `S3Adapter`, which is backed by [Amazon S3](https://aws.amazon.com/s3/); or the `GCSAdapter`, which is backed by [Google Cloud Storage](https://cloud.google.com/storage/). -You can use the following environment variable setup the S3 adapter +`GridStoreAdapter` is used by default and requires no setup, but if you're interested in using S3 or GCS, additional configuration information is available below. + +###### Configuring `S3Adapter` + +You can use the following environment variable setup to enable the S3 adapter: ```js S3_ACCESS_KEY @@ -149,6 +154,19 @@ S3_DIRECT_ACCESS ``` +###### Configuring `GCSAdapter` + +You can use the following environment variable setup to enable the GCS adapter: + +```js +GCP_PROJECT_ID +GCP_KEYFILE_PATH +GCS_BUCKET +GCS_BUCKET_PREFIX +GCS_DIRECT_ACCESS + +``` + ## Contributing We really want Parse to be yours, to see it grow and thrive in the open source community. Please see the [Contributing to Parse Server guide](CONTRIBUTING.md). diff --git a/spec/AdapterLoader.spec.js b/spec/AdapterLoader.spec.js index 69381fc5..56bf0d44 100644 --- a/spec/AdapterLoader.spec.js +++ b/spec/AdapterLoader.spec.js @@ -3,44 +3,45 @@ var loadAdapter = require("../src/Adapters/AdapterLoader").loadAdapter; var FilesAdapter = require("../src/Adapters/Files/FilesAdapter").default; var ParsePushAdapter = require("../src/Adapters/Push/ParsePushAdapter"); var S3Adapter = require("../src/Adapters/Files/S3Adapter").default; +var GCSAdapter = require("../src/Adapters/Files/GCSAdapter").default; describe("AdapterLoader", ()=>{ - + it("should instantiate an adapter from string in object", (done) => { var adapterPath = require('path').resolve("./spec/MockAdapter"); var adapter = loadAdapter({ adapter: adapterPath, options: { - key: "value", + key: "value", foo: "bar" } }); - + expect(adapter instanceof Object).toBe(true); expect(adapter.options.key).toBe("value"); expect(adapter.options.foo).toBe("bar"); done(); }); - + it("should instantiate an adapter from string", (done) => { var adapterPath = require('path').resolve("./spec/MockAdapter"); var adapter = loadAdapter(adapterPath); - + expect(adapter instanceof Object).toBe(true); done(); }); - + it("should instantiate an adapter from string that is module", (done) => { var adapterPath = require('path').resolve("./src/Adapters/Files/FilesAdapter"); var adapter = loadAdapter({ adapter: adapterPath }); - + expect(adapter instanceof FilesAdapter).toBe(true); done(); }); - + it("should instantiate an adapter from function/Class", (done) => { var adapter = loadAdapter({ adapter: FilesAdapter @@ -48,27 +49,27 @@ describe("AdapterLoader", ()=>{ expect(adapter instanceof FilesAdapter).toBe(true); done(); }); - + it("should instantiate the default adapter from Class", (done) => { var adapter = loadAdapter(null, FilesAdapter); expect(adapter instanceof FilesAdapter).toBe(true); done(); }); - + it("should use the default adapter", (done) => { var defaultAdapter = new FilesAdapter(); var adapter = loadAdapter(null, defaultAdapter); expect(adapter instanceof FilesAdapter).toBe(true); done(); }); - + it("should use the provided adapter", (done) => { var originalAdapter = new FilesAdapter(); var adapter = loadAdapter(originalAdapter); expect(adapter).toBe(originalAdapter); done(); }); - + it("should fail loading an improperly configured adapter", (done) => { var Adapter = function(options) { if (!options.foo) { @@ -79,14 +80,14 @@ describe("AdapterLoader", ()=>{ param: "key", doSomething: function() {} }; - + expect(() => { var adapter = loadAdapter(adapterOptions, Adapter); expect(adapter).toEqual(adapterOptions); }).not.toThrow("foo is required for that adapter"); done(); }); - + it("should load push adapter from options", (done) => { var options = { ios: { @@ -100,7 +101,7 @@ describe("AdapterLoader", ()=>{ }).not.toThrow(); done(); }); - + it("should load S3Adapter from direct passing", (done) => { var s3Adapter = new S3Adapter("key", "secret", "bucket") expect(() => { @@ -109,4 +110,13 @@ describe("AdapterLoader", ()=>{ }).not.toThrow(); done(); }) + + it("should load GCSAdapter from direct passing", (done) => { + var gcsAdapter = new GCSAdapter("projectId", "path/to/keyfile", "bucket") + expect(() => { + var adapter = loadAdapter(gcsAdapter, FilesAdapter); + expect(adapter).toBe(gcsAdapter); + }).not.toThrow(); + done(); + }) }); diff --git a/spec/FilesController.spec.js b/spec/FilesController.spec.js index 183dcb27..3b2108e7 100644 --- a/spec/FilesController.spec.js +++ b/spec/FilesController.spec.js @@ -32,21 +32,21 @@ describe("FilesController",()=>{ console.log("set S3_ACCESS_KEY and S3_SECRET_KEY to test S3Adapter") } - if (process.env.GCP_PROJECT_ID && process.env.GCP_KEYFILE_PATH && process.env.GCS_BUCKET_NAME) { + if (process.env.GCP_PROJECT_ID && process.env.GCP_KEYFILE_PATH && process.env.GCS_BUCKET) { // Test the GCS Adapter - var gcsAdapter = new GCSAdapter(process.env.GCP_PROJECT_ID, process.env.GCP_KEYFILE_PATH, process.env.GCS_BUCKET_NAME); + var gcsAdapter = new GCSAdapter(process.env.GCP_PROJECT_ID, process.env.GCP_KEYFILE_PATH, process.env.GCS_BUCKET); FCTestFactory.testAdapter("GCSAdapter", gcsAdapter); // Test GCS with direct access - var gcsDirectAccessAdapter = new GCSAdapter(process.env.GCP_PROJECT_ID, process.env.GCP_KEYFILE_PATH, process.env.GCS_BUCKET_NAME, { + var gcsDirectAccessAdapter = new GCSAdapter(process.env.GCP_PROJECT_ID, process.env.GCP_KEYFILE_PATH, process.env.GCS_BUCKET, { directAccess: true }); FCTestFactory.testAdapter("GCSAdapterDirect", gcsDirectAccessAdapter); } else if (!process.env.TRAVIS) { - console.log("set GCP_PROJECT_ID, GCP_KEYFILE_PATH, and GCS_BUCKET_NAME to test GCSAdapter") + console.log("set GCP_PROJECT_ID, GCP_KEYFILE_PATH, and GCS_BUCKET to test GCSAdapter") } }); diff --git a/src/Adapters/Files/GCSAdapter.js b/src/Adapters/Files/GCSAdapter.js index 8bd19447..8fb34af8 100644 --- a/src/Adapters/Files/GCSAdapter.js +++ b/src/Adapters/Files/GCSAdapter.js @@ -28,7 +28,7 @@ export class GCSAdapter extends FilesAdapter { constructor( projectId = requiredOrFromEnvironment('GCP_PROJECT_ID', 'projectId'), keyFilename = requiredOrFromEnvironment('GCP_KEYFILE_PATH', 'keyfile path'), - bucket = requiredOrFromEnvironment('GCS_BUCKET_NAME', 'bucket name'), + bucket = requiredOrFromEnvironment('GCS_BUCKET', 'bucket name'), { bucketPrefix = fromEnvironmentOrDefault('GCS_BUCKET_PREFIX', ''), directAccess = fromEnvironmentOrDefault('GCS_DIRECT_ACCESS', false) } = {}) { super(); From 5cdcadea36a4d326c29118dcc6a80f1355770838 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Mon, 7 Mar 2016 08:26:35 -0500 Subject: [PATCH 11/29] Fixes bug when querying equalTo on objectId and relation - Adds $eq operator in transform - Makes $eq operator on objectId when adding $in operator --- spec/ParseRelation.spec.js | 40 +++++++++++++++++++++++++++ src/Controllers/DatabaseController.js | 17 ++++++++---- src/transform.js | 1 + 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/spec/ParseRelation.spec.js b/spec/ParseRelation.spec.js index e1416ecb..403628ed 100644 --- a/spec/ParseRelation.spec.js +++ b/spec/ParseRelation.spec.js @@ -329,6 +329,46 @@ describe('Parse.Relation testing', () => { }); }); + it("query on pointer and relation fields with equal bis", (done) => { + var ChildObject = Parse.Object.extend("ChildObject"); + var childObjects = []; + for (var i = 0; i < 10; i++) { + childObjects.push(new ChildObject({x: i})); + } + + Parse.Object.saveAll(childObjects).then(() => { + var ParentObject = Parse.Object.extend("ParentObject"); + var parent = new ParentObject(); + parent.set("x", 4); + var relation = parent.relation("toChilds"); + relation.add(childObjects[0]); + relation.add(childObjects[1]); + relation.add(childObjects[2]); + + var parent2 = new ParentObject(); + parent2.set("x", 3); + parent2.relation("toChilds").add(childObjects[2]); + + var parents = []; + parents.push(parent); + parents.push(parent2); + parents.push(new ParentObject()); + + return Parse.Object.saveAll(parents).then(() => { + var query = new Parse.Query(ParentObject); + query.equalTo("objectId", parent2.id); + // childObjects[2] is in 2 relations + // before the fix, that woul yield 2 results + query.equalTo("toChilds", childObjects[2]); + + return query.find().then((list) => { + equal(list.length, 1, "There should be 1 result"); + done(); + }); + }); + }); + }); + it("or queries on pointer and relation fields", (done) => { var ChildObject = Parse.Object.extend("ChildObject"); var childObjects = []; diff --git a/src/Controllers/DatabaseController.js b/src/Controllers/DatabaseController.js index 98243acb..042a086d 100644 --- a/src/Controllers/DatabaseController.js +++ b/src/Controllers/DatabaseController.js @@ -455,13 +455,18 @@ DatabaseController.prototype.reduceRelationKeys = function(className, query) { DatabaseController.prototype.addInObjectIdsIds = function(ids, query) { if (typeof query.objectId == 'string') { - query.objectId = {'$in': [query.objectId]}; + // Add equality op as we are sure + // we had a constraint on that one + query.objectId = {'$eq': query.objectId}; } - query.objectId = query.objectId || {}; - let queryIn = [].concat(query.objectId['$in'] || [], ids || []); - // make a set and spread to remove duplicates - query.objectId = {'$in': [...new Set(queryIn)]}; - return query; + query.objectId = query.objectId || {}; + let queryIn = [].concat(query.objectId['$in'] || [], ids || []); + // make a set and spread to remove duplicates + // replace the $in operator as other constraints + // may be set + query.objectId['$in'] = [...new Set(queryIn)]; + + return query; } // Runs a query on the database. diff --git a/src/transform.js b/src/transform.js index 8d75b58e..738f2453 100644 --- a/src/transform.js +++ b/src/transform.js @@ -412,6 +412,7 @@ function transformConstraint(constraint, inArray) { case '$gte': case '$exists': case '$ne': + case '$eq': answer[key] = transformAtom(constraint[key], true, {inArray: inArray}); break; From 963811d0225cdb8ef1d027b382711ae9dca7875c Mon Sep 17 00:00:00 2001 From: Drew Gross Date: Mon, 7 Mar 2016 13:44:45 -0800 Subject: [PATCH 12/29] Handle legacy _client_permissions key in _SCHEMA. Fixes #888. --- spec/Schema.spec.js | 29 +++++++++++++++++++++++++++++ src/Routers/SchemasRouter.js | 30 +++++------------------------- src/Schema.js | 22 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 25 deletions(-) diff --git a/spec/Schema.spec.js b/spec/Schema.spec.js index 6ab08d6a..2912067f 100644 --- a/spec/Schema.spec.js +++ b/spec/Schema.spec.js @@ -703,4 +703,33 @@ describe('Schema', () => { }); done(); }); + + it('handles legacy _client_permissions keys without crashing', done => { + Schema.mongoSchemaToSchemaAPIResponse({ + "_id":"_Installation", + "_client_permissions":{ + "get":true, + "find":true, + "update":true, + "create":true, + "delete":true, + }, + "_metadata":{ + "class_permissions":{ + "get":{"*":true}, + "find":{"*":true}, + "update":{"*":true}, + "create":{"*":true}, + "delete":{"*":true}, + "addField":{"*":true}, + } + }, + "installationId":"string", + "deviceToken":"string", + "deviceType":"string", + "channels":"array", + "user":"*_User", + }); + done(); + }); }); diff --git a/src/Routers/SchemasRouter.js b/src/Routers/SchemasRouter.js index 59fef02d..0d1a4cf9 100644 --- a/src/Routers/SchemasRouter.js +++ b/src/Routers/SchemasRouter.js @@ -4,7 +4,7 @@ var express = require('express'), Parse = require('parse/node').Parse, Schema = require('../Schema'); -import PromiseRouter from '../PromiseRouter'; +import PromiseRouter from '../PromiseRouter'; import * as middleware from "../middlewares"; function classNameMismatchResponse(bodyClass, pathClass) { @@ -14,30 +14,10 @@ function classNameMismatchResponse(bodyClass, pathClass) { ); } -function mongoSchemaAPIResponseFields(schema) { - var fieldNames = Object.keys(schema).filter(key => key !== '_id' && key !== '_metadata'); - var response = fieldNames.reduce((obj, fieldName) => { - obj[fieldName] = Schema.mongoFieldTypeToSchemaAPIType(schema[fieldName]) - return obj; - }, {}); - response.ACL = {type: 'ACL'}; - response.createdAt = {type: 'Date'}; - response.updatedAt = {type: 'Date'}; - response.objectId = {type: 'String'}; - return response; -} - -function mongoSchemaToSchemaAPIResponse(schema) { - return { - className: schema._id, - fields: mongoSchemaAPIResponseFields(schema), - }; -} - function getAllSchemas(req) { return req.config.database.adaptiveCollection('_SCHEMA') .then(collection => collection.find({})) - .then(schemas => schemas.map(mongoSchemaToSchemaAPIResponse)) + .then(schemas => schemas.map(Schema.mongoSchemaToSchemaAPIResponse)) .then(schemas => ({ response: { results: schemas }})); } @@ -51,7 +31,7 @@ function getOneSchema(req) { } return results[0]; }) - .then(schema => ({ response: mongoSchemaToSchemaAPIResponse(schema) })); + .then(schema => ({ response: Schema.mongoSchemaToSchemaAPIResponse(schema) })); } function createSchema(req) { @@ -68,7 +48,7 @@ function createSchema(req) { return req.config.database.loadSchema() .then(schema => schema.addClassIfNotExists(className, req.body.fields)) - .then(result => ({ response: mongoSchemaToSchemaAPIResponse(result) })); + .then(result => ({ response: Schema.mongoSchemaToSchemaAPIResponse(result) })); } function modifySchema(req) { @@ -118,7 +98,7 @@ function modifySchema(req) { if (err) { reject(err); } - resolve({ response: mongoSchemaToSchemaAPIResponse(mongoObject.result)}); + resolve({ response: Schema.mongoSchemaToSchemaAPIResponse(mongoObject.result)}); }) })); }); diff --git a/src/Schema.js b/src/Schema.js index 545b4953..3e25b09d 100644 --- a/src/Schema.js +++ b/src/Schema.js @@ -760,6 +760,27 @@ function getObjectType(obj) { return 'object'; } +const nonFieldSchemaKeys = ['_id', '_metadata', '_client_permissions']; +function mongoSchemaAPIResponseFields(schema) { + var fieldNames = Object.keys(schema).filter(key => nonFieldSchemaKeys.indexOf(key) === -1); + var response = fieldNames.reduce((obj, fieldName) => { + obj[fieldName] = mongoFieldTypeToSchemaAPIType(schema[fieldName]) + return obj; + }, {}); + response.ACL = {type: 'ACL'}; + response.createdAt = {type: 'Date'}; + response.updatedAt = {type: 'Date'}; + response.objectId = {type: 'String'}; + return response; +} + +function mongoSchemaToSchemaAPIResponse(schema) { + return { + className: schema._id, + fields: mongoSchemaAPIResponseFields(schema), + }; +} + module.exports = { load: load, classNameIsValid: classNameIsValid, @@ -768,4 +789,5 @@ module.exports = { schemaAPITypeToMongoFieldType: schemaAPITypeToMongoFieldType, buildMergedSchemaObject: buildMergedSchemaObject, mongoFieldTypeToSchemaAPIType: mongoFieldTypeToSchemaAPIType, + mongoSchemaToSchemaAPIResponse, }; From cea4b2bd6add0994983e7ba672c63c018e2933ed Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Mon, 7 Mar 2016 14:06:46 -0800 Subject: [PATCH 13/29] Migrate and fix GlobalConfig database storage. --- src/Routers/GlobalConfigRouter.js | 38 ++++++++++++------------------- 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/src/Routers/GlobalConfigRouter.js b/src/Routers/GlobalConfigRouter.js index 53abdac5..156cecf6 100644 --- a/src/Routers/GlobalConfigRouter.js +++ b/src/Routers/GlobalConfigRouter.js @@ -1,38 +1,28 @@ // global_config.js -var Parse = require('parse/node').Parse; - import PromiseRouter from '../PromiseRouter'; import * as middleware from "../middlewares"; export class GlobalConfigRouter extends PromiseRouter { getGlobalConfig(req) { - return req.config.database.rawCollection('_GlobalConfig') - .then(coll => coll.findOne({'_id': 1})) - .then(globalConfig => ({response: { params: globalConfig.params }})) - .catch(() => ({ - status: 404, - response: { - code: Parse.Error.INVALID_KEY_NAME, - error: 'config does not exist', + return req.config.database.adaptiveCollection('_GlobalConfig') + .then(coll => coll.find({ '_id': 1 }, { limit: 1 })) + .then(results => { + if (results.length != 1) { + // If there is no config in the database - return empty config. + return { response: { params: {} } }; } - })); + let globalConfig = results[0]; + return { response: { params: globalConfig.params } }; + }); } + updateGlobalConfig(req) { - return req.config.database.rawCollection('_GlobalConfig') - .then(coll => coll.findOneAndUpdate({ _id: 1 }, { $set: req.body })) - .then(response => { - return { response: { result: true } } - }) - .catch(() => ({ - status: 404, - response: { - code: Parse.Error.INVALID_KEY_NAME, - error: 'config cannot be updated', - } - })); + return req.config.database.adaptiveCollection('_GlobalConfig') + .then(coll => coll.upsertOne({ _id: 1 }, { $set: req.body })) + .then(() => ({ response: { result: true } })); } - + mountRoutes() { this.route('GET', '/config', req => { return this.getGlobalConfig(req) }); this.route('PUT', '/config', middleware.promiseEnforceMasterKeyAccess, req => { return this.updateGlobalConfig(req) }); From fa6954169e309a257933cf2bb27bdc42c27f1bc8 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Mon, 7 Mar 2016 14:11:43 -0800 Subject: [PATCH 14/29] Migrate ParseGlobalConfig.spec to new database storage API. --- spec/ParseGlobalConfig.spec.js | 54 +++++++++---------- src/Adapters/Storage/Mongo/MongoCollection.js | 4 ++ 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/spec/ParseGlobalConfig.spec.js b/spec/ParseGlobalConfig.spec.js index 399c9ee6..176c7860 100644 --- a/spec/ParseGlobalConfig.spec.js +++ b/spec/ParseGlobalConfig.spec.js @@ -5,21 +5,21 @@ var Parse = require('parse/node').Parse; let Config = require('../src/Config'); describe('a GlobalConfig', () => { - beforeEach(function(done) { + beforeEach(function (done) { let config = new Config('test'); - config.database.rawCollection('_GlobalConfig') - .then(coll => coll.updateOne({ '_id': 1}, { $set: { params: { companies: ['US', 'DK'] } } }, { upsert: true })) + config.database.adaptiveCollection('_GlobalConfig') + .then(coll => coll.upsertOne({ '_id': 1 }, { $set: { params: { companies: ['US', 'DK'] } } })) .then(done()); }); it('can be retrieved', (done) => { request.get({ - url: 'http://localhost:8378/1/config', - json: true, + url : 'http://localhost:8378/1/config', + json : true, headers: { 'X-Parse-Application-Id': 'test', - 'X-Parse-Master-Key': 'test', - }, + 'X-Parse-Master-Key' : 'test' + } }, (error, response, body) => { expect(response.statusCode).toEqual(200); expect(body.params.companies).toEqual(['US', 'DK']); @@ -29,13 +29,13 @@ describe('a GlobalConfig', () => { it('can be updated when a master key exists', (done) => { request.put({ - url: 'http://localhost:8378/1/config', - json: true, - body: { params: { companies: ['US', 'DK', 'SE'] } }, + url : 'http://localhost:8378/1/config', + json : true, + body : { params: { companies: ['US', 'DK', 'SE'] } }, headers: { 'X-Parse-Application-Id': 'test', - 'X-Parse-Master-Key': 'test' - }, + 'X-Parse-Master-Key' : 'test' + } }, (error, response, body) => { expect(response.statusCode).toEqual(200); expect(body.result).toEqual(true); @@ -45,35 +45,35 @@ describe('a GlobalConfig', () => { it('fail to update if master key is missing', (done) => { request.put({ - url: 'http://localhost:8378/1/config', - json: true, - body: { params: { companies: [] } }, + url : 'http://localhost:8378/1/config', + json : true, + body : { params: { companies: [] } }, headers: { 'X-Parse-Application-Id': 'test', - 'X-Parse-REST-API-Key': 'rest' - }, + 'X-Parse-REST-API-Key' : 'rest' + } }, (error, response, body) => { expect(response.statusCode).toEqual(403); expect(body.error).toEqual('unauthorized: master key is required'); done(); }); - }); + }); it('failed getting config when it is missing', (done) => { let config = new Config('test'); - config.database.rawCollection('_GlobalConfig') - .then(coll => coll.deleteOne({ '_id': 1}, {}, {})) - .then(_ => { + config.database.adaptiveCollection('_GlobalConfig') + .then(coll => coll.deleteOne({ '_id': 1 })) + .then(() => { request.get({ - url: 'http://localhost:8378/1/config', - json: true, + url : 'http://localhost:8378/1/config', + json : true, headers: { 'X-Parse-Application-Id': 'test', - 'X-Parse-Master-Key': 'test', - }, + 'X-Parse-Master-Key' : 'test' + } }, (error, response, body) => { - expect(response.statusCode).toEqual(404); - expect(body.code).toEqual(Parse.Error.INVALID_KEY_NAME); + expect(response.statusCode).toEqual(200); + expect(body.params).toEqual({}); done(); }); }); diff --git a/src/Adapters/Storage/Mongo/MongoCollection.js b/src/Adapters/Storage/Mongo/MongoCollection.js index 84f47899..f19705c4 100644 --- a/src/Adapters/Storage/Mongo/MongoCollection.js +++ b/src/Adapters/Storage/Mongo/MongoCollection.js @@ -75,6 +75,10 @@ export default class MongoCollection { }); } + deleteOne(query) { + return this._mongoCollection.deleteOne(query); + } + remove(query) { return this._mongoCollection.remove(query); } From f2ead46580ed25637469a41b202e72a7cf6f3db4 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Mon, 7 Mar 2016 14:16:26 -0800 Subject: [PATCH 15/29] Remove .rawCollection method from DatabaseController. --- src/Adapters/Storage/Mongo/MongoCollection.js | 4 ++++ src/Controllers/DatabaseController.js | 6 +----- src/Controllers/PushController.js | 16 ++++++---------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/Adapters/Storage/Mongo/MongoCollection.js b/src/Adapters/Storage/Mongo/MongoCollection.js index f19705c4..78c90fef 100644 --- a/src/Adapters/Storage/Mongo/MongoCollection.js +++ b/src/Adapters/Storage/Mongo/MongoCollection.js @@ -64,6 +64,10 @@ export default class MongoCollection { return this._mongoCollection.update(query, update, { upsert: true }); } + updateMany(query, update) { + return this._mongoCollection.updateMany(query, update); + } + // Atomically find and delete an object based on query. // The result is the promise with an object that was in the database before deleting. // Postgres Note: Translates directly to `DELETE * FROM ... RETURNING *`, which will return data after delete is done. diff --git a/src/Controllers/DatabaseController.js b/src/Controllers/DatabaseController.js index 042a086d..83c2703a 100644 --- a/src/Controllers/DatabaseController.js +++ b/src/Controllers/DatabaseController.js @@ -35,7 +35,7 @@ DatabaseController.prototype.collection = function(className) { throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, 'invalid className: ' + className); } - return this.rawCollection(className); + return this.adapter.collection(this.collectionPrefix + className); }; DatabaseController.prototype.adaptiveCollection = function(className) { @@ -46,10 +46,6 @@ DatabaseController.prototype.collectionExists = function(className) { return this.adapter.collectionExists(this.collectionPrefix + className); }; -DatabaseController.prototype.rawCollection = function(className) { - return this.adapter.collection(this.collectionPrefix + className); -}; - DatabaseController.prototype.dropCollection = function(className) { return this.adapter.dropCollection(this.collectionPrefix + className); }; diff --git a/src/Controllers/PushController.js b/src/Controllers/PushController.js index 55cb6095..fb03ab52 100644 --- a/src/Controllers/PushController.js +++ b/src/Controllers/PushController.js @@ -63,23 +63,19 @@ export class PushController extends AdaptableController { let badgeUpdate = Promise.resolve(); if (body.badge) { - var op = {}; + let op = {}; if (body.badge == "Increment") { - op = {'$inc': {'badge': 1}} + op = { $inc: { badge: 1 } } } else if (Number(body.badge)) { - op = {'$set': {'badge': body.badge } } + op = { $set: { badge: body.badge } } } else { throw "Invalid value for badge, expected number or 'Increment'"; } let updateWhere = deepcopy(where); + updateWhere.deviceType = 'ios'; // Only on iOS! - // Only on iOS! - updateWhere.deviceType = 'ios'; - - // TODO: @nlutsenko replace with better thing - badgeUpdate = config.database.rawCollection("_Installation").then((coll) => { - return coll.update(updateWhere, op, { multi: true }); - }); + badgeUpdate = config.database.adaptiveCollection("_Installation") + .then(coll => coll.updateMany(updateWhere, op)); } return badgeUpdate.then(() => { From 47061d8e98c8dfa69125dc4cf5dd02ade29d61ed Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Mon, 7 Mar 2016 14:23:05 -0800 Subject: [PATCH 16/29] Migrate PushRouter to shared master-key middleware. --- spec/PushRouter.spec.js | 34 ------------------------- src/Routers/PushRouter.js | 52 ++++++++++++++------------------------- 2 files changed, 18 insertions(+), 68 deletions(-) diff --git a/spec/PushRouter.spec.js b/spec/PushRouter.spec.js index e7273dd5..d71f9f5c 100644 --- a/spec/PushRouter.spec.js +++ b/spec/PushRouter.spec.js @@ -2,40 +2,6 @@ var PushRouter = require('../src/Routers/PushRouter').PushRouter; var request = require('request'); describe('PushRouter', () => { - it('can check valid master key of request', (done) => { - // Make mock request - var request = { - info: { - masterKey: 'masterKey' - }, - config: { - masterKey: 'masterKey' - } - } - - expect(() => { - PushRouter.validateMasterKey(request); - }).not.toThrow(); - done(); - }); - - it('can check invalid master key of request', (done) => { - // Make mock request - var request = { - info: { - masterKey: 'masterKey' - }, - config: { - masterKey: 'masterKeyAgain' - } - } - - expect(() => { - PushRouter.validateMasterKey(request); - }).toThrow(); - done(); - }); - it('can get query condition when channels is set', (done) => { // Make mock request var request = { diff --git a/src/Routers/PushRouter.js b/src/Routers/PushRouter.js index f75d9998..c3af0d28 100644 --- a/src/Routers/PushRouter.js +++ b/src/Routers/PushRouter.js @@ -1,57 +1,42 @@ -import PushController from '../Controllers/PushController' import PromiseRouter from '../PromiseRouter'; +import * as middleware from "../middlewares"; +import { Parse } from "parse/node"; export class PushRouter extends PromiseRouter { mountRoutes() { - this.route("POST", "/push", req => { return this.handlePOST(req); }); - } - - /** - * Check whether the api call has master key or not. - * @param {Object} request A request object - */ - static validateMasterKey(req) { - if (req.info.masterKey !== req.config.masterKey) { - throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, - 'Master key is invalid, you should only use master key to send push'); - } + this.route("POST", "/push", middleware.promiseEnforceMasterKeyAccess, PushRouter.handlePOST); } - handlePOST(req) { - // TODO: move to middlewares when support for Promise middlewares - PushRouter.validateMasterKey(req); - + static handlePOST(req) { const pushController = req.config.pushController; if (!pushController) { - throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, - 'Push controller is not set'); + throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, 'Push controller is not set'); } - var where = PushRouter.getQueryCondition(req); - + let where = PushRouter.getQueryCondition(req); pushController.sendPush(req.body, where, req.config, req.auth); return Promise.resolve({ - response: { - 'result': true - } + response: { + 'result': true + } }); } - - /** + + /** * Get query condition from the request body. - * @param {Object} request A request object + * @param {Object} req A request object * @returns {Object} The query condition, the where field in a query api call */ static getQueryCondition(req) { - var body = req.body || {}; - var hasWhere = typeof body.where !== 'undefined'; - var hasChannels = typeof body.channels !== 'undefined'; + let body = req.body || {}; + let hasWhere = typeof body.where !== 'undefined'; + let hasChannels = typeof body.channels !== 'undefined'; - var where; + let where; if (hasWhere && hasChannels) { throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, - 'Channels and query can not be set at the same time.'); + 'Channels and query can not be set at the same time.'); } else if (hasWhere) { where = body.where; } else if (hasChannels) { @@ -62,11 +47,10 @@ export class PushRouter extends PromiseRouter { } } else { throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, - 'Channels and query should be set at least one.'); + 'Channels and query should be set at least one.'); } return where; } - } export default PushRouter; From de0f71cc9e4f29a668b9d73aed4a1b459831196e Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Mon, 7 Mar 2016 14:25:04 -0800 Subject: [PATCH 17/29] Remove useless masterKey validation in PushController.sendPush. --- spec/PushController.spec.js | 25 ------------------------- src/Controllers/PushController.js | 14 +------------- 2 files changed, 1 insertion(+), 38 deletions(-) diff --git a/spec/PushController.spec.js b/spec/PushController.spec.js index 1821d1a3..64a7959a 100644 --- a/spec/PushController.spec.js +++ b/spec/PushController.spec.js @@ -3,31 +3,6 @@ var PushController = require('../src/Controllers/PushController').PushController var Config = require('../src/Config'); describe('PushController', () => { - it('can check valid master key of request', (done) => { - // Make mock request - var auth = { - isMaster: true - } - - expect(() => { - PushController.validateMasterKey(auth); - }).not.toThrow(); - done(); - }); - - it('can check invalid master key of request', (done) => { - // Make mock request - var auth = { - isMaster: false - } - - expect(() => { - PushController.validateMasterKey(auth); - }).toThrow(); - done(); - }); - - it('can validate device type when no device type is set', (done) => { // Make query condition var where = { diff --git a/src/Controllers/PushController.js b/src/Controllers/PushController.js index fb03ab52..116c78ed 100644 --- a/src/Controllers/PushController.js +++ b/src/Controllers/PushController.js @@ -36,17 +36,6 @@ export class PushController extends AdaptableController { } } } - - /** - * Check whether the api call has master key or not. - * @param {Object} request A request object - */ - static validateMasterKey(auth = {}) { - if (!auth.isMaster) { - throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, - 'Master key is invalid, you should only use master key to send push'); - } - } sendPush(body = {}, where = {}, config, auth) { var pushAdapter = this.adapter; @@ -54,7 +43,6 @@ export class PushController extends AdaptableController { throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, 'Push adapter is not available'); } - PushController.validateMasterKey(auth); PushController.validatePushType(where, pushAdapter.getValidPushTypes()); // Replace the expiration_time with a valid Unix epoch milliseconds time body['expiration_time'] = PushController.getExpirationTime(body); @@ -140,6 +128,6 @@ export class PushController extends AdaptableController { expectedAdapterType() { return PushAdapter; } -}; +} export default PushController; From 654a540b6acf2e17806027699003f91ed4313581 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Mon, 7 Mar 2016 15:42:42 -0800 Subject: [PATCH 18/29] Fix race condition in GlobalConfig test. --- spec/ParseGlobalConfig.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/ParseGlobalConfig.spec.js b/spec/ParseGlobalConfig.spec.js index 176c7860..4b684553 100644 --- a/spec/ParseGlobalConfig.spec.js +++ b/spec/ParseGlobalConfig.spec.js @@ -5,11 +5,11 @@ var Parse = require('parse/node').Parse; let Config = require('../src/Config'); describe('a GlobalConfig', () => { - beforeEach(function (done) { + beforeEach(done => { let config = new Config('test'); config.database.adaptiveCollection('_GlobalConfig') .then(coll => coll.upsertOne({ '_id': 1 }, { $set: { params: { companies: ['US', 'DK'] } } })) - .then(done()); + .then(() => { done(); }); }); it('can be retrieved', (done) => { From bf96f0d28ab8d24bacaede2375816e1f708788d5 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Mon, 7 Mar 2016 17:19:55 -0500 Subject: [PATCH 19/29] Fixes problems related to increment badge - name conventions are aweful in PushController - properly looks at the badge into body.data instead of body - We may want to refactor that as it's confusing to use a full body --- spec/Parse.Push.spec.js | 59 +++++++++++++++++++++++++++++++ spec/PushController.spec.js | 14 ++++---- src/Controllers/PushController.js | 37 ++++++++++--------- 3 files changed, 87 insertions(+), 23 deletions(-) create mode 100644 spec/Parse.Push.spec.js diff --git a/spec/Parse.Push.spec.js b/spec/Parse.Push.spec.js new file mode 100644 index 00000000..a2b71d5f --- /dev/null +++ b/spec/Parse.Push.spec.js @@ -0,0 +1,59 @@ +describe('Parse.Push', () => { + it('should properly send push', (done) => { + var pushAdapter = { + send: function(body, installations) { + var badge = body.data.badge; + installations.forEach((installation) => { + if (installation.deviceType == "ios") { + expect(installation.badge).toEqual(badge); + expect(installation.originalBadge+1).toEqual(installation.badge); + } else { + expect(installation.badge).toBeUndefined(); + } + }); + return Promise.resolve({ + body: body, + installations: installations + }); + }, + getValidPushTypes: function() { + return ["ios", "android"]; + } + } + setServerConfiguration({ + appId: Parse.applicationId, + masterKey: Parse.masterKey, + serverURL: Parse.serverURL, + push: { + adapter: pushAdapter + } + }); + var installations = []; + while(installations.length != 10) { + var installation = new Parse.Object("_Installation"); + installation.set("installationId", "installation_"+installations.length); + installation.set("deviceToken","device_token_"+installations.length) + installation.set("badge", installations.length); + installation.set("originalBadge", installations.length); + installation.set("deviceType", "ios"); + installations.push(installation); + } + Parse.Object.saveAll(installations).then(() => { + return Parse.Push.send({ + where: { + deviceType: 'ios' + }, + data: { + badge: 'Increment', + alert: 'Hello world!' + } + }, {useMasterKey: true}); + }) + .then(() => { + done(); + }, (err) => { + console.error(err); + done(); + }); + }); +}); \ No newline at end of file diff --git a/spec/PushController.spec.js b/spec/PushController.spec.js index 64a7959a..e2ced56f 100644 --- a/spec/PushController.spec.js +++ b/spec/PushController.spec.js @@ -107,10 +107,10 @@ describe('PushController', () => { it('properly increment badges', (done) => { - var payload = { + var payload = {data:{ alert: "Hello World!", badge: "Increment", - } + }} var installations = []; while(installations.length != 10) { var installation = new Parse.Object("_Installation"); @@ -132,7 +132,7 @@ describe('PushController', () => { var pushAdapter = { send: function(body, installations) { - var badge = body.badge; + var badge = body.data.badge; installations.forEach((installation) => { if (installation.deviceType == "ios") { expect(installation.badge).toEqual(badge); @@ -171,10 +171,10 @@ describe('PushController', () => { it('properly set badges to 1', (done) => { - var payload = { + var payload = {data: { alert: "Hello World!", badge: 1, - } + }} var installations = []; while(installations.length != 10) { var installation = new Parse.Object("_Installation"); @@ -188,7 +188,7 @@ describe('PushController', () => { var pushAdapter = { send: function(body, installations) { - var badge = body.badge; + var badge = body.data.badge; installations.forEach((installation) => { expect(installation.badge).toEqual(badge); expect(1).toEqual(installation.badge); @@ -219,6 +219,6 @@ describe('PushController', () => { done(); }); - }) + }); }); diff --git a/src/Controllers/PushController.js b/src/Controllers/PushController.js index 116c78ed..8dbc6a59 100644 --- a/src/Controllers/PushController.js +++ b/src/Controllers/PushController.js @@ -48,55 +48,60 @@ export class PushController extends AdaptableController { body['expiration_time'] = PushController.getExpirationTime(body); // TODO: If the req can pass the checking, we return immediately instead of waiting // pushes to be sent. We probably change this behaviour in the future. - let badgeUpdate = Promise.resolve(); + let badgeUpdate = () => { + return Promise.resolve(); + } - if (body.badge) { + if (body.data && body.data.badge) { + let badge = body.data.badge; let op = {}; - if (body.badge == "Increment") { + if (badge == "Increment") { op = { $inc: { badge: 1 } } - } else if (Number(body.badge)) { - op = { $set: { badge: body.badge } } + } else if (Number(badge)) { + op = { $set: { badge: badge } } } else { throw "Invalid value for badge, expected number or 'Increment'"; } let updateWhere = deepcopy(where); updateWhere.deviceType = 'ios'; // Only on iOS! - badgeUpdate = config.database.adaptiveCollection("_Installation") + badgeUpdate = () => { + return config.database.adaptiveCollection("_Installation") .then(coll => coll.updateMany(updateWhere, op)); + } } - return badgeUpdate.then(() => { - return rest.find(config, auth, '_Installation', where) + return badgeUpdate().then(() => { + return rest.find(config, auth, '_Installation', where); }).then((response) => { - if (body.badge && body.badge == "Increment") { + if (body.data && body.data.badge && body.data.badge == "Increment") { // Collect the badges to reduce the # of calls let badgeInstallationsMap = response.results.reduce((map, installation) => { let badge = installation.badge; if (installation.deviceType != "ios") { badge = UNSUPPORTED_BADGE_KEY; } - map[badge] = map[badge] || []; - map[badge].push(installation); + map[badge+''] = map[badge+''] || []; + map[badge+''].push(installation); return map; }, {}); - + // Map the on the badges count and return the send result let promises = Object.keys(badgeInstallationsMap).map((badge) => { let payload = deepcopy(body); if (badge == UNSUPPORTED_BADGE_KEY) { - delete payload.badge; + delete payload.data.badge; } else { - payload.badge = parseInt(badge); + payload.data.badge = parseInt(badge); } - return pushAdapter.send(payload, badgeInstallationsMap[badge]); + return pushAdapter.send(payload, badgeInstallationsMap[badge]); }); return Promise.all(promises); } return pushAdapter.send(body, response.results); }); } - + /** * Get expiration time from the request body. * @param {Object} request A request object From 438cf58d4c436f8480fa7ac74b2d7edfc05d2f66 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Mon, 7 Mar 2016 22:45:46 -0800 Subject: [PATCH 20/29] Fix early server response in Schema validation. --- src/Schema.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Schema.js b/src/Schema.js index 3e25b09d..13ccb7cd 100644 --- a/src/Schema.js +++ b/src/Schema.js @@ -448,9 +448,12 @@ class Schema { geocount++; } if (geocount > 1) { - throw new Parse.Error( - Parse.Error.INCORRECT_TYPE, - 'there can only be one geopoint field in a class'); + // Make sure all field validation operations run before we return. + // If not - we are continuing to run logic, but already provided response from the server. + return promise.then(() => { + return Promise.reject(new Parse.Error(Parse.Error.INCORRECT_TYPE, + 'there can only be one geopoint field in a class')); + }); } if (!expected) { continue; From 0abd5a59312ec127c6a3fe5c7a6283fff3822870 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Mon, 7 Mar 2016 22:45:54 -0800 Subject: [PATCH 21/29] Re-enable GeoPoint test. --- spec/ParseGeoPoint.spec.js | 58 ++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/spec/ParseGeoPoint.spec.js b/spec/ParseGeoPoint.spec.js index 66613603..7d6a8291 100644 --- a/spec/ParseGeoPoint.spec.js +++ b/spec/ParseGeoPoint.spec.js @@ -39,37 +39,33 @@ describe('Parse.GeoPoint testing', () => { }); }); -// -// This test is disabled, since it's extremely flaky on Travis-CI. -// Tracking issue: https://github.com/ParsePlatform/parse-server/issues/572 -// -// it('geo line', (done) => { -// var line = []; -// for (var i = 0; i < 10; ++i) { -// var obj = new TestObject(); -// var point = new Parse.GeoPoint(i * 4.0 - 12.0, i * 3.2 - 11.0); -// obj.set('location', point); -// obj.set('construct', 'line'); -// obj.set('seq', i); -// line.push(obj); -// } -// Parse.Object.saveAll(line, { -// success: function() { -// var query = new Parse.Query(TestObject); -// var point = new Parse.GeoPoint(24, 19); -// query.equalTo('construct', 'line'); -// query.withinMiles('location', point, 10000); -// query.find({ -// success: function(results) { -// equal(results.length, 10); -// equal(results[0].get('seq'), 9); -// equal(results[3].get('seq'), 6); -// done(); -// } -// }); -// } -// }); -// }); + it('geo line', (done) => { + var line = []; + for (var i = 0; i < 10; ++i) { + var obj = new TestObject(); + var point = new Parse.GeoPoint(i * 4.0 - 12.0, i * 3.2 - 11.0); + obj.set('location', point); + obj.set('construct', 'line'); + obj.set('seq', i); + line.push(obj); + } + Parse.Object.saveAll(line, { + success: function() { + var query = new Parse.Query(TestObject); + var point = new Parse.GeoPoint(24, 19); + query.equalTo('construct', 'line'); + query.withinMiles('location', point, 10000); + query.find({ + success: function(results) { + equal(results.length, 10); + equal(results[0].get('seq'), 9); + equal(results[3].get('seq'), 6); + done(); + } + }); + } + }); + }); it('geo max distance large', (done) => { var objects = []; From a163327ac9ae5765698b032d1bf8e8271baf14c2 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Mon, 7 Mar 2016 19:26:40 -0800 Subject: [PATCH 22/29] Remove usages of non-adaptive collection inside DatabaseController. --- src/Adapters/Storage/Mongo/MongoCollection.js | 8 +- src/Controllers/DatabaseController.js | 132 +++++++++--------- src/Controllers/HooksController.js | 2 +- 3 files changed, 74 insertions(+), 68 deletions(-) diff --git a/src/Adapters/Storage/Mongo/MongoCollection.js b/src/Adapters/Storage/Mongo/MongoCollection.js index 78c90fef..6e1a73c1 100644 --- a/src/Adapters/Storage/Mongo/MongoCollection.js +++ b/src/Adapters/Storage/Mongo/MongoCollection.js @@ -57,6 +57,10 @@ export default class MongoCollection { }) } + insertOne(object) { + return this._mongoCollection.insertOne(object); + } + // Atomically updates data in the database for a single (first) object that matched the query // If there is nothing that matches the query - does insert // Postgres Note: `INSERT ... ON CONFLICT UPDATE` that is available since 9.5. @@ -83,8 +87,8 @@ export default class MongoCollection { return this._mongoCollection.deleteOne(query); } - remove(query) { - return this._mongoCollection.remove(query); + deleteMany(query) { + return this._mongoCollection.deleteMany(query); } drop() { diff --git a/src/Controllers/DatabaseController.js b/src/Controllers/DatabaseController.js index 83c2703a..e2761bd5 100644 --- a/src/Controllers/DatabaseController.js +++ b/src/Controllers/DatabaseController.js @@ -54,6 +54,14 @@ function returnsTrue() { return true; } +DatabaseController.prototype.validateClassName = function(className) { + if (!Schema.classNameIsValid(className)) { + const error = new Parse.Error(Parse.Error.INVALID_CLASS_NAME, 'invalid className: ' + className); + return Promise.reject(error); + } + return Promise.resolve(); +}; + // Returns a promise for a schema object. // If we are provided a acceptor, then we run it on the schema. // If the schema isn't accepted, we reload it at most once. @@ -230,30 +238,28 @@ DatabaseController.prototype.handleRelationUpdates = function(className, // Adds a relation. // Returns a promise that resolves successfully iff the add was successful. -DatabaseController.prototype.addRelation = function(key, fromClassName, - fromId, toId) { - var doc = { +DatabaseController.prototype.addRelation = function(key, fromClassName, fromId, toId) { + let doc = { relatedId: toId, - owningId: fromId + owningId : fromId }; - var className = '_Join:' + key + ':' + fromClassName; - return this.collection(className).then((coll) => { - return coll.update(doc, doc, {upsert: true}); + let className = `_Join:${key}:${fromClassName}`; + return this.adaptiveCollection(className).then((coll) => { + return coll.upsertOne(doc, doc); }); }; // Removes a relation. // Returns a promise that resolves successfully iff the remove was // successful. -DatabaseController.prototype.removeRelation = function(key, fromClassName, - fromId, toId) { +DatabaseController.prototype.removeRelation = function(key, fromClassName, fromId, toId) { var doc = { relatedId: toId, owningId: fromId }; - var className = '_Join:' + key + ':' + fromClassName; - return this.collection(className).then((coll) => { - return coll.remove(doc); + let className = `_Join:${key}:${fromClassName}`; + return this.adaptiveCollection(className).then(coll => { + return coll.deleteOne(doc); }); }; @@ -269,40 +275,36 @@ DatabaseController.prototype.destroy = function(className, query, options = {}) var aclGroup = options.acl || []; var schema; - return this.loadSchema().then((s) => { - schema = s; - if (!isMaster) { - return schema.validatePermission(className, aclGroup, 'delete'); - } - return Promise.resolve(); - }).then(() => { - - return this.collection(className); - }).then((coll) => { - var mongoWhere = transform.transformWhere(schema, className, query); - - if (options.acl) { - var writePerms = [ - {_wperm: {'$exists': false}} - ]; - for (var entry of options.acl) { - writePerms.push({_wperm: {'$in': [entry]}}); + return this.loadSchema() + .then(s => { + schema = s; + if (!isMaster) { + return schema.validatePermission(className, aclGroup, 'delete'); } - mongoWhere = {'$and': [mongoWhere, {'$or': writePerms}]}; - } + return Promise.resolve(); + }) + .then(() => this.adaptiveCollection(className)) + .then(collection => { + let mongoWhere = transform.transformWhere(schema, className, query); - return coll.remove(mongoWhere); - }).then((resp) => { - //Check _Session to avoid changing password failed without any session. - if (resp.result.n === 0 && className !== "_Session") { - return Promise.reject( - new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, - 'Object not found.')); - - } - }, (error) => { - throw error; - }); + if (options.acl) { + var writePerms = [ + { _wperm: { '$exists': false } } + ]; + for (var entry of options.acl) { + writePerms.push({ _wperm: { '$in': [entry] } }); + } + mongoWhere = { '$and': [mongoWhere, { '$or': writePerms }] }; + } + return collection.deleteMany(mongoWhere); + }) + .then(resp => { + //Check _Session to avoid changing password failed without any session. + // TODO: @nlutsenko Stop relying on `result.n` + if (resp.result.n === 0 && className !== "_Session") { + throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Object not found.'); + } + }); }; // Inserts an object into the database. @@ -312,21 +314,21 @@ DatabaseController.prototype.create = function(className, object, options) { var isMaster = !('acl' in options); var aclGroup = options.acl || []; - return this.loadSchema().then((s) => { - schema = s; - if (!isMaster) { - return schema.validatePermission(className, aclGroup, 'create'); - } - return Promise.resolve(); - }).then(() => { - - return this.handleRelationUpdates(className, null, object); - }).then(() => { - return this.collection(className); - }).then((coll) => { - var mongoObject = transform.transformCreate(schema, className, object); - return coll.insert([mongoObject]); - }); + return this.validateClassName(className) + .then(() => this.loadSchema()) + .then(s => { + schema = s; + if (!isMaster) { + return schema.validatePermission(className, aclGroup, 'create'); + } + return Promise.resolve(); + }) + .then(() => this.handleRelationUpdates(className, null, object)) + .then(() => this.adaptiveCollection(className)) + .then(coll => { + var mongoObject = transform.transformCreate(schema, className, object); + return coll.insertOne(mongoObject); + }); }; // Runs a mongo query on the database. @@ -386,14 +388,14 @@ DatabaseController.prototype.owningIds = function(className, key, relatedIds) { // equal-to-pointer constraints on relation fields. // Returns a promise that resolves when query is mutated DatabaseController.prototype.reduceInRelation = function(className, query, schema) { - + // Search for an in-relation or equal-to-relation // Make it sequential for now, not sure of paralleization side effects if (query['$or']) { let ors = query['$or']; return Promise.all(ors.map((aQuery, index) => { return this.reduceInRelation(className, aQuery, schema).then((aQuery) => { - query['$or'][index] = aQuery; + query['$or'][index] = aQuery; }) })); } @@ -413,14 +415,14 @@ DatabaseController.prototype.reduceInRelation = function(className, query, schem relatedIds = [query[key].objectId]; } return this.owningIds(className, key, relatedIds).then((ids) => { - delete query[key]; + delete query[key]; this.addInObjectIdsIds(ids, query); return Promise.resolve(query); }); } return Promise.resolve(query); }) - + return Promise.all(promises).then(() => { return Promise.resolve(query); }) @@ -429,13 +431,13 @@ DatabaseController.prototype.reduceInRelation = function(className, query, schem // Modifies query so that it no longer has $relatedTo // Returns a promise that resolves when query is mutated DatabaseController.prototype.reduceRelationKeys = function(className, query) { - + if (query['$or']) { return Promise.all(query['$or'].map((aQuery) => { return this.reduceRelationKeys(className, aQuery); })); } - + var relatedTo = query['$relatedTo']; if (relatedTo) { return this.relatedIds( diff --git a/src/Controllers/HooksController.js b/src/Controllers/HooksController.js index fbc7e920..cbf26f8a 100644 --- a/src/Controllers/HooksController.js +++ b/src/Controllers/HooksController.js @@ -71,7 +71,7 @@ export class HooksController { _removeHooks(query) { return this.getCollection().then(collection => { - return collection.remove(query); + return collection.deleteMany(query); }).then(() => { return {}; }); From fb5b8fb58fff99d09e101c2e55880cc87a6938ad Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Mon, 7 Mar 2016 20:56:26 -0800 Subject: [PATCH 23/29] Migrate Schema.js to adaptive mongo collection. --- src/Adapters/Storage/Mongo/MongoCollection.js | 6 +++- src/Controllers/DatabaseController.js | 18 +++------- src/Schema.js | 33 +++++++++---------- 3 files changed, 25 insertions(+), 32 deletions(-) diff --git a/src/Adapters/Storage/Mongo/MongoCollection.js b/src/Adapters/Storage/Mongo/MongoCollection.js index 6e1a73c1..1e501824 100644 --- a/src/Adapters/Storage/Mongo/MongoCollection.js +++ b/src/Adapters/Storage/Mongo/MongoCollection.js @@ -54,7 +54,7 @@ export default class MongoCollection { return this._mongoCollection.findAndModify(query, [], update, { new: true }).then(document => { // Value is the object where mongo returns multiple fields. return document.value; - }) + }); } insertOne(object) { @@ -68,6 +68,10 @@ export default class MongoCollection { return this._mongoCollection.update(query, update, { upsert: true }); } + updateOne(query, update) { + return this._mongoCollection.updateOne(query, update); + } + updateMany(query, update) { return this._mongoCollection.updateMany(query, update); } diff --git a/src/Controllers/DatabaseController.js b/src/Controllers/DatabaseController.js index e2761bd5..f261db56 100644 --- a/src/Controllers/DatabaseController.js +++ b/src/Controllers/DatabaseController.js @@ -28,16 +28,6 @@ DatabaseController.prototype.connect = function() { return this.adapter.connect(); }; -// Returns a promise for a Mongo collection. -// Generally just for internal use. -DatabaseController.prototype.collection = function(className) { - if (!Schema.classNameIsValid(className)) { - throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, - 'invalid className: ' + className); - } - return this.adapter.collection(this.collectionPrefix + className); -}; - DatabaseController.prototype.adaptiveCollection = function(className) { return this.adapter.adaptiveCollection(this.collectionPrefix + className); }; @@ -68,9 +58,9 @@ DatabaseController.prototype.validateClassName = function(className) { DatabaseController.prototype.loadSchema = function(acceptor = returnsTrue) { if (!this.schemaPromise) { - this.schemaPromise = this.collection('_SCHEMA').then((coll) => { + this.schemaPromise = this.adaptiveCollection('_SCHEMA').then(collection => { delete this.schemaPromise; - return Schema.load(coll); + return Schema.load(collection); }); return this.schemaPromise; } @@ -79,9 +69,9 @@ DatabaseController.prototype.loadSchema = function(acceptor = returnsTrue) { if (acceptor(schema)) { return schema; } - this.schemaPromise = this.collection('_SCHEMA').then((coll) => { + this.schemaPromise = this.adaptiveCollection('_SCHEMA').then(collection => { delete this.schemaPromise; - return Schema.load(coll); + return Schema.load(collection); }); return this.schemaPromise; }); diff --git a/src/Schema.js b/src/Schema.js index 13ccb7cd..a3bf8245 100644 --- a/src/Schema.js +++ b/src/Schema.js @@ -168,12 +168,12 @@ function schemaAPITypeToMongoFieldType(type) { // '_metadata' is ignored for now // Everything else is expected to be a userspace field. class Schema { - collection; + _collection; data; perms; constructor(collection) { - this.collection = collection; + this._collection = collection; // this.data[className][fieldName] tells you the type of that field this.data = {}; @@ -184,8 +184,8 @@ class Schema { reloadData() { this.data = {}; this.perms = {}; - return this.collection.find({}, {}).toArray().then(mongoSchema => { - for (let obj of mongoSchema) { + return this._collection.find({}).then(results => { + for (let obj of results) { let className = null; let classData = {}; let permsData = null; @@ -231,7 +231,7 @@ class Schema { return Promise.reject(mongoObject); } - return this.collection.insertOne(mongoObject.result) + return this._collection.insertOne(mongoObject.result) .then(result => result.ops[0]) .catch(error => { if (error.code === 11000) { //Mongo's duplicate key error @@ -268,7 +268,7 @@ class Schema { 'schema is frozen, cannot add: ' + className); } // We don't have this class. Update the schema - return this.collection.insert([{_id: className}]).then(() => { + return this._collection.insertOne({ _id: className }).then(() => { // The schema update succeeded. Reload the schema return this.reloadData(); }, () => { @@ -280,10 +280,9 @@ class Schema { }).then(() => { // Ensure that the schema now validates return this.validateClassName(className, true); - }, (error) => { + }, () => { // The schema still doesn't validate. Give up - throw new Parse.Error(Parse.Error.INVALID_JSON, - 'schema class name does not revalidate'); + throw new Parse.Error(Parse.Error.INVALID_JSON, 'schema class name does not revalidate'); }); } @@ -296,7 +295,7 @@ class Schema { } }; update = {'$set': update}; - return this.collection.findAndModify(query, {}, update, {}).then(() => { + return this._collection.updateOne(query, update).then(() => { // The update succeeded. Reload the schema return this.reloadData(); }); @@ -354,12 +353,12 @@ class Schema { // We don't have this field. Update the schema. // Note that we use the $exists guard and $set to avoid race // conditions in the database. This is important! - var query = {_id: className}; - query[key] = {'$exists': false}; + var query = { _id: className }; + query[key] = { '$exists': false }; var update = {}; update[key] = type; update = {'$set': update}; - return this.collection.findAndModify(query, {}, update, {}).then(() => { + return this._collection.upsertOne(query, update).then(() => { // The update succeeded. Reload the schema return this.reloadData(); }, () => { @@ -422,14 +421,14 @@ class Schema { // for non-relations, remove all the data. // This is necessary to ensure that the data is still gone if they add the same field. - return database.collection(className) + return database.adaptiveCollection(className) .then(collection => { - var mongoFieldName = this.data[className][fieldName].startsWith('*') ? '_p_' + fieldName : fieldName; - return collection.update({}, { "$unset": { [mongoFieldName] : null } }, { multi: true }); + let mongoFieldName = this.data[className][fieldName].startsWith('*') ? `_p_${fieldName}` : fieldName; + return collection.updateMany({}, { "$unset": { [mongoFieldName]: null } }); }); }) // Save the _SCHEMA object - .then(() => this.collection.update({ _id: className }, { $unset: {[fieldName]: null }})); + .then(() => this._collection.updateOne({ _id: className }, { $unset: { [fieldName]: null } })); }); } From 49eb9df1ef585819f24b9cdfc96edcf194eec84f Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Mon, 7 Mar 2016 20:56:51 -0800 Subject: [PATCH 24/29] Remove private Schema API usage from SchemasRouter. --- src/Routers/SchemasRouter.js | 41 +++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/Routers/SchemasRouter.js b/src/Routers/SchemasRouter.js index 0d1a4cf9..74b15285 100644 --- a/src/Routers/SchemasRouter.js +++ b/src/Routers/SchemasRouter.js @@ -18,7 +18,7 @@ function getAllSchemas(req) { return req.config.database.adaptiveCollection('_SCHEMA') .then(collection => collection.find({})) .then(schemas => schemas.map(Schema.mongoSchemaToSchemaAPIResponse)) - .then(schemas => ({ response: { results: schemas }})); + .then(schemas => ({ response: { results: schemas } })); } function getOneSchema(req) { @@ -65,7 +65,7 @@ function modifySchema(req) { throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${req.params.className} does not exist.`); } - let existingFields = Object.assign(schema.data[className], {_id: className}); + let existingFields = Object.assign(schema.data[className], { _id: className }); Object.keys(submittedFields).forEach(name => { let field = submittedFields[name]; if (existingFields[name] && field.__op !== 'Delete') { @@ -83,24 +83,27 @@ function modifySchema(req) { } // Finally we have checked to make sure the request is valid and we can start deleting fields. - // Do all deletions first, then a single save to _SCHEMA collection to handle all additions. - let deletionPromises = []; - Object.keys(submittedFields).forEach(submittedFieldName => { - if (submittedFields[submittedFieldName].__op === 'Delete') { - let promise = schema.deleteField(submittedFieldName, className, req.config.database); - deletionPromises.push(promise); + // Do all deletions first, then add fields to avoid duplicate geopoint error. + let deletePromises = []; + let insertedFields = []; + Object.keys(submittedFields).forEach(fieldName => { + if (submittedFields[fieldName].__op === 'Delete') { + const promise = schema.deleteField(fieldName, className, req.config.database); + deletePromises.push(promise); + } else { + insertedFields.push(fieldName); } }); - - return Promise.all(deletionPromises) - .then(() => new Promise((resolve, reject) => { - schema.collection.update({_id: className}, mongoObject.result, {w: 1}, (err, docs) => { - if (err) { - reject(err); - } - resolve({ response: Schema.mongoSchemaToSchemaAPIResponse(mongoObject.result)}); - }) - })); + return Promise.all(deletePromises) // Delete Everything + .then(() => schema.reloadData()) // Reload our Schema, so we have all the new values + .then(() => { + let promises = insertedFields.map(fieldName => { + const mongoType = mongoObject.result[fieldName]; + return schema.validateField(className, fieldName, mongoType); + }); + return Promise.all(promises); + }) + .then(() => ({ response: Schema.mongoSchemaToSchemaAPIResponse(mongoObject.result) })); }); } @@ -140,7 +143,7 @@ function deleteSchema(req) { // We've dropped the collection now, so delete the item from _SCHEMA // and clear the _Join collections return req.config.database.adaptiveCollection('_SCHEMA') - .then(coll => coll.findOneAndDelete({_id: req.params.className})) + .then(coll => coll.findOneAndDelete({ _id: req.params.className })) .then(document => { if (document === null) { //tried to delete non-existent class From 440099267d07efa326dba2aaec7821df86025877 Mon Sep 17 00:00:00 2001 From: Carmen Date: Tue, 8 Mar 2016 20:23:55 +0800 Subject: [PATCH 25/29] #911 support params option in Parse.Cloud.httpRequest --- spec/HTTPRequest.spec.js | 37 ++++++++++++++++++++++++++++++++++- src/cloud-code/httpRequest.js | 9 ++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/spec/HTTPRequest.spec.js b/spec/HTTPRequest.spec.js index ad4e289f..abc5decf 100644 --- a/spec/HTTPRequest.spec.js +++ b/spec/HTTPRequest.spec.js @@ -24,7 +24,11 @@ app.get("/301", function(req, res){ app.post('/echo', function(req, res){ res.json(req.body); -}) +}); + +app.get('/qs', function(req, res){ + res.json(req.query); +}); app.listen(13371); @@ -193,4 +197,35 @@ describe("httpRequest", () => { } }); }) + + it("should params object to query string", (done) => { + httpRequest({ + url: httpRequestServer+"/qs", + params: { + foo: "bar" + } + }).then(function(httpResponse){ + expect(httpResponse.status).toBe(200); + expect(httpResponse.data).toEqual({foo: "bar"}); + done(); + }, function(){ + fail("should not fail"); + done(); + }) + }); + + it("should params string to query string", (done) => { + httpRequest({ + url: httpRequestServer+"/qs", + params: "foo=bar&foo2=bar2" + }).then(function(httpResponse){ + expect(httpResponse.status).toBe(200); + expect(httpResponse.data).toEqual({foo: "bar", foo2: 'bar2'}); + done(); + }, function(){ + fail("should not fail"); + done(); + }) + }); + }); diff --git a/src/cloud-code/httpRequest.js b/src/cloud-code/httpRequest.js index 4e8ff654..35af79ca 100644 --- a/src/cloud-code/httpRequest.js +++ b/src/cloud-code/httpRequest.js @@ -1,4 +1,5 @@ var request = require("request"), + querystring = require('querystring'), Parse = require('parse/node').Parse; var encodeBody = function(body, headers = {}) { @@ -34,7 +35,13 @@ module.exports = function(options) { options.body = encodeBody(options.body, options.headers); // set follow redirects to false by default options.followRedirect = options.followRedirects == true; - + // support params options + if (typeof options.params === 'object') { + options.qs = options.params; + } else if (typeof options.params === 'string') { + options.qs = querystring.parse(options.params); + } + request(options, (error, response, body) => { if (error) { if (callbacks.error) { From 2f63c5528da25c5e91ee671da79bec81e3b9ab3a Mon Sep 17 00:00:00 2001 From: Marco129 Date: Wed, 9 Mar 2016 02:15:27 +0800 Subject: [PATCH 26/29] Minimize db query --- src/RestQuery.js | 4 +--- src/RestWrite.js | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/RestQuery.js b/src/RestQuery.js index 1e0f344e..a6770f47 100644 --- a/src/RestQuery.js +++ b/src/RestQuery.js @@ -168,9 +168,7 @@ RestQuery.prototype.validateClientClassCreation = function() { let sysClass = ['_User', '_Installation', '_Role', '_Session', '_Product']; if (this.config.allowClientClassCreation === false && !this.auth.isMaster && sysClass.indexOf(this.className) === -1) { - return this.config.database.loadSchema().then((schema) => { - return schema.hasClass(this.className) - }).then((hasClass) => { + return this.config.database.collectionExists(this.className).then((hasClass) => { if (hasClass === true) { return Promise.resolve(); } diff --git a/src/RestWrite.js b/src/RestWrite.js index 72eda1fc..9e07c93a 100644 --- a/src/RestWrite.js +++ b/src/RestWrite.js @@ -112,9 +112,7 @@ RestWrite.prototype.validateClientClassCreation = function() { let sysClass = ['_User', '_Installation', '_Role', '_Session', '_Product']; if (this.config.allowClientClassCreation === false && !this.auth.isMaster && sysClass.indexOf(this.className) === -1) { - return this.config.database.loadSchema().then((schema) => { - return schema.hasClass(this.className) - }).then((hasClass) => { + return this.config.database.collectionExists(this.className).then((hasClass) => { if (hasClass === true) { return Promise.resolve(); } From b9f08d9694cf9ee2f648de999a9c9a44dbfea4b4 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Tue, 8 Mar 2016 16:08:41 -0800 Subject: [PATCH 27/29] Do not mutate parameter object in DatabaseController. --- src/Controllers/DatabaseController.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Controllers/DatabaseController.js b/src/Controllers/DatabaseController.js index f261db56..80eea63b 100644 --- a/src/Controllers/DatabaseController.js +++ b/src/Controllers/DatabaseController.js @@ -6,6 +6,7 @@ var Parse = require('parse/node').Parse; var Schema = require('./../Schema'); var transform = require('./../transform'); +const deepcopy = require('deepcopy'); // options can contain: // collectionPrefix: the string to put in front of every collection name. @@ -130,6 +131,9 @@ DatabaseController.prototype.untransformObject = function( // one of the provided strings must provide the caller with // write permissions. DatabaseController.prototype.update = function(className, query, update, options) { + // Make a copy of the object, so we don't mutate the incoming data. + update = deepcopy(update); + var acceptor = function(schema) { return schema.hasKeys(className, Object.keys(query)); }; @@ -300,6 +304,9 @@ DatabaseController.prototype.destroy = function(className, query, options = {}) // Inserts an object into the database. // Returns a promise that resolves successfully iff the object saved. DatabaseController.prototype.create = function(className, object, options) { + // Make a copy of the object, so we don't mutate the incoming data. + object = deepcopy(object); + var schema; var isMaster = !('acl' in options); var aclGroup = options.acl || []; From 0f07c5204eb48119527d718a594b91926d1500e6 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Tue, 8 Mar 2016 16:15:49 -0800 Subject: [PATCH 28/29] Add test validating that we have ACL propagate to before/after save hooks. --- spec/ParseAPI.spec.js | 99 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 7 deletions(-) diff --git a/spec/ParseAPI.spec.js b/spec/ParseAPI.spec.js index b4f7f8e0..bd29bcc7 100644 --- a/spec/ParseAPI.spec.js +++ b/spec/ParseAPI.spec.js @@ -4,6 +4,7 @@ var DatabaseAdapter = require('../src/DatabaseAdapter'); var request = require('request'); +const Parse = require("parse/node"); describe('miscellaneous', function() { it('create a GameScore object', function(done) { @@ -372,8 +373,8 @@ describe('miscellaneous', function() { done(); }); }); - - it('test cloud function shoud echo keys', function(done) { + + it('test cloud function should echo keys', function(done) { Parse.Cloud.run('echoKeys').then((result) => { expect(result.applicationId).toEqual(Parse.applicationId); expect(result.masterKey).toEqual(Parse.masterKey); @@ -399,7 +400,7 @@ describe('miscellaneous', function() { expect(results.length).toEqual(1); expect(results[0]['foo']).toEqual('bar'); done(); - }).fail( err => { + }).fail(err => { fail(err); done(); }) @@ -415,9 +416,9 @@ describe('miscellaneous', function() { // Make sure the required mock for all tests is unset. Parse.Cloud._removeHook("Triggers", "beforeSave", "GameScore"); done(); - }); - - it('object is set on create and update', done => { + }); + + it('object is set on create and update', done => { let triggerTime = 0; // Register a mock beforeSave hook Parse.Cloud.beforeSave('GameScore', (req, res) => { @@ -683,7 +684,7 @@ describe('miscellaneous', function() { // Make sure the checking has been triggered expect(triggerTime).toBe(2); // Clear mock afterSave - Parse.Cloud._removeHook("Triggers", "afterSave", "GameScore"); + Parse.Cloud._removeHook("Triggers", "afterSave", "GameScore"); done(); }, function(error) { console.error(error); @@ -732,6 +733,90 @@ describe('miscellaneous', function() { }); }); + it('beforeSave receives ACL', done => { + let triggerTime = 0; + // Register a mock beforeSave hook + Parse.Cloud.beforeSave('GameScore', function(req, res) { + let object = req.object; + if (triggerTime == 0) { + let acl = object.getACL(); + expect(acl.getPublicReadAccess()).toBeTruthy(); + expect(acl.getPublicWriteAccess()).toBeTruthy(); + } else if (triggerTime == 1) { + let acl = object.getACL(); + expect(acl.getPublicReadAccess()).toBeFalsy(); + expect(acl.getPublicWriteAccess()).toBeTruthy(); + } else { + res.error(); + } + triggerTime++; + res.success(); + }); + + let obj = new Parse.Object('GameScore'); + let acl = new Parse.ACL(); + acl.setPublicReadAccess(true); + acl.setPublicWriteAccess(true); + obj.setACL(acl); + obj.save().then(() => { + acl.setPublicReadAccess(false); + obj.setACL(acl); + return obj.save(); + }).then(() => { + // Make sure the checking has been triggered + expect(triggerTime).toBe(2); + // Clear mock afterSave + Parse.Cloud._removeHook("Triggers", "beforeSave", "GameScore"); + done(); + }, error => { + console.error(error); + fail(error); + done(); + }); + }); + + it('afterSave receives ACL', done => { + let triggerTime = 0; + // Register a mock beforeSave hook + Parse.Cloud.afterSave('GameScore', function(req, res) { + let object = req.object; + if (triggerTime == 0) { + let acl = object.getACL(); + expect(acl.getPublicReadAccess()).toBeTruthy(); + expect(acl.getPublicWriteAccess()).toBeTruthy(); + } else if (triggerTime == 1) { + let acl = object.getACL(); + expect(acl.getPublicReadAccess()).toBeFalsy(); + expect(acl.getPublicWriteAccess()).toBeTruthy(); + } else { + res.error(); + } + triggerTime++; + res.success(); + }); + + let obj = new Parse.Object('GameScore'); + let acl = new Parse.ACL(); + acl.setPublicReadAccess(true); + acl.setPublicWriteAccess(true); + obj.setACL(acl); + obj.save().then(() => { + acl.setPublicReadAccess(false); + obj.setACL(acl); + return obj.save(); + }).then(() => { + // Make sure the checking has been triggered + expect(triggerTime).toBe(2); + // Clear mock afterSave + Parse.Cloud._removeHook("Triggers", "afterSave", "GameScore"); + done(); + }, error => { + console.error(error); + fail(error); + done(); + }); + }); + it('test cloud function error handling', (done) => { // Register a function which will fail Parse.Cloud.define('willFail', (req, res) => { From e93873f7b10265ba9cf71fd7b673897cc5aec377 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Tue, 8 Mar 2016 17:08:07 -0800 Subject: [PATCH 29/29] Do not require where clause in condition on queries. --- spec/ParseQuery.spec.js | 46 ++++++++++++++++++++++++++++++++--------- src/RestQuery.js | 2 -- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/spec/ParseQuery.spec.js b/spec/ParseQuery.spec.js index 9171d12c..2c5cb5d4 100644 --- a/spec/ParseQuery.spec.js +++ b/spec/ParseQuery.spec.js @@ -2,6 +2,9 @@ // hungry/js/test/parse_query_test.js // // Some new tests are added. +'use strict'; + +const Parse = require('parse/node'); describe('Parse.Query testing', () => { it("basic query", function(done) { @@ -1574,6 +1577,29 @@ describe('Parse.Query testing', () => { }); }); + it("dontSelect query without conditions", function(done) { + const RestaurantObject = Parse.Object.extend("Restaurant"); + const PersonObject = Parse.Object.extend("Person"); + const objects = [ + new RestaurantObject({ location: "Djibouti" }), + new RestaurantObject({ location: "Ouagadougou" }), + new PersonObject({ name: "Bob", hometown: "Djibouti" }), + new PersonObject({ name: "Tom", hometown: "Yoloblahblahblah" }), + new PersonObject({ name: "Billy", hometown: "Ouagadougou" }) + ]; + + Parse.Object.saveAll(objects, function() { + const query = new Parse.Query(RestaurantObject); + const mainQuery = new Parse.Query(PersonObject); + mainQuery.doesNotMatchKeyInQuery("hometown", "location", query); + mainQuery.find().then(results => { + equal(results.length, 1); + equal(results[0].get('name'), 'Tom'); + done(); + }); + }); + }); + it("object with length", function(done) { var TestObject = Parse.Object.extend("TestObject"); var obj = new TestObject(); @@ -2088,7 +2114,7 @@ describe('Parse.Query testing', () => { console.log(error); }); }); - + // #371 it('should properly interpret a query', (done) => { var query = new Parse.Query("C1"); @@ -2104,7 +2130,7 @@ describe('Parse.Query testing', () => { done(); }) }); - + it('should properly interpret a query', (done) => { var user = new Parse.User(); user.set("username", "foo"); @@ -2112,22 +2138,22 @@ describe('Parse.Query testing', () => { return user.save().then( (user) => { var objIdQuery = new Parse.Query("_User").equalTo("objectId", user.id); var blockedUserQuery = user.relation("blockedUsers").query(); - + var aResponseQuery = new Parse.Query("MatchRelationshipActivityResponse"); aResponseQuery.equalTo("userA", user); aResponseQuery.equalTo("userAResponse", 1); - + var bResponseQuery = new Parse.Query("MatchRelationshipActivityResponse"); bResponseQuery.equalTo("userB", user); bResponseQuery.equalTo("userBResponse", 1); - + var matchOr = Parse.Query.or(aResponseQuery, bResponseQuery); var matchRelationshipA = new Parse.Query("_User"); matchRelationshipA.matchesKeyInQuery("objectId", "userAObjectId", matchOr); var matchRelationshipB = new Parse.Query("_User"); matchRelationshipB.matchesKeyInQuery("objectId", "userBObjectId", matchOr); - - + + var orQuery = Parse.Query.or(objIdQuery, blockedUserQuery, matchRelationshipA, matchRelationshipB); var query = new Parse.Query("_User"); query.doesNotMatchQuery("objectId", orQuery); @@ -2140,8 +2166,8 @@ describe('Parse.Query testing', () => { fail("should not fail"); done(); }); - - + + }); - + }); diff --git a/src/RestQuery.js b/src/RestQuery.js index a6770f47..c14ac415 100644 --- a/src/RestQuery.js +++ b/src/RestQuery.js @@ -324,12 +324,10 @@ RestQuery.prototype.replaceDontSelect = function() { !dontSelectValue.key || typeof dontSelectValue.query !== 'object' || !dontSelectValue.query.className || - !dontSelectValue.query.where || Object.keys(dontSelectValue).length !== 2) { throw new Parse.Error(Parse.Error.INVALID_QUERY, 'improper usage of $dontSelect'); } - var subquery = new RestQuery( this.config, this.auth, dontSelectValue.query.className, dontSelectValue.query.where);