@@ -0,0 +1,3322 @@
+package com.excilys.shooflers.dashboard.server;%0A%0Aimport com.excilys.shooflers.dashboard.server.property.DashboardProperties;%0Aimport com.excilys.shooflers.dashboard.server.security.interceptor.CorsInterceptor;%0Aimport org.apache.commons.codec.binary.Base64;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.boot.test.SpringApplicationConfiguration;%0Aimport org.springframework.test.context.junit4.SpringJUnit4ClassRunner;%0Aimport org.springframework.test.context.web.WebAppConfiguration;%0Aimport org.springframework.test.web.servlet.MockMvc;%0Aimport org.springframework.test.web.servlet.setup.MockMvcBuilders;%0Aimport org.springframework.web.context.WebApplicationContext;%0A%0Aimport java.net.URI;%0A%0Aimport static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;%0Aimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;%0A%0A/**%0A * Test the Security Feature.%0A *%0A * @author Mickael%0A */%0A@RunWith(SpringJUnit4ClassRunner.class)%0A@SpringApplicationConfiguration(DashboardApplication.class)%0A@WebAppConfiguration%0Apublic class RequireValidApikeyTest %7B%0A%0A    public static final String HEADER_AUTHORIZATION = %22Authorization%22;%0A%0A    /**%0A     * Client Rest%0A     */%0A    private MockMvc mockMvc;%0A%0A    @Autowired%0A    private DashboardProperties props;%0A%0A    @Autowired%0A    private WebApplicationContext webApplicationContext;%0A%0A    /**%0A     * @return Content of header to authenticate to the API REST with default admin credentials.%0A     * (withtout the key work Basic at the start)%0A     */%0A    private String getHeaderAuthenticationContent() %7B%0A        return getHeaderAuthenticationContent(props.getAdminLogin(), props.getAdminPassword());%0A    %7D%0A%0A    /**%0A     * Content of header to authenticate to the API REST with default admin credentials.%0A     *%0A     * @param login    Login to login%0A     * @param password Password to use%0A     * @return Content of the header Authentication%0A     */%0A    private String getHeaderAuthenticationContent(String login, String password) %7B%0A        return Base64.encodeBase64String((login + %22:%22 + password).getBytes());%0A    %7D%0A%0A    @Before%0A    public void setUp() throws Exception %7B%0A        // Set up the REST client%0A        this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();%0A    %7D%0A%0A    @Test%0A    public void bundleNotAccessible() throws Exception %7B%0A        mockMvc.perform(get(URI.create(%22/bundle%22)))%0A                .andExpect(status().isUnauthorized())%0A        ;%0A    %7D%0A%0A    @Test%0A    public void attemptToUseAuthentication() throws Exception %7B%0A        mockMvc.perform(get(URI.create(%22/bundle%22)).header(HEADER_AUTHORIZATION, getHeaderAuthenticationContent(%22admin%22, %22admin%22)))%0A                .andExpect(status().isUnauthorized())%0A        ;%0A    %7D%0A%0A    @Test%0A    public void goodApikey() throws Exception %7B%0A        mockMvc.perform(get(URI.create(%22/bundle%22)).header(CorsInterceptor.HEADER_API_KEY, props.getApiKey()))%0A                .andExpect(status().isOk())%0A        ;%0A    %7D%0A%0A    @Test%0A    public void wrongApikey() throws Exception %7B%0A        mockMvc.perform(get(URI.create(%22/bundle%22)).header(CorsInterceptor.HEADER_API_KEY, %22phoenix wrong%22))%0A                .andExpect(status().isUnauthorized())%0A        ;%0A    %7D%0A%7D%0A

Test @REquireValidApiKey