<commit_before>package org.springframework.security.config.http

import org.springframework.beans.factory.BeanCreationException
import org.springframework.security.util.FieldUtils
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter

/**
 *
 * @author <NAME>
 */
class FormLoginConfigTests extends AbstractHttpConfigTests {

    def formLoginWithNoLoginPageAddsDefaultLoginPageFilter() {
        httpAutoConfig('ant') {
            form-login()
        }
        createAppContext()
        filtersMatchExpectedAutoConfigList();
    }

    def 'Form login alwaysUseDefaultTarget sets correct property'() {
        xml.http {
            'form-login'('default-target-url':'/default', 'always-use-default-target': 'true')
        }
        createAppContext()
        def filter = getFilter(UsernamePasswordAuthenticationFilter.class);

        expect:
        FieldUtils.getFieldValue(filter, 'successHandler.defaultTargetUrl') == '/default';
        FieldUtils.getFieldValue(filter, 'successHandler.alwaysUseDefaultTargetUrl') == true;
    }

    def invalidLoginPageIsDetected() {
        when:
        xml.http {
            'form-login'('login-page': 'noLeadingSlash')
        }
        createAppContext()

        then:
        BeanCreationException e = thrown();
    }

    def invalidDefaultTargetUrlIsDetected() {
        when:
        xml.http {
            'form-login'('default-target-url': 'noLeadingSlash')
        }
        createAppContext()

        then:
        BeanCreationException e = thrown();
    }

    def customSuccessAndFailureHandlersCanBeSetThroughTheNamespace() {
        xml.http {
            'form-login'('authentication-success-handler-ref': 'sh', 'authentication-failure-handler-ref':'fh')
        }
        bean('sh', SavedRequestAwareAuthenticationSuccessHandler.class.name)
        bean('fh', SimpleUrlAuthenticationFailureHandler.class.name)
        createAppContext()

        def apf = getFilter(UsernamePasswordAuthenticationFilter.class);

        expect:
        FieldUtils.getFieldValue(apf, "successHandler") == appContext.getBean("sh");
        FieldUtils.getFieldValue(apf, "failureHandler") == appContext.getBean("fh")
    }
}<commit_msg>SEC-1445: Tests for setting of username and password parameter names through the form-login element.<commit_after>package org.springframework.security.config.http

import org.springframework.beans.factory.BeanCreationException
import org.springframework.security.util.FieldUtils
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter

/**
 *
 * @author <NAME>
 */
class FormLoginConfigTests extends AbstractHttpConfigTests {

    def formLoginWithNoLoginPageAddsDefaultLoginPageFilter() {
        httpAutoConfig('ant') {
            form-login()
        }
        createAppContext()
        filtersMatchExpectedAutoConfigList();
    }

    def 'Form login alwaysUseDefaultTarget sets correct property'() {
        xml.http {
            'form-login'('default-target-url':'/default', 'always-use-default-target': 'true')
        }
        createAppContext()
        def filter = getFilter(UsernamePasswordAuthenticationFilter.class);

        expect:
        FieldUtils.getFieldValue(filter, 'successHandler.defaultTargetUrl') == '/default';
        FieldUtils.getFieldValue(filter, 'successHandler.alwaysUseDefaultTargetUrl') == true;
    }

    def invalidLoginPageIsDetected() {
        when:
        xml.http {
            'form-login'('login-page': 'noLeadingSlash')
        }
        createAppContext()

        then:
        BeanCreationException e = thrown();
    }

    def invalidDefaultTargetUrlIsDetected() {
        when:
        xml.http {
            'form-login'('default-target-url': 'noLeadingSlash')
        }
        createAppContext()

        then:
        BeanCreationException e = thrown();
    }

    def customSuccessAndFailureHandlersCanBeSetThroughTheNamespace() {
        xml.http {
            'form-login'('authentication-success-handler-ref': 'sh', 'authentication-failure-handler-ref':'fh')
        }
        bean('sh', SavedRequestAwareAuthenticationSuccessHandler.class.name)
        bean('fh', SimpleUrlAuthenticationFailureHandler.class.name)
        createAppContext()

        def apf = getFilter(UsernamePasswordAuthenticationFilter.class);

        expect:
        FieldUtils.getFieldValue(apf, "successHandler") == appContext.getBean("sh");
        FieldUtils.getFieldValue(apf, "failureHandler") == appContext.getBean("fh")
    }

    def usernameAndPasswordParametersCanBeSetThroughNamespace() {
        xml.http {
            'form-login'('username-parameter': 'xname', 'password-parameter':'<PASSWORD>')
        }
        createAppContext()

        def apf = getFilter(UsernamePasswordAuthenticationFilter.class);

        expect:
        apf.usernameParameter == 'xname';
        apf.passwordParameter == '<PASSWORD>'
    }
}
{'fraction_non_alphanumeric': 0.10555972952667167, 'fraction_numerical': 0.0007513148009015778, 'mean_word_length': 3.270248596631917, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 9}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6277075', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1354, 'n_words': 206}