|
| 1 | +// SPDX-FileCopyrightText: 2026 Sequent Tech <legal@sequentech.io> |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 4 | +package sequent.keycloak.voter_enrollment; |
| 5 | + |
| 6 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 8 | +import static org.mockito.ArgumentMatchers.eq; |
| 9 | +import static org.mockito.Mockito.mock; |
| 10 | +import static org.mockito.Mockito.never; |
| 11 | +import static org.mockito.Mockito.verify; |
| 12 | +import static org.mockito.Mockito.when; |
| 13 | + |
| 14 | +import jakarta.ws.rs.core.MultivaluedMap; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Set; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.keycloak.authentication.FormContext; |
| 21 | +import org.keycloak.forms.login.LoginFormsProvider; |
| 22 | +import org.keycloak.http.HttpRequest; |
| 23 | +import org.keycloak.models.KeycloakSession; |
| 24 | +import org.keycloak.protocol.oidc.endpoints.AuthorizationEndpoint; |
| 25 | +import org.keycloak.sessions.AuthenticationSessionModel; |
| 26 | +import org.keycloak.userprofile.AttributeMetadata; |
| 27 | +import org.keycloak.userprofile.Attributes; |
| 28 | +import org.keycloak.userprofile.UserProfile; |
| 29 | +import org.keycloak.userprofile.UserProfileContext; |
| 30 | +import org.keycloak.userprofile.UserProfileProvider; |
| 31 | +import org.mockito.ArgumentMatchers; |
| 32 | + |
| 33 | +class LoginHintPrefillTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + void extractsOnlyBoundedNamespacedClientNotes() { |
| 37 | + Map<String, String> clientNotes = |
| 38 | + Map.of( |
| 39 | + clientNote("username"), |
| 40 | + "voter@example.com", |
| 41 | + clientNote("dateOfBirth"), |
| 42 | + "2000-01-01", |
| 43 | + "state", |
| 44 | + "oidc-state"); |
| 45 | + |
| 46 | + assertEquals( |
| 47 | + Map.of("username", "voter@example.com", "dateOfBirth", "2000-01-01"), |
| 48 | + LoginHintPrefill.extractHints(clientNotes)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void rejectsAnInvalidHintSetWithoutReturningPartialData() { |
| 53 | + Map<String, String> tooManyHints = new HashMap<>(); |
| 54 | + for (int index = 0; index <= LoginHintPrefill.MAX_HINT_COUNT; index++) { |
| 55 | + tooManyHints.put(clientNote("field" + index), "value" + index); |
| 56 | + } |
| 57 | + |
| 58 | + assertThrows(IllegalArgumentException.class, () -> LoginHintPrefill.extractHints(tooManyHints)); |
| 59 | + assertThrows( |
| 60 | + IllegalArgumentException.class, |
| 61 | + () -> LoginHintPrefill.extractHints(Map.of(clientNote("first name"), "value"))); |
| 62 | + assertThrows( |
| 63 | + IllegalArgumentException.class, |
| 64 | + () -> LoginHintPrefill.extractHints(Map.of(clientNote("username"), " "))); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void keepsOnlyExplicitWritableManagedAttributes() { |
| 69 | + Attributes attributes = mock(Attributes.class); |
| 70 | + AttributeMetadata metadata = mock(AttributeMetadata.class); |
| 71 | + when(attributes.getWritable()) |
| 72 | + .thenReturn( |
| 73 | + Map.of( |
| 74 | + "username", List.of("voter@example.com"), |
| 75 | + "dateOfBirth", List.of("2000-01-01"), |
| 76 | + "unmanaged", List.of("value"))); |
| 77 | + when(attributes.getUnmanagedAttributes()).thenReturn(Map.of("unmanaged", List.of("value"))); |
| 78 | + when(attributes.getMetadata("username")).thenReturn(metadata); |
| 79 | + when(attributes.getMetadata("dateOfBirth")).thenReturn(metadata); |
| 80 | + |
| 81 | + MultivaluedMap<String, String> result = |
| 82 | + LoginHintPrefill.filterWritableHints( |
| 83 | + Map.of( |
| 84 | + "username", "voter@example.com", |
| 85 | + "dateOfBirth", "2000-01-01", |
| 86 | + "verificationStatus", "VERIFIED", |
| 87 | + "unmanaged", "value", |
| 88 | + "password", "secret"), |
| 89 | + attributes, |
| 90 | + Set.of("verificationStatus")); |
| 91 | + |
| 92 | + assertEquals( |
| 93 | + Map.of("username", List.of("voter@example.com"), "dateOfBirth", List.of("2000-01-01")), |
| 94 | + result); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void stockActionPrefillsOnlyInitialGetRender() { |
| 99 | + FormContext context = mock(FormContext.class); |
| 100 | + LoginFormsProvider form = mock(LoginFormsProvider.class); |
| 101 | + HttpRequest request = mock(HttpRequest.class); |
| 102 | + AuthenticationSessionModel authenticationSession = mock(AuthenticationSessionModel.class); |
| 103 | + KeycloakSession session = mock(KeycloakSession.class); |
| 104 | + UserProfileProvider profileProvider = mock(UserProfileProvider.class); |
| 105 | + UserProfile profile = mock(UserProfile.class); |
| 106 | + Attributes attributes = mock(Attributes.class); |
| 107 | + AttributeMetadata metadata = mock(AttributeMetadata.class); |
| 108 | + |
| 109 | + when(context.getHttpRequest()).thenReturn(request); |
| 110 | + when(request.getHttpMethod()).thenReturn("GET"); |
| 111 | + when(context.getAuthenticationSession()).thenReturn(authenticationSession); |
| 112 | + when(authenticationSession.getClientNotes()) |
| 113 | + .thenReturn(Map.of(clientNote("username"), "voter@example.com")); |
| 114 | + when(context.getSession()).thenReturn(session); |
| 115 | + when(session.getProvider(UserProfileProvider.class)).thenReturn(profileProvider); |
| 116 | + when(profileProvider.create( |
| 117 | + eq(UserProfileContext.REGISTRATION), ArgumentMatchers.<Map<String, ?>>any())) |
| 118 | + .thenReturn(profile); |
| 119 | + when(profile.getAttributes()).thenReturn(attributes); |
| 120 | + when(attributes.getWritable()).thenReturn(Map.of("username", List.of("voter@example.com"))); |
| 121 | + when(attributes.getUnmanagedAttributes()).thenReturn(Map.of()); |
| 122 | + when(attributes.getMetadata("username")).thenReturn(metadata); |
| 123 | + |
| 124 | + new LoginHintRegistrationPrefill().buildPage(context, form); |
| 125 | + |
| 126 | + verify(form).setFormData(ArgumentMatchers.<MultivaluedMap<String, String>>any()); |
| 127 | + |
| 128 | + when(request.getHttpMethod()).thenReturn("POST"); |
| 129 | + new LoginHintRegistrationPrefill().buildPage(context, form); |
| 130 | + |
| 131 | + verify(form).setFormData(ArgumentMatchers.<MultivaluedMap<String, String>>any()); |
| 132 | + verify(profileProvider, never()) |
| 133 | + .create(eq(UserProfileContext.ACCOUNT), ArgumentMatchers.<Map<String, ?>>any()); |
| 134 | + } |
| 135 | + |
| 136 | + private static String clientNote(String attributeName) { |
| 137 | + return AuthorizationEndpoint.LOGIN_SESSION_NOTE_ADDITIONAL_REQ_PARAMS_PREFIX |
| 138 | + + LoginHintPrefill.HINT_PREFIX |
| 139 | + + attributeName; |
| 140 | + } |
| 141 | +} |
0 commit comments