@@ -3929,6 +3929,126 @@ describe('saveFile hooks', () => {
3929
3929
} ) ;
3930
3930
} ) ;
3931
3931
3932
+ describe ( 'Parse.File hooks' , ( ) => {
3933
+ it ( 'find hooks should run' , async ( ) => {
3934
+ const file = new Parse . File ( 'popeye.txt' , [ 1 , 2 , 3 ] , 'text/plain' ) ;
3935
+ await file . save ( { useMasterKey : true } ) ;
3936
+ const user = await Parse . User . signUp ( 'username' , 'password' ) ;
3937
+ const hooks = {
3938
+ beforeFind ( req ) {
3939
+ expect ( req ) . toBeDefined ( ) ;
3940
+ expect ( req . file ) . toBeDefined ( ) ;
3941
+ expect ( req . triggerName ) . toBe ( 'beforeFind' ) ;
3942
+ expect ( req . master ) . toBeFalse ( ) ;
3943
+ expect ( req . log ) . toBeDefined ( ) ;
3944
+ } ,
3945
+ afterFind ( req ) {
3946
+ expect ( req ) . toBeDefined ( ) ;
3947
+ expect ( req . file ) . toBeDefined ( ) ;
3948
+ expect ( req . triggerName ) . toBe ( 'afterFind' ) ;
3949
+ expect ( req . master ) . toBeFalse ( ) ;
3950
+ expect ( req . log ) . toBeDefined ( ) ;
3951
+ expect ( req . forceDownload ) . toBeFalse ( ) ;
3952
+ } ,
3953
+ } ;
3954
+ for ( const hook in hooks ) {
3955
+ spyOn ( hooks , hook ) . and . callThrough ( ) ;
3956
+ Parse . Cloud [ hook ] ( Parse . File , hooks [ hook ] ) ;
3957
+ }
3958
+ await request ( {
3959
+ url : file . url ( ) ,
3960
+ headers : {
3961
+ 'X-Parse-Application-Id' : 'test' ,
3962
+ 'X-Parse-REST-API-Key' : 'rest' ,
3963
+ 'X-Parse-Session-Token' : user . getSessionToken ( ) ,
3964
+ } ,
3965
+ } ) ;
3966
+ for ( const hook in hooks ) {
3967
+ expect ( hooks [ hook ] ) . toHaveBeenCalled ( ) ;
3968
+ }
3969
+ } ) ;
3970
+
3971
+ it ( 'beforeFind can throw' , async ( ) => {
3972
+ const file = new Parse . File ( 'popeye.txt' , [ 1 , 2 , 3 ] , 'text/plain' ) ;
3973
+ await file . save ( { useMasterKey : true } ) ;
3974
+ const user = await Parse . User . signUp ( 'username' , 'password' ) ;
3975
+ const hooks = {
3976
+ beforeFind ( ) {
3977
+ throw 'unauthorized' ;
3978
+ } ,
3979
+ afterFind ( ) { } ,
3980
+ } ;
3981
+ for ( const hook in hooks ) {
3982
+ spyOn ( hooks , hook ) . and . callThrough ( ) ;
3983
+ Parse . Cloud [ hook ] ( Parse . File , hooks [ hook ] ) ;
3984
+ }
3985
+ await expectAsync (
3986
+ request ( {
3987
+ url : file . url ( ) ,
3988
+ headers : {
3989
+ 'X-Parse-Application-Id' : 'test' ,
3990
+ 'X-Parse-REST-API-Key' : 'rest' ,
3991
+ 'X-Parse-Session-Token' : user . getSessionToken ( ) ,
3992
+ } ,
3993
+ } ) . catch ( e => {
3994
+ throw new Parse . Error ( e . data . code , e . data . error ) ;
3995
+ } )
3996
+ ) . toBeRejectedWith ( new Parse . Error ( Parse . Error . SCRIPT_FAILED , 'unauthorized' ) ) ;
3997
+
3998
+ expect ( hooks . beforeFind ) . toHaveBeenCalled ( ) ;
3999
+ expect ( hooks . afterFind ) . not . toHaveBeenCalled ( ) ;
4000
+ } ) ;
4001
+
4002
+ it ( 'afterFind can throw' , async ( ) => {
4003
+ const file = new Parse . File ( 'popeye.txt' , [ 1 , 2 , 3 ] , 'text/plain' ) ;
4004
+ await file . save ( { useMasterKey : true } ) ;
4005
+ const user = await Parse . User . signUp ( 'username' , 'password' ) ;
4006
+ const hooks = {
4007
+ beforeFind ( ) { } ,
4008
+ afterFind ( ) {
4009
+ throw 'unauthorized' ;
4010
+ } ,
4011
+ } ;
4012
+ for ( const hook in hooks ) {
4013
+ spyOn ( hooks , hook ) . and . callThrough ( ) ;
4014
+ Parse . Cloud [ hook ] ( Parse . File , hooks [ hook ] ) ;
4015
+ }
4016
+ await expectAsync (
4017
+ request ( {
4018
+ url : file . url ( ) ,
4019
+ headers : {
4020
+ 'X-Parse-Application-Id' : 'test' ,
4021
+ 'X-Parse-REST-API-Key' : 'rest' ,
4022
+ 'X-Parse-Session-Token' : user . getSessionToken ( ) ,
4023
+ } ,
4024
+ } ) . catch ( e => {
4025
+ throw new Parse . Error ( e . data . code , e . data . error ) ;
4026
+ } )
4027
+ ) . toBeRejectedWith ( new Parse . Error ( Parse . Error . SCRIPT_FAILED , 'unauthorized' ) ) ;
4028
+ for ( const hook in hooks ) {
4029
+ expect ( hooks [ hook ] ) . toHaveBeenCalled ( ) ;
4030
+ }
4031
+ } ) ;
4032
+
4033
+ it ( 'can force download' , async ( ) => {
4034
+ const file = new Parse . File ( 'popeye.txt' , [ 1 , 2 , 3 ] , 'text/plain' ) ;
4035
+ await file . save ( { useMasterKey : true } ) ;
4036
+ const user = await Parse . User . signUp ( 'username' , 'password' ) ;
4037
+ Parse . Cloud . afterFind ( Parse . File , req => {
4038
+ req . forceDownload = true ;
4039
+ } ) ;
4040
+ const response = await request ( {
4041
+ url : file . url ( ) ,
4042
+ headers : {
4043
+ 'X-Parse-Application-Id' : 'test' ,
4044
+ 'X-Parse-REST-API-Key' : 'rest' ,
4045
+ 'X-Parse-Session-Token' : user . getSessionToken ( ) ,
4046
+ } ,
4047
+ } ) ;
4048
+ expect ( response . headers [ 'content-disposition' ] ) . toBe ( `attachment;filename=${ file . _name } ` ) ;
4049
+ } ) ;
4050
+ } ) ;
4051
+
3932
4052
describe ( 'Cloud Config hooks' , ( ) => {
3933
4053
function testConfig ( ) {
3934
4054
return Parse . Config . save ( { internal : 'i' , string : 's' , number : 12 } , { internal : true } ) ;
0 commit comments