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