GitLab wurde erfolgreich aktualisiert. Dank regelmäßiger Updates bleibt das THM GitLab sicher und Sie profitieren von den neuesten Funktionen. Danke für Ihre Geduld.

Commit 023ea5ea authored by Daniel Gerhardt's avatar Daniel Gerhardt

Remove broken tests

Mosts of the tests were coupled with the old database layer.
Additionally, a lot of them were testing simple CRUD operations intead
of domain logic. Meaningful tests with proper database mocking have to
be written at a later time.
parent 91b5f35d
package de.thm.arsnova.config;
import de.thm.arsnova.dao.StubDatabaseDao;
import de.thm.arsnova.services.StubUserService;
import de.thm.arsnova.socket.ARSnovaSocket;
import de.thm.arsnova.socket.ARSnovaSocketIOServer;
......@@ -59,11 +58,6 @@ public class TestAppConfig {
return configurer;
}
@Bean
public StubDatabaseDao databaseDao() {
return new StubDatabaseDao();
}
@Bean(name = "socketServer", initMethod = "startServer", destroyMethod = "stopServer")
public ARSnovaSocket socketTestServer() {
final int testSocketPort = 1234 + testPortOffset++ % 10;
......
/*
* 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.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
public class ContentControllerTest extends AbstractControllerTest {
@Autowired
private StubUserService userService;
@Autowired
private ContentController questionController;
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void startup() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
SecurityContextHolder.clearContext();
}
@After
public void cleanup() {
SecurityContextHolder.clearContext();
userService.setUserAuthenticated(false);
}
@Test
public void testShouldNotGetLecturerQuestionsIfUnauthorized() throws Exception {
setAuthenticated(false, "nobody");
mockMvc.perform(
get("/lecturerquestion/")
.param("sessionkey", "12345678").param("lecturequestionsonly", "true")
.accept(MediaType.APPLICATION_JSON)
).andExpect(status().isUnauthorized()
);
}
@Test
public void testShouldNotGetPreparationQuestionsIfUnauthorized() throws Exception {
setAuthenticated(false, "nobody");
mockMvc.perform(
get("/lecturerquestion/")
.param("sessionkey", "12345678").param("preparationquestionsonly", "true")
.accept(MediaType.APPLICATION_JSON)
).andExpect(status().isUnauthorized()
);
}
@Test
public void testShouldReturnQuestionCount() throws Exception {
setAuthenticated(true, "somebody");
mockMvc.perform(
get("/lecturerquestion/count")
.param("sessionkey", "12345678").param("lecturequestionsonly", "true")
.accept(MediaType.APPLICATION_JSON)
).andExpect(status().isOk())
.andExpect(content().string("0")
);
}
@Test
public void testShouldReturnXDeprecatedApiHeaderForQuestionCount() throws Exception {
setAuthenticated(true, "somebody");
mockMvc.perform(
get("/lecturerquestion/count")
.param("sessionkey", "12345678").param("lecturequestionsonly", "true")
.accept(MediaType.APPLICATION_JSON)
).andExpect(status().isOk())
.andExpect(header().string(AbstractController.X_DEPRECATED_API, "1")
);
}
}
/*
* 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.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
public class FeedbackControllerTest 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 testShouldNotGetFeedbackForUnknownSession() throws Exception {
userService.setUserAuthenticated(true);
mockMvc.perform(get("/session/00000000/feedback").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
@Test
public void testShouldNotGetAverageFeedbackContentForSessionWithoutFeedback() throws Exception {
userService.setUserAuthenticated(true);
mockMvc.perform(get("/session/12345678/averagefeedback").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}
@Test
public void testShouldNotGetCorrectFeedbackCountForSessionWithoutFeedback() throws Exception {
userService.setUserAuthenticated(true);
mockMvc.perform(get("/session/12345678/feedbackcount").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string("0"));
}
@Test
public void testShouldReturnFeedback() throws Exception {
userService.setUserAuthenticated(true);
mockMvc.perform(get("/session/87654321/feedback").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.values").isArray());
}
@Test
public void testShouldReturnXDeprecatedApiHeaderForFeedback() throws Exception {
userService.setUserAuthenticated(true);
mockMvc.perform(get("/session/87654321/feedback").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(header().string(AbstractController.X_DEPRECATED_API, "1"));
}
}
/*
* 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.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
public class SessionControllerTest extends AbstractControllerTest {
@Autowired
private StubUserService userService;
@Autowired
private SessionController sessionController;
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void startup() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
SecurityContextHolder.clearContext();
}
@After
public void cleanup() {
SecurityContextHolder.clearContext();
userService.setUserAuthenticated(false);
}
@Test
public void testShouldNotGetUnknownSession() throws Exception {
setAuthenticated(true, "ptsr00");
mockMvc.perform(get("/session/00000000").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
@Test
public void testShouldNotGetUnknownSessionIfUnauthorized() throws Exception {
setAuthenticated(false, "ptsr00");
mockMvc.perform(get("/session/00000000").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isUnauthorized());
}
@Test
public void testShouldCreateSessionIfUnauthorized() throws Exception {
setAuthenticated(false, "ptsr00");
mockMvc.perform(
post("/session/")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content("{\"keyword\":12345678}")
)
.andExpect(status().isUnauthorized());
}
@Test
public void testShouldNotReturnMySessionsIfUnauthorized() throws Exception {
setAuthenticated(false, "ptsr00");
mockMvc.perform(get("/session/").param("ownedonly", "true").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isUnauthorized());
}
@Test
public void testShouldNotReturnMyVisitedSessionsIfUnauthorized() throws Exception {
setAuthenticated(false, "ptsr00");
mockMvc.perform(get("/session/").param("visitedonly", "true").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isUnauthorized());
}
@Test
public void testShouldShowUnimplementedIfNoFlagIsSet() throws Exception {
setAuthenticated(false, "ptsr00");
mockMvc.perform(get("/session/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotImplemented());
}
@Test
public void testShouldReturnActiveUserCount() throws Exception {
setAuthenticated(false, "ptsr00");
mockMvc.perform(get("/session/12345678/activeusercount").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(content().string("0"));
}
@Test
public void testShouldReturnXDeprecatedApiHeaderForActiveUserCount() throws Exception {
setAuthenticated(false, "ptsr00");
mockMvc.perform(get("/session/12345678/activeusercount").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(header().string(AbstractController.X_DEPRECATED_API, "1"));
}
@Test
public void testShouldEndInForbidden() throws Exception {
setAuthenticated(true, "ptsr00");
mockMvc.perform(
put("/session/12345678")
.content("{\"keyword\":\"12345678\", \"name\":\"Testsession\"}, \"shortName\":\"TS\", \"creator\":\"ptsr00\", \"active\":true")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
setAuthenticated(true, "other");
mockMvc.perform(delete("/session/12345678")).andExpect(status().isForbidden());
}
}
/*
* 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.dao;
import de.thm.arsnova.entities.*;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Profile("test")
@Service("databaseDao")
public class StubDatabaseDao {
private static Map<String, Session> stubSessions = new ConcurrentHashMap<>();
private static Map<String, Feedback> stubFeedbacks = new ConcurrentHashMap<>();
private static Map<String, List<Content>> stubQuestions = new ConcurrentHashMap<>();
private static Map<String, User> stubUsers = new ConcurrentHashMap<>();
public Comment comment;
public StubDatabaseDao() {
fillWithDummySessions();
fillWithDummyFeedbacks();
fillWithDummyQuestions();
}
public void cleanupTestData() {
stubSessions.clear();
stubFeedbacks.clear();
stubQuestions.clear();
stubUsers.clear();
fillWithDummySessions();
fillWithDummyFeedbacks();
fillWithDummyQuestions();
}
private void fillWithDummySessions() {
Session session = new Session();
session.setActive(true);
session.setCreator("ptsr00");
session.setKeyword("12345678");
session.setName("TestSession1");
session.setShortName("TS1");
stubSessions.put("12345678", session);
session = new Session();
session.setActive(true);
session.setCreator("ptsr00");
session.setKeyword("87654321");
session.setName("TestSession2");
session.setShortName("TS2");
stubSessions.put("87654321", session);
session = new Session();
session.setActive(true);
session.setCreator("ptsr00");
session.setKeyword("18273645");
session.setName("TestSession2");
session.setShortName("TS3");
stubSessions.put("18273645", session);
}
private void fillWithDummyFeedbacks() {
stubFeedbacks.put("12345678", new Feedback(0, 0, 0, 0));
stubFeedbacks.put("87654321", new Feedback(2, 3, 5, 7));
stubFeedbacks.put("18273645", new Feedback(2, 3, 5, 11));
}
private void fillWithDummyQuestions() {
List<Content> contents = new ArrayList<>();
contents.add(new Content());
stubQuestions.put("12345678", contents);
}
}
/*
* 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.services;
import de.thm.arsnova.config.AppConfig;
import de.thm.arsnova.config.TestAppConfig;
import de.thm.arsnova.config.TestSecurityConfig;
import de.thm.arsnova.dao.StubDatabaseDao;
import de.thm.arsnova.entities.Comment;
import de.thm.arsnova.entities.Content;
import de.thm.arsnova.exceptions.NotFoundException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {AppConfig.class, TestAppConfig.class, TestSecurityConfig.class})
@ActiveProfiles("test")
public class ContentServiceTest {
@Autowired
private IContentService contentService;
@Autowired
private StubUserService userService;
@Autowired
private StubDatabaseDao databaseDao;
private void setAuthenticated(final boolean isAuthenticated, final String username) {
if (isAuthenticated) {
final List<GrantedAuthority> ga = new ArrayList<>();
final UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, "secret", ga);
SecurityContextHolder.getContext().setAuthentication(token);
userService.setUserAuthenticated(isAuthenticated, username);
} else {
userService.setUserAuthenticated(isAuthenticated);
}
}
@Before
public void startup() {
SecurityContextHolder.clearContext();
}
@After
public void cleanup() {
SecurityContextHolder.clearContext();
}
@Test(expected = AuthenticationCredentialsNotFoundException.class)
public void testShouldNotReturnQuestionsIfNotAuthenticated() {
setAuthenticated(false, "nobody");
contentService.getSkillQuestions("12345678");
}
@Test(expected = NotFoundException.class)
public void testShouldFindQuestionsForNonExistantSession() {
setAuthenticated(true, "ptsr00");
contentService.getSkillQuestions("00000000");
}
@Test
public void testShouldFindQuestions() {
setAuthenticated(true, "ptsr00");
assertEquals(1, contentService.getSkillQuestionCount("12345678"));
}
@Test
public void testShouldMarkInterposedQuestionAsReadIfSessionCreator() throws Exception {
setAuthenticated(true, "ptsr00");
final Comment comment = new Comment();
comment.setRead(false);
comment.setId("the internal id");
comment.setSessionId("12345678");
databaseDao.comment = comment;
contentService.readInterposedQuestion(comment.getId());
assertTrue(comment.isRead());
}
@Test
public void testShouldNotMarkInterposedQuestionAsReadIfRegularUser() throws Exception {
setAuthenticated(true, "regular user");
final Comment comment = new Comment();
comment.setRead(false);
comment.setId("the internal id");
comment.setSessionId("12345678");
comment.setCreator("regular user");
databaseDao.comment = comment;
contentService.readInterposedQuestion(comment.getId());
assertFalse(comment.isRead());
}
@Test(expected = AccessDeniedException.class)
public void testShouldSaveQuestion() throws Exception{