Merge pull request #228 from drew-gross/master
Implement GET /schemas/:className
This commit is contained in:
20
schemas.js
20
schemas.js
@@ -64,6 +64,26 @@ function getAllSchemas(req) {
|
|||||||
}}));
|
}}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getOneSchema(req) {
|
||||||
|
if (!req.auth.isMaster) {
|
||||||
|
return Promise.resolve({
|
||||||
|
status: 401,
|
||||||
|
response: {error: 'unauthorized'},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return req.config.database.collection('_SCHEMA')
|
||||||
|
.then(coll => coll.findOne({'_id': req.params.className}))
|
||||||
|
.then(schema => ({response: mongoSchemaToSchemaAPIResponse(schema)}))
|
||||||
|
.catch(() => ({
|
||||||
|
status: 400,
|
||||||
|
response: {
|
||||||
|
code: 103,
|
||||||
|
error: 'class ' + req.params.className + ' does not exist',
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
router.route('GET', '/schemas', getAllSchemas);
|
router.route('GET', '/schemas', getAllSchemas);
|
||||||
|
router.route('GET', '/schemas/:className', getOneSchema);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -1,5 +1,60 @@
|
|||||||
var request = require('request');
|
var request = require('request');
|
||||||
var dd = require('deep-diff');
|
var dd = require('deep-diff');
|
||||||
|
var hasAllPODobject = () => {
|
||||||
|
var obj = new Parse.Object('HasAllPOD');
|
||||||
|
obj.set('aNumber', 5);
|
||||||
|
obj.set('aString', 'string');
|
||||||
|
obj.set('aBool', true);
|
||||||
|
obj.set('aDate', new Date());
|
||||||
|
obj.set('aObject', {k1: 'value', k2: true, k3: 5});
|
||||||
|
obj.set('aArray', ['contents', true, 5]);
|
||||||
|
obj.set('aGeoPoint', new Parse.GeoPoint({latitude: 0, longitude: 0}));
|
||||||
|
obj.set('aFile', new Parse.File('f.txt', { base64: 'V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE=' }));
|
||||||
|
var objACL = new Parse.ACL();
|
||||||
|
objACL.setPublicWriteAccess(false);
|
||||||
|
obj.setACL(objACL);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
var expectedResponseForHasAllPOD = {
|
||||||
|
className: 'HasAllPOD',
|
||||||
|
fields: {
|
||||||
|
//Default fields
|
||||||
|
ACL: {type: 'ACL'},
|
||||||
|
createdAt: {type: 'Date'},
|
||||||
|
updatedAt: {type: 'Date'},
|
||||||
|
objectId: {type: 'String'},
|
||||||
|
//Custom fields
|
||||||
|
aNumber: {type: 'Number'},
|
||||||
|
aString: {type: 'String'},
|
||||||
|
aBool: {type: 'Boolean'},
|
||||||
|
aDate: {type: 'Date'},
|
||||||
|
aObject: {type: 'Object'},
|
||||||
|
aArray: {type: 'Array'},
|
||||||
|
aGeoPoint: {type: 'GeoPoint'},
|
||||||
|
aFile: {type: 'File'}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
var expectedResponseforHasPointersAndRelations = {
|
||||||
|
className: 'HasPointersAndRelations',
|
||||||
|
fields: {
|
||||||
|
//Default fields
|
||||||
|
ACL: {type: 'ACL'},
|
||||||
|
createdAt: {type: 'Date'},
|
||||||
|
updatedAt: {type: 'Date'},
|
||||||
|
objectId: {type: 'String'},
|
||||||
|
//Custom fields
|
||||||
|
aPointer: {
|
||||||
|
type: 'Pointer',
|
||||||
|
targetClass: 'HasAllPOD',
|
||||||
|
},
|
||||||
|
aRelation: {
|
||||||
|
type: 'Relation',
|
||||||
|
targetClass: 'HasAllPOD',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
describe('schemas', () => {
|
describe('schemas', () => {
|
||||||
it('requires the master key to get all schemas', (done) => {
|
it('requires the master key to get all schemas', (done) => {
|
||||||
@@ -17,6 +72,21 @@ describe('schemas', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('requires the master key to get one schema', (done) => {
|
||||||
|
request.get({
|
||||||
|
url: 'http://localhost:8378/1/schemas/SomeSchema',
|
||||||
|
json: true,
|
||||||
|
headers: {
|
||||||
|
'X-Parse-Application-Id': 'test',
|
||||||
|
'X-Parse-REST-API-Key': 'rest',
|
||||||
|
},
|
||||||
|
}, (error, response, body) => {
|
||||||
|
expect(response.statusCode).toEqual(401);
|
||||||
|
expect(body.error).toEqual('unauthorized');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('responds with empty list when there are no schemas', done => {
|
it('responds with empty list when there are no schemas', done => {
|
||||||
request.get({
|
request.get({
|
||||||
url: 'http://localhost:8378/1/schemas',
|
url: 'http://localhost:8378/1/schemas',
|
||||||
@@ -32,79 +102,66 @@ describe('schemas', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('responds with a list of schemas after creating objects', done => {
|
it('responds with a list of schemas after creating objects', done => {
|
||||||
var obj1 = new Parse.Object('HasAllPOD');
|
var obj1 = hasAllPODobject();
|
||||||
obj1.set('aNumber', 5);
|
obj1.save().then(savedObj1 => {
|
||||||
obj1.set('aString', 'string');
|
var obj2 = new Parse.Object('HasPointersAndRelations');
|
||||||
obj1.set('aBool', true);
|
obj2.set('aPointer', savedObj1);
|
||||||
obj1.set('aDate', new Date());
|
var relation = obj2.relation('aRelation');
|
||||||
obj1.set('aObject', {k1: 'value', k2: true, k3: 5});
|
relation.add(obj1);
|
||||||
obj1.set('aArray', ['contents', true, 5]);
|
return obj2.save();
|
||||||
obj1.set('aGeoPoint', new Parse.GeoPoint({latitude: 0, longitude: 0}));
|
}).then(() => {
|
||||||
obj1.set('aFile', new Parse.File('f.txt', { base64: 'V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE=' }));
|
request.get({
|
||||||
var obj1ACL = new Parse.ACL();
|
url: 'http://localhost:8378/1/schemas',
|
||||||
obj1ACL.setPublicWriteAccess(false);
|
json: true,
|
||||||
obj1.setACL(obj1ACL);
|
headers: {
|
||||||
|
'X-Parse-Application-Id': 'test',
|
||||||
|
'X-Parse-Master-Key': 'test',
|
||||||
|
},
|
||||||
|
}, (error, response, body) => {
|
||||||
|
var expected = {
|
||||||
|
results: [expectedResponseForHasAllPOD,expectedResponseforHasPointersAndRelations]
|
||||||
|
};
|
||||||
|
expect(body).toEqual(expected);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
obj1.save().then(savedObj1 => {
|
it('responds with a single schema', done => {
|
||||||
var obj2 = new Parse.Object('HasPointersAndRelations');
|
var obj = hasAllPODobject();
|
||||||
obj2.set('aPointer', savedObj1);
|
obj.save().then(() => {
|
||||||
var relation = obj2.relation('aRelation');
|
request.get({
|
||||||
relation.add(obj1);
|
url: 'http://localhost:8378/1/schemas/HasAllPOD',
|
||||||
return obj2.save();
|
json: true,
|
||||||
}).then(() => {
|
headers: {
|
||||||
request.get({
|
'X-Parse-Application-Id': 'test',
|
||||||
url: 'http://localhost:8378/1/schemas',
|
'X-Parse-Master-Key': 'test',
|
||||||
json: true,
|
},
|
||||||
headers: {
|
}, (error, response, body) => {
|
||||||
'X-Parse-Application-Id': 'test',
|
expect(body).toEqual(expectedResponseForHasAllPOD);
|
||||||
'X-Parse-Master-Key': 'test',
|
done();
|
||||||
},
|
|
||||||
}, (error, response, body) => {
|
|
||||||
var expected = {
|
|
||||||
results: [
|
|
||||||
{
|
|
||||||
className: 'HasAllPOD',
|
|
||||||
fields: {
|
|
||||||
//Default fields
|
|
||||||
ACL: {type: 'ACL'},
|
|
||||||
createdAt: {type: 'Date'},
|
|
||||||
updatedAt: {type: 'Date'},
|
|
||||||
objectId: {type: 'String'},
|
|
||||||
//Custom fields
|
|
||||||
aNumber: {type: 'Number'},
|
|
||||||
aString: {type: 'String'},
|
|
||||||
aBool: {type: 'Boolean'},
|
|
||||||
aDate: {type: 'Date'},
|
|
||||||
aObject: {type: 'Object'},
|
|
||||||
aArray: {type: 'Array'},
|
|
||||||
aGeoPoint: {type: 'GeoPoint'},
|
|
||||||
aFile: {type: 'File'}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
className: 'HasPointersAndRelations',
|
|
||||||
fields: {
|
|
||||||
//Default fields
|
|
||||||
ACL: {type: 'ACL'},
|
|
||||||
createdAt: {type: 'Date'},
|
|
||||||
updatedAt: {type: 'Date'},
|
|
||||||
objectId: {type: 'String'},
|
|
||||||
//Custom fields
|
|
||||||
aPointer: {
|
|
||||||
type: 'Pointer',
|
|
||||||
targetClass: 'HasAllPOD',
|
|
||||||
},
|
|
||||||
aRelation: {
|
|
||||||
type: 'Relation',
|
|
||||||
targetClass: 'HasAllPOD',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
expect(body).toEqual(expected);
|
|
||||||
done();
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('treats class names case sensitively', done => {
|
||||||
|
var obj = hasAllPODobject();
|
||||||
|
obj.save().then(() => {
|
||||||
|
request.get({
|
||||||
|
url: 'http://localhost:8378/1/schemas/HASALLPOD',
|
||||||
|
json: true,
|
||||||
|
headers: {
|
||||||
|
'X-Parse-Application-Id': 'test',
|
||||||
|
'X-Parse-Master-Key': 'test',
|
||||||
|
},
|
||||||
|
}, (error, response, body) => {
|
||||||
|
expect(response.statusCode).toEqual(400);
|
||||||
|
expect(body).toEqual({
|
||||||
|
code: 103,
|
||||||
|
error: 'class HASALLPOD does not exist',
|
||||||
|
});
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user