Skip to content

Commit 589ff21

Browse files
committed
Fix some stuff
1 parent 4ac6821 commit 589ff21

File tree

9 files changed

+86
-62
lines changed

9 files changed

+86
-62
lines changed

spec/MongoStorageAdapter.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('MongoStorageAdapter', () => {
4949

5050
it('stores objectId in _id', done => {
5151
let adapter = new MongoStorageAdapter({ uri: databaseURI });
52-
adapter.createObject('Foo', {}, { objectId: 'abcde' })
52+
adapter.createObject('Foo', { fields: {} }, { objectId: 'abcde' })
5353
.then(() => adapter._rawFind('Foo', {}))
5454
.then(results => {
5555
expect(results.length).toEqual(1);

spec/ParseUser.spec.js

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
var request = require('request');
1111
var passwordCrypto = require('../src/password');
1212
var Config = require('../src/Config');
13+
const rp = require('request-promise');
1314

1415
function verifyACL(user) {
1516
const ACL = user.getACL();
@@ -2131,7 +2132,7 @@ describe('Parse.User testing', () => {
21312132
let database = new Config(Parse.applicationId).database;
21322133
database.create('_User', {
21332134
username: 'user',
2134-
password: '$2a$10$8/wZJyEuiEaobBBqzTG.jeY.XSFJd0rzaN//ososvEI4yLqI.4aie',
2135+
_hashed_password: '$2a$10$8/wZJyEuiEaobBBqzTG.jeY.XSFJd0rzaN//ososvEI4yLqI.4aie',
21352136
_auth_data_facebook: null
21362137
}, {}).then(() => {
21372138
return new Promise((resolve, reject) => {
@@ -2258,42 +2259,43 @@ describe('Parse.User testing', () => {
22582259
});
22592260

22602261
it('should fail to become user with expired token', (done) => {
2261-
Parse.User.signUp("auser", "somepass", null, {
2262-
success: function(user) {
2263-
request.get({
2264-
url: 'http://localhost:8378/1/classes/_Session',
2265-
json: true,
2266-
headers: {
2267-
'X-Parse-Application-Id': 'test',
2268-
'X-Parse-Master-Key': 'test',
2269-
},
2270-
}, (error, response, body) => {
2271-
var id = body.results[0].objectId;
2272-
var expiresAt = new Date((new Date()).setYear(2015));
2273-
var token = body.results[0].sessionToken;
2274-
request.put({
2275-
url: "http://localhost:8378/1/classes/_Session/" + id,
2276-
json: true,
2277-
headers: {
2278-
'X-Parse-Application-Id': 'test',
2279-
'X-Parse-Master-Key': 'test',
2280-
},
2281-
body: {
2282-
expiresAt: { __type: "Date", iso: expiresAt.toISOString() },
2283-
},
2284-
}, (error, response, body) => {
2285-
Parse.User.become(token)
2286-
.then(() => { fail("Should not have succeded"); })
2287-
.fail((err) => {
2288-
expect(err.code).toEqual(209);
2289-
expect(err.message).toEqual("Session token is expired.");
2290-
Parse.User.logOut() // Logout to prevent polluting CLI with messages
2291-
.then(done);
2292-
});
2293-
});
2294-
});
2295-
}
2296-
});
2262+
let token;
2263+
Parse.User.signUp("auser", "somepass", null)
2264+
.then(user => rp({
2265+
method: 'GET',
2266+
url: 'http://localhost:8378/1/classes/_Session',
2267+
json: true,
2268+
headers: {
2269+
'X-Parse-Application-Id': 'test',
2270+
'X-Parse-Master-Key': 'test',
2271+
},
2272+
}))
2273+
.then(body => {
2274+
var id = body.results[0].objectId;
2275+
var expiresAt = new Date((new Date()).setYear(2015));
2276+
token = body.results[0].sessionToken;
2277+
return rp({
2278+
method: 'PUT',
2279+
url: "http://localhost:8378/1/classes/_Session/" + id,
2280+
json: true,
2281+
headers: {
2282+
'X-Parse-Application-Id': 'test',
2283+
'X-Parse-Master-Key': 'test',
2284+
},
2285+
body: {
2286+
expiresAt: { __type: "Date", iso: expiresAt.toISOString() },
2287+
},
2288+
})
2289+
})
2290+
.then(() => Parse.User.become(token))
2291+
.then(() => {
2292+
fail("Should not have succeded")
2293+
done();
2294+
}, error => {
2295+
expect(error.code).toEqual(209);
2296+
expect(error.message).toEqual("Session token is expired.");
2297+
done();
2298+
})
22972299
});
22982300

22992301
it('should not create extraneous session tokens', (done) => {

spec/PointerPermissions.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ describe('Pointer Permissions', () => {
3636
expect(res.length).toBe(1);
3737
expect(res[0].id).toBe(obj.id);
3838
done();
39-
}).catch((err) => {
40-
fail('Should not fail');
39+
}).catch(error => {
40+
fail(JSON.stringify(error));
4141
done();
4242
});
4343
});

spec/Schema.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ describe('SchemaController', () => {
238238
});
239239
Promise.all([p1,p2])
240240
.catch(error => {
241+
console.log(error);
241242
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
242243
expect(error.message).toEqual('Class NewClass already exists.');
243244
done();
@@ -693,7 +694,7 @@ describe('SchemaController', () => {
693694
objectId: { type: 'String' },
694695
updatedAt: { type: 'Date' },
695696
createdAt: { type: 'Date' },
696-
ACL: { type: 'ACL' }
697+
ACL: { type: 'ACL' },
697698
};
698699
expect(dd(schema.data.NewClass, expectedSchema)).toEqual(undefined);
699700
done();

spec/Uniqueness.spec.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,4 @@ describe('Uniqueness', function() {
100100
done();
101101
});
102102
});
103-
104-
it('adding a unique index to an existing field works even if it has nulls', done => {
105-
106-
});
107-
108-
it('adding a unique index to an existing field doesnt prevent you from adding new documents with nulls', done => {
109-
110-
});
111103
});

spec/helper.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ beforeEach(done => {
140140
});
141141

142142
afterEach(function(done) {
143+
let afterLogOut = () => {
144+
if (Object.keys(openConnections).length > 0) {
145+
fail('There were open connections to the server left after the test finished');
146+
}
147+
done();
148+
};
143149
Parse.Cloud._removeAllHooks();
144150
databaseAdapter.getAllClasses()
145151
.then(allSchemas => {
@@ -157,16 +163,7 @@ afterEach(function(done) {
157163
});
158164
})
159165
.then(() => Parse.User.logOut())
160-
.then(() => {
161-
if (Object.keys(openConnections).length > 0) {
162-
fail('There were open connections to the server left after the test finished');
163-
}
164-
done();
165-
})
166-
.catch(error => {
167-
fail(JSON.stringify(error));
168-
done();
169-
});
166+
.then(afterLogOut, afterLogOut)
170167
});
171168

172169
var TestObject = Parse.Object.extend({

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ const storageAdapterAllCollections = mongoAdapter => {
3434
});
3535
}
3636

37+
const convertParseSchemaToMongoSchema = ({...schema}) => {
38+
delete schema.fields._rperm;
39+
delete schema.fields._wperm;
40+
return schema;
41+
}
42+
3743
export class MongoStorageAdapter {
3844
// Private
3945
_uri: string;
@@ -97,6 +103,7 @@ export class MongoStorageAdapter {
97103
}
98104

99105
createClass(className, schema) {
106+
schema = convertParseSchemaToMongoSchema(schema);
100107
return this._schemaCollection()
101108
.then(schemaCollection => schemaCollection.addSchema(className, schema.fields, schema.classLevelPermissions));
102109
}
@@ -192,6 +199,7 @@ export class MongoStorageAdapter {
192199
// and should infer from the type. Or maybe does need the schema for validations. Or maybe needs
193200
// the schem only for the legacy mongo format. We'll figure that out later.
194201
createObject(className, schema, object) {
202+
schema = convertParseSchemaToMongoSchema(schema);
195203
const mongoObject = parseObjectToMongoObjectForCreate(className, object, schema);
196204
return this._adaptiveCollection(className)
197205
.then(collection => collection.insertOne(mongoObject))
@@ -208,6 +216,7 @@ export class MongoStorageAdapter {
208216
// If no objects match, reject with OBJECT_NOT_FOUND. If objects are found and deleted, resolve with undefined.
209217
// If there is some other error, reject with INTERNAL_SERVER_ERROR.
210218
deleteObjectsByQuery(className, schema, query) {
219+
schema = convertParseSchemaToMongoSchema(schema);
211220
return this._adaptiveCollection(className)
212221
.then(collection => {
213222
let mongoWhere = transformWhere(className, query, schema);
@@ -225,6 +234,7 @@ export class MongoStorageAdapter {
225234

226235
// Apply the update to all objects that match the given Parse Query.
227236
updateObjectsByQuery(className, schema, query, update) {
237+
schema = convertParseSchemaToMongoSchema(schema);
228238
const mongoUpdate = transformUpdate(className, update, schema);
229239
const mongoWhere = transformWhere(className, query, schema);
230240
return this._adaptiveCollection(className)
@@ -234,6 +244,7 @@ export class MongoStorageAdapter {
234244
// Atomically finds and updates an object based on query.
235245
// Return value not currently well specified.
236246
findOneAndUpdate(className, schema, query, update) {
247+
schema = convertParseSchemaToMongoSchema(schema);
237248
const mongoUpdate = transformUpdate(className, update, schema);
238249
const mongoWhere = transformWhere(className, query, schema);
239250
return this._adaptiveCollection(className)
@@ -243,6 +254,7 @@ export class MongoStorageAdapter {
243254

244255
// Hopefully we can get rid of this. It's only used for config and hooks.
245256
upsertOneObject(className, schema, query, update) {
257+
schema = convertParseSchemaToMongoSchema(schema);
246258
const mongoUpdate = transformUpdate(className, update, schema);
247259
const mongoWhere = transformWhere(className, query, schema);
248260
return this._adaptiveCollection(className)
@@ -251,6 +263,7 @@ export class MongoStorageAdapter {
251263

252264
// Executes a find. Accepts: className, query in Parse format, and { skip, limit, sort }.
253265
find(className, schema, query, { skip, limit, sort }) {
266+
schema = convertParseSchemaToMongoSchema(schema);
254267
let mongoWhere = transformWhere(className, query, schema);
255268
let mongoSort = _.mapKeys(sort, (value, fieldName) => transformKey(className, fieldName, schema));
256269
return this._adaptiveCollection(className)
@@ -264,6 +277,7 @@ export class MongoStorageAdapter {
264277
// Way of determining if a field is nullable. Undefined doesn't count against uniqueness,
265278
// which is why we use sparse indexes.
266279
ensureUniqueness(className, schema, fieldNames) {
280+
schema = convertParseSchemaToMongoSchema(schema);
267281
let indexCreationRequest = {};
268282
let mongoFieldNames = fieldNames.map(fieldName => transformKey(className, fieldName, schema));
269283
mongoFieldNames.forEach(fieldName => {
@@ -287,6 +301,7 @@ export class MongoStorageAdapter {
287301

288302
// Executs a count.
289303
count(className, schema, query) {
304+
schema = convertParseSchemaToMongoSchema(schema);
290305
return this._adaptiveCollection(className)
291306
.then(collection => collection.count(transformWhere(className, query, schema)));
292307
}

src/Controllers/DatabaseController.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ const validateQuery = query => {
6767
}
6868

6969
Object.keys(query).forEach(key => {
70-
if (query[key].$regex) {
71-
if (typeof query[key].$options === 'string') {g
70+
if (query && query[key] && query[key].$regex) {
71+
if (typeof query[key].$options === 'string') {
7272
if (!query[key].$options.match(/^[imxs]+$/)) {
7373
throw new Parse.Error(Parse.Error.INVALID_QUERY, `Bad $options value for query: ${query[key].$options}`);
7474
}
@@ -754,7 +754,7 @@ DatabaseController.prototype.deleteSchema = function(className) {
754754
})
755755
.then(schema => {
756756
return this.collectionExists(className)
757-
.then(exist => this.adapter.count(className))
757+
.then(exist => this.adapter.count(className, { fields: {} }))
758758
.then(count => {
759759
if (count > 0) {
760760
throw new Parse.Error(255, `Class ${className} is not empty, contains ${count} objects, cannot drop schema.`);

src/Controllers/SchemaController.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ const convertSchemaToAdapterSchema = schema => {
234234
return schema;
235235
}
236236

237+
const convertAdapterSchemaToParseSchema = ({...schema}) => {
238+
delete schema.fields._rperm;
239+
delete schema.fields._wperm;
240+
241+
schema.fields.ACL = { type: 'ACL' };
242+
243+
if (schema.className === '_User') {
244+
delete schema.fields._hashed_password;
245+
schema.fields.password = { type: 'String' };
246+
}
247+
248+
return schema;
249+
}
250+
237251
const injectDefaultSchema = schema => ({
238252
className: schema.className,
239253
fields: {
@@ -316,6 +330,7 @@ class SchemaController {
316330
}
317331

318332
return this._dbAdapter.createClass(className, convertSchemaToAdapterSchema({ fields, classLevelPermissions, className }))
333+
.then(convertAdapterSchemaToParseSchema)
319334
.catch(error => {
320335
if (error && error.code === Parse.Error.DUPLICATE_VALUE) {
321336
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
@@ -342,6 +357,8 @@ class SchemaController {
342357
}
343358
});
344359

360+
delete existingFields._rperm;
361+
delete existingFields._wperm;
345362
let newSchema = buildMergedSchemaObject(existingFields, submittedFields);
346363
let validationError = this.validateSchemaData(className, newSchema, classLevelPermissions);
347364
if (validationError) {

0 commit comments

Comments
 (0)