11import { resetJest , resetRedis , setupResponse } from '../helpers' ;
22import { HttpForwardAuth } from '../../nodes/HttpForwardAuth/HttpForwardAuth.node' ;
3+ import { FORWARDED_USER_HEADER } from '../../nodes/HttpForwardAuth/constants' ;
34
45jest . mock ( 'redis' , ( ) => ( {
56 __esModule : true ,
@@ -13,10 +14,77 @@ describe('Response Suite', () => {
1314 afterEach ( resetRedis ) ;
1415
1516 it ( 'Should fail if no HttpForwardAuthTrigger is found on workflow' , async ( ) => {
16- const { context } = setupResponse ( ) ;
17+ const { context, mocks } = setupResponse ( { parentNodes : [ ] } ) ;
1718 const node = new HttpForwardAuth ( ) ;
1819
19- const bindExecute = node . execute . bind ( context ) ;
20- expect ( ( ) => bindExecute ( ) ) . rejects . toThrow ( 'No HttpForwardAuthTrigger node found in the workflow' )
20+ try {
21+ await node . execute . bind ( context ) ( ) ;
22+ } catch ( error ) {
23+ expect ( error . message ) . toBe ( 'No HttpForwardAuthTrigger node found in the workflow' ) ;
24+ expect ( mocks . sendResponseMock ) . toHaveBeenCalledWith ( {
25+ body : '500 Internal Server Error' ,
26+ __bodyResolved : true ,
27+ headers : {
28+ [ FORWARDED_USER_HEADER ] : '' ,
29+ } ,
30+ statusCode : 500 ,
31+ } ) ;
32+ }
33+ expect . assertions ( 2 ) ;
2134 } ) ;
22- } )
35+
36+ it ( 'Should not fail if continueOnFail is true' , async ( ) => {
37+ const { context, mocks } = setupResponse ( { parentNodes : [ ] , continueOnFail : true } ) ;
38+ const node = new HttpForwardAuth ( ) ;
39+
40+ expect ( await node . execute . bind ( context ) ( ) ) . toStrictEqual ( [
41+ [ { json : { error : 'No HttpForwardAuthTrigger node found in the workflow' } } ] ,
42+ ] ) ;
43+ expect ( mocks . sendResponseMock ) . toHaveBeenCalledWith ( {
44+ body : '500 Internal Server Error' ,
45+ __bodyResolved : true ,
46+ headers : {
47+ [ FORWARDED_USER_HEADER ] : '' ,
48+ } ,
49+ statusCode : 500 ,
50+ } ) ;
51+ } ) ;
52+
53+ it ( 'Should authenticate user successfully' , async ( ) => {
54+ const { context, mocks } = setupResponse ( { params : { userID : 'user' } } ) ;
55+ const node = new HttpForwardAuth ( ) ;
56+
57+ expect ( await node . execute . bind ( context ) ( ) ) . toStrictEqual ( [
58+ [ { json : { remoteIp : 'REMOTE_IP' , status : 'success' , user : 'user' } } ] ,
59+ ] ) ;
60+ expect ( mocks . sendResponseMock ) . toHaveBeenCalledWith ( {
61+ __bodyResolved : true ,
62+ body : undefined ,
63+ headers : {
64+ 'Set-Cookie' : expect . stringMatching (
65+ / n 8 n _ h f a _ s e s s i o n = [ A - Z a - z 0 - 9 ] { 32 } ; H t t p O n l y ; S a m e S i t e = L a x ; E x p i r e s = M o n , \d { 1 , 2 } \w { 3 } \d { 4 } \d { 2 } : \d { 2 } : \d { 2 } G M T ; P a t h = \/ ; S e c u r e / g,
66+ ) ,
67+ location : 'http://localhost:8080/protected' ,
68+ } ,
69+ statusCode : 302 ,
70+ } ) ;
71+ } ) ;
72+
73+ it ( 'Should fail to authenticate the user' , async ( ) => {
74+ const { context, mocks } = setupResponse ( ) ;
75+ const node = new HttpForwardAuth ( ) ;
76+
77+ expect ( await node . execute . bind ( context ) ( ) ) . toStrictEqual ( [
78+ [ { json : { remoteIp : 'REMOTE_IP' , status : 'fail' } } ] ,
79+ ] ) ;
80+ expect ( mocks . sendResponseMock ) . toHaveBeenCalledWith ( {
81+ __bodyResolved : true ,
82+ body : 'http://localhost:8080/login|Validation Error' ,
83+ headers : {
84+ 'X-Forwarded-User' : '' ,
85+ 'content-type' : 'text/html' ,
86+ } ,
87+ statusCode : 401 ,
88+ } ) ;
89+ } ) ;
90+ } ) ;
0 commit comments