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
138
139
140
141
142
143
144
145
146
147
148
149
150
|
package org.berzerkula.builddb.controllers;
import org.berzerkula.builddb.BuilddbConstants;
import org.berzerkula.builddb.config.SecurityConfig;
import org.berzerkula.builddb.models.Pkg;
import org.berzerkula.builddb.repositories.TestH2PkgRepository;
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.List;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
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 BuilddbPkgControllerTest {
@Autowired
private TestH2PkgRepository pkgRepository;
@Autowired
private MockMvc mockMvc;
@Test
@WithMockUser(roles=BuilddbConstants.ROLE_CLIENT)
void shouldReturnEmptyPackageListView() throws Exception {
this.mockMvc.perform(get("/pkgs"))
.andExpect(status().isOk())
.andExpect(view().name("pkgs/index"))
.andDo(print());
}
@Test
@WithMockUser(roles=BuilddbConstants.ROLE_CLIENT)
void shouldReturnAddPackageView() throws Exception {
this.mockMvc.perform(get("/pkgs/add"))
.andExpect(status().isOk())
.andExpect(view().name("pkgs/add"))
.andDo(print());
}
@Test
@WithMockUser(roles=BuilddbConstants.ROLE_CLIENT)
void shouldReturnEditPackageView() throws Exception {
Pkg pkg = new Pkg(1, "test", "1.2.3", "", "", "", "", "", "");
pkgRepository.save(pkg);
List<Pkg> pkgs = pkgRepository.findAll();
pkg = pkgs.get(0);
Integer id = pkg.getId();
this.mockMvc.perform(get("/pkgs/edit?id=" + id))
.andExpect(status().isOk())
.andExpect(view().name("pkgs/edit"))
.andDo(print());
}
@Test
@WithMockUser(roles=BuilddbConstants.ROLE_CLIENT)
void shouldGetPopupWhenDeletePackage() throws Exception {
Pkg pkg = new Pkg(1, "test", "1.2.3", "", "", "", "", "", "");
pkgRepository.save(pkg);
List<Pkg> pkgs = pkgRepository.findAll();
pkg = pkgs.get(0);
Integer id = pkg.getId();
this.mockMvc.perform(get("/pkgs/delete?id=" + id))
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/pkgs/"))
.andDo(print());
}
@Test
@WithMockUser(roles=BuilddbConstants.ROLE_CLIENT)
void shouldAddPkgValidationError() throws Exception {
mockMvc.perform(post("/pkgs/add")
.with(csrf())
.formField("sequence", "")
.formField("name", "")
.formField("version", "")
.formField("configure", "")
.formField("build", "")
.formField("install", "")
.formField("setup", "")
.formField("notes", "")
.formField("url", ""))
.andExpect(status().isOk())
.andExpect(view().name("pkgs/add"))
.andDo(print());
}
@Test
@WithMockUser(roles=BuilddbConstants.ROLE_CLIENT)
void shouldAddPkgSuccess() throws Exception {
mockMvc.perform(post("/pkgs/add")
.with(csrf())
.formField("sequence", "123")
.formField("name", "test")
.formField("version", "1.2.3-test")
.formField("configure", "testconf")
.formField("build", "testbuild")
.formField("install", "testinstall")
.formField("setup", "testsetup")
.formField("notes", "testnotes")
.formField("url", "http://test.org/test-1.2.3.tar.gz"))
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/pkgs"))
.andDo(print());
}
@Test
@WithMockUser(roles=BuilddbConstants.ROLE_CLIENT)
void shouldEditPkgSuccess() throws Exception {
Pkg pkg = new Pkg(123, "test", "1.2.3", "", "", "", "", "", "");
pkgRepository.save(pkg);
List<Pkg> pkgs = pkgRepository.findAll();
pkg = pkgs.get(0);
Integer id = pkg.getId();
this.mockMvc.perform(post("/pkgs/edit?id=" + id)
.with(csrf())
.formField("sequence", "123")
.formField("name", "test")
.formField("version", "4.5.6-test")
.formField("configure", "")
.formField("build", "")
.formField("install", "")
.formField("setup", "")
.formField("notes", "")
.formField("url", ""))
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/pkgs/#" + id))
.andDo(print());
}
}
|