|
| 1 | +/* |
| 2 | + * Copyright (c) 2004-2026, University of Oslo |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * 3. Neither the name of the copyright holder nor the names of its contributors |
| 16 | + * may be used to endorse or promote products derived from this software without |
| 17 | + * specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 23 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 26 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +package org.hisp.dhis.webapi.filter; |
| 31 | + |
| 32 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 | +import static org.mockito.Mockito.never; |
| 34 | +import static org.mockito.Mockito.verify; |
| 35 | + |
| 36 | +import jakarta.servlet.FilterChain; |
| 37 | +import jakarta.servlet.http.HttpServletResponse; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 40 | +import org.mockito.Mock; |
| 41 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 42 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 43 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 44 | + |
| 45 | +/** |
| 46 | + * Unit tests for {@link LegacyDhisWebLoginRedirectFilter}. |
| 47 | + * |
| 48 | + * @author Morten Svanæs |
| 49 | + */ |
| 50 | +@ExtendWith(MockitoExtension.class) |
| 51 | +class LegacyDhisWebLoginRedirectFilterTest { |
| 52 | + |
| 53 | + private final LegacyDhisWebLoginRedirectFilter filter = new LegacyDhisWebLoginRedirectFilter(); |
| 54 | + |
| 55 | + @Mock private FilterChain chain; |
| 56 | + |
| 57 | + @Test |
| 58 | + void redirectsBareLegacyPath() throws Exception { |
| 59 | + MockHttpServletResponse response = doFilter("/dhis-web-login", null, ""); |
| 60 | + assertRedirect(response, "/login"); |
| 61 | + verify(chain, never()) |
| 62 | + .doFilter(org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.any()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void redirectsLegacyRoot() throws Exception { |
| 67 | + MockHttpServletResponse response = doFilter("/dhis-web-login/", null, ""); |
| 68 | + assertRedirect(response, "/login/"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void redirectsIndexHtmlPreservingHash() throws Exception { |
| 73 | + // The hash fragment is client-side and never sent to the server, so the |
| 74 | + // filter only needs to redirect /index.html — the browser keeps the hash. |
| 75 | + MockHttpServletResponse response = doFilter("/dhis-web-login/index.html", null, ""); |
| 76 | + assertRedirect(response, "/login/index.html"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void redirectsNestedPath() throws Exception { |
| 81 | + MockHttpServletResponse response = doFilter("/dhis-web-login/static/main.js", null, ""); |
| 82 | + assertRedirect(response, "/login/static/main.js"); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void preservesQueryString() throws Exception { |
| 87 | + MockHttpServletResponse response = doFilter("/dhis-web-login/", "oidcFailure=true", ""); |
| 88 | + assertRedirect(response, "/login/?oidcFailure=true"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void preservesContextPath() throws Exception { |
| 93 | + MockHttpServletResponse response = doFilter("/dhis/dhis-web-login/index.html", null, "/dhis"); |
| 94 | + assertRedirect(response, "/dhis/login/index.html"); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void doesNotRedirectFalsePositivePrefix() throws Exception { |
| 99 | + // /dhis-web-loginabc starts with /dhis-web-login but isn't the legacy login app. |
| 100 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/dhis-web-loginabc"); |
| 101 | + request.setRequestURI("/dhis-web-loginabc"); |
| 102 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 103 | + filter.doFilter(request, response, chain); |
| 104 | + assertEquals(HttpServletResponse.SC_OK, response.getStatus()); |
| 105 | + verify(chain).doFilter(request, response); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void doesNotRedirectUnrelatedPath() throws Exception { |
| 110 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/api/me"); |
| 111 | + request.setRequestURI("/api/me"); |
| 112 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 113 | + filter.doFilter(request, response, chain); |
| 114 | + assertEquals(HttpServletResponse.SC_OK, response.getStatus()); |
| 115 | + verify(chain).doFilter(request, response); |
| 116 | + } |
| 117 | + |
| 118 | + private MockHttpServletResponse doFilter( |
| 119 | + String requestUri, String queryString, String contextPath) throws Exception { |
| 120 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri); |
| 121 | + request.setRequestURI(requestUri); |
| 122 | + request.setContextPath(contextPath); |
| 123 | + if (queryString != null) { |
| 124 | + request.setQueryString(queryString); |
| 125 | + } |
| 126 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 127 | + filter.doFilter(request, response, chain); |
| 128 | + return response; |
| 129 | + } |
| 130 | + |
| 131 | + private static void assertRedirect(MockHttpServletResponse response, String expectedLocation) { |
| 132 | + assertEquals(HttpServletResponse.SC_MOVED_PERMANENTLY, response.getStatus()); |
| 133 | + assertEquals(expectedLocation, response.getHeader("Location")); |
| 134 | + } |
| 135 | +} |
0 commit comments