|
| 1 | +const rp = require('request-promise'); |
| 2 | +const defaultHeaders = { |
| 3 | + 'X-Parse-Application-Id': 'test', |
| 4 | + 'X-Parse-Rest-API-Key': 'rest' |
| 5 | +} |
| 6 | +const masterKeyHeaders = { |
| 7 | + 'X-Parse-Application-Id': 'test', |
| 8 | + 'X-Parse-Rest-API-Key': 'rest', |
| 9 | + 'X-Parse-Master-Key': 'test' |
| 10 | +} |
| 11 | +const defaultOptions = { |
| 12 | + headers: defaultHeaders, |
| 13 | + json: true |
| 14 | +} |
| 15 | +const masterKeyOptions = { |
| 16 | + headers: masterKeyHeaders, |
| 17 | + json: true |
| 18 | +} |
| 19 | + |
| 20 | +describe('JobSchedule', () => { |
| 21 | + it('should create _JobSchedule with masterKey', (done) => { |
| 22 | + const jobSchedule = new Parse.Object('_JobSchedule'); |
| 23 | + jobSchedule.set({ |
| 24 | + 'jobName': 'MY Cool Job' |
| 25 | + }); |
| 26 | + jobSchedule.save(null, {useMasterKey: true}).then(() => { |
| 27 | + done(); |
| 28 | + }) |
| 29 | + .catch(done.fail); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should fail creating _JobSchedule without masterKey', (done) => { |
| 33 | + const jobSchedule = new Parse.Object('_JobSchedule'); |
| 34 | + jobSchedule.set({ |
| 35 | + 'jobName': 'SomeJob' |
| 36 | + }); |
| 37 | + jobSchedule.save(null).then(done.fail) |
| 38 | + .catch(done); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should reject access when not using masterKey (/jobs)', (done) => { |
| 42 | + rp.get(Parse.serverURL + '/cloud_code/jobs', defaultOptions).then(done.fail, done); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should reject access when not using masterKey (/jobs/data)', (done) => { |
| 46 | + rp.get(Parse.serverURL + '/cloud_code/jobs/data', defaultOptions).then(done.fail, done); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should reject access when not using masterKey (PUT /jobs/id)', (done) => { |
| 50 | + rp.put(Parse.serverURL + '/cloud_code/jobs/jobId', defaultOptions).then(done.fail, done); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should reject access when not using masterKey (PUT /jobs/id)', (done) => { |
| 54 | + rp.del(Parse.serverURL + '/cloud_code/jobs/jobId', defaultOptions).then(done.fail, done); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should allow access when using masterKey (/jobs)', (done) => { |
| 58 | + rp.get(Parse.serverURL + '/cloud_code/jobs', masterKeyOptions).then(done, done.fail); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should create a job schedule', (done) => { |
| 62 | + Parse.Cloud.job('job', () => {}); |
| 63 | + const options = Object.assign({}, masterKeyOptions, { |
| 64 | + body: { |
| 65 | + job_schedule: { |
| 66 | + jobName: 'job' |
| 67 | + } |
| 68 | + } |
| 69 | + }); |
| 70 | + rp.post(Parse.serverURL + '/cloud_code/jobs', options).then((res) => { |
| 71 | + expect(res.objectId).not.toBeUndefined(); |
| 72 | + }) |
| 73 | + .then(() => { |
| 74 | + return rp.get(Parse.serverURL + '/cloud_code/jobs', masterKeyOptions); |
| 75 | + }) |
| 76 | + .then((res) => { |
| 77 | + expect(res.length).toBe(1); |
| 78 | + }) |
| 79 | + .then(done) |
| 80 | + .catch(done.fail); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should fail creating a job with an invalid name', (done) => { |
| 84 | + const options = Object.assign({}, masterKeyOptions, { |
| 85 | + body: { |
| 86 | + job_schedule: { |
| 87 | + jobName: 'job' |
| 88 | + } |
| 89 | + } |
| 90 | + }); |
| 91 | + rp.post(Parse.serverURL + '/cloud_code/jobs', options) |
| 92 | + .then(done.fail) |
| 93 | + .catch(done); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should update a job', (done) => { |
| 97 | + Parse.Cloud.job('job1', () => {}); |
| 98 | + Parse.Cloud.job('job2', () => {}); |
| 99 | + const options = Object.assign({}, masterKeyOptions, { |
| 100 | + body: { |
| 101 | + job_schedule: { |
| 102 | + jobName: 'job1' |
| 103 | + } |
| 104 | + } |
| 105 | + }); |
| 106 | + rp.post(Parse.serverURL + '/cloud_code/jobs', options).then((res) => { |
| 107 | + expect(res.objectId).not.toBeUndefined(); |
| 108 | + return rp.put(Parse.serverURL + '/cloud_code/jobs/' + res.objectId, Object.assign(options, { |
| 109 | + body: { |
| 110 | + job_schedule: { |
| 111 | + jobName: 'job2' |
| 112 | + } |
| 113 | + } |
| 114 | + })); |
| 115 | + }) |
| 116 | + .then(() => { |
| 117 | + return rp.get(Parse.serverURL + '/cloud_code/jobs', masterKeyOptions); |
| 118 | + }) |
| 119 | + .then((res) => { |
| 120 | + expect(res.length).toBe(1); |
| 121 | + expect(res[0].jobName).toBe('job2'); |
| 122 | + }) |
| 123 | + .then(done) |
| 124 | + .catch(done.fail); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should fail updating a job with an invalid name', (done) => { |
| 128 | + Parse.Cloud.job('job1', () => {}); |
| 129 | + const options = Object.assign({}, masterKeyOptions, { |
| 130 | + body: { |
| 131 | + job_schedule: { |
| 132 | + jobName: 'job1' |
| 133 | + } |
| 134 | + } |
| 135 | + }); |
| 136 | + rp.post(Parse.serverURL + '/cloud_code/jobs', options).then((res) => { |
| 137 | + expect(res.objectId).not.toBeUndefined(); |
| 138 | + return rp.put(Parse.serverURL + '/cloud_code/jobs/' + res.objectId, Object.assign(options, { |
| 139 | + body: { |
| 140 | + job_schedule: { |
| 141 | + jobName: 'job2' |
| 142 | + } |
| 143 | + } |
| 144 | + })); |
| 145 | + }) |
| 146 | + .then(done.fail) |
| 147 | + .catch(done); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should destroy a job', (done) => { |
| 151 | + Parse.Cloud.job('job', () => {}); |
| 152 | + const options = Object.assign({}, masterKeyOptions, { |
| 153 | + body: { |
| 154 | + job_schedule: { |
| 155 | + jobName: 'job' |
| 156 | + } |
| 157 | + } |
| 158 | + }); |
| 159 | + rp.post(Parse.serverURL + '/cloud_code/jobs', options).then((res) => { |
| 160 | + expect(res.objectId).not.toBeUndefined(); |
| 161 | + return rp.del(Parse.serverURL + '/cloud_code/jobs/' + res.objectId, masterKeyOptions); |
| 162 | + }) |
| 163 | + .then(() => { |
| 164 | + return rp.get(Parse.serverURL + '/cloud_code/jobs', masterKeyOptions); |
| 165 | + }) |
| 166 | + .then((res) => { |
| 167 | + expect(res.length).toBe(0); |
| 168 | + }) |
| 169 | + .then(done) |
| 170 | + .catch(done.fail); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should properly return job data', (done) => { |
| 174 | + Parse.Cloud.job('job1', () => {}); |
| 175 | + Parse.Cloud.job('job2', () => {}); |
| 176 | + const options = Object.assign({}, masterKeyOptions, { |
| 177 | + body: { |
| 178 | + job_schedule: { |
| 179 | + jobName: 'job1' |
| 180 | + } |
| 181 | + } |
| 182 | + }); |
| 183 | + rp.post(Parse.serverURL + '/cloud_code/jobs', options).then((res) => { |
| 184 | + expect(res.objectId).not.toBeUndefined(); |
| 185 | + }) |
| 186 | + .then(() => { |
| 187 | + return rp.get(Parse.serverURL + '/cloud_code/jobs/data', masterKeyOptions); |
| 188 | + }) |
| 189 | + .then((res) => { |
| 190 | + expect(res.in_use).toEqual(['job1']); |
| 191 | + expect(res.jobs).toContain('job1'); |
| 192 | + expect(res.jobs).toContain('job2'); |
| 193 | + expect(res.jobs.length).toBe(2); |
| 194 | + }) |
| 195 | + .then(done) |
| 196 | + .catch(done.fail); |
| 197 | + }); |
| 198 | +}); |
0 commit comments