|
| 1 | +'use strict'; |
| 2 | +const Config = require('../lib/Config'); |
| 3 | +const Definitions = require('../lib/Options/Definitions'); |
| 4 | +const request = require('../lib/request'); |
| 5 | +const rest = require('../lib/rest'); |
| 6 | +const auth = require('../lib/Auth'); |
| 7 | +const uuid = require('uuid'); |
| 8 | + |
| 9 | +describe_only_db('mongo')('Idempotency', () => { |
| 10 | + // Parameters |
| 11 | + /** Enable TTL expiration simulated by removing entry instead of waiting for MongoDB TTL monitor which |
| 12 | + runs only every 60s, so it can take up to 119s until entry removal - ain't nobody got time for that */ |
| 13 | + const SIMULATE_TTL = true; |
| 14 | + // Helpers |
| 15 | + async function deleteRequestEntry(reqId) { |
| 16 | + const config = Config.get(Parse.applicationId); |
| 17 | + const res = await rest.find( |
| 18 | + config, |
| 19 | + auth.master(config), |
| 20 | + '_Idempotency', |
| 21 | + { reqId: reqId }, |
| 22 | + { limit: 1 } |
| 23 | + ); |
| 24 | + await rest.del( |
| 25 | + config, |
| 26 | + auth.master(config), |
| 27 | + '_Idempotency', |
| 28 | + res.results[0].objectId); |
| 29 | + } |
| 30 | + async function setup(options) { |
| 31 | + await reconfigureServer({ |
| 32 | + appId: Parse.applicationId, |
| 33 | + masterKey: Parse.masterKey, |
| 34 | + serverURL: Parse.serverURL, |
| 35 | + idempotencyOptions: options, |
| 36 | + }); |
| 37 | + } |
| 38 | + // Setups |
| 39 | + beforeEach(async () => { |
| 40 | + if (SIMULATE_TTL) { jasmine.DEFAULT_TIMEOUT_INTERVAL = 200000; } |
| 41 | + await setup({ |
| 42 | + paths: [ |
| 43 | + "functions/.*", |
| 44 | + "jobs/.*", |
| 45 | + "classes/.*", |
| 46 | + "users", |
| 47 | + "installations" |
| 48 | + ], |
| 49 | + ttl: 30, |
| 50 | + }); |
| 51 | + }); |
| 52 | + // Tests |
| 53 | + it('should enforce idempotency for cloud code function', async () => { |
| 54 | + let counter = 0; |
| 55 | + Parse.Cloud.define('myFunction', () => { |
| 56 | + counter++; |
| 57 | + }); |
| 58 | + const params = { |
| 59 | + method: 'POST', |
| 60 | + url: 'http://localhost:8378/1/functions/myFunction', |
| 61 | + headers: { |
| 62 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 63 | + 'X-Parse-Master-Key': Parse.masterKey, |
| 64 | + 'X-Parse-Request-Id': 'abc-123' |
| 65 | + } |
| 66 | + }; |
| 67 | + expect(Config.get(Parse.applicationId).idempotencyOptions.ttl).toBe(30); |
| 68 | + await request(params); |
| 69 | + await request(params).then(fail, e => { |
| 70 | + expect(e.status).toEqual(400); |
| 71 | + expect(e.data.error).toEqual("Duplicate request"); |
| 72 | + }); |
| 73 | + expect(counter).toBe(1); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should delete request entry after TTL', async () => { |
| 77 | + let counter = 0; |
| 78 | + Parse.Cloud.define('myFunction', () => { |
| 79 | + counter++; |
| 80 | + }); |
| 81 | + const params = { |
| 82 | + method: 'POST', |
| 83 | + url: 'http://localhost:8378/1/functions/myFunction', |
| 84 | + headers: { |
| 85 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 86 | + 'X-Parse-Master-Key': Parse.masterKey, |
| 87 | + 'X-Parse-Request-Id': 'abc-123' |
| 88 | + } |
| 89 | + }; |
| 90 | + await expectAsync(request(params)).toBeResolved(); |
| 91 | + if (SIMULATE_TTL) { |
| 92 | + await deleteRequestEntry('abc-123'); |
| 93 | + } else { |
| 94 | + await new Promise(resolve => setTimeout(resolve, 130000)); |
| 95 | + } |
| 96 | + await expectAsync(request(params)).toBeResolved(); |
| 97 | + expect(counter).toBe(2); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should enforce idempotency for cloud code jobs', async () => { |
| 101 | + let counter = 0; |
| 102 | + Parse.Cloud.job('myJob', () => { |
| 103 | + counter++; |
| 104 | + }); |
| 105 | + const params = { |
| 106 | + method: 'POST', |
| 107 | + url: 'http://localhost:8378/1/jobs/myJob', |
| 108 | + headers: { |
| 109 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 110 | + 'X-Parse-Master-Key': Parse.masterKey, |
| 111 | + 'X-Parse-Request-Id': 'abc-123' |
| 112 | + } |
| 113 | + }; |
| 114 | + await expectAsync(request(params)).toBeResolved(); |
| 115 | + await request(params).then(fail, e => { |
| 116 | + expect(e.status).toEqual(400); |
| 117 | + expect(e.data.error).toEqual("Duplicate request"); |
| 118 | + }); |
| 119 | + expect(counter).toBe(1); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should enforce idempotency for class object creation', async () => { |
| 123 | + let counter = 0; |
| 124 | + Parse.Cloud.afterSave('MyClass', () => { |
| 125 | + counter++; |
| 126 | + }); |
| 127 | + const params = { |
| 128 | + method: 'POST', |
| 129 | + url: 'http://localhost:8378/1/classes/MyClass', |
| 130 | + headers: { |
| 131 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 132 | + 'X-Parse-Master-Key': Parse.masterKey, |
| 133 | + 'X-Parse-Request-Id': 'abc-123' |
| 134 | + } |
| 135 | + }; |
| 136 | + await expectAsync(request(params)).toBeResolved(); |
| 137 | + await request(params).then(fail, e => { |
| 138 | + expect(e.status).toEqual(400); |
| 139 | + expect(e.data.error).toEqual("Duplicate request"); |
| 140 | + }); |
| 141 | + expect(counter).toBe(1); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should enforce idempotency for user object creation', async () => { |
| 145 | + let counter = 0; |
| 146 | + Parse.Cloud.afterSave('_User', () => { |
| 147 | + counter++; |
| 148 | + }); |
| 149 | + const params = { |
| 150 | + method: 'POST', |
| 151 | + url: 'http://localhost:8378/1/users', |
| 152 | + body: { |
| 153 | + username: "user", |
| 154 | + password: "pass" |
| 155 | + }, |
| 156 | + headers: { |
| 157 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 158 | + 'X-Parse-Master-Key': Parse.masterKey, |
| 159 | + 'X-Parse-Request-Id': 'abc-123' |
| 160 | + } |
| 161 | + }; |
| 162 | + await expectAsync(request(params)).toBeResolved(); |
| 163 | + await request(params).then(fail, e => { |
| 164 | + expect(e.status).toEqual(400); |
| 165 | + expect(e.data.error).toEqual("Duplicate request"); |
| 166 | + }); |
| 167 | + expect(counter).toBe(1); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should enforce idempotency for installation object creation', async () => { |
| 171 | + let counter = 0; |
| 172 | + Parse.Cloud.afterSave('_Installation', () => { |
| 173 | + counter++; |
| 174 | + }); |
| 175 | + const params = { |
| 176 | + method: 'POST', |
| 177 | + url: 'http://localhost:8378/1/installations', |
| 178 | + body: { |
| 179 | + installationId: "1", |
| 180 | + deviceType: "ios" |
| 181 | + }, |
| 182 | + headers: { |
| 183 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 184 | + 'X-Parse-Master-Key': Parse.masterKey, |
| 185 | + 'X-Parse-Request-Id': 'abc-123' |
| 186 | + } |
| 187 | + }; |
| 188 | + await expectAsync(request(params)).toBeResolved(); |
| 189 | + await request(params).then(fail, e => { |
| 190 | + expect(e.status).toEqual(400); |
| 191 | + expect(e.data.error).toEqual("Duplicate request"); |
| 192 | + }); |
| 193 | + expect(counter).toBe(1); |
| 194 | + }); |
| 195 | + |
| 196 | + it('should not interfere with calls of different request ID', async () => { |
| 197 | + let counter = 0; |
| 198 | + Parse.Cloud.afterSave('MyClass', () => { |
| 199 | + counter++; |
| 200 | + }); |
| 201 | + const promises = [...Array(100).keys()].map(() => { |
| 202 | + const params = { |
| 203 | + method: 'POST', |
| 204 | + url: 'http://localhost:8378/1/classes/MyClass', |
| 205 | + headers: { |
| 206 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 207 | + 'X-Parse-Master-Key': Parse.masterKey, |
| 208 | + 'X-Parse-Request-Id': uuid.v4() |
| 209 | + } |
| 210 | + }; |
| 211 | + return request(params); |
| 212 | + }); |
| 213 | + await expectAsync(Promise.all(promises)).toBeResolved(); |
| 214 | + expect(counter).toBe(100); |
| 215 | + }); |
| 216 | + |
| 217 | + it('should re-throw any other error unchanged when writing request entry fails for any other reason', async () => { |
| 218 | + spyOn(rest, 'create').and.rejectWith(new Parse.Error(0, "some other error")); |
| 219 | + Parse.Cloud.define('myFunction', () => {}); |
| 220 | + const params = { |
| 221 | + method: 'POST', |
| 222 | + url: 'http://localhost:8378/1/functions/myFunction', |
| 223 | + headers: { |
| 224 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 225 | + 'X-Parse-Master-Key': Parse.masterKey, |
| 226 | + 'X-Parse-Request-Id': 'abc-123' |
| 227 | + } |
| 228 | + }; |
| 229 | + await request(params).then(fail, e => { |
| 230 | + expect(e.status).toEqual(400); |
| 231 | + expect(e.data.error).toEqual("some other error"); |
| 232 | + }); |
| 233 | + }); |
| 234 | + |
| 235 | + it('should use default configuration when none is set', async () => { |
| 236 | + await setup({}); |
| 237 | + expect(Config.get(Parse.applicationId).idempotencyOptions.ttl).toBe(Definitions.IdempotencyOptions.ttl.default); |
| 238 | + expect(Config.get(Parse.applicationId).idempotencyOptions.paths).toBe(Definitions.IdempotencyOptions.paths.default); |
| 239 | + }); |
| 240 | + |
| 241 | + it('should throw on invalid configuration', async () => { |
| 242 | + await expectAsync(setup({ paths: 1 })).toBeRejected(); |
| 243 | + await expectAsync(setup({ ttl: 'a' })).toBeRejected(); |
| 244 | + await expectAsync(setup({ ttl: 0 })).toBeRejected(); |
| 245 | + await expectAsync(setup({ ttl: -1 })).toBeRejected(); |
| 246 | + }); |
| 247 | +}); |
0 commit comments