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 PassCommandHandler 31 * 32 * @version $Revision: $ - $Date: $ 33 * 34 * @author Chris Mair 35 */ 36class PassCommandHandlerTest extends AbstractFakeCommandHandlerTest { 37 38 def USERNAME = "user123" 39 def PASSWORD = "password123" 40 def userAccount 41 42 void testHandleCommand_UserExists_PasswordCorrect() { 43 serverConfiguration.userAccounts[USERNAME] = userAccount 44 commandHandler.handleCommand(createCommand([PASSWORD]), session) 45 assertSessionReply(ReplyCodes.PASS_OK) 46 assertUserAccountInSession(true) 47 } 48 49 void testHandleCommand_UserExists_PasswordIncorrect() { 50 serverConfiguration.userAccounts[USERNAME] = userAccount 51 commandHandler.handleCommand(createCommand(["wrong"]), session) 52 assertSessionReply(ReplyCodes.PASS_LOG_IN_FAILED) 53 assertUserAccountInSession(false) 54 } 55 56 void testHandleCommand_UserExists_PasswordWrongButIgnored() { 57 userAccount.passwordCheckedDuringValidation = false 58 serverConfiguration.userAccounts[USERNAME] = userAccount 59 commandHandler.handleCommand(createCommand(["wrong"]), session) 60 assertSessionReply(ReplyCodes.PASS_OK) 61 assertUserAccountInSession(true) 62 } 63 64 void testHandleCommand_UserDoesNotExist() { 65 commandHandler.handleCommand(createCommand([PASSWORD]), session) 66 assertSessionReply(ReplyCodes.PASS_LOG_IN_FAILED) 67 assertUserAccountInSession(false) 68 } 69 70 void testHandleCommand_UsernameNotSetInSession() { 71 session.removeAttribute(SessionKeys.USERNAME) 72 testHandleCommand_MissingRequiredSessionAttribute() 73 assertUserAccountInSession(false) 74 } 75 76 void testHandleCommand_MissingPasswordParameter() { 77 testHandleCommand_MissingRequiredParameter([]) 78 assertUserAccountInSession(false) 79 } 80 81 void testHandleCommand_EmptyPasswordParameter() { 82 testHandleCommand_MissingRequiredParameter([""]) 83 assertUserAccountInSession(false) 84 } 85 86 //------------------------------------------------------------------------- 87 // Helper Methods 88 //------------------------------------------------------------------------- 89 90 void setUp() { 91 super.setUp() 92 93 userAccount = new UserAccount() 94 userAccount.username = USERNAME 95 userAccount.password = PASSWORD 96 97 session.setAttribute(SessionKeys.USERNAME, USERNAME) 98 } 99 100 CommandHandler createCommandHandler() { 101 new PassCommandHandler() 102 } 103 104 Command createValidCommand() { 105 return new Command(CommandNames.PASS, [PASSWORD]) 106 } 107 108 /** 109 * Assert that the UserAccount object is in the session, depending on the value of isUserAccountInSession. 110 * @param isUserAccountInSession - true if the UserAccount is expected in the session; false if it is not expected 111 */ 112 private void assertUserAccountInSession(boolean isUserAccountInSession) { 113 def expectedValue = isUserAccountInSession ? userAccount : null 114 assert session.getAttribute(SessionKeys.USER_ACCOUNT) == expectedValue 115 } 116}