UserCommandHandlerTest.groovy revision 7d4a3a2990f1a386eaf9d50cf05a9d45fab32de6
1/* 2 * Copyright 2008 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package org.mockftpserver.fake.command 17 18import org.mockftpserver.test.AbstractGroovyTest 19import org.mockftpserver.core.command.Command 20import org.mockftpserver.core.command.CommandHandler 21import org.mockftpserver.core.command.CommandNames 22import org.mockftpserver.core.session.StubSession 23import org.mockftpserver.core.session.SessionKeys 24import org.mockftpserver.fake.StubServerConfiguration 25import org.mockftpserver.fake.user.UserAccount 26import org.apache.log4j.Logger 27import org.mockftpserver.core.command.ReplyCodes 28 29/** 30 * Tests for UserCommandHandler 31 * 32 * @version $Revision$ - $Date$ 33 * 34 * @author Chris Mair 35 */ 36class UserCommandHandlerTest extends AbstractFakeCommandHandlerTest { 37 38 def USERNAME = "user123" 39 40 void testHandleCommand_UserExists() { 41 serverConfiguration.userAccounts[USERNAME] = new UserAccount() 42 commandHandler.handleCommand(createCommand([USERNAME]), session) 43 assertSessionReply(ReplyCodes.USER_NEED_PASSWORD_OK) 44 assertUsernameInSession(true) 45 } 46 47 void testHandleCommand_NoSuchUser() { 48 commandHandler.handleCommand(createCommand([USERNAME]), session) 49 // Will return OK, even if username is not recognized 50 assertSessionReply(ReplyCodes.USER_NEED_PASSWORD_OK) 51 assertUsernameInSession(true) 52 } 53 54 void testHandleCommand_PasswordNotRequiredForLogin() { 55 def userAccount = new UserAccount(passwordRequiredForLogin:false) 56 serverConfiguration.userAccounts[USERNAME] = userAccount 57 58 commandHandler.handleCommand(createCommand([USERNAME]), session) 59 assertSessionReply(ReplyCodes.USER_LOGGED_IN_OK) 60 assert session.getAttribute(SessionKeys.USER_ACCOUNT) == userAccount 61 assertUsernameInSession(false) 62 } 63 64 void testHandleCommand_MissingUsernameParameter() { 65 testHandleCommand_MissingRequiredParameter([]) 66 assertUsernameInSession(false) 67 } 68 69 void testHandleCommand_EmptyUsernameParameter() { 70 testHandleCommand_MissingRequiredParameter([""]) 71 assertUsernameInSession(false) 72 } 73 74 //------------------------------------------------------------------------- 75 // Helper Methods 76 //------------------------------------------------------------------------- 77 78 CommandHandler createCommandHandler() { 79 new UserCommandHandler() 80 } 81 82 Command createValidCommand() { 83 return new Command(CommandNames.USER, [USERNAME]) 84 } 85 86 /** 87 * Assert that the Username is stored in the session, depending on the value of isUsernameInSession. 88 * @param isUsernameInSession - true if the Username is expected in the session; false if it is not expected 89 */ 90 private void assertUsernameInSession(boolean isUsernameInSession) { 91 def expectedValue = isUsernameInSession ? USERNAME : null 92 assert session.getAttribute(SessionKeys.USERNAME) == expectedValue 93 } 94}