|
| 1 | +package org.commcare.formplayer.application; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.mockStatic; |
| 6 | +import static org.mockito.Mockito.when; |
| 7 | + |
| 8 | +import org.commcare.formplayer.beans.auth.HqUserDetailsBean; |
| 9 | +import org.commcare.formplayer.exceptions.FormNotFoundException; |
| 10 | +import org.commcare.formplayer.objects.SerializableFormSession; |
| 11 | +import org.commcare.formplayer.util.RequestUtils; |
| 12 | +import org.commcare.modern.database.TableBuilder; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.mockito.MockedStatic; |
| 15 | + |
| 16 | +import java.util.Optional; |
| 17 | + |
| 18 | +/** |
| 19 | + * Unit tests for {@link FormSessionFactory#verifyPublicSessionOwnership}: a public web apps session |
| 20 | + * may only load the form session its own one-time link created, never another user's by id. |
| 21 | + */ |
| 22 | +public class FormSessionFactoryTest { |
| 23 | + |
| 24 | + private final FormSessionFactory factory = new FormSessionFactory(); |
| 25 | + |
| 26 | + private HqUserDetailsBean publicBean(String username, String domain) { |
| 27 | + HqUserDetailsBean bean = new HqUserDetailsBean(domain, new String[]{domain}, username, |
| 28 | + false, new String[]{}, new String[]{}); |
| 29 | + bean.setPublicSession(true); |
| 30 | + return bean; |
| 31 | + } |
| 32 | + |
| 33 | + private SerializableFormSession session(String username, String domain) { |
| 34 | + SerializableFormSession session = mock(SerializableFormSession.class); |
| 35 | + // Sessions persist the scrubbed username (see FormSession's new-session constructor). |
| 36 | + when(session.getUsername()).thenReturn(TableBuilder.scrubName(username)); |
| 37 | + when(session.getDomain()).thenReturn(domain); |
| 38 | + when(session.getId()).thenReturn("session-id"); |
| 39 | + return session; |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void publicSession_cannotLoadAnotherUsersFormSession() { |
| 44 | + SerializableFormSession victimSession = session("victim@domain", "domain"); |
| 45 | + try (MockedStatic<RequestUtils> mocked = mockStatic(RequestUtils.class)) { |
| 46 | + mocked.when(RequestUtils::getUserDetails) |
| 47 | + .thenReturn(Optional.of(publicBean("public_abc@domain", "domain"))); |
| 48 | + assertThrows(FormNotFoundException.class, |
| 49 | + () -> factory.verifyPublicSessionOwnership(victimSession)); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void publicSession_cannotLoadFormSessionInAnotherDomain() { |
| 55 | + SerializableFormSession otherDomainSession = session("public_abc@other", "other"); |
| 56 | + try (MockedStatic<RequestUtils> mocked = mockStatic(RequestUtils.class)) { |
| 57 | + mocked.when(RequestUtils::getUserDetails) |
| 58 | + .thenReturn(Optional.of(publicBean("public_abc@domain", "domain"))); |
| 59 | + assertThrows(FormNotFoundException.class, |
| 60 | + () -> factory.verifyPublicSessionOwnership(otherDomainSession)); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void publicSession_canLoadItsOwnFormSession() { |
| 66 | + SerializableFormSession ownSession = session("public_abc@domain", "domain"); |
| 67 | + try (MockedStatic<RequestUtils> mocked = mockStatic(RequestUtils.class)) { |
| 68 | + mocked.when(RequestUtils::getUserDetails) |
| 69 | + .thenReturn(Optional.of(publicBean("public_abc@domain", "domain"))); |
| 70 | + // Its own session passes the check (no exception). |
| 71 | + factory.verifyPublicSessionOwnership(ownSession); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void nonPublicSession_ownershipCheckIsSkipped() { |
| 77 | + // A regular session is unaffected: the check only constrains public sessions. |
| 78 | + SerializableFormSession anySession = session("someone-else@domain", "domain"); |
| 79 | + try (MockedStatic<RequestUtils> mocked = mockStatic(RequestUtils.class)) { |
| 80 | + mocked.when(RequestUtils::getUserDetails) |
| 81 | + .thenReturn(Optional.of(new HqUserDetailsBean("domain", "user"))); |
| 82 | + factory.verifyPublicSessionOwnership(anySession); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void noAuthenticatedUser_ownershipCheckIsSkipped() { |
| 88 | + // Non-request / unauthenticated contexts (e.g. purge tasks) must not be blocked. |
| 89 | + SerializableFormSession anySession = session("someone@domain", "domain"); |
| 90 | + try (MockedStatic<RequestUtils> mocked = mockStatic(RequestUtils.class)) { |
| 91 | + mocked.when(RequestUtils::getUserDetails).thenReturn(Optional.empty()); |
| 92 | + factory.verifyPublicSessionOwnership(anySession); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments