-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement one-time-salt use and add comprehensive tests
- Loading branch information
Showing
4 changed files
with
255 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
app/src/test/java/org/apache/roller/weblogger/ui/core/filters/LoadSaltFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package org.apache.roller.weblogger.ui.core.filters; | ||
|
||
import org.apache.roller.weblogger.pojos.User; | ||
import org.apache.roller.weblogger.ui.core.RollerSession; | ||
import org.apache.roller.weblogger.ui.rendering.util.cache.SaltCache; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
public class LoadSaltFilterTest { | ||
|
||
private LoadSaltFilter filter; | ||
|
||
@Mock | ||
private HttpServletRequest request; | ||
|
||
@Mock | ||
private HttpServletResponse response; | ||
|
||
@Mock | ||
private FilterChain chain; | ||
|
||
@Mock | ||
private RollerSession rollerSession; | ||
|
||
@Mock | ||
private SaltCache saltCache; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
filter = new LoadSaltFilter(); | ||
} | ||
|
||
@Test | ||
public void testDoFilterGeneratesSalt() throws Exception { | ||
try (MockedStatic<RollerSession> mockedRollerSession = mockStatic(RollerSession.class); | ||
MockedStatic<SaltCache> mockedSaltCache = mockStatic(SaltCache.class)) { | ||
|
||
mockedRollerSession.when(() -> RollerSession.getRollerSession(request)).thenReturn(rollerSession); | ||
mockedSaltCache.when(SaltCache::getInstance).thenReturn(saltCache); | ||
|
||
when(rollerSession.getAuthenticatedUser()).thenReturn(new TestUser("userId")); | ||
|
||
filter.doFilter(request, response, chain); | ||
|
||
verify(request).setAttribute(eq("salt"), anyString()); | ||
verify(saltCache).put(anyString(), eq("userId")); | ||
verify(chain).doFilter(request, response); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDoFilterWithNullRollerSession() throws Exception { | ||
try (MockedStatic<RollerSession> mockedRollerSession = mockStatic(RollerSession.class); | ||
MockedStatic<SaltCache> mockedSaltCache = mockStatic(SaltCache.class)) { | ||
|
||
mockedRollerSession.when(() -> RollerSession.getRollerSession(request)).thenReturn(null); | ||
mockedSaltCache.when(SaltCache::getInstance).thenReturn(saltCache); | ||
|
||
filter.doFilter(request, response, chain); | ||
|
||
verify(request, never()).setAttribute(eq("salt"), anyString()); | ||
verify(saltCache, never()).put(anyString(), anyString()); | ||
verify(chain).doFilter(request, response); | ||
} | ||
} | ||
|
||
private static class TestUser extends User { | ||
private final String id; | ||
|
||
TestUser(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
} | ||
} |
144 changes: 144 additions & 0 deletions
144
app/src/test/java/org/apache/roller/weblogger/ui/core/filters/ValidateSaltFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package org.apache.roller.weblogger.ui.core.filters; | ||
|
||
import org.apache.roller.weblogger.pojos.User; | ||
import org.apache.roller.weblogger.ui.core.RollerSession; | ||
import org.apache.roller.weblogger.ui.rendering.util.cache.SaltCache; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class ValidateSaltFilterTest { | ||
|
||
private ValidateSaltFilter filter; | ||
|
||
@Mock | ||
private HttpServletRequest request; | ||
|
||
@Mock | ||
private HttpServletResponse response; | ||
|
||
@Mock | ||
private FilterChain chain; | ||
|
||
@Mock | ||
private RollerSession rollerSession; | ||
|
||
@Mock | ||
private SaltCache saltCache; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
filter = new ValidateSaltFilter(); | ||
} | ||
|
||
@Test | ||
public void testDoFilterWithGetMethod() throws Exception { | ||
when(request.getMethod()).thenReturn("GET"); | ||
|
||
filter.doFilter(request, response, chain); | ||
|
||
verify(chain).doFilter(request, response); | ||
} | ||
|
||
@Test | ||
public void testDoFilterWithPostMethodAndValidSalt() throws Exception { | ||
try (MockedStatic<RollerSession> mockedRollerSession = mockStatic(RollerSession.class); | ||
MockedStatic<SaltCache> mockedSaltCache = mockStatic(SaltCache.class)) { | ||
|
||
mockedRollerSession.when(() -> RollerSession.getRollerSession(request)).thenReturn(rollerSession); | ||
mockedSaltCache.when(SaltCache::getInstance).thenReturn(saltCache); | ||
|
||
when(request.getMethod()).thenReturn("POST"); | ||
when(request.getServletPath()).thenReturn("/someurl"); | ||
when(request.getParameter("salt")).thenReturn("validSalt"); | ||
when(saltCache.get("validSalt")).thenReturn("userId"); | ||
when(rollerSession.getAuthenticatedUser()).thenReturn(new TestUser("userId")); | ||
|
||
filter.doFilter(request, response, chain); | ||
|
||
verify(chain).doFilter(request, response); | ||
verify(saltCache).remove("validSalt"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDoFilterWithPostMethodAndInvalidSalt() throws Exception { | ||
try (MockedStatic<RollerSession> mockedRollerSession = mockStatic(RollerSession.class); | ||
MockedStatic<SaltCache> mockedSaltCache = mockStatic(SaltCache.class)) { | ||
|
||
mockedRollerSession.when(() -> RollerSession.getRollerSession(request)).thenReturn(rollerSession); | ||
mockedSaltCache.when(SaltCache::getInstance).thenReturn(saltCache); | ||
|
||
when(request.getMethod()).thenReturn("POST"); | ||
when(request.getServletPath()).thenReturn("/someurl"); | ||
when(request.getParameter("salt")).thenReturn("invalidSalt"); | ||
when(saltCache.get("invalidSalt")).thenReturn(null); | ||
|
||
assertThrows(ServletException.class, () -> { | ||
filter.doFilter(request, response, chain); | ||
}); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDoFilterWithPostMethodAndMismatchedUserId() throws Exception { | ||
try (MockedStatic<RollerSession> mockedRollerSession = mockStatic(RollerSession.class); | ||
MockedStatic<SaltCache> mockedSaltCache = mockStatic(SaltCache.class)) { | ||
|
||
mockedRollerSession.when(() -> RollerSession.getRollerSession(request)).thenReturn(rollerSession); | ||
mockedSaltCache.when(SaltCache::getInstance).thenReturn(saltCache); | ||
|
||
when(request.getMethod()).thenReturn("POST"); | ||
when(request.getServletPath()).thenReturn("/someurl"); | ||
when(request.getParameter("salt")).thenReturn("validSalt"); | ||
when(saltCache.get("validSalt")).thenReturn("differentUserId"); | ||
when(rollerSession.getAuthenticatedUser()).thenReturn(new TestUser("userId")); | ||
|
||
assertThrows(ServletException.class, () -> { | ||
filter.doFilter(request, response, chain); | ||
}); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDoFilterWithPostMethodAndNullRollerSession() throws Exception { | ||
try (MockedStatic<RollerSession> mockedRollerSession = mockStatic(RollerSession.class); | ||
MockedStatic<SaltCache> mockedSaltCache = mockStatic(SaltCache.class)) { | ||
|
||
mockedRollerSession.when(() -> RollerSession.getRollerSession(request)).thenReturn(null); | ||
mockedSaltCache.when(SaltCache::getInstance).thenReturn(saltCache); | ||
|
||
when(request.getMethod()).thenReturn("POST"); | ||
when(request.getServletPath()).thenReturn("/someurl"); | ||
when(request.getParameter("salt")).thenReturn("validSalt"); | ||
when(saltCache.get("validSalt")).thenReturn(""); | ||
|
||
filter.doFilter(request, response, chain); | ||
|
||
verify(saltCache, never()).remove("validSalt"); | ||
} | ||
} | ||
private static class TestUser extends User { | ||
private final String id; | ||
|
||
TestUser(String id) { | ||
this.id = id; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return id; | ||
} | ||
} | ||
} |