<commit_before>/* Copyright 2004 Acegi Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.sf.acegisecurity.taglibs.authz;

import junit.framework.TestCase;

import net.sf.acegisecurity.GrantedAuthority;
import net.sf.acegisecurity.GrantedAuthorityImpl;
import net.sf.acegisecurity.context.ContextHolder;
import net.sf.acegisecurity.context.SecureContextImpl;
import net.sf.acegisecurity.providers.TestingAuthenticationToken;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;


/**
 * DOCUMENT ME!
 *
 * @author <NAME>
 * @version $Id$
 */
public class AuthorizeTagTests extends TestCase {
    //~ Instance fields ========================================================

    private final AuthorizeTag authorizeTag = new AuthorizeTag();
    private SecureContextImpl context;
    private TestingAuthenticationToken currentUser;

    //~ Methods ================================================================

    public void testDefaultsToNotOutputtingBodyWhenNoRequiredAuthorities()
        throws JspException {
        assertEquals("", authorizeTag.getIfAllGranted());
        assertEquals("", authorizeTag.getIfAnyGranted());
        assertEquals("", authorizeTag.getIfNotGranted());

        assertEquals("prevents body output - no authorities granted",
            Tag.SKIP_BODY, authorizeTag.doStartTag());
    }

    public void testOutputsBodyIfOneRolePresent() throws JspException {
        authorizeTag.setIfAnyGranted("ROLE_TELLER");
        assertEquals("authorized - ROLE_TELLER in both sets",
            Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
    }

    public void testOutputsBodyWhenAllGranted() throws JspException {
        authorizeTag.setIfAllGranted("ROLE_SUPERVISOR,ROLE_TELLER");
        assertEquals("allows request - all required roles granted on principal",
            Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
    }

    public void testOutputsBodyWhenNotGrantedSatisfied()
        throws JspException {
        authorizeTag.setIfNotGranted("ROLE_BANKER");
        assertEquals("allows request - principal doesn't have ROLE_BANKER",
            Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
    }

    public void testPreventsBodyOutputIfNoSecureContext()
        throws JspException {
        ContextHolder.setContext(null);
        authorizeTag.setIfAnyGranted("ROLE_BANKER");

        assertEquals("prevents output - no context defined", Tag.SKIP_BODY,
            authorizeTag.doStartTag());
    }

    public void testSkipsBodyIfNoAnyRolePresent() throws JspException {
        authorizeTag.setIfAnyGranted("ROLE_BANKER");
        assertEquals("unauthorized - ROLE_BANKER not in granted authorities",
            Tag.SKIP_BODY, authorizeTag.doStartTag());
    }

    public void testSkipsBodyWhenMissingAnAllGranted()
        throws JspException {
        authorizeTag.setIfAllGranted("ROLE_SUPERVISOR,ROLE_TELLER,ROLE_BANKER");
        assertEquals("prevents request - missing ROLE_BANKER on principal",
            Tag.SKIP_BODY, authorizeTag.doStartTag());
    }

    public void testSkipsBodyWhenNotGrantedUnsatisfied()
        throws JspException {
        authorizeTag.setIfNotGranted("ROLE_TELLER");
        assertEquals("prevents request - principal has ROLE_TELLER",
            Tag.SKIP_BODY, authorizeTag.doStartTag());
    }

    public void testUsesAllAuthoritiesToDetermineAccess() {
        authorizeTag.setIfAllGranted("ROLE_SUPERVISOR,ROLE_BANKER");
        authorizeTag.setIfAnyGranted("ROLE_BANKER");
        authorizeTag.setIfNotGranted("ROLE_RESTRICTED");

        currentUser = new TestingAuthenticationToken("abc", "<PASSWORD>",
                new GrantedAuthority[] {new GrantedAuthorityImpl(
                        "ROLE_SUPERVISOR"), new GrantedAuthorityImpl(
                        "ROLE_BANKER"), new GrantedAuthorityImpl(
                        "ROLE_RESTRICTED"),});
        context.setAuthentication(currentUser);
    }

    protected void setUp() throws Exception {
        super.setUp();

        currentUser = new TestingAuthenticationToken("abc", "<PASSWORD>",
                new GrantedAuthority[] {new GrantedAuthorityImpl(
                        "ROLE_SUPERVISOR"), new GrantedAuthorityImpl(
                        "ROLE_TELLER"),});

        context = new SecureContextImpl();
        context.setAuthentication(currentUser);

        ContextHolder.setContext(context);
    }

    protected void tearDown() throws Exception {
        ContextHolder.setContext(null);
    }
}<commit_msg>* test/net/sf/acegisecurity/taglibs/authz/AuthorizeTagTests.java:   Removed testUsesAllAuthoritiesToDetermineAccess(), because it wasn't   asserting anything.  Needs to be rewritten.<commit_after>/* Copyright 2004 Acegi Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.sf.acegisecurity.taglibs.authz;

import junit.framework.TestCase;

import net.sf.acegisecurity.GrantedAuthority;
import net.sf.acegisecurity.GrantedAuthorityImpl;
import net.sf.acegisecurity.context.ContextHolder;
import net.sf.acegisecurity.context.SecureContextImpl;
import net.sf.acegisecurity.providers.TestingAuthenticationToken;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;


/**
 * DOCUMENT ME!
 *
 * @author <NAME>
 * @version $Id$
 */
public class AuthorizeTagTests extends TestCase {
    //~ Instance fields ========================================================

    private final AuthorizeTag authorizeTag = new AuthorizeTag();
    private SecureContextImpl context;
    private TestingAuthenticationToken currentUser;

    //~ Methods ================================================================

    public void testDefaultsToNotOutputtingBodyWhenNoRequiredAuthorities()
        throws JspException {
        assertEquals("", authorizeTag.getIfAllGranted());
        assertEquals("", authorizeTag.getIfAnyGranted());
        assertEquals("", authorizeTag.getIfNotGranted());

        assertEquals("prevents body output - no authorities granted",
            Tag.SKIP_BODY, authorizeTag.doStartTag());
    }

    public void testOutputsBodyIfOneRolePresent() throws JspException {
        authorizeTag.setIfAnyGranted("ROLE_TELLER");
        assertEquals("authorized - ROLE_TELLER in both sets",
            Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
    }

    public void testOutputsBodyWhenAllGranted() throws JspException {
        authorizeTag.setIfAllGranted("ROLE_SUPERVISOR,ROLE_TELLER");
        assertEquals("allows request - all required roles granted on principal",
            Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
    }

    public void testOutputsBodyWhenNotGrantedSatisfied()
        throws JspException {
        authorizeTag.setIfNotGranted("ROLE_BANKER");
        assertEquals("allows request - principal doesn't have ROLE_BANKER",
            Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
    }

    public void testPreventsBodyOutputIfNoSecureContext()
        throws JspException {
        ContextHolder.setContext(null);
        authorizeTag.setIfAnyGranted("ROLE_BANKER");

        assertEquals("prevents output - no context defined", Tag.SKIP_BODY,
            authorizeTag.doStartTag());
    }

    public void testSkipsBodyIfNoAnyRolePresent() throws JspException {
        authorizeTag.setIfAnyGranted("ROLE_BANKER");
        assertEquals("unauthorized - ROLE_BANKER not in granted authorities",
            Tag.SKIP_BODY, authorizeTag.doStartTag());
    }

    public void testSkipsBodyWhenMissingAnAllGranted()
        throws JspException {
        authorizeTag.setIfAllGranted("ROLE_SUPERVISOR,ROLE_TELLER,ROLE_BANKER");
        assertEquals("prevents request - missing ROLE_BANKER on principal",
            Tag.SKIP_BODY, authorizeTag.doStartTag());
    }

    public void testSkipsBodyWhenNotGrantedUnsatisfied()
        throws JspException {
        authorizeTag.setIfNotGranted("ROLE_TELLER");
        assertEquals("prevents request - principal has ROLE_TELLER",
            Tag.SKIP_BODY, authorizeTag.doStartTag());
    }

    protected void setUp() throws Exception {
        super.setUp();

        currentUser = new TestingAuthenticationToken("abc", "<PASSWORD>",
                new GrantedAuthority[] {new GrantedAuthorityImpl(
                        "ROLE_SUPERVISOR"), new GrantedAuthorityImpl(
                        "ROLE_TELLER"),});

        context = new SecureContextImpl();
        context.setAuthentication(currentUser);

        ContextHolder.setContext(context);
    }

    protected void tearDown() throws Exception {
        ContextHolder.setContext(null);
    }
}
{'fraction_non_alphanumeric': 0.10913784202374806, 'fraction_numerical': 0.0016520392359318534, 'mean_word_length': 3.5602636534839927, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '260458', 'n_tokens_mistral': 2792, 'n_tokens_neox': 2595, 'n_words': 584}