|
| 1 | +import { mock } from 'vitest-mock-extended'; |
| 2 | + |
| 3 | +import type { ResWithSafeRedirect } from '~/server/middlewares/safe-redirect'; |
| 4 | + |
| 5 | +import type { ApiV3Response } from './apiv3/interfaces/apiv3-response'; |
| 6 | +import { sendLoginSuccessResponse } from './login-success-response'; |
| 7 | + |
| 8 | +type LoginSuccessResponse = ApiV3Response & ResWithSafeRedirect; |
| 9 | + |
| 10 | +describe('sendLoginSuccessResponse', () => { |
| 11 | + const redirectTo = '/path/to/redirect'; |
| 12 | + |
| 13 | + // Contract: the response TRANSPORT the client experiences (JSON body vs HTTP 302), |
| 14 | + // not which internal helper is called. See issue #11384. |
| 15 | + |
| 16 | + describe('when respondWithRedirect is omitted (AJAX login form: local & LDAP)', () => { |
| 17 | + it('responds with JSON { redirectTo } and never issues a redirect', () => { |
| 18 | + // Arrange |
| 19 | + const res = mock<LoginSuccessResponse>(); |
| 20 | + |
| 21 | + // Act |
| 22 | + sendLoginSuccessResponse(res, redirectTo); |
| 23 | + |
| 24 | + // Assert: the client reads redirectTo from the body and navigates itself. |
| 25 | + // A 302 here would be followed silently by the XHR (the #11384 regression). |
| 26 | + expect(res.apiv3).toHaveBeenCalledWith({ redirectTo }); |
| 27 | + expect(res.safeRedirect).not.toHaveBeenCalled(); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('when respondWithRedirect is false', () => { |
| 32 | + it('responds with JSON { redirectTo } and never issues a redirect', () => { |
| 33 | + const res = mock<LoginSuccessResponse>(); |
| 34 | + |
| 35 | + sendLoginSuccessResponse(res, redirectTo, false); |
| 36 | + |
| 37 | + expect(res.apiv3).toHaveBeenCalledWith({ redirectTo }); |
| 38 | + expect(res.safeRedirect).not.toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('when respondWithRedirect is true (full-page callback: OAuth/SAML)', () => { |
| 43 | + it('responds with an HTTP 302 and never returns a JSON body', () => { |
| 44 | + const res = mock<LoginSuccessResponse>(); |
| 45 | + |
| 46 | + sendLoginSuccessResponse(res, redirectTo, true); |
| 47 | + |
| 48 | + expect(res.safeRedirect).toHaveBeenCalledWith(redirectTo); |
| 49 | + expect(res.apiv3).not.toHaveBeenCalled(); |
| 50 | + }); |
| 51 | + }); |
| 52 | +}); |
0 commit comments