diff --git a/src/inttest/java/com/faforever/api/AbstractIntegrationTest.java b/src/inttest/java/com/faforever/api/AbstractIntegrationTest.java new file mode 100644 index 000000000..05ef5e557 --- /dev/null +++ b/src/inttest/java/com/faforever/api/AbstractIntegrationTest.java @@ -0,0 +1,54 @@ +package com.faforever.api; + +import com.faforever.api.error.ErrorCode; +import com.faforever.integration.OAuthHelper; +import org.json.JSONObject; +import org.junit.Before; +import org.junit.runner.RunWith; +import org.skyscreamer.jsonassert.JSONAssert; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.jdbc.Sql.ExecutionPhase; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import javax.transaction.Transactional; + +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@ActiveProfiles("integration-test") +@Import(OAuthHelper.class) +@Transactional +@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/createUsers.sql") +public abstract class AbstractIntegrationTest { + protected final static String AUTH_USER = "USER"; + protected final static String AUTH_MODERATOR = "MODERATOR"; + protected final static String AUTH_ADMIN = "ADMIN"; + @Autowired + protected OAuthHelper oAuthHelper; + protected MockMvc mockMvc; + @Autowired + protected WebApplicationContext context; + + @Before + public void setUp() { + this.mockMvc = MockMvcBuilders + .webAppContextSetup(this.context) + .apply(springSecurity()) + .build(); + } + + protected void assertApiError(MvcResult mvcResult, ErrorCode errorCode) throws Exception { + JSONObject resonseJson = new JSONObject(mvcResult.getResponse().getContentAsString()); + JSONAssert.assertEquals(String.format("{\"errors\":[{\"code\":\"%s\"}]}", errorCode.getCode()), resonseJson, false); + } +} diff --git a/src/inttest/java/com/faforever/api/config/MockConfiguration.java b/src/inttest/java/com/faforever/api/config/MockConfiguration.java deleted file mode 100644 index e6386e0a5..000000000 --- a/src/inttest/java/com/faforever/api/config/MockConfiguration.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.faforever.api.config; - -import com.faforever.api.user.AnopeUserRepository; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; -import org.springframework.context.annotation.Profile; - -import static org.mockito.Mockito.mock; - -@Profile("integration-test") -@Configuration -public class MockConfiguration { - @Bean - @Primary - public AnopeUserRepository anopeUserRepository() { - return mock(AnopeUserRepository.class); - } -} diff --git a/src/inttest/java/com/faforever/api/user/UserControllerTest.java b/src/inttest/java/com/faforever/api/user/UserControllerTest.java index 914667d46..a3f5b747d 100644 --- a/src/inttest/java/com/faforever/api/user/UserControllerTest.java +++ b/src/inttest/java/com/faforever/api/user/UserControllerTest.java @@ -1,72 +1,37 @@ package com.faforever.api.user; + +import com.faforever.api.AbstractIntegrationTest; import com.faforever.api.data.domain.User; import com.faforever.api.error.ErrorCode; import com.faforever.api.security.OAuthScope; -import com.faforever.integration.OAuthHelper; import com.google.common.collect.Sets; -import org.json.JSONObject; -import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.skyscreamer.jsonassert.JSONAssert; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.context.annotation.Import; +import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.HttpHeaders; import org.springframework.security.test.context.support.WithUserDetails; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.jdbc.Sql; -import org.springframework.test.context.jdbc.Sql.ExecutionPhase; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.request.RequestPostProcessor; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.util.MultiValueMap; -import org.springframework.web.context.WebApplicationContext; -import javax.transaction.Transactional; import java.util.Collections; import static junitx.framework.Assert.assertEquals; -import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -@ActiveProfiles("integration-test") -@Import(OAuthHelper.class) -@Transactional -@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/createUsers.sql") -public class UserControllerTest { - protected final static String AUTH_USER = "USER"; - protected final static String AUTH_MODERATOR = "MODERATOR"; - protected final static String AUTH_ADMIN = "ADMIN"; - MockMvc mockMvc; - @Autowired - private WebApplicationContext context; - @Autowired - private OAuthHelper oAuthHelper; +public class UserControllerTest extends AbstractIntegrationTest { + @MockBean + private AnopeUserRepository anopeUserRepository; @Autowired private UserRepository userRepository; - @Before - public void setUp() { - this.mockMvc = MockMvcBuilders - .webAppContextSetup(this.context) - .apply(springSecurity()) - .build(); - } - - void assertApiError(MvcResult mvcResult, ErrorCode errorCode) throws Exception { - JSONObject resonseJson = new JSONObject(mvcResult.getResponse().getContentAsString()); - JSONAssert.assertEquals(String.format("{\"errors\":[{\"code\":\"%s\"}]}", errorCode.getCode()), resonseJson, false); - } - @Test @WithUserDetails(AUTH_USER) public void changePasswordWithSuccess() throws Exception { @@ -80,6 +45,7 @@ public void changePasswordWithSuccess() throws Exception { User user = userRepository.findOneByLoginIgnoreCase(AUTH_USER).get(); assertEquals(user.getPassword(), "5c29a959abce4eda5f0e7a4e7ea53dce4fa0f0abbe8eaa63717e2fed5f193d31"); + verify(anopeUserRepository, times(1)).updatePassword(eq(AUTH_USER), anyString()); } @Test