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