<commit_before>package edu.northwestern.bioinformatics.studycalendar.web;

import edu.northwestern.bioinformatics.studycalendar.StudyCalendarError;
import edu.northwestern.bioinformatics.studycalendar.security.authorization.LegacyModeSwitch;
import edu.northwestern.bioinformatics.studycalendar.web.accesscontrol.PscAuthorizedHandler;
import junit.framework.TestCase;
import org.restlet.resource.Resource;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.servlet.mvc.Controller;

import java.io.File;
import java.util.LinkedHashSet;
import java.util.Set;

import static edu.northwestern.bioinformatics.studycalendar.web.StudyCalendarTestWebApplicationContextBuilder.createWebApplicationContextForServlet;

/**
 * @author <NAME>
 */
public class DispatcherServletInitializationTest extends TestCase {
    private MockServletContext servletContext;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        servletContext = new MockServletContext(findWebappDirectory(), new FileSystemResourceLoader());
    }

    private String findWebappDirectory() {
        File dir = new File("src/main/webapp");
        if (dir.exists()) {
            return dir.getPath();
        }
        dir = new File("web", dir.toString());
        if (dir.exists()) {
            return dir.getPath();
        }
        throw new IllegalStateException("Could not find webapp path");
    }

    public void testSpringServletContext() throws Exception {
        assertDispatcherServletConfigLoads("spring");
    }

    public void testPublicServletContext() throws Exception {
        assertDispatcherServletConfigLoads("public");
    }

    public void testSetupServletContext() throws Exception {
        assertDispatcherServletConfigLoads("setup");
    }

    public void testRestfulApiServletContext() throws Exception {
        assertDispatcherServletConfigLoads("restful-api");
    }

    public void testAllResourcesArePrototypeScope() throws Exception {
        ApplicationContext ctxt = createWebApplicationContextForServlet("restful-api", servletContext);

        String[] beanNames = ctxt.getBeanNamesForType(Resource.class);
        assertTrue("Should have found at least one resource", beanNames.length > 0);

        for (String beanName : beanNames) {
            Object firstTime = ctxt.getBean(beanName);
            Object secondTime = ctxt.getBean(beanName);
            assertNotSame(beanName + " is not a prototype", firstTime, secondTime);
        }
    }

    public void testAllUiControllersHaveAuthorizationInformation() throws Exception {
        ApplicationContext ctxt = createWebApplicationContextForServlet("spring", servletContext);

        String[] beanNames = ctxt.getBeanNamesForType(Controller.class);
        Set<String> noAuthorizationInfo = new LinkedHashSet<String>();

        for (String beanName : beanNames) {
            Object controller = ctxt.getBean(beanName);
            if (!(controller instanceof PscAuthorizedHandler)) {
                noAuthorizationInfo.add(controller.getClass().getName());
            }
        }

        StringBuilder msg = new StringBuilder().append("The following ").
            append(noAuthorizationInfo.size()).
            append(" controllers contain no unified authorization information and therefore will never execute: ");
        for (String s : noAuthorizationInfo) {
            msg.append("\n  ").append(s);
        }

        if (((LegacyModeSwitch) ctxt.getBean("authorizationLegacyModeSwitch")).isOn()) {
            System.out.println(msg.toString());
        } else {
            fail(msg.toString());
        }
    }

    private void assertDispatcherServletConfigLoads(String servletName) throws Exception {
        try {
            createWebApplicationContextForServlet(servletName, servletContext);
        } catch (Exception e) {
            throw new StudyCalendarError("Loading the configuration for MVC servlet '" + servletName
                + "' failed:  " + e.getMessage(), e);
        }
    }
}<commit_msg>Don't fail if no controllers are actually missing auth info.<commit_after>package edu.northwestern.bioinformatics.studycalendar.web;

import edu.northwestern.bioinformatics.studycalendar.StudyCalendarError;
import edu.northwestern.bioinformatics.studycalendar.security.authorization.LegacyModeSwitch;
import edu.northwestern.bioinformatics.studycalendar.web.accesscontrol.PscAuthorizedHandler;
import junit.framework.TestCase;
import org.restlet.resource.Resource;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.servlet.mvc.Controller;

import java.io.File;
import java.util.LinkedHashSet;
import java.util.Set;

import static edu.northwestern.bioinformatics.studycalendar.web.StudyCalendarTestWebApplicationContextBuilder.createWebApplicationContextForServlet;

/**
 * @author <NAME>
 */
public class DispatcherServletInitializationTest extends TestCase {
    private MockServletContext servletContext;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        servletContext = new MockServletContext(findWebappDirectory(), new FileSystemResourceLoader());
    }

    private String findWebappDirectory() {
        File dir = new File("src/main/webapp");
        if (dir.exists()) {
            return dir.getPath();
        }
        dir = new File("web", dir.toString());
        if (dir.exists()) {
            return dir.getPath();
        }
        throw new IllegalStateException("Could not find webapp path");
    }

    public void testSpringServletContext() throws Exception {
        assertDispatcherServletConfigLoads("spring");
    }

    public void testPublicServletContext() throws Exception {
        assertDispatcherServletConfigLoads("public");
    }

    public void testSetupServletContext() throws Exception {
        assertDispatcherServletConfigLoads("setup");
    }

    public void testRestfulApiServletContext() throws Exception {
        assertDispatcherServletConfigLoads("restful-api");
    }

    public void testAllResourcesArePrototypeScope() throws Exception {
        ApplicationContext ctxt = createWebApplicationContextForServlet("restful-api", servletContext);

        String[] beanNames = ctxt.getBeanNamesForType(Resource.class);
        assertTrue("Should have found at least one resource", beanNames.length > 0);

        for (String beanName : beanNames) {
            Object firstTime = ctxt.getBean(beanName);
            Object secondTime = ctxt.getBean(beanName);
            assertNotSame(beanName + " is not a prototype", firstTime, secondTime);
        }
    }

    public void testAllUiControllersHaveAuthorizationInformation() throws Exception {
        ApplicationContext ctxt = createWebApplicationContextForServlet("spring", servletContext);

        String[] beanNames = ctxt.getBeanNamesForType(Controller.class);
        Set<String> noAuthorizationInfo = new LinkedHashSet<String>();

        for (String beanName : beanNames) {
            Object controller = ctxt.getBean(beanName);
            if (!(controller instanceof PscAuthorizedHandler)) {
                noAuthorizationInfo.add(controller.getClass().getName());
            }
        }

        if (!noAuthorizationInfo.isEmpty()) {
            StringBuilder msg = new StringBuilder().append("The following ").
                append(noAuthorizationInfo.size()).
                append(" controllers contain no unified authorization information and therefore will never execute: ");
            for (String s : noAuthorizationInfo) {
                msg.append("\n  ").append(s);
            }

            if (((LegacyModeSwitch) ctxt.getBean("authorizationLegacyModeSwitch")).isOn()) {
                System.out.println(msg.toString());
            } else {
                fail(msg.toString());
            }
        }
    }

    private void assertDispatcherServletConfigLoads(String servletName) throws Exception {
        try {
            createWebApplicationContextForServlet(servletName, servletContext);
        } catch (Exception e) {
            throw new StudyCalendarError("Loading the configuration for MVC servlet '" + servletName
                + "' failed:  " + e.getMessage(), e);
        }
    }
}
{'fraction_non_alphanumeric': 0.0927992482086221, 'fraction_numerical': 0.00023493480559144838, 'mean_word_length': 3.3087044534412957, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '836584', 'n_tokens_mistral': 2148, 'n_tokens_neox': 2058, 'n_words': 453}