forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseCloudTest.js
153 lines (134 loc) · 4.31 KB
/
ParseCloudTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict';
const assert = require('assert');
const Parse = require('../../node');
const sleep = require('./sleep');
const waitForJobStatus = async (jobStatusId, status) => {
const checkJobStatus = async () => {
const result = await Parse.Cloud.getJobStatus(jobStatusId);
return result && result.get('status') === status;
};
while (!(await checkJobStatus())) {
await sleep(100);
}
};
describe('Parse Cloud', () => {
it('run function', done => {
const params = { key1: 'value2', key2: 'value1' };
Parse.Cloud.run('bar', params)
.then(result => {
assert.equal('Foo', result);
done();
})
.catch(done.fail);
});
it('run function with user', done => {
const params = { key1: 'value2', key2: 'value1' };
const user = new Parse.User();
user.setUsername('someuser');
user.setPassword('somepassword');
user
.signUp()
.then(() => {
return Parse.Cloud.run('bar', params);
})
.then(resp => {
assert.equal('Foo', resp);
return user.destroy({ useMasterKey: true });
})
.then(() => {
done();
})
.catch(done.fail);
});
it('run function failed', done => {
const params = { key1: 'value1', key2: 'value2' };
Parse.Cloud.run('bar', params)
.then(done.fail)
.catch(error => {
assert.equal(error.code, Parse.Error.SCRIPT_FAILED);
done();
});
});
it('run function name fail', done => {
const params = { key1: 'value1' };
Parse.Cloud.run('unknown_function', params)
.then(done.fail)
.catch(error => {
assert.equal(error.message, 'Invalid function: "unknown_function"');
done();
});
});
it('run function with geopoint params does not fail', done => {
const params = { key1: new Parse.GeoPoint(50, 50) };
Parse.Cloud.run('unknown_function', params)
.then(null)
.catch(error => {
assert.equal(error.message, 'Invalid function: "unknown_function"');
done();
});
});
it('run function with object params fail', done => {
const object = new Parse.Object('TestClass');
const params = { key1: object };
try {
Parse.Cloud.run('bar', params);
} catch (e) {
assert.equal(e, 'Error: Parse Objects not allowed here');
done();
}
});
it('run function with undefined', done => {
Parse.Cloud.run('CloudFunctionUndefined', {}).then(result => {
assert.strictEqual(result, undefined);
done();
});
});
it('run job', async () => {
const params = { startedBy: 'Monty Python' };
const jobStatusId = await Parse.Cloud.startJob('CloudJob1', params);
expect(jobStatusId).toBeDefined();
await waitForJobStatus(jobStatusId, 'succeeded');
const jobStatus = await Parse.Cloud.getJobStatus(jobStatusId);
assert.equal(jobStatus.get('status'), 'succeeded');
assert.equal(jobStatus.get('params').startedBy, 'Monty Python');
});
it('run long job', async () => {
const jobStatusId = await Parse.Cloud.startJob('CloudJob2');
let jobStatus = await Parse.Cloud.getJobStatus(jobStatusId);
assert.equal(jobStatus.get('status'), 'running');
await waitForJobStatus(jobStatusId, 'succeeded');
jobStatus = await Parse.Cloud.getJobStatus(jobStatusId);
assert.equal(jobStatus.get('status'), 'succeeded');
});
it('run bad job', done => {
Parse.Cloud.startJob('bad_job')
.then(null)
.catch(error => {
assert.equal(error.code, Parse.Error.SCRIPT_FAILED);
assert.equal(error.message, 'Invalid job.');
done();
});
});
it('run failing job', async () => {
const jobStatusId = await Parse.Cloud.startJob('CloudJobFailing');
await waitForJobStatus(jobStatusId, 'failed');
const jobStatus = await Parse.Cloud.getJobStatus(jobStatusId);
assert.equal(jobStatus.get('status'), 'failed');
assert.equal(jobStatus.get('message'), 'cloud job failed');
});
it('get jobs data', done => {
Parse.Cloud.getJobsData().then(result => {
assert.equal(result.in_use.length, 0);
assert.equal(result.jobs.length, 3);
done();
});
});
it('invalid job status id', done => {
Parse.Cloud.getJobStatus('not-a-real-id')
.then(null)
.catch(error => {
assert.equal(error.message, 'Object not found.');
done();
});
});
});