|
| 1 | +/* |
| 2 | + * Copyright 2016 ThoughtWorks, Inc. |
| 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 | + */ |
| 16 | + |
| 17 | +package com.thoughtworks.go.server.controller; |
| 18 | + |
| 19 | +import com.thoughtworks.go.i18n.Localizer; |
| 20 | +import com.thoughtworks.go.plugin.access.authentication.AuthenticationPluginRegistry; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 24 | +import org.springframework.ui.ModelMap; |
| 25 | +import org.springframework.web.servlet.ModelAndView; |
| 26 | + |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import static org.hamcrest.core.Is.is; |
| 30 | +import static org.junit.Assert.assertThat; |
| 31 | +import static org.mockito.Mockito.mock; |
| 32 | + |
| 33 | +public class AuthorizationControllerTest { |
| 34 | + |
| 35 | + Localizer localizer; |
| 36 | + AuthenticationPluginRegistry authenticationPluginRegistry; |
| 37 | + |
| 38 | + private AuthorizationController authorizationController; |
| 39 | + private MockHttpServletResponse response; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setUp() throws Exception { |
| 43 | + authenticationPluginRegistry = mock(AuthenticationPluginRegistry.class); |
| 44 | + localizer = mock(Localizer.class); |
| 45 | + |
| 46 | + authorizationController = new AuthorizationController(localizer, authenticationPluginRegistry); |
| 47 | + response = new MockHttpServletResponse(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void shouldAddCacheControlHeaderToTheResponse() throws Exception { |
| 52 | + authorizationController.login(false, response); |
| 53 | + |
| 54 | + assertThat(response.getHeader("Cache-Control"), is("no-cache, must-revalidate, no-store")); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void shouldAddPragmaHeaderToTheResponse() throws Exception { |
| 59 | + authorizationController.login(false, response); |
| 60 | + |
| 61 | + assertThat(response.getHeader("Pragma"), is("no-cache")); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void shouldSetModel() throws Exception { |
| 66 | + ModelAndView responseModel = authorizationController.login(false, response); |
| 67 | + |
| 68 | + Map<String, Object> modelMap = new ModelMap() {{ |
| 69 | + put("login_error", false); |
| 70 | + put("l", localizer); |
| 71 | + put("authentication_plugin_registry", authenticationPluginRegistry); |
| 72 | + }}; |
| 73 | + |
| 74 | + Map<String, Object> responseModelMap = responseModel.getModel(); |
| 75 | + assertThat(responseModelMap, is(modelMap)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void shouldRedirectToGo() throws Exception { |
| 80 | + authorizationController.securityCheckHandlerWhenAuthenticationProcessingFilterIsOff(response); |
| 81 | + |
| 82 | + assertThat(response.getRedirectedUrl(), is("/go")); |
| 83 | + } |
| 84 | +} |
0 commit comments