PassCommandHandlerTest.groovy revision 016b6dd21f1a552e28f4c6894b586b770241b0ed
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 PassCommandHandler
32 *
33 * @version $Revision: $ - $Date: $
34 *
35 * @author Chris Mair
36 */
37class PassCommandHandlerTest extends AbstractFakeCommandHandlerTest {
38
39    def USERNAME = "user123"
40    def PASSWORD = "password123"
41    def userAccount
42
43    void testHandleCommand_UserExists_PasswordCorrect() {
44        serverConfiguration.userAccounts[USERNAME] = userAccount
45		commandHandler.handleCommand(createCommand([PASSWORD]), session)
46        assertSessionReply(ReplyCodes.PASS_OK)
47        assertUserAccountInSession(true)
48	}
49
50    void testHandleCommand_UserExists_PasswordIncorrect() {
51        serverConfiguration.userAccounts[USERNAME] = userAccount
52		commandHandler.handleCommand(createCommand(["wrong"]), session)
53        assertSessionReply(ReplyCodes.PASS_LOG_IN_FAILED)
54        assertUserAccountInSession(false)
55    }
56
57    void testHandleCommand_UserExists_PasswordWrongButIgnored() {
58        userAccount.passwordCheckedDuringValidation = false
59        serverConfiguration.userAccounts[USERNAME] = userAccount
60		commandHandler.handleCommand(createCommand(["wrong"]), session)
61        assertSessionReply(ReplyCodes.PASS_OK)
62        assertUserAccountInSession(true)
63    }
64
65    void testHandleCommand_UserDoesNotExist() {
66		commandHandler.handleCommand(createCommand([PASSWORD]), session)
67        assertSessionReply(ReplyCodes.PASS_LOG_IN_FAILED)
68        assertUserAccountInSession(false)
69    }
70
71    void testHandleCommand_UsernameNotSetInSession() {
72        session.removeAttribute(SessionKeys.USERNAME)
73        testHandleCommand_MissingRequiredSessionAttribute()
74        assertUserAccountInSession(false)
75	}
76
77    void testHandleCommand_MissingPasswordParameter() {
78        testHandleCommand_MissingRequiredParameter([])
79        assertUserAccountInSession(false)
80    }
81
82    void testHandleCommand_EmptyPasswordParameter() {
83        testHandleCommand_MissingRequiredParameter([""])
84        assertUserAccountInSession(false)
85    }
86
87    //-------------------------------------------------------------------------
88    // Helper Methods
89    //-------------------------------------------------------------------------
90
91	void setUp() {
92	    super.setUp()
93
94	    userAccount = new UserAccount()
95        userAccount.username = USERNAME
96        userAccount.password = PASSWORD
97
98        session.setAttribute(SessionKeys.USERNAME, USERNAME)
99        this.commandHandlerRequiresLogin = false
100	}
101
102	CommandHandler createCommandHandler() {
103	    new PassCommandHandler()
104	}
105
106    Command createValidCommand() {
107        return new Command(CommandNames.PASS, [PASSWORD])
108    }
109
110    /**
111     * Assert that the UserAccount object is in the session, depending on the value of isUserAccountInSession.
112     * @param isUserAccountInSession - true if the UserAccount is expected in the session; false if it is not expected
113     */
114    private void assertUserAccountInSession(boolean isUserAccountInSession) {
115        def expectedValue = isUserAccountInSession ? userAccount : null
116        assert session.getAttribute(SessionKeys.USER_ACCOUNT) == expectedValue
117    }
118}