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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
package org.berzerkula.builddb.controllers;
import org.berzerkula.builddb.config.SecurityConfig;
import org.berzerkula.builddb.models.AppUser;
import org.berzerkula.builddb.repositories.AppUserRepository;
import org.junit.jupiter.api.Test;
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.context.annotation.Import;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import java.util.Date;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
@Import(SecurityConfig.class)
@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class BuilddbAccountControllerTest {
@Autowired
private AppUserRepository appUserRepository;
@Autowired
private MockMvc mockMvc;
@Test
void shouldReturnIndexOk() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(view().name("index"))
.andDo(print());
}
@Test
void shouldReturnLoginOk() throws Exception {
mockMvc.perform(get("/login"))
.andExpect(status().isOk())
.andExpect(view().name("login"))
.andDo(print());
}
@Test
void shouldProfileRedirectToLogin() throws Exception {
mockMvc.perform(get("/profile"))
.andExpect(status().is3xxRedirection())
.andDo(print());
}
@WithMockUser(roles="client")
@Test
void shouldReturnProfileView() throws Exception {
AppUser appUser = new AppUser();
appUser.setFirstName("John");
appUser.setLastName("Doe");
appUser.setEmail("test@berzerkula.org");
appUser.setPhone("555-555-5555");
appUser.setAddress("612 Wolf Avenue");
appUser.setRole("client");
appUser.setPassword("password");
appUser.setCreatedAt(new Date());
appUserRepository.save(appUser);
this.mockMvc.perform(get("/profile")
.with(user("test@berzerkula.org")))
.andExpect(status().isOk())
.andExpect(view().name("profile"))
.andDo(print());
}
@Test
void shouldReturnRegisterOk() throws Exception {
mockMvc.perform(get("/register"))
.andExpect(status().isOk())
.andExpect(view().name("register"))
.andDo(print());
}
@Test
void shouldRegisterRequiredValidationError() throws Exception {
mockMvc.perform(post("/register")
.with(csrf())
.formField("firstName", "")
.formField("lastName", "")
.formField("email", "")
.formField("phone", "")
.formField("address", "")
.formField("password", "")
.formField("confirmPassword", ""))
.andExpect(status().isOk())
.andExpect(view().name("register"))
.andDo(print());
}
@Test
void shouldRegisterPasswordValidationError() throws Exception {
mockMvc.perform(post("/register")
.with(csrf())
.formField("firstName", "John")
.formField("lastName", "Doe")
.formField("email", "test@berzerkula.org")
.formField("phone", "")
.formField("address", "")
.formField("password", "password")
.formField("confirmPassword", "passwrd"))
.andExpect(status().isOk())
.andExpect(view().name("register"))
.andDo(print());
}
@Test
void shouldRegisterSuccess() throws Exception {
mockMvc.perform(post("/register")
.with(csrf())
.formField("firstName", "John")
.formField("lastName", "Doe")
.formField("email", "test@berzerkula.org")
.formField("phone", "555-555-5555")
.formField("address", "612 Wolf Avenue")
.formField("password", "password")
.formField("confirmPassword", "password"))
.andExpect(status().isOk())
.andExpect(view().name("register"))
.andDo(print());
}
}
|