Skip to content

Commit 9776362

Browse files
committed
Begin isolating object creation code into an externalizable API. (#1569)
* Tidy up transformKeyValue * Specialize transformKeyValue for object creation * remove keys that never appear in creation requests * rename function * remove local var * early exit for simple keys * Refactor create * Force class creation when creating an object * Pass parameters to key value transformer * No need to check for array in this func * start using Parse Format schema in MongoTransform * Remove call to getExpectedType * add tests to ensure client can't see _PushStatus
1 parent 59b4047 commit 9776362

File tree

6 files changed

+225
-71
lines changed

6 files changed

+225
-71
lines changed

spec/MongoTransform.spec.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ var dummySchema = {
2323
};
2424

2525

26-
describe('parseObjectToMongoObject', () => {
26+
describe('parseObjectToMongoObjectForCreate', () => {
2727

2828
it('a basic number', (done) => {
2929
var input = {five: 5};
30-
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
30+
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input, {
31+
fields: {five: {type: 'Number'}}
32+
});
3133
jequal(input, output);
3234
done();
3335
});
@@ -37,7 +39,7 @@ describe('parseObjectToMongoObject', () => {
3739
createdAt: "2015-10-06T21:24:50.332Z",
3840
updatedAt: "2015-10-06T21:24:50.332Z"
3941
};
40-
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
42+
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input);
4143
expect(output._created_at instanceof Date).toBe(true);
4244
expect(output._updated_at instanceof Date).toBe(true);
4345
done();
@@ -49,43 +51,53 @@ describe('parseObjectToMongoObject', () => {
4951
objectId: 'myId',
5052
className: 'Blah',
5153
};
52-
var out = transform.parseObjectToMongoObject(dummySchema, null, {pointers: [pointer]});
54+
var out = transform.parseObjectToMongoObjectForCreate(dummySchema, null, {pointers: [pointer]},{
55+
fields: {pointers: {type: 'Array'}}
56+
});
5357
jequal([pointer], out.pointers);
5458
done();
5559
});
5660

57-
it('a delete op', (done) => {
61+
//TODO: object creation requests shouldn't be seeing __op delete, it makes no sense to
62+
//have __op delete in a new object. Figure out what this should actually be testing.
63+
notWorking('a delete op', (done) => {
5864
var input = {deleteMe: {__op: 'Delete'}};
59-
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
65+
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input);
6066
jequal(output, {});
6167
done();
6268
});
6369

6470
it('basic ACL', (done) => {
6571
var input = {ACL: {'0123': {'read': true, 'write': true}}};
66-
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
72+
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input);
6773
// This just checks that it doesn't crash, but it should check format.
6874
done();
6975
});
7076

7177
describe('GeoPoints', () => {
7278
it('plain', (done) => {
7379
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
74-
var out = transform.parseObjectToMongoObject(dummySchema, null, {location: geoPoint});
80+
var out = transform.parseObjectToMongoObjectForCreate(dummySchema, null, {location: geoPoint},{
81+
fields: {location: {type: 'GeoPoint'}}
82+
});
7583
expect(out.location).toEqual([180, -180]);
7684
done();
7785
});
7886

7987
it('in array', (done) => {
8088
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
81-
var out = transform.parseObjectToMongoObject(dummySchema, null, {locations: [geoPoint, geoPoint]});
89+
var out = transform.parseObjectToMongoObjectForCreate(dummySchema, null, {locations: [geoPoint, geoPoint]},{
90+
fields: {locations: {type: 'Array'}}
91+
});
8292
expect(out.locations).toEqual([geoPoint, geoPoint]);
8393
done();
8494
});
8595

8696
it('in sub-object', (done) => {
8797
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
88-
var out = transform.parseObjectToMongoObject(dummySchema, null, { locations: { start: geoPoint }});
98+
var out = transform.parseObjectToMongoObjectForCreate(dummySchema, null, { locations: { start: geoPoint }},{
99+
fields: {locations: {type: 'Object'}}
100+
});
89101
expect(out).toEqual({ locations: { start: geoPoint } });
90102
done();
91103
});
@@ -196,7 +208,9 @@ describe('transform schema key changes', () => {
196208
var input = {
197209
somePointer: {__type: 'Pointer', className: 'Micro', objectId: 'oft'}
198210
};
199-
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
211+
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input, {
212+
fields: {somePointer: {type: 'Pointer'}}
213+
});
200214
expect(typeof output._p_somePointer).toEqual('string');
201215
expect(output._p_somePointer).toEqual('Micro$oft');
202216
done();
@@ -206,7 +220,9 @@ describe('transform schema key changes', () => {
206220
var input = {
207221
userPointer: {__type: 'Pointer', className: '_User', objectId: 'qwerty'}
208222
};
209-
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
223+
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input, {
224+
fields: {userPointer: {type: 'Pointer'}}
225+
});
210226
expect(typeof output._p_userPointer).toEqual('string');
211227
expect(output._p_userPointer).toEqual('_User$qwerty');
212228
done();
@@ -219,7 +235,7 @@ describe('transform schema key changes', () => {
219235
"Kevin": { "write": true }
220236
}
221237
};
222-
var output = transform.parseObjectToMongoObject(dummySchema, null, input);
238+
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input);
223239
expect(typeof output._rperm).toEqual('object');
224240
expect(typeof output._wperm).toEqual('object');
225241
expect(output.ACL).toBeUndefined();

spec/Parse.Push.spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
let request = require('request');
34

45
describe('Parse.Push', () => {
56

@@ -89,4 +90,57 @@ describe('Parse.Push', () => {
8990
done();
9091
});
9192
});
93+
94+
it('should not allow clients to query _PushStatus', done => {
95+
setup()
96+
.then(() => Parse.Push.send({
97+
where: {
98+
deviceType: 'ios'
99+
},
100+
data: {
101+
badge: 'increment',
102+
alert: 'Hello world!'
103+
}
104+
}, {useMasterKey: true}))
105+
.then(() => {
106+
request.get({
107+
url: 'http://localhost:8378/1/classes/_PushStatus',
108+
json: true,
109+
headers: {
110+
'X-Parse-Application-Id': 'test',
111+
},
112+
}, (error, response, body) => {
113+
expect(body.results.length).toEqual(0);
114+
done();
115+
});
116+
});
117+
});
118+
119+
it('should allow master key to query _PushStatus', done => {
120+
setup()
121+
.then(() => Parse.Push.send({
122+
where: {
123+
deviceType: 'ios'
124+
},
125+
data: {
126+
badge: 'increment',
127+
alert: 'Hello world!'
128+
}
129+
}, {useMasterKey: true}))
130+
.then(() => {
131+
request.get({
132+
url: 'http://localhost:8378/1/classes/_PushStatus',
133+
json: true,
134+
headers: {
135+
'X-Parse-Application-Id': 'test',
136+
'X-Parse-Master-Key': 'test',
137+
},
138+
}, (error, response, body) => {
139+
expect(body.results.length).toEqual(1);
140+
expect(body.results[0].query).toEqual('{"deviceType":"ios"}');
141+
expect(body.results[0].payload).toEqual('{"badge":"increment","alert":"Hello world!"}');
142+
done();
143+
});
144+
});
145+
});
92146
});

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,16 @@ export class MongoStorageAdapter {
145145
// this adapter doesn't know about the schema, return a promise that rejects with
146146
// undefined as the reason.
147147
getOneSchema(className) {
148-
return this.schemaCollection().then(schemasCollection => schemasCollection._fechOneSchemaFrom_SCHEMA(className));
148+
return this.schemaCollection()
149+
.then(schemasCollection => schemasCollection._fechOneSchemaFrom_SCHEMA(className));
149150
}
150151

151-
// TODO: As yet not particularly well specified. Creates an object. Does it really need the schema?
152-
// or can it fetch the schema itself? Also the schema is not currently a Parse format schema, and it
153-
// should be, if we are passing it at all.
154-
createObject(className, object, schema) {
155-
const mongoObject = transform.parseObjectToMongoObject(schema, className, object);
152+
// TODO: As yet not particularly well specified. Creates an object. Shouldn't need the
153+
// schemaController, but MongoTransform still needs it :( maybe shouldn't even need the schema,
154+
// and should infer from the type. Or maybe does need the schema for validations. Or maybe needs
155+
// the schem only for the legacy mongo format. We'll figure that out later.
156+
createObject(className, object, schemaController, parseFormatSchema) {
157+
const mongoObject = transform.parseObjectToMongoObjectForCreate(schemaController, className, object, parseFormatSchema);
156158
return this.adaptiveCollection(className)
157159
.then(collection => collection.insertOne(mongoObject));
158160
}

0 commit comments

Comments
 (0)