Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Idempotency #1210

Merged
merged 10 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions integration/cloud/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ Parse.Cloud.define('UpdateUser', function (request) {
return user.save(null, { useMasterKey: true });
});

Parse.Cloud.define('CloudFunctionIdempotency', function () {
const object = new Parse.Object('IdempotencyItem');
return object.save(null, { useMasterKey: true });
});

Parse.Cloud.define('CloudFunctionUndefined', function() {
return undefined;
});
Expand Down
9 changes: 7 additions & 2 deletions integration/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ const api = new ParseServer({
},
verbose: false,
silent: true,
idempotencyOptions: {
paths: ['functions/CloudFunctionIdempotency'],
ttl: 120
}
});

app.use('/parse', api);

const TestUtils = require('parse-server').TestUtils;

app.get('/clear', (req, res) => {
TestUtils.destroyAllDataPermanently().then(() => {
app.get('/clear/:fast', (req, res) => {
const { fast } = req.params;
TestUtils.destroyAllDataPermanently(fast).then(() => {
res.send('{}');
});
});
Expand Down
49 changes: 49 additions & 0 deletions integration/test/IdempotencyTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

const assert = require('assert');
const clear = require('./clear');
const Parse = require('../../node');

const Item = Parse.Object.extend('IdempotencyItem');

describe('Idempotency', () => {
beforeEach((done) => {
Parse.initialize('integration', null, 'notsosecret');
Parse.CoreManager.set('SERVER_URL', 'http://localhost:1337/parse');
Parse.Storage._clear();
clear().then(() => {
done();
});
});

it('duplicate cloud code function request', async () => {
const restController = Parse.CoreManager.getRESTController();
const XHR = restController._getXHR();
function DuplicateXHR() {
const xhr = new XHR();
const send = xhr.send;
xhr.send = function () {
this.setRequestHeader('X-Parse-Request-Id', '1234');
send.apply(this, arguments);
}
return xhr;
}
restController._setXHR(DuplicateXHR);
await Parse.Cloud.run('CloudFunctionIdempotency');
try {
await Parse.Cloud.run('CloudFunctionIdempotency');
} catch (e) {
assert.equal(e.code, Parse.Error.DUPLICATE_REQUEST);
}
try {
await Parse.Cloud.run('CloudFunctionIdempotency');
} catch (e) {
assert.equal(e.code, Parse.Error.DUPLICATE_REQUEST);
}
const query = new Parse.Query(Item);
const results = await query.find();
assert.equal(results.length, 1);

restController._setXHR(XHR);
});
});
4 changes: 2 additions & 2 deletions integration/test/ParseQueryTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1804,12 +1804,12 @@ describe('Parse Query', () => {
];
const objects = [];
for (const i in subjects) {
const obj = new TestObject({ comment: subjects[i] });
const obj = new TestObject({ subject: subjects[i] });
objects.push(obj);
}
return Parse.Object.saveAll(objects).then(() => {
const q = new Parse.Query(TestObject);
q.fullText('comment', 'coffee');
q.fullText('subject', 'coffee');
q.ascending('$score');
q.select('$score');
return q.find();
Expand Down
10 changes: 8 additions & 2 deletions integration/test/clear.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const Parse = require('../../node');

module.exports = function() {
return Parse._ajax('GET', 'http://localhost:1337/clear', '');
/**
* Destroys all data in the database
* Calls /clear route in integration/test/server.js
*
* @param {boolean} fast set to true if it's ok to just drop objects and not indexes.
*/
module.exports = function(fast = true) {
return Parse._ajax('GET', `http://localhost:1337/clear/${fast}`, '');
};
Loading