PassCommandHandlerTest.groovy revision 6fed98e9c695a9024ede347900c2689979ab5785
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.user.UserAccount
24
25/**
26 * Tests for PassCommandHandler
27 *
28 * @version $Revision$ - $Date$
29 *
30 * @author Chris Mair
31 */
32class PassCommandHandlerTest extends AbstractFakeCommandHandlerTest {
33
34    def USERNAME = "user123"
35    def PASSWORD = "password123"
36    def HOME_DIRECTORY = "/"
37    UserAccount userAccount
38
39    void testHandleCommand_UserExists_PasswordCorrect() {
40        serverConfiguration.userAccounts[USERNAME] = userAccount
41        commandHandler.handleCommand(createCommand([PASSWORD]), session)
42        assertSessionReply(ReplyCodes.PASS_OK, 'pass')
43        assertUserAccountInSession(true)
44        assertCurrentDirectory(HOME_DIRECTORY)
45    }
46
47    void testHandleCommand_UserExists_PasswordCorrect_AccountRequired() {
48        serverConfiguration.userAccounts[USERNAME] = userAccount
49        userAccount.accountRequiredForLogin = true
50        commandHandler.handleCommand(createCommand([PASSWORD]), session)
51        assertSessionReply(ReplyCodes.PASS_NEED_ACCOUNT, 'pass.needAccount')
52        assertUserAccountInSession(true)
53        assertCurrentDirectory(HOME_DIRECTORY)
54    }
55
56    void testHandleCommand_UserExists_PasswordIncorrect() {
57        serverConfiguration.userAccounts[USERNAME] = userAccount
58        commandHandler.handleCommand(createCommand(["wrong"]), session)
59        assertSessionReply(ReplyCodes.PASS_LOG_IN_FAILED, 'pass.loginFailed')
60        assertUserAccountInSession(false)
61        assertCurrentDirectory(null)
62    }
63
64    void testHandleCommand_UserExists_PasswordWrongButIgnored() {
65        userAccount.passwordCheckedDuringValidation = false
66        serverConfiguration.userAccounts[USERNAME] = userAccount
67        commandHandler.handleCommand(createCommand(["wrong"]), session)
68        assertSessionReply(ReplyCodes.PASS_OK, 'pass')
69        assertUserAccountInSession(true)
70        assertCurrentDirectory(HOME_DIRECTORY)
71    }
72
73    void testHandleCommand_UserExists_HomeDirectoryNotDefinedForUserAccount() {
74        userAccount.homeDirectory = ''
75        serverConfiguration.userAccounts[USERNAME] = userAccount
76        commandHandler.handleCommand(createCommand([PASSWORD]), session)
77        assertSessionReply(ReplyCodes.USER_ACCOUNT_NOT_VALID, "login.userAccountNotValid")
78        assertUserAccountInSession(false)
79        assertCurrentDirectory(null)
80    }
81
82    void testHandleCommand_UserExists_HomeDirectoryDoesNotExist() {
83        userAccount.homeDirectory = '/abc/def'
84        serverConfiguration.userAccounts[USERNAME] = userAccount
85        commandHandler.handleCommand(createCommand([PASSWORD]), session)
86        assertSessionReply(ReplyCodes.USER_ACCOUNT_NOT_VALID, "login.homeDirectoryNotValid")
87        assertUserAccountInSession(false)
88        assertCurrentDirectory(null)
89    }
90
91    void testHandleCommand_UserDoesNotExist() {
92        commandHandler.handleCommand(createCommand([PASSWORD]), session)
93        assertSessionReply(ReplyCodes.USER_ACCOUNT_NOT_VALID, "login.userAccountNotValid")
94        assertUserAccountInSession(false)
95        assertCurrentDirectory(null)
96    }
97
98    void testHandleCommand_UsernameNotSetInSession() {
99        session.removeAttribute(SessionKeys.USERNAME)
100        testHandleCommand_MissingRequiredSessionAttribute()
101        assertUserAccountInSession(false)
102        assertCurrentDirectory(null)
103    }
104
105    void testHandleCommand_MissingPasswordParameter() {
106        testHandleCommand_MissingRequiredParameter([])
107        assertUserAccountInSession(false)
108        assertCurrentDirectory(null)
109    }
110
111    //-------------------------------------------------------------------------
112    // Abstract and Overridden Methods
113    //-------------------------------------------------------------------------
114
115    void setUp() {
116        super.setUp()
117
118        fileSystem.createDirectory(HOME_DIRECTORY)
119
120        userAccount = new UserAccount()
121        userAccount.username = USERNAME
122        userAccount.password = PASSWORD
123        userAccount.homeDirectory = HOME_DIRECTORY
124
125        session.setAttribute(SessionKeys.USERNAME, USERNAME)
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}