Skip to content

Commit 887bb4d

Browse files
committed
Finish Test Suites
1 parent a2ad514 commit 887bb4d

File tree

3 files changed

+88
-8
lines changed

3 files changed

+88
-8
lines changed

nodes/HttpForwardAuth/HttpForwardAuth.node.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ export class HttpForwardAuth implements INodeType {
107107
};
108108
this.sendResponse(response);
109109
} catch (error) {
110+
this.sendResponse({
111+
body: '500 Internal Server Error',
112+
__bodyResolved: true,
113+
headers: {
114+
[FORWARDED_USER_HEADER]: '',
115+
},
116+
statusCode: 500,
117+
});
118+
110119
if (this.continueOnFail()) {
111120
returnData = [{ json: { error: (error as { message: string }).message } }];
112121
return [returnData];

test/helpers.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type ResponseOpts = {
4242
data?: Record<string, unknown>,
4343
params?: TriggerOpts['params']
4444
}
45+
continueOnFail: boolean;
4546
};
4647

4748
export const setupRedis = (mockOverrides?: Record<string, unknown>) => {
@@ -183,7 +184,8 @@ const defaultResponseOpts: ResponseOpts = {
183184
loginTemplate: '#LOGIN_URL#|#ERROR_MESSAGE#'
184185
},
185186
data: { remoteIp: 'REMOTE_IP' }
186-
}
187+
},
188+
continueOnFail: false
187189
};
188190

189191
export const setupResponse = (opts?: Partial<ResponseOpts>) => {
@@ -194,7 +196,7 @@ export const setupResponse = (opts?: Partial<ResponseOpts>) => {
194196
...defaultResponseOpts.params,
195197
...(opts?.params ?? {}),
196198
},
197-
parentNodes: [...(opts?.parentNodes ?? [])],
199+
parentNodes: [...(opts?.parentNodes ?? defaultResponseOpts.parentNodes)],
198200
triggerData: {
199201
params: {
200202
...defaultResponseOpts.triggerData.params,
@@ -204,7 +206,8 @@ export const setupResponse = (opts?: Partial<ResponseOpts>) => {
204206
...defaultResponseOpts.triggerData.data,
205207
...(opts?.triggerData?.data ?? {})
206208
}
207-
}
209+
},
210+
continueOnFail: opts?.continueOnFail ?? defaultResponseOpts.continueOnFail
208211
};
209212

210213
const mocks = {
@@ -227,7 +230,7 @@ export const setupResponse = (opts?: Partial<ResponseOpts>) => {
227230
}
228231
}),
229232
sendResponse: sendResponseMock,
230-
continueOnFail: () => false,
233+
continueOnFail: () => store.continueOnFail,
231234
});
232235

233236
return { context, mocks };

test/nodes/response.test.ts

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { resetJest, resetRedis, setupResponse } from '../helpers';
22
import { HttpForwardAuth } from '../../nodes/HttpForwardAuth/HttpForwardAuth.node';
3+
import { FORWARDED_USER_HEADER } from '../../nodes/HttpForwardAuth/constants';
34

45
jest.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+
/n8n_hfa_session=[A-Za-z0-9]{32}; HttpOnly; SameSite=Lax; Expires=Mon, \d{1,2} \w{3} \d{4} \d{2}:\d{2}:\d{2} GMT; Path=\/; Secure/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

Comments
 (0)