1
+ var Parse = require ( 'parse/node' ) . Parse ;
1
2
var request = require ( 'request' ) ;
2
3
var dd = require ( 'deep-diff' ) ;
4
+
3
5
var hasAllPODobject = ( ) => {
4
6
var obj = new Parse . Object ( 'HasAllPOD' ) ;
5
7
obj . set ( 'aNumber' , 5 ) ;
@@ -16,7 +18,7 @@ var hasAllPODobject = () => {
16
18
return obj ;
17
19
}
18
20
19
- var expectedResponseForHasAllPOD = {
21
+ var plainOldDataSchema = {
20
22
className : 'HasAllPOD' ,
21
23
fields : {
22
24
//Default fields
@@ -36,7 +38,7 @@ var expectedResponseForHasAllPOD = {
36
38
} ,
37
39
} ;
38
40
39
- var expectedResponseforHasPointersAndRelations = {
41
+ var pointersAndRelationsSchema = {
40
42
className : 'HasPointersAndRelations' ,
41
43
fields : {
42
44
//Default fields
@@ -56,17 +58,30 @@ var expectedResponseforHasPointersAndRelations = {
56
58
} ,
57
59
}
58
60
61
+ var noAuthHeaders = {
62
+ 'X-Parse-Application-Id' : 'test' ,
63
+ } ;
64
+
65
+ var restKeyHeaders = {
66
+ 'X-Parse-Application-Id' : 'test' ,
67
+ 'X-Parse-REST-API-Key' : 'rest' ,
68
+ } ;
69
+
70
+ var masterKeyHeaders = {
71
+ 'X-Parse-Application-Id' : 'test' ,
72
+ 'X-Parse-Master-Key' : 'test' ,
73
+ } ;
74
+
59
75
describe ( 'schemas' , ( ) => {
60
76
it ( 'requires the master key to get all schemas' , ( done ) => {
61
77
request . get ( {
62
78
url : 'http://localhost:8378/1/schemas' ,
63
79
json : true ,
64
- headers : {
65
- 'X-Parse-Application-Id' : 'test' ,
66
- 'X-Parse-REST-API-Key' : 'rest' ,
67
- } ,
80
+ headers : noAuthHeaders ,
68
81
} , ( error , response , body ) => {
69
- expect ( response . statusCode ) . toEqual ( 401 ) ;
82
+ //api.parse.com uses status code 401, but due to the lack of keys
83
+ //being necessary in parse-server, 403 makes more sense
84
+ expect ( response . statusCode ) . toEqual ( 403 ) ;
70
85
expect ( body . error ) . toEqual ( 'unauthorized' ) ;
71
86
done ( ) ;
72
87
} ) ;
@@ -76,25 +91,31 @@ describe('schemas', () => {
76
91
request . get ( {
77
92
url : 'http://localhost:8378/1/schemas/SomeSchema' ,
78
93
json : true ,
79
- headers : {
80
- 'X-Parse-Application-Id' : 'test' ,
81
- 'X-Parse-REST-API-Key' : 'rest' ,
82
- } ,
94
+ headers : restKeyHeaders ,
83
95
} , ( error , response , body ) => {
84
96
expect ( response . statusCode ) . toEqual ( 401 ) ;
85
97
expect ( body . error ) . toEqual ( 'unauthorized' ) ;
86
98
done ( ) ;
87
99
} ) ;
88
100
} ) ;
89
101
102
+ it ( 'asks for the master key if you use the rest key' , ( done ) => {
103
+ request . get ( {
104
+ url : 'http://localhost:8378/1/schemas' ,
105
+ json : true ,
106
+ headers : restKeyHeaders ,
107
+ } , ( error , response , body ) => {
108
+ expect ( response . statusCode ) . toEqual ( 401 ) ;
109
+ expect ( body . error ) . toEqual ( 'master key not specified' ) ;
110
+ done ( ) ;
111
+ } ) ;
112
+ } ) ;
113
+
90
114
it ( 'responds with empty list when there are no schemas' , done => {
91
115
request . get ( {
92
116
url : 'http://localhost:8378/1/schemas' ,
93
117
json : true ,
94
- headers : {
95
- 'X-Parse-Application-Id' : 'test' ,
96
- 'X-Parse-Master-Key' : 'test' ,
97
- } ,
118
+ headers : masterKeyHeaders ,
98
119
} , ( error , response , body ) => {
99
120
expect ( body . results ) . toEqual ( [ ] ) ;
100
121
done ( ) ;
@@ -113,13 +134,10 @@ describe('schemas', () => {
113
134
request . get ( {
114
135
url : 'http://localhost:8378/1/schemas' ,
115
136
json : true ,
116
- headers : {
117
- 'X-Parse-Application-Id' : 'test' ,
118
- 'X-Parse-Master-Key' : 'test' ,
119
- } ,
137
+ headers : masterKeyHeaders ,
120
138
} , ( error , response , body ) => {
121
139
var expected = {
122
- results : [ expectedResponseForHasAllPOD , expectedResponseforHasPointersAndRelations ]
140
+ results : [ plainOldDataSchema , pointersAndRelationsSchema ]
123
141
} ;
124
142
expect ( body ) . toEqual ( expected ) ;
125
143
done ( ) ;
@@ -133,12 +151,9 @@ describe('schemas', () => {
133
151
request . get ( {
134
152
url : 'http://localhost:8378/1/schemas/HasAllPOD' ,
135
153
json : true ,
136
- headers : {
137
- 'X-Parse-Application-Id' : 'test' ,
138
- 'X-Parse-Master-Key' : 'test' ,
139
- } ,
154
+ headers : masterKeyHeaders ,
140
155
} , ( error , response , body ) => {
141
- expect ( body ) . toEqual ( expectedResponseForHasAllPOD ) ;
156
+ expect ( body ) . toEqual ( plainOldDataSchema ) ;
142
157
done ( ) ;
143
158
} ) ;
144
159
} ) ;
@@ -150,10 +165,7 @@ describe('schemas', () => {
150
165
request . get ( {
151
166
url : 'http://localhost:8378/1/schemas/HASALLPOD' ,
152
167
json : true ,
153
- headers : {
154
- 'X-Parse-Application-Id' : 'test' ,
155
- 'X-Parse-Master-Key' : 'test' ,
156
- } ,
168
+ headers : masterKeyHeaders ,
157
169
} , ( error , response , body ) => {
158
170
expect ( response . statusCode ) . toEqual ( 400 ) ;
159
171
expect ( body ) . toEqual ( {
@@ -164,4 +176,146 @@ describe('schemas', () => {
164
176
} ) ;
165
177
} ) ;
166
178
} ) ;
179
+
180
+ it ( 'requires the master key to create a schema' , done => {
181
+ request . post ( {
182
+ url : 'http://localhost:8378/1/schemas' ,
183
+ json : true ,
184
+ headers : noAuthHeaders ,
185
+ body : {
186
+ className : 'MyClass' ,
187
+ }
188
+ } , ( error , response , body ) => {
189
+ expect ( response . statusCode ) . toEqual ( 403 ) ;
190
+ expect ( body . error ) . toEqual ( 'unauthorized' ) ;
191
+ done ( ) ;
192
+ } ) ;
193
+ } ) ;
194
+
195
+ it ( 'asks for the master key if you use the rest key' , done => {
196
+ request . post ( {
197
+ url : 'http://localhost:8378/1/schemas' ,
198
+ json : true ,
199
+ headers : restKeyHeaders ,
200
+ body : {
201
+ className : 'MyClass' ,
202
+ } ,
203
+ } , ( error , response , body ) => {
204
+ expect ( response . statusCode ) . toEqual ( 401 ) ;
205
+ expect ( body . error ) . toEqual ( 'master key not specified' ) ;
206
+ done ( ) ;
207
+ } ) ;
208
+ } ) ;
209
+
210
+ it ( 'sends an error if you use mismatching class names' , done => {
211
+ request . post ( {
212
+ url : 'http://localhost:8378/1/schemas/A' ,
213
+ headers : masterKeyHeaders ,
214
+ json : true ,
215
+ body : {
216
+ className : 'B' ,
217
+ }
218
+ } , ( error , response , body ) => {
219
+ expect ( response . statusCode ) . toEqual ( 400 ) ;
220
+ expect ( body ) . toEqual ( {
221
+ code : Parse . Error . INVALID_CLASS_NAME ,
222
+ error : 'class name mismatch between B and A' ,
223
+ } ) ;
224
+ done ( ) ;
225
+ } ) ;
226
+ } ) ;
227
+
228
+ it ( 'sends an error if you use no class name' , done => {
229
+ request . post ( {
230
+ url : 'http://localhost:8378/1/schemas' ,
231
+ headers : masterKeyHeaders ,
232
+ json : true ,
233
+ body : { } ,
234
+ } , ( error , response , body ) => {
235
+ expect ( response . statusCode ) . toEqual ( 400 ) ;
236
+ expect ( body ) . toEqual ( {
237
+ code : 135 ,
238
+ error : 'POST /schemas needs class name' ,
239
+ } ) ;
240
+ done ( ) ;
241
+ } )
242
+ } ) ;
243
+
244
+ it ( 'sends an error if you try to create the same class twice' , done => {
245
+ request . post ( {
246
+ url : 'http://localhost:8378/1/schemas' ,
247
+ headers : masterKeyHeaders ,
248
+ json : true ,
249
+ body : {
250
+ className : 'A' ,
251
+ } ,
252
+ } , ( error , response , body ) => {
253
+ expect ( error ) . toEqual ( null ) ;
254
+ request . post ( {
255
+ url : 'http://localhost:8378/1/schemas' ,
256
+ headers : masterKeyHeaders ,
257
+ json : true ,
258
+ body : {
259
+ className : 'A' ,
260
+ }
261
+ } , ( error , response , body ) => {
262
+ expect ( response . statusCode ) . toEqual ( 400 ) ;
263
+ expect ( body ) . toEqual ( {
264
+ code : Parse . Error . INVALID_CLASS_NAME ,
265
+ error : 'class A already exists' ,
266
+ } ) ;
267
+ done ( ) ;
268
+ } ) ;
269
+ } ) ;
270
+ } ) ;
271
+
272
+ it ( 'responds with all fields when you create a class' , done => {
273
+ request . post ( {
274
+ url : 'http://localhost:8378/1/schemas' ,
275
+ headers : masterKeyHeaders ,
276
+ json : true ,
277
+ body : {
278
+ className : "NewClass" ,
279
+ fields : {
280
+ foo : { type : 'Number' } ,
281
+ ptr : { type : 'Pointer' , targetClass : 'SomeClass' }
282
+ }
283
+ }
284
+ } , ( error , response , body ) => {
285
+ expect ( body ) . toEqual ( {
286
+ className : 'NewClass' ,
287
+ fields : {
288
+ ACL : { type : 'ACL' } ,
289
+ createdAt : { type : 'Date' } ,
290
+ updatedAt : { type : 'Date' } ,
291
+ objectId : { type : 'String' } ,
292
+ foo : { type : 'Number' } ,
293
+ ptr : { type : 'Pointer' , targetClass : 'SomeClass' } ,
294
+ }
295
+ } ) ;
296
+ done ( ) ;
297
+ } ) ;
298
+ } ) ;
299
+
300
+ it ( 'lets you specify class name in both places' , done => {
301
+ request . post ( {
302
+ url : 'http://localhost:8378/1/schemas/NewClass' ,
303
+ headers : masterKeyHeaders ,
304
+ json : true ,
305
+ body : {
306
+ className : "NewClass" ,
307
+ }
308
+ } , ( error , response , body ) => {
309
+ expect ( body ) . toEqual ( {
310
+ className : 'NewClass' ,
311
+ fields : {
312
+ ACL : { type : 'ACL' } ,
313
+ createdAt : { type : 'Date' } ,
314
+ updatedAt : { type : 'Date' } ,
315
+ objectId : { type : 'String' } ,
316
+ }
317
+ } ) ;
318
+ done ( ) ;
319
+ } ) ;
320
+ } ) ;
167
321
} ) ;
0 commit comments