@@ -2269,21 +2269,43 @@ describe('afterFind hooks', () => {
2269
2269
expect ( ( ) => {
2270
2270
Parse . Cloud . beforeLogin ( ( ) => { } ) ;
2271
2271
} ) . not . toThrow (
2272
- 'Only the _User class is allowed for the beforeLogin trigger '
2272
+ 'Only the _User class is allowed for the beforeLogin and afterLogin triggers '
2273
2273
) ;
2274
2274
expect ( ( ) => {
2275
2275
Parse . Cloud . beforeLogin ( '_User' , ( ) => { } ) ;
2276
2276
} ) . not . toThrow (
2277
- 'Only the _User class is allowed for the beforeLogin trigger '
2277
+ 'Only the _User class is allowed for the beforeLogin and afterLogin triggers '
2278
2278
) ;
2279
2279
expect ( ( ) => {
2280
2280
Parse . Cloud . beforeLogin ( Parse . User , ( ) => { } ) ;
2281
2281
} ) . not . toThrow (
2282
- 'Only the _User class is allowed for the beforeLogin trigger '
2282
+ 'Only the _User class is allowed for the beforeLogin and afterLogin triggers '
2283
2283
) ;
2284
2284
expect ( ( ) => {
2285
2285
Parse . Cloud . beforeLogin ( 'SomeClass' , ( ) => { } ) ;
2286
- } ) . toThrow ( 'Only the _User class is allowed for the beforeLogin trigger' ) ;
2286
+ } ) . toThrow (
2287
+ 'Only the _User class is allowed for the beforeLogin and afterLogin triggers'
2288
+ ) ;
2289
+ expect ( ( ) => {
2290
+ Parse . Cloud . afterLogin ( ( ) => { } ) ;
2291
+ } ) . not . toThrow (
2292
+ 'Only the _User class is allowed for the beforeLogin and afterLogin triggers'
2293
+ ) ;
2294
+ expect ( ( ) => {
2295
+ Parse . Cloud . afterLogin ( '_User' , ( ) => { } ) ;
2296
+ } ) . not . toThrow (
2297
+ 'Only the _User class is allowed for the beforeLogin and afterLogin triggers'
2298
+ ) ;
2299
+ expect ( ( ) => {
2300
+ Parse . Cloud . afterLogin ( Parse . User , ( ) => { } ) ;
2301
+ } ) . not . toThrow (
2302
+ 'Only the _User class is allowed for the beforeLogin and afterLogin triggers'
2303
+ ) ;
2304
+ expect ( ( ) => {
2305
+ Parse . Cloud . afterLogin ( 'SomeClass' , ( ) => { } ) ;
2306
+ } ) . toThrow (
2307
+ 'Only the _User class is allowed for the beforeLogin and afterLogin triggers'
2308
+ ) ;
2287
2309
expect ( ( ) => {
2288
2310
Parse . Cloud . afterLogout ( ( ) => { } ) ;
2289
2311
} ) . not . toThrow ( ) ;
@@ -2574,3 +2596,66 @@ describe('beforeLogin hook', () => {
2574
2596
expect ( afterFinds ) . toEqual ( 1 ) ;
2575
2597
} ) ;
2576
2598
} ) ;
2599
+
2600
+ describe ( 'afterLogin hook' , ( ) => {
2601
+ it ( 'should run afterLogin after successful login' , async done => {
2602
+ let hit = 0 ;
2603
+ Parse . Cloud . afterLogin ( req => {
2604
+ hit ++ ;
2605
+ expect ( req . object . get ( 'username' ) ) . toEqual ( 'testuser' ) ;
2606
+ } ) ;
2607
+
2608
+ await Parse . User . signUp ( 'testuser' , 'p@ssword' ) ;
2609
+ const user = await Parse . User . logIn ( 'testuser' , 'p@ssword' ) ;
2610
+ expect ( hit ) . toBe ( 1 ) ;
2611
+ expect ( user ) . toBeDefined ( ) ;
2612
+ expect ( user . getUsername ( ) ) . toBe ( 'testuser' ) ;
2613
+ expect ( user . getSessionToken ( ) ) . toBeDefined ( ) ;
2614
+ done ( ) ;
2615
+ } ) ;
2616
+
2617
+ it ( 'should not run afterLogin after unsuccessful login' , async done => {
2618
+ let hit = 0 ;
2619
+ Parse . Cloud . afterLogin ( req => {
2620
+ hit ++ ;
2621
+ expect ( req . object . get ( 'username' ) ) . toEqual ( 'testuser' ) ;
2622
+ } ) ;
2623
+
2624
+ await Parse . User . signUp ( 'testuser' , 'p@ssword' ) ;
2625
+ try {
2626
+ await Parse . User . logIn ( 'testuser' , 'badpassword' ) ;
2627
+ } catch ( e ) {
2628
+ expect ( e . code ) . toBe ( Parse . Error . OBJECT_NOT_FOUND ) ;
2629
+ }
2630
+ expect ( hit ) . toBe ( 0 ) ;
2631
+ done ( ) ;
2632
+ } ) ;
2633
+
2634
+ it ( 'should not run afterLogin on sign up' , async done => {
2635
+ let hit = 0 ;
2636
+ Parse . Cloud . afterLogin ( req => {
2637
+ hit ++ ;
2638
+ expect ( req . object . get ( 'username' ) ) . toEqual ( 'testuser' ) ;
2639
+ } ) ;
2640
+
2641
+ const user = await Parse . User . signUp ( 'testuser' , 'p@ssword' ) ;
2642
+ expect ( user ) . toBeDefined ( ) ;
2643
+ expect ( hit ) . toBe ( 0 ) ;
2644
+ done ( ) ;
2645
+ } ) ;
2646
+
2647
+ it ( 'should have expected data in request' , async done => {
2648
+ Parse . Cloud . afterLogin ( req => {
2649
+ expect ( req . object ) . toBeDefined ( ) ;
2650
+ expect ( req . user ) . toBeDefined ( ) ;
2651
+ expect ( req . headers ) . toBeDefined ( ) ;
2652
+ expect ( req . ip ) . toBeDefined ( ) ;
2653
+ expect ( req . installationId ) . toBeDefined ( ) ;
2654
+ expect ( req . context ) . toBeUndefined ( ) ;
2655
+ } ) ;
2656
+
2657
+ await Parse . User . signUp ( 'testuser' , 'p@ssword' ) ;
2658
+ await Parse . User . logIn ( 'testuser' , 'p@ssword' ) ;
2659
+ done ( ) ;
2660
+ } ) ;
2661
+ } ) ;
0 commit comments