/* * This file is part of ARSnova Backend. * Copyright (C) 2012-2017 The ARSnova Team * * ARSnova Backend is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * ARSnova Backend is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package de.thm.arsnova.controller; import de.thm.arsnova.services.StubUserService; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import static org.junit.Assert.assertEquals; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; public class LoginControllerTest extends AbstractControllerTest { @Autowired private StubUserService userService; private MockMvc mockMvc; @Autowired private WebApplicationContext webApplicationContext; @Before public void setup() { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test public void testGuestLogin() throws Exception { mockMvc.perform( get("/doLogin") .param("type", "guest") ).andExpect(status().isOk()); } @Test public void testReuseGuestLogin() throws Exception { mockMvc.perform( get("/doLogin") .param("type", "guest").param("user","Guest1234567890") ).andExpect(status().isOk()); final Authentication auth = SecurityContextHolder.getContext() .getAuthentication(); assertEquals(auth.getClass(), UsernamePasswordAuthenticationToken.class); assertEquals("Guest1234567890", auth.getName()); } @Test @Ignore("Causes 'ServletException: Circular view path' for an unknown reason.") public void testUser() throws Exception { userService.setUserAuthenticated(true); mockMvc.perform(get("/whoami").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.username").value("ptsr00")) .andExpect(jsonPath("$.type").value("ldap")); } @Test public void testLogoutWithoutRedirect() throws Exception { mockMvc.perform(get("/logout")) .andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl("/")); } }