async function setup(options) { await reconfigureServer({ appId: Parse.applicationId, masterKey: Parse.masterKey, serverURL: Parse.serverURL, idempotencyOptions: options, }); }
_hasMasterKey(request, validKeyPairs) { if (!validKeyPairs || validKeyPairs.size == 0 || !validKeyPairs.has('masterKey')) { return false; } if (!request || !Object.prototype.hasOwnProperty.call(request, 'masterKey')) { return false; } return request.masterKey === validKeyPairs.get('masterKey'); }
it('should re-throw any other error unchanged when writing request entry fails for any other reason', async () => { spyOn(rest, 'create').and.rejectWith(new Parse.Error(0, "some other error")); Parse.Cloud.define('myFunction', () => {}); const params = { method: 'POST', url: 'http://localhost:8378/1/functions/myFunction', headers: { 'X-Parse-Application-Id': Parse.applicationId, 'X-Parse-Master-Key': Parse.masterKey, 'X-Parse-Request-Id': 'abc-123' } }; await request(params).then(fail, e => { expect(e.status).toEqual(400); expect(e.data.error).toEqual("some other error"); }); });
applyPlayground(app) { if (!app || !app.get) { (0, _requiredParameter.default)('You must provide an Express.js app instance!'); } app.get(this.config.playgroundPath || (0, _requiredParameter.default)('You must provide a config.playgroundPath to applyPlayground!'), (_req, res) => { res.setHeader('Content-Type', 'text/html'); res.write((0, _graphqlPlaygroundHtml.renderPlaygroundPage)({ endpoint: this.config.graphQLPath, version: '1.7.25', subscriptionEndpoint: this.config.subscriptionsPath, headers: { 'X-Parse-Application-Id': this.parseServer.config.appId, 'X-Parse-Master-Key': this.parseServer.config.masterKey } })); res.end(); }); }
it('test cloud function should echo keys', function(done) { Parse.Cloud.define('echoKeys', function() { return { applicationId: Parse.applicationId, masterKey: Parse.masterKey, javascriptKey: Parse.javascriptKey, }; }); Parse.Cloud.run('echoKeys').then(result => { expect(result.applicationId).toEqual(Parse.applicationId); expect(result.masterKey).toEqual(Parse.masterKey); expect(result.javascriptKey).toEqual(Parse.javascriptKey); done(); }); });
it('should not interfere with calls of different request ID', async () => { let counter = 0; Parse.Cloud.afterSave('MyClass', () => { counter++; }); const promises = [...Array(100).keys()].map(() => { const params = { method: 'POST', url: 'http://localhost:8378/1/classes/MyClass', headers: { 'X-Parse-Application-Id': Parse.applicationId, 'X-Parse-Master-Key': Parse.masterKey, 'X-Parse-Request-Id': uuid.v4() } }; return request(params); }); await expectAsync(Promise.all(promises)).toBeResolved(); expect(counter).toBe(100); });
assert(json.config.mysql.secret === '<String len: 10>'); assert(json.config.mysql.secretNumber === '<Number>'); assert(json.config.mysql.masterKey === '<String len: 17>'); assert(json.config.mysql.accessKey === '<String len: 17>'); assert(json.config.mysql.consumerSecret === '<String len: 22>');
it('should run with master key', done => { expect(() => { Parse.Cloud.job('myJob', (req, res) => { expect(req.functionName).toBeUndefined(); expect(req.jobName).toBe('myJob'); expect(typeof req.jobId).toBe('string'); expect(typeof req.message).toBe('function'); expect(typeof res).toBe('undefined'); done(); }); }).not.toThrow(); request({ method: 'POST', url: 'http://localhost:8378/1/jobs/myJob', headers: { 'X-Parse-Application-Id': Parse.applicationId, 'X-Parse-Master-Key': Parse.masterKey, }, }).then( () => {}, err => { fail(err); done(); } ); });
it('should fail trying to create a malformed function (REST)', done => { request({ method: 'POST', url: Parse.serverURL + '/hooks/functions', headers: { 'X-Parse-Application-Id': Parse.applicationId, 'X-Parse-Master-Key': Parse.masterKey, }, body: JSON.stringify({ functionName: 'SomeFunction' }), }).then(fail, response => { const body = response.data; expect(body.error).toBe('invalid hook declaration'); expect(body.code).toBe(143); done(); }); });
it('should define a job', done => { expect(() => { Parse.Cloud.job('myJob', () => {}); }).not.toThrow(); request({ method: 'POST', url: 'http://localhost:8378/1/jobs/myJob', headers: { 'X-Parse-Application-Id': Parse.applicationId, 'X-Parse-Master-Key': Parse.masterKey, }, }).then( () => { done(); }, err => { fail(err); done(); } ); });
// Tests it('should enforce idempotency for cloud code function', async () => { let counter = 0; Parse.Cloud.define('myFunction', () => { counter++; }); const params = { method: 'POST', url: 'http://localhost:8378/1/functions/myFunction', headers: { 'X-Parse-Application-Id': Parse.applicationId, 'X-Parse-Master-Key': Parse.masterKey, 'X-Parse-Request-Id': 'abc-123' } }; expect(Config.get(Parse.applicationId).idempotencyOptions.ttl).toBe(30); await request(params); await request(params).then(fail, e => { expect(e.status).toEqual(400); expect(e.data.error).toEqual("Duplicate request"); }); expect(counter).toBe(1); });
it('should enforce idempotency for cloud code jobs', async () => { let counter = 0; Parse.Cloud.job('myJob', () => { counter++; }); const params = { method: 'POST', url: 'http://localhost:8378/1/jobs/myJob', headers: { 'X-Parse-Application-Id': Parse.applicationId, 'X-Parse-Master-Key': Parse.masterKey, 'X-Parse-Request-Id': 'abc-123' } }; await expectAsync(request(params)).toBeResolved(); await request(params).then(fail, e => { expect(e.status).toEqual(400); expect(e.data.error).toEqual("Duplicate request"); }); expect(counter).toBe(1); });
it('should enforce idempotency for class object creation', async () => { let counter = 0; Parse.Cloud.afterSave('MyClass', () => { counter++; }); const params = { method: 'POST', url: 'http://localhost:8378/1/classes/MyClass', headers: { 'X-Parse-Application-Id': Parse.applicationId, 'X-Parse-Master-Key': Parse.masterKey, 'X-Parse-Request-Id': 'abc-123' } }; await expectAsync(request(params)).toBeResolved(); await request(params).then(fail, e => { expect(e.status).toEqual(400); expect(e.data.error).toEqual("Duplicate request"); }); expect(counter).toBe(1); });
it('should enforce idempotency for user object creation', async () => { let counter = 0; Parse.Cloud.afterSave('_User', () => { counter++; }); const params = { method: 'POST', url: 'http://localhost:8378/1/users', body: { username: "user", password: "pass" }, headers: { 'X-Parse-Application-Id': Parse.applicationId, 'X-Parse-Master-Key': Parse.masterKey, 'X-Parse-Request-Id': 'abc-123' } }; await expectAsync(request(params)).toBeResolved(); await request(params).then(fail, e => { expect(e.status).toEqual(400); expect(e.data.error).toEqual("Duplicate request"); }); expect(counter).toBe(1); });
it('should run with master key basic auth', done => { expect(() => { Parse.Cloud.job('myJob', (req, res) => { expect(req.functionName).toBeUndefined(); expect(req.jobName).toBe('myJob'); expect(typeof req.jobId).toBe('string'); expect(typeof req.message).toBe('function'); expect(typeof res).toBe('undefined'); done(); }); }).not.toThrow(); request({ method: 'POST', url: `http://${Parse.applicationId}:${Parse.masterKey}@localhost:8378/1/jobs/myJob`, }).then( () => {}, err => { fail(err); done(); } ); });