UserCommandHandlerTest.groovy revision ad98f727deebfd941b446f0f82337b2244e43b6c
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.test.AbstractGroovyTest
19import org.mockftpserver.core.command.Command
20import org.mockftpserver.core.command.CommandHandler
21import org.mockftpserver.core.command.CommandNames
22import org.mockftpserver.core.session.StubSession
23import org.mockftpserver.core.session.SessionKeys
24import org.mockftpserver.fake.StubServerConfiguration
25import org.mockftpserver.fake.user.UserAccount
26import org.apache.log4j.Logger
27import org.mockftpserver.core.command.ReplyCodes
28import org.mockftpserver.core.util.AssertFailedException
29
30/**
31 * Tests for UserCommandHandler
32 *
33 * @version $Revision: $ - $Date: $
34 *
35 * @author Chris Mair
36 */
37class UserCommandHandlerTest extends AbstractFakeCommandHandlerTest {
38
39    def USERNAME = "user123"
40
41    void testHandleCommand_UserExists() {
42        serverConfiguration.userAccounts[USERNAME] = new UserAccount()
43		commandHandler.handleCommand(createCommand([USERNAME]), session)
44        assertSessionReply(ReplyCodes.USER_NEED_PASSWORD_OK)
45        assertUsernameInSession(true)
46	}
47
48    void testHandleCommand_NoSuchUser() {
49		commandHandler.handleCommand(createCommand([USERNAME]), session)
50		// Will return OK, even if username is not recognized
51        assertSessionReply(ReplyCodes.USER_NEED_PASSWORD_OK)
52        assertUsernameInSession(true)
53	}
54
55    void testHandleCommand_PasswordNotRequiredForLogin() {
56        def userAccount = new UserAccount(passwordRequiredForLogin:false)
57        serverConfiguration.userAccounts[USERNAME] = userAccount
58
59        commandHandler.handleCommand(createCommand([USERNAME]), session)
60        assertSessionReply(ReplyCodes.USER_LOGGED_IN_OK)
61        assert session.getAttribute(SessionKeys.USER_ACCOUNT) == userAccount
62        assertUsernameInSession(false)
63	}
64
65    void testHandleCommand_MissingUsernameParameter() {
66        testHandleCommand_MissingRequiredParameter([])
67        assertUsernameInSession(false)
68    }
69
70    void testHandleCommand_EmptyUsernameParameter() {
71        testHandleCommand_MissingRequiredParameter([""])
72        assertUsernameInSession(false)
73    }
74
75    //-------------------------------------------------------------------------
76    // Helper Methods
77    //-------------------------------------------------------------------------
78
79	CommandHandler createCommandHandler() {
80	    new UserCommandHandler()
81	}
82
83    Command createValidCommand() {
84        return new Command(CommandNames.USER, [USERNAME])
85    }
86
87    /**
88     * Assert that the Username is stored in the session, depending on the value of isUsernameInSession.
89     * @param isUsernameInSession - true if the Username is expected in the session; false if it is not expected
90     */
91    private void assertUsernameInSession(boolean isUsernameInSession) {
92        def expectedValue = isUsernameInSession ? USERNAME : null
93        assert session.getAttribute(SessionKeys.USERNAME) == expectedValue
94    }
95}