juanpablo-santos commented on code in PR #377: URL: https://github.com/apache/jspwiki/pull/377#discussion_r1956556473
########## jspwiki-main/src/main/java/org/apache/wiki/auth/DefaultUserManager.java: ########## @@ -64,6 +64,7 @@ Licensed to the Apache Software Foundation (ASF) under one import java.security.Principal; import java.text.MessageFormat; import java.util.List; +import java.util.Locale; Review Comment: needed? ########## jspwiki-main/src/test/java/org/apache/wiki/auth/DefaultUserManagerTest.java: ########## @@ -0,0 +1,795 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ + +package org.apache.wiki.auth; + +import org.apache.wiki.TestEngine; +import org.apache.wiki.api.core.Command; +import org.apache.wiki.api.core.Context; +import org.apache.wiki.api.core.Engine; +import org.apache.wiki.api.core.Page; +import org.apache.wiki.api.core.Session; +import org.apache.wiki.api.exceptions.WikiException; +import org.apache.wiki.auth.user.UserProfile; +import org.apache.wiki.event.WikiEvent; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import javax.security.auth.Subject; +import javax.servlet.AsyncContext; +import javax.servlet.DispatcherType; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletInputStream; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import javax.servlet.http.HttpSessionContext; +import javax.servlet.http.HttpUpgradeHandler; +import javax.servlet.http.Part; +import java.io.BufferedReader; +import java.io.IOException; +import java.security.Permission; +import java.security.Principal; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +class DefaultUserManagerTrimTest { + + private Engine engine; + private DefaultUserManager userManager; + + @BeforeEach + public void setUp() throws Exception { + // Initialize a minimal test engine. + engine = new TestEngine(); + // Get the UserManager (which should be an instance of DefaultUserManager). + userManager = ( DefaultUserManager ) engine.getManager( UserManager.class ); + } + + @Test + public void testParseProfileTrimsFields() throws WikiException { + // Create a dummy HttpServletRequest that returns parameters with extra whitespace. + HttpServletRequest request = new DummyHttpServletRequest(); + // Create a dummy Context that returns our dummy request and a dummy Session. + Context dummyContext = new DummyContext( engine, request ); + + // Call parseProfile, which should trim the input values. + UserProfile profile = userManager.parseProfile( dummyContext ); + + // Verify that the login name, fullname, and email have been trimmed. + Assertions.assertEquals( "admin", profile.getLoginName(), "Login name should be trimmed" ); + Assertions.assertEquals( "Administrator", profile.getFullname(), "Full name should be trimmed" ); + Assertions.assertEquals( "ad...@example.com", profile.getEmail(), "Email should be trimmed" ); + } + + // --- Minimal dummy implementations --- + + // A very basic HttpServletRequest implementation supporting getParameter. + private static class DummyHttpServletRequest implements HttpServletRequest { Review Comment: Both `DummyHttpServletRequest` and `DummyContext` should better be mocked using mockito ########## jspwiki-main/src/test/java/org/apache/wiki/auth/DefaultUserManagerTest.java: ########## @@ -0,0 +1,795 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ + +package org.apache.wiki.auth; + +import org.apache.wiki.TestEngine; +import org.apache.wiki.api.core.Command; +import org.apache.wiki.api.core.Context; +import org.apache.wiki.api.core.Engine; +import org.apache.wiki.api.core.Page; +import org.apache.wiki.api.core.Session; +import org.apache.wiki.api.exceptions.WikiException; +import org.apache.wiki.auth.user.UserProfile; +import org.apache.wiki.event.WikiEvent; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import javax.security.auth.Subject; +import javax.servlet.AsyncContext; +import javax.servlet.DispatcherType; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletInputStream; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import javax.servlet.http.HttpSessionContext; +import javax.servlet.http.HttpUpgradeHandler; +import javax.servlet.http.Part; +import java.io.BufferedReader; +import java.io.IOException; +import java.security.Permission; +import java.security.Principal; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +class DefaultUserManagerTrimTest { + + private Engine engine; + private DefaultUserManager userManager; + + @BeforeEach + public void setUp() throws Exception { + // Initialize a minimal test engine. + engine = new TestEngine(); Review Comment: may be static fields, instantiated with something like: ``` private static TestEngine engine = TestEngine.build(); private static UserManager userManager = engine.getManager( DefaultUserManager.class ); ``` so if not needed, we avoid instantiating them if/when adding new tests here ########## jspwiki-main/src/main/java/org/apache/wiki/auth/DefaultUserManager.java: ########## @@ -292,10 +293,10 @@ public UserProfile parseProfile( final Context context ) { String password = request.getParameter( PARAM_PASSWORD ); String fullname = request.getParameter( PARAM_FULLNAME ); String email = request.getParameter( PARAM_EMAIL ); - loginName = InputValidator.isBlank( loginName ) ? null : loginName; + loginName = InputValidator.isBlank( loginName ) ? null : loginName.trim(); Review Comment: `InputValidator.isBlank( loginName ) ? null : loginName.trim();` can be reduced to (commons-lang3)`StringUtils.trim( loginName )`, and same for the other two fields. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@jspwiki.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org