This repository was archived by the owner on Mar 14, 2023. It is now read-only.
forked from JavaOPs/topjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestUtil.java
More file actions
35 lines (27 loc) · 1.47 KB
/
Copy pathTestUtil.java
File metadata and controls
35 lines (27 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package ru.javawebinar.topjava;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.RequestPostProcessor;
import ru.javawebinar.topjava.model.User;
import java.io.UnsupportedEncodingException;
public class TestUtil {
public static ResultActions print(ResultActions action) throws UnsupportedEncodingException {
System.out.println(getContent(action));
return action;
}
public static String getContent(ResultActions action) throws UnsupportedEncodingException {
return action.andReturn().getResponse().getContentAsString();
}
public static void mockAuthorize(User user) {
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(new AuthorizedUser(user), null, user.getRoles()));
}
public static RequestPostProcessor userHttpBasic(User user) {
return SecurityMockMvcRequestPostProcessors.httpBasic(user.getEmail(), user.getPassword());
}
public static RequestPostProcessor userAuth(User user) {
return SecurityMockMvcRequestPostProcessors.authentication(new UsernamePasswordAuthenticationToken(user.getEmail(), user.getPassword()));
}
}