-
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 7c5bb4e
Showing
9 changed files
with
221 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
109 changes: 109 additions & 0 deletions
109
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,109 @@ | ||
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.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) | ||
@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.
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,45 @@ | ||
package com.faforever.integration; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.common.OAuth2AccessToken; | ||
import org.springframework.security.oauth2.provider.OAuth2Authentication; | ||
import org.springframework.security.oauth2.provider.OAuth2Request; | ||
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices; | ||
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.test.web.servlet.request.RequestPostProcessor; | ||
|
||
import java.util.Set; | ||
|
||
@Component | ||
public class OAuthHelper { | ||
|
||
@Autowired | ||
AuthorizationServerTokenServices tokenservice; | ||
|
||
@Autowired | ||
JwtAccessTokenConverter jwtAccessTokenConverter; | ||
|
||
public RequestPostProcessor addBearerToken(Set<String> scope) { | ||
return mockRequest -> { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
OAuth2Request oauth2Request = createOAuth2Request(scope); | ||
OAuth2Authentication oauth2auth = new OAuth2Authentication(oauth2Request, authentication); | ||
OAuth2AccessToken token = tokenservice.createAccessToken(oauth2auth); | ||
token = jwtAccessTokenConverter.enhance(token, oauth2auth); | ||
|
||
// Set Authorization header to use Bearer | ||
mockRequest.addHeader("Authorization", "Bearer " + token.getValue()); | ||
return mockRequest; | ||
}; | ||
} | ||
|
||
@NotNull | ||
private OAuth2Request createOAuth2Request(Set<String> scope) { | ||
return new OAuth2Request(null, "test", null, true, scope, null, null, null, null); | ||
} | ||
|
||
} |