-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuthenticationTest.java
More file actions
70 lines (57 loc) · 2.34 KB
/
AuthenticationTest.java
File metadata and controls
70 lines (57 loc) · 2.34 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package io.es.web;
import io.es.entity.User;
import io.es.repository.RepositoriesInitializer;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class AuthenticationTest {
@Autowired
private MockMvc mvc;
@Autowired
private RepositoriesInitializer initializer;
private final User user = User.builder().username("guest").password("q").build();
@Before
public void initialize() {
initializer.initialize();
}
@Test
public void login() throws Exception {
mvc.perform(formLogin().user(user.getUsername()).password(user.getPassword())).
andExpect(authenticated());
}
@Test
public void loginFailed() throws Exception {
mvc.perform(formLogin().user(user.getUsername()).password("<incorrect>")).
andExpect(unauthenticated());
}
@Test
public void accessProtectedResourcesAuthenticated() throws Exception {
mvc.perform(get("/api/resources").session(getSession())).
andExpect(status().isOk());
}
@Test
public void accessProtectedResourcesUnauthenticated() throws Exception {
mvc.perform(get("/api/resources")).
andExpect(status().is4xxClientError());
}
private MockHttpSession getSession() throws Exception {
return (MockHttpSession)
mvc.perform(formLogin().user(user.getUsername()).password(user.getPassword())).
andExpect(authenticated()).
andReturn().getRequest().getSession(false);
}
}