@@ -3921,6 +3921,162 @@ describe('saveFile hooks', () => {
3921
3921
} ) ;
3922
3922
} ) ;
3923
3923
3924
+ describe ( 'Cloud Config hooks' , ( ) => {
3925
+ function testConfig ( ) {
3926
+ return Parse . Config . save ( { internal : 'i' , string : 's' , number : 12 } , { internal : true } ) ;
3927
+ }
3928
+
3929
+ it ( 'beforeSave(Parse.Config) can run hook with new config' , async ( ) => {
3930
+ let count = 0 ;
3931
+ Parse . Cloud . beforeSave ( Parse . Config , ( req ) => {
3932
+ expect ( req . object ) . toBeDefined ( ) ;
3933
+ expect ( req . original ) . toBeUndefined ( ) ;
3934
+ expect ( req . user ) . toBeUndefined ( ) ;
3935
+ expect ( req . headers ) . toBeDefined ( ) ;
3936
+ expect ( req . ip ) . toBeDefined ( ) ;
3937
+ expect ( req . installationId ) . toBeDefined ( ) ;
3938
+ expect ( req . context ) . toBeDefined ( ) ;
3939
+ const config = req . object ;
3940
+ expect ( config . get ( 'internal' ) ) . toBe ( 'i' ) ;
3941
+ expect ( config . get ( 'string' ) ) . toBe ( 's' ) ;
3942
+ expect ( config . get ( 'number' ) ) . toBe ( 12 ) ;
3943
+ count += 1 ;
3944
+ } ) ;
3945
+ await testConfig ( ) ;
3946
+ const config = await Parse . Config . get ( { useMasterKey : true } ) ;
3947
+ expect ( config . get ( 'internal' ) ) . toBe ( 'i' ) ;
3948
+ expect ( config . get ( 'string' ) ) . toBe ( 's' ) ;
3949
+ expect ( config . get ( 'number' ) ) . toBe ( 12 ) ;
3950
+ expect ( count ) . toBe ( 1 ) ;
3951
+ } ) ;
3952
+
3953
+ it ( 'beforeSave(Parse.Config) can run hook with existing config' , async ( ) => {
3954
+ let count = 0 ;
3955
+ Parse . Cloud . beforeSave ( Parse . Config , ( req ) => {
3956
+ if ( count === 0 ) {
3957
+ expect ( req . object . get ( 'number' ) ) . toBe ( 12 ) ;
3958
+ expect ( req . original ) . toBeUndefined ( ) ;
3959
+ }
3960
+ if ( count === 1 ) {
3961
+ expect ( req . object . get ( 'number' ) ) . toBe ( 13 ) ;
3962
+ expect ( req . original . get ( 'number' ) ) . toBe ( 12 ) ;
3963
+ }
3964
+ count += 1 ;
3965
+ } ) ;
3966
+ await testConfig ( ) ;
3967
+ await Parse . Config . save ( { number : 13 } ) ;
3968
+ expect ( count ) . toBe ( 2 ) ;
3969
+ } ) ;
3970
+
3971
+ it ( 'beforeSave(Parse.Config) should not change config if nothing is returned' , async ( ) => {
3972
+ let count = 0 ;
3973
+ Parse . Cloud . beforeSave ( Parse . Config , ( ) => {
3974
+ count += 1 ;
3975
+ return ;
3976
+ } ) ;
3977
+ await testConfig ( ) ;
3978
+ const config = await Parse . Config . get ( { useMasterKey : true } ) ;
3979
+ expect ( config . get ( 'internal' ) ) . toBe ( 'i' ) ;
3980
+ expect ( config . get ( 'string' ) ) . toBe ( 's' ) ;
3981
+ expect ( config . get ( 'number' ) ) . toBe ( 12 ) ;
3982
+ expect ( count ) . toBe ( 1 ) ;
3983
+ } ) ;
3984
+
3985
+ it ( 'beforeSave(Parse.Config) throw custom error' , async ( ) => {
3986
+ Parse . Cloud . beforeSave ( Parse . Config , ( ) => {
3987
+ throw new Parse . Error ( Parse . Error . SCRIPT_FAILED , 'It should fail' ) ;
3988
+ } ) ;
3989
+ try {
3990
+ await testConfig ( ) ;
3991
+ fail ( 'error should have thrown' ) ;
3992
+ } catch ( e ) {
3993
+ expect ( e . code ) . toBe ( Parse . Error . SCRIPT_FAILED ) ;
3994
+ expect ( e . message ) . toBe ( 'It should fail' ) ;
3995
+ }
3996
+ } ) ;
3997
+
3998
+ it ( 'beforeSave(Parse.Config) throw string error' , async ( ) => {
3999
+ Parse . Cloud . beforeSave ( Parse . Config , ( ) => {
4000
+ throw 'before save failed' ;
4001
+ } ) ;
4002
+ try {
4003
+ await testConfig ( ) ;
4004
+ fail ( 'error should have thrown' ) ;
4005
+ } catch ( e ) {
4006
+ expect ( e . code ) . toBe ( Parse . Error . SCRIPT_FAILED ) ;
4007
+ expect ( e . message ) . toBe ( 'before save failed' ) ;
4008
+ }
4009
+ } ) ;
4010
+
4011
+ it ( 'beforeSave(Parse.Config) throw empty error' , async ( ) => {
4012
+ Parse . Cloud . beforeSave ( Parse . Config , ( ) => {
4013
+ throw null ;
4014
+ } ) ;
4015
+ try {
4016
+ await testConfig ( ) ;
4017
+ fail ( 'error should have thrown' ) ;
4018
+ } catch ( e ) {
4019
+ expect ( e . code ) . toBe ( Parse . Error . SCRIPT_FAILED ) ;
4020
+ expect ( e . message ) . toBe ( 'Script failed. Unknown error.' ) ;
4021
+ }
4022
+ } ) ;
4023
+
4024
+ it ( 'afterSave(Parse.Config) can run hook with new config' , async ( ) => {
4025
+ let count = 0 ;
4026
+ Parse . Cloud . afterSave ( Parse . Config , ( req ) => {
4027
+ expect ( req . object ) . toBeDefined ( ) ;
4028
+ expect ( req . original ) . toBeUndefined ( ) ;
4029
+ expect ( req . user ) . toBeUndefined ( ) ;
4030
+ expect ( req . headers ) . toBeDefined ( ) ;
4031
+ expect ( req . ip ) . toBeDefined ( ) ;
4032
+ expect ( req . installationId ) . toBeDefined ( ) ;
4033
+ expect ( req . context ) . toBeDefined ( ) ;
4034
+ const config = req . object ;
4035
+ expect ( config . get ( 'internal' ) ) . toBe ( 'i' ) ;
4036
+ expect ( config . get ( 'string' ) ) . toBe ( 's' ) ;
4037
+ expect ( config . get ( 'number' ) ) . toBe ( 12 ) ;
4038
+ count += 1 ;
4039
+ } ) ;
4040
+ await testConfig ( ) ;
4041
+ const config = await Parse . Config . get ( { useMasterKey : true } ) ;
4042
+ expect ( config . get ( 'internal' ) ) . toBe ( 'i' ) ;
4043
+ expect ( config . get ( 'string' ) ) . toBe ( 's' ) ;
4044
+ expect ( config . get ( 'number' ) ) . toBe ( 12 ) ;
4045
+ expect ( count ) . toBe ( 1 ) ;
4046
+ } ) ;
4047
+
4048
+ it ( 'afterSave(Parse.Config) can run hook with existing config' , async ( ) => {
4049
+ let count = 0 ;
4050
+ Parse . Cloud . afterSave ( Parse . Config , ( req ) => {
4051
+ if ( count === 0 ) {
4052
+ expect ( req . object . get ( 'number' ) ) . toBe ( 12 ) ;
4053
+ expect ( req . original ) . toBeUndefined ( ) ;
4054
+ }
4055
+ if ( count === 1 ) {
4056
+ expect ( req . object . get ( 'number' ) ) . toBe ( 13 ) ;
4057
+ expect ( req . original . get ( 'number' ) ) . toBe ( 12 ) ;
4058
+ }
4059
+ count += 1 ;
4060
+ } ) ;
4061
+ await testConfig ( ) ;
4062
+ await Parse . Config . save ( { number : 13 } ) ;
4063
+ expect ( count ) . toBe ( 2 ) ;
4064
+ } ) ;
4065
+
4066
+ it ( 'afterSave(Parse.Config) should throw error' , async ( ) => {
4067
+ Parse . Cloud . afterSave ( Parse . Config , ( ) => {
4068
+ throw new Parse . Error ( 400 , 'It should fail' ) ;
4069
+ } ) ;
4070
+ try {
4071
+ await testConfig ( ) ;
4072
+ fail ( 'error should have thrown' ) ;
4073
+ } catch ( e ) {
4074
+ expect ( e . code ) . toBe ( 400 ) ;
4075
+ expect ( e . message ) . toBe ( 'It should fail' ) ;
4076
+ }
4077
+ } ) ;
4078
+ } ) ;
4079
+
3924
4080
describe ( 'sendEmail' , ( ) => {
3925
4081
it ( 'can send email via Parse.Cloud' , async done => {
3926
4082
const emailAdapter = {
0 commit comments