-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22c93d3
commit 1baccd3
Showing
10 changed files
with
242 additions
and
95 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/inttest/java/com/faforever/api/config/MockConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
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); | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
src/inttest/java/com/faforever/api/user/UserControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package com.faforever.api.user; | ||
|
||
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.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.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; | ||
|
||
@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 changePassword_success() throws Exception { | ||
MultiValueMap<String, String> params = new HttpHeaders(); | ||
params.add("currentPassword", AUTH_USER); | ||
params.add("newPassword", "newPassword"); | ||
|
||
RequestPostProcessor oauthToken = oAuthHelper.addBearerToken(Sets.newHashSet(OAuthScope._WRITE_ACCOUNT_DATA)); | ||
mockMvc.perform(post("/users/changePassword").with(oauthToken).params(params)) | ||
.andExpect(status().isOk()); | ||
|
||
User user = userRepository.findOneByLoginIgnoreCase(AUTH_USER).get(); | ||
assertEquals(user.getPassword(), "5c29a959abce4eda5f0e7a4e7ea53dce4fa0f0abbe8eaa63717e2fed5f193d31"); | ||
} | ||
|
||
@Test | ||
@WithUserDetails(AUTH_USER) | ||
public void changePassword_wrongScope() throws Exception { | ||
MultiValueMap<String, String> params = new HttpHeaders(); | ||
params.add("currentPassword", AUTH_USER); | ||
params.add("newPassword", "newPassword"); | ||
|
||
RequestPostProcessor oauthToken = oAuthHelper.addBearerToken(Collections.emptySet()); | ||
mockMvc.perform(post("/users/changePassword").with(oauthToken).params(params)) | ||
.andExpect(status().isForbidden()); | ||
} | ||
|
||
@Test | ||
@WithUserDetails(AUTH_USER) | ||
public void changePassword_wrongPassword() throws Exception { | ||
MultiValueMap<String, String> params = new HttpHeaders(); | ||
params.add("currentPassword", "wrongPassword"); | ||
params.add("newPassword", "newPassword"); | ||
|
||
RequestPostProcessor oauthToken = oAuthHelper.addBearerToken(Sets.newHashSet(OAuthScope._WRITE_ACCOUNT_DATA)); | ||
MvcResult mvcResult = mockMvc.perform(post("/users/changePassword").with(oauthToken).params(params)) | ||
.andExpect(status().is4xxClientError()) | ||
.andReturn(); | ||
|
||
assertApiError(mvcResult, ErrorCode.PASSWORD_CHANGE_FAILED_WRONG_PASSWORD); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
DELETE FROM oauth_clients; | ||
DELETE FROM login; | ||
|
||
INSERT INTO oauth_clients (id, name, client_secret, client_type, redirect_uris, default_redirect_uri, default_scope) | ||
VALUES ('test', 'test', 'test', 'public', 'http://localhost', 'http://localhost', | ||
'read_events read_achievements upload_map upload_mod write_account_data'); | ||
|
||
INSERT INTO login (id, login, email, password) | ||
VALUES (1, 'USER', 'user@faforever.com', '92b7b421992ef490f3b75898ec0e511f1a5c02422819d89719b20362b023ee4f'); | ||
INSERT INTO login (id, login, email, password) | ||
VALUES (2, 'MODERATOR', 'moderator@faforever.com', '778ac5b81fa251b450f827846378739caee510c31b01cfa9d31822b88bed8441'); | ||
INSERT INTO login (id, login, email, password) | ||
VALUES (3, 'ADMIN', 'admin@faforever.com', '835d6dc88b708bc646d6db82c853ef4182fabbd4a8de59c213f2b5ab3ae7d9be'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 0 additions & 92 deletions
92
src/test/java/com/faforever/api/data/JsonApiNameIntegrationTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.