@@ -33,6 +33,12 @@ AV.Cloud.beforeSave("TestReview", function(request, response){
3333 }
3434} ) ;
3535
36+ AV . Cloud . beforeSave ( "ErrorObject" , function ( request , response ) {
37+ var a = { } ;
38+ a . noThisMethod ( ) ;
39+ response . success ( ) ;
40+ } )
41+
3642AV . Cloud . afterSave ( "TestReview" , function ( request ) {
3743 assert . equal ( request . object . className , 'TestReview' ) ;
3844 assert . equal ( request . object . id , '5403e36be4b0b77b5746b292' ) ;
@@ -42,6 +48,24 @@ AV.Cloud.afterSave("TestError", function(request) {
4248 noThisMethod ( ) ;
4349} ) ;
4450
51+ AV . Cloud . afterUpdate ( "TestClass" , function ( request ) {
52+ var bizTime = new Date ( ) ;
53+ request . object . set ( 'bizTime' , bizTime ) ;
54+ request . object . save ( null , {
55+ success : function ( obj ) {
56+ assert . equal ( bizTime , obj . get ( 'bizTime' ) ) ;
57+ }
58+ } ) ;
59+ } ) ;
60+
61+ AV . Cloud . beforeDelete ( "TestClass" , function ( request , response ) {
62+ assert . equal ( request . object . className , 'TestClass' ) ;
63+ if ( request . object . get ( 'foo' ) === 'important' ) {
64+ return response . error ( 'important note' ) ;
65+ }
66+ response . success ( ) ;
67+ } ) ;
68+
4569AV . Cloud . onVerified ( 'sms' , function ( request ) {
4670 assert . equal ( request . object . id , '54fd6a03e4b06c41e00b1f40' ) ;
4771} ) ;
@@ -75,10 +99,8 @@ describe('hook', function() {
7599 } )
76100 . expect ( 200 )
77101 . expect ( {
78- "result" : {
79- "stars" : 1 ,
80- "comment" : "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567..."
81- }
102+ "stars" : 1 ,
103+ "comment" : "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567..."
82104 } , done ) ;
83105 } ) ;
84106
@@ -116,16 +138,52 @@ describe('hook', function() {
116138 } )
117139 . expect ( 200 )
118140 . expect ( {
119- "result" : {
120- "user" : {
121- "__type" : "Pointer" ,
122- "className" : "_User" ,
123- "objectId" : "52aebbdee4b0c8b6fa455aa7"
124- }
141+ "user" : {
142+ "__type" : "Pointer" ,
143+ "className" : "_User" ,
144+ "objectId" : "52aebbdee4b0c8b6fa455aa7"
125145 }
126146 } , done ) ;
127147 } ) ;
128148
149+ it ( "beforeSave_throw_error" , function ( done ) {
150+ var ori = console . warn ;
151+ var warnLogs = [ ] ;
152+ console . warn = function ( ) {
153+ warnLogs . push ( arguments ) ;
154+ }
155+ request ( AV . Cloud )
156+ . post ( "/1.1/functions/ErrorObject/beforeSave" )
157+ . set ( 'X-AVOSCloud-Application-Id' , appId )
158+ . set ( 'X-AVOSCloud-Application-Key' , appKey )
159+ . send ( {
160+ object : {
161+ foo : 'bar'
162+ }
163+ } )
164+ . expect ( 500 , function ( err , res ) {
165+ res . body . should . eql ( { code : 1 , error : 'undefined is not a function' } ) ;
166+ console . warn = ori ;
167+ warnLogs . length . should . equal ( 1 ) ;
168+ warnLogs [ 0 ] [ 0 ] . split ( '\n' ) [ 0 ] . should . equal ( "Execute \'__before_save_for_ErrorObject\' failed with error: TypeError: undefined is not a function" ) ;
169+ done ( ) ;
170+ } ) ;
171+ } ) ;
172+
173+ it ( "beforeSave_not_found" , function ( done ) {
174+ request ( AV . Cloud )
175+ . post ( "/1.1/functions/NoThisObject/beforeSave" )
176+ . set ( 'X-AVOSCloud-Application-Id' , appId )
177+ . set ( 'X-AVOSCloud-Application-Key' , appKey )
178+ . send ( {
179+ object : {
180+ foo : 'bar'
181+ }
182+ } )
183+ . expect ( 404 )
184+ . expect ( { code : 1 , error : "LeanEngine not found hook '__before_save_for_NoThisObject' for app '" + appId + "' on development." } , done ) ;
185+ } ) ;
186+
129187 it ( 'afterSave' , function ( done ) {
130188 request ( AV . Cloud )
131189 . post ( '/1/functions/TestReview/afterSave' )
@@ -186,8 +244,60 @@ describe('hook', function() {
186244 }
187245 }
188246 } )
247+ . expect ( 404 )
248+ . expect ( { code : 1 , error : "LeanEngine not found hook \'__after_save_for_NoThisClass\' for app \'" + appId + "\' on development." } , done ) ;
249+ } ) ;
250+
251+ it ( 'afterUpdate' , function ( done ) {
252+ request ( AV . Cloud )
253+ . post ( '/1/functions/TestClass/afterUpdate' )
254+ . set ( 'X-AVOSCloud-Application-Id' , appId )
255+ . set ( 'X-AVOSCloud-Application-Key' , appKey )
256+ . set ( 'Content-Type' , 'application/json' )
257+ . send ( {
258+ object : {
259+ objectId : '556904d8e4b09419960c14bd' ,
260+ foo : 'bar'
261+ }
262+ } )
263+ . expect ( 200 , function ( err , res ) {
264+ res . body . should . eql ( { result : 'ok' } ) ;
265+ setTimeout ( function ( ) { // 等待数据更新
266+ done ( ) ;
267+ } , 1000 ) ;
268+ } ) ;
269+ } ) ;
270+
271+ it ( 'should be deleted' , function ( done ) {
272+ request ( AV . Cloud )
273+ . post ( '/1/functions/TestClass/beforeDelete' )
274+ . set ( 'X-AVOSCloud-Application-Id' , appId )
275+ . set ( 'X-AVOSCloud-Application-Key' , appKey )
276+ . set ( 'Content-Type' , 'application/json' )
277+ . send ( {
278+ object : {
279+ objectId : '55690242e4b09419960c01f5' ,
280+ foo : 'bar'
281+ }
282+ } )
283+ . expect ( 200 )
284+ . expect ( { } , done ) ;
285+ } ) ;
286+
287+ it ( 'should not be deleted' , function ( done ) {
288+ request ( AV . Cloud )
289+ . post ( '/1/functions/TestClass/beforeDelete' )
290+ . set ( 'X-AVOSCloud-Application-Id' , appId )
291+ . set ( 'X-AVOSCloud-Application-Key' , appKey )
292+ . set ( 'Content-Type' , 'application/json' )
293+ . send ( {
294+ object : {
295+ objectId : '55690242e4b09419960c01f6' ,
296+ foo : 'important'
297+ }
298+ } )
189299 . expect ( 400 )
190- . expect ( { code : 1 , error : "Cloud code not find hook \'__after_save_for_NoThisClass\' for app \'" + appId + "\' on development. " } , done ) ;
300+ . expect ( { code : 1 , error : "important note " } , done ) ;
191301 } ) ;
192302
193303 it ( 'onVerified' , function ( done ) {
0 commit comments