UserCommandHandlerTest.groovy revision bd576ae311a45a994ae6b457fb2e5bb0ffe0d6b5
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.core.command.Command 19import org.mockftpserver.core.command.CommandHandler 20import org.mockftpserver.core.command.CommandNames 21import org.mockftpserver.core.command.ReplyCodes 22import org.mockftpserver.core.session.SessionKeys 23import org.mockftpserver.fake.UserAccount 24 25 26/** 27 * Tests for UserCommandHandler 28 * 29 * @version $Revision$ - $Date$ 30 * 31 * @author Chris Mair 32 */ 33class UserCommandHandlerTest extends AbstractFakeCommandHandlerTest { 34 35 static final USERNAME = "user123" 36 static final HOME_DIRECTORY = "/" 37 UserAccount userAccount 38 39 boolean testNotLoggedIn = false 40 41 void testHandleCommand_UserExists() { 42 serverConfiguration.userAccounts[USERNAME] = userAccount 43 handleCommand([USERNAME]) 44 assertSessionReply(ReplyCodes.USER_NEED_PASSWORD_OK, 'user.needPassword') 45 assertUsernameInSession(true) 46 assertCurrentDirectory(null) 47 } 48 49 void testHandleCommand_NoSuchUser() { 50 handleCommand([USERNAME]) 51 // Will return OK, even if username is not recognized 52 assertSessionReply(ReplyCodes.USER_NEED_PASSWORD_OK, 'user.needPassword') 53 assertUsernameInSession(true) 54 assertCurrentDirectory(null) 55 } 56 57 void testHandleCommand_PasswordNotRequiredForLogin() { 58 userAccount.passwordRequiredForLogin = false 59 serverConfiguration.userAccounts[USERNAME] = userAccount 60 61 handleCommand([USERNAME]) 62 assertSessionReply(ReplyCodes.USER_LOGGED_IN_OK, 'user.loggedIn') 63 assert session.getAttribute(SessionKeys.USER_ACCOUNT) == userAccount 64 assertUsernameInSession(false) 65 assertCurrentDirectory(HOME_DIRECTORY) 66 } 67 68 void testHandleCommand_UserExists_HomeDirectoryNotDefinedForUser() { 69 userAccount.homeDirectory = '' 70 serverConfiguration.userAccounts[USERNAME] = userAccount 71 handleCommand([USERNAME]) 72 assertSessionReply(ReplyCodes.USER_ACCOUNT_NOT_VALID, "login.userAccountNotValid") 73 assertUsernameInSession(false) 74 assertCurrentDirectory(null) 75 } 76 77 void testHandleCommand_UserExists_HomeDirectoryDoesNotExist() { 78 userAccount.homeDirectory = '/abc/def' 79 serverConfiguration.userAccounts[USERNAME] = userAccount 80 handleCommand([USERNAME]) 81 assertSessionReply(ReplyCodes.USER_ACCOUNT_NOT_VALID, "login.homeDirectoryNotValid") 82 assertUsernameInSession(false) 83 assertCurrentDirectory(null) 84 } 85 86 void testHandleCommand_MissingUsernameParameter() { 87 testHandleCommand_MissingRequiredParameter([]) 88 assertUsernameInSession(false) 89 assertCurrentDirectory(null) 90 } 91 92 //------------------------------------------------------------------------- 93 // Abstract and Overridden Methods 94 //------------------------------------------------------------------------- 95 96 void setUp() { 97 super.setUp() 98 99 createDirectory(HOME_DIRECTORY) 100 userAccount = new UserAccount(username: USERNAME, homeDirectory: HOME_DIRECTORY) 101 } 102 103 CommandHandler createCommandHandler() { 104 new UserCommandHandler() 105 } 106 107 Command createValidCommand() { 108 return new Command(CommandNames.USER, [USERNAME]) 109 } 110 111 //------------------------------------------------------------------------- 112 // Helper Methods 113 //------------------------------------------------------------------------- 114 115 /** 116 * Assert that the Username is stored in the session, depending on the value of isUsernameInSession. 117 * @param isUsernameInSession - true if the Username is expected in the session; false if it is not expected 118 */ 119 private void assertUsernameInSession(boolean isUsernameInSession) { 120 def expectedValue = isUsernameInSession ? USERNAME : null 121 assert session.getAttribute(SessionKeys.USERNAME) == expectedValue 122 } 123 124 /** 125 * Assert that the current directory is set in the session, but only if currentDirectory is not null. 126 * @param currentDirectory - the curent directory expected in the session; null if it is not expected 127 */ 128 private void assertCurrentDirectory(String currentDirectory) { 129 assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == currentDirectory 130 } 131}