Skip to content

Commit

Permalink
Merge branch 'feature/controller/user-controller' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
serjihsklovski committed Jun 20, 2017
2 parents 772e1a2 + f267087 commit 6077348
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 39 deletions.
10 changes: 6 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
<!-- Hibernate -->
<hibernate.version>5.2.10.Final</hibernate.version>

<!--Jackson java8time Serializer/Deserializer-->
<jackson-datatype-jsr310.version>2.8.5</jackson-datatype-jsr310.version>

<!--Google Guava-->
<guava.version>22.0</guava.version>
</properties>
Expand Down Expand Up @@ -136,7 +133,6 @@
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson-datatype-jsr310.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
Expand All @@ -146,6 +142,12 @@
<version>${guava.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.primitives.Longs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
Expand Down Expand Up @@ -181,7 +182,7 @@ public User registerUser(@RequestBody User user,
userRepository.save(user);
}

response.setHeader("Location", "/users/" + user.getId());
response.setHeader(HttpHeaders.LOCATION, "/users/" + user.getId());
return user;
}

Expand Down Expand Up @@ -245,7 +246,7 @@ public void patchUser(@PathVariable Long id,

} else if (auth.getName().equals(user.getEmail())) {

if (username != null) user.setUsername(firstName);
if (username != null) user.setUsername(username);
if ((available != null) && (!available)) user.setAvailable(false);
if (firstName != null) user.setFirstName(firstName);
if (lastName != null) user.setLastName(lastName);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/teapot/backend/model/UserAuthority.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public enum UserAuthority implements GrantedAuthority {

@Override
public String getAuthority() {
return name();
return "ROLE_" + name();
}
}
4 changes: 2 additions & 2 deletions src/main/resources/messages_ru.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mail.confirm.text=Нажмите на ссылку, чтобы подтвердить вам e-mail адрес: {0}
mail.confirm.subject=[Teapot.org] Подтверждение регистрации
mail.confirm.text=Нажмите на ссылку, чтобы подтвердить Ваш e-mail адрес: {0}
mail.confirm.subject=[Teapot.org] Подтверждение регистрации
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,63 @@

import org.junit.Assert;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JacksonJsonParser;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.mock.http.MockHttpOutputMessage;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;
import org.teapot.backend.model.User;
import org.teapot.backend.model.UserAuthority;
import org.teapot.backend.repository.UserRepository;
import org.teapot.backend.test.AbstractIT;

import javax.transaction.Transactional;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public abstract class AbstractControllerIT extends AbstractIT {

private static final String CLIENT_ID = "client";
private static final String CLIENT_SECRET = "secret";

protected MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(),
Charset.forName("utf8"));

protected MockMvc mockMvc;

protected JacksonJsonParser parser = new JacksonJsonParser();

private HttpMessageConverter mappingJackson2HttpMessageConverter;

@Autowired
protected UserRepository userRepository;

@Autowired
protected PasswordEncoder passwordEncoder;

protected User userWithAdminRole = new User();
protected User userWithUserRole = new User();

protected String adminAccessToken;
protected String userAccessToken;

@Autowired
private void setConverters(HttpMessageConverter<?>[] converters) {
mappingJackson2HttpMessageConverter = Arrays.stream(converters)
Expand All @@ -41,16 +68,57 @@ private void setConverters(HttpMessageConverter<?>[] converters) {
}

@Autowired
public void setMockMvc(WebApplicationContext webApplicationContext) throws Exception {
public void setMockMvc(WebApplicationContext wac, FilterChainProxy filter) throws Exception {
mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.webAppContextSetup(wac)
.addFilters(filter)
.build();
}

@SuppressWarnings("unchecked")
protected String json(Object object) throws IOException {
MockHttpOutputMessage mockHttpOutputMessage = new MockHttpOutputMessage();
mappingJackson2HttpMessageConverter.write(
object, MediaType.APPLICATION_JSON, mockHttpOutputMessage);
return mockHttpOutputMessage.getBodyAsString();
}

private String obtainAccessToken(String email, String password) throws Exception {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("grant_type", "password");
params.add("username", email);
params.add("password", password);

String resultString = mockMvc.perform(post("/oauth/token")
.params(params)
.with(httpBasic(CLIENT_ID, CLIENT_SECRET))
.accept(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andReturn().getResponse().getContentAsString();


return parser.parseMap(resultString).get("access_token").toString();
}

@Before
public void obtainAccessTokens() throws Exception {
userRepository.deleteAllInBatch();

userWithAdminRole.setEmail("admin@auth.com");
userWithAdminRole.setUsername("admin");
userWithAdminRole.setPassword(passwordEncoder.encode("pass"));
userWithAdminRole.setAuthority(UserAuthority.ADMIN);
userWithAdminRole.setActivated(true);
userRepository.save(userWithAdminRole);
adminAccessToken = obtainAccessToken(userWithAdminRole.getEmail(), "pass");

userWithUserRole.setEmail("user@auth.com");
userWithUserRole.setUsername("user");
userWithUserRole.setPassword(passwordEncoder.encode("pass"));
userWithUserRole.setAuthority(UserAuthority.USER);
userWithUserRole.setActivated(true);
userRepository.save(userWithUserRole);
userAccessToken = obtainAccessToken(userWithUserRole.getEmail(), "pass");
}
}
Loading

0 comments on commit 6077348

Please sign in to comment.