Home | History | Annotate | Download | only in command
      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  */
     16 package org.mockftpserver.fake.command
     17 
     18 import org.mockftpserver.core.command.Command
     19 import org.mockftpserver.core.command.CommandHandler
     20 import org.mockftpserver.core.command.CommandNames
     21 import org.mockftpserver.core.command.ReplyCodes
     22 import org.mockftpserver.core.session.SessionKeys
     23 import org.mockftpserver.fake.UserAccount
     24 
     25 /**
     26  * Tests for PassCommandHandler
     27  *
     28  * @version $Revision$ - $Date$
     29  *
     30  * @author Chris Mair
     31  */
     32 class PassCommandHandlerTest extends AbstractFakeCommandHandlerTestCase {
     33 
     34     def USERNAME = "user123"
     35     def PASSWORD = "password123"
     36     def HOME_DIRECTORY = "/"
     37     UserAccount userAccount
     38 
     39     boolean testNotLoggedIn = false
     40 
     41     void testHandleCommand_UserExists_PasswordCorrect() {
     42         serverConfiguration.userAccounts[USERNAME] = userAccount
     43         handleCommand([PASSWORD])
     44         assertSessionReply(ReplyCodes.PASS_OK, 'pass')
     45         assertUserAccountInSession(true)
     46         assertCurrentDirectory(HOME_DIRECTORY)
     47     }
     48 
     49     void testHandleCommand_UserExists_PasswordCorrect_AccountRequired() {
     50         serverConfiguration.userAccounts[USERNAME] = userAccount
     51         userAccount.accountRequiredForLogin = true
     52         handleCommand([PASSWORD])
     53         assertSessionReply(ReplyCodes.PASS_NEED_ACCOUNT, 'pass.needAccount')
     54         assertUserAccountInSession(true)
     55         assertCurrentDirectory(HOME_DIRECTORY)
     56     }
     57 
     58     void testHandleCommand_UserExists_PasswordIncorrect() {
     59         serverConfiguration.userAccounts[USERNAME] = userAccount
     60         handleCommand(["wrong"])
     61         assertSessionReply(ReplyCodes.PASS_LOG_IN_FAILED, 'pass.loginFailed')
     62         assertUserAccountInSession(false)
     63         assertCurrentDirectory(null)
     64     }
     65 
     66     void testHandleCommand_UserExists_PasswordWrongButIgnored() {
     67         userAccount.passwordCheckedDuringValidation = false
     68         serverConfiguration.userAccounts[USERNAME] = userAccount
     69         handleCommand(["wrong"])
     70         assertSessionReply(ReplyCodes.PASS_OK, 'pass')
     71         assertUserAccountInSession(true)
     72         assertCurrentDirectory(HOME_DIRECTORY)
     73     }
     74 
     75     void testHandleCommand_UserExists_HomeDirectoryNotDefinedForUserAccount() {
     76         userAccount.homeDirectory = ''
     77         serverConfiguration.userAccounts[USERNAME] = userAccount
     78         handleCommand([PASSWORD])
     79         assertSessionReply(ReplyCodes.USER_ACCOUNT_NOT_VALID, "login.userAccountNotValid")
     80         assertUserAccountInSession(false)
     81         assertCurrentDirectory(null)
     82     }
     83 
     84     void testHandleCommand_UserExists_HomeDirectoryDoesNotExist() {
     85         userAccount.homeDirectory = '/abc/def'
     86         serverConfiguration.userAccounts[USERNAME] = userAccount
     87         handleCommand([PASSWORD])
     88         assertSessionReply(ReplyCodes.USER_ACCOUNT_NOT_VALID, "login.homeDirectoryNotValid")
     89         assertUserAccountInSession(false)
     90         assertCurrentDirectory(null)
     91     }
     92 
     93     void testHandleCommand_UserDoesNotExist() {
     94         handleCommand([PASSWORD])
     95         assertSessionReply(ReplyCodes.USER_ACCOUNT_NOT_VALID, "login.userAccountNotValid")
     96         assertUserAccountInSession(false)
     97         assertCurrentDirectory(null)
     98     }
     99 
    100     void testHandleCommand_UsernameNotSetInSession() {
    101         session.removeAttribute(SessionKeys.USERNAME)
    102         testHandleCommand_MissingRequiredSessionAttribute()
    103         assertUserAccountInSession(false)
    104         assertCurrentDirectory(null)
    105     }
    106 
    107     void testHandleCommand_MissingPasswordParameter() {
    108         testHandleCommand_MissingRequiredParameter([])
    109         assertUserAccountInSession(false)
    110         assertCurrentDirectory(null)
    111     }
    112 
    113     //-------------------------------------------------------------------------
    114     // Abstract and Overridden Methods
    115     //-------------------------------------------------------------------------
    116 
    117     void setUp() {
    118         super.setUp()
    119 
    120         createDirectory(HOME_DIRECTORY)
    121 
    122         userAccount = new UserAccount(USERNAME, PASSWORD, HOME_DIRECTORY)
    123 
    124         session.setAttribute(SessionKeys.USERNAME, USERNAME)
    125         session.removeAttribute(SessionKeys.USER_ACCOUNT)
    126     }
    127 
    128     CommandHandler createCommandHandler() {
    129         new PassCommandHandler()
    130     }
    131 
    132     Command createValidCommand() {
    133         return new Command(CommandNames.PASS, [PASSWORD])
    134     }
    135 
    136     //-------------------------------------------------------------------------
    137     // Helper Methods
    138     //-------------------------------------------------------------------------
    139 
    140     /**
    141      * Assert that the UserAccount object is in the session, depending on the value of isUserAccountInSession.
    142      * @param isUserAccountInSession - true if the UserAccount is expected in the session; false if it is not expected
    143      */
    144     private void assertUserAccountInSession(boolean isUserAccountInSession) {
    145         def expectedValue = isUserAccountInSession ? userAccount : null
    146         assert session.getAttribute(SessionKeys.USER_ACCOUNT) == expectedValue
    147     }
    148 
    149     /**
    150      * Assert that the current directory is set in the session, but only if currentDirectory is not null.
    151      * @param currentDirectory - the curent directory expected in the session; null if it is not expected
    152      */
    153     private void assertCurrentDirectory(String currentDirectory) {
    154         assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == currentDirectory
    155     }
    156 }