1
+ package com .example .springpipelinedemo .controller ;
2
+
3
+ import com .example .springpipelinedemo .SpringPipelineDemoApplication ;
4
+ import com .example .springpipelinedemo .model .User ;
5
+ import com .example .springpipelinedemo .service .UserService ;
6
+ import com .fasterxml .jackson .databind .ObjectMapper ;
7
+ import org .apache .tomcat .util .codec .binary .Base64 ;
8
+ import org .junit .Before ;
9
+ import org .junit .Test ;
10
+ import org .junit .runner .RunWith ;
11
+ import org .springframework .beans .factory .annotation .Autowired ;
12
+ import org .springframework .boot .test .context .SpringBootTest ;
13
+ import org .springframework .boot .test .mock .mockito .MockBean ;
14
+ import org .springframework .http .HttpHeaders ;
15
+ import org .springframework .security .crypto .password .PasswordEncoder ;
16
+ import org .springframework .test .annotation .DirtiesContext ;
17
+ import org .springframework .test .context .TestPropertySource ;
18
+ import org .springframework .test .context .junit4 .SpringRunner ;
19
+ import org .springframework .test .context .web .WebAppConfiguration ;
20
+ import org .springframework .test .web .servlet .MockMvc ;
21
+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
22
+ import org .springframework .web .context .WebApplicationContext ;
23
+
24
+ import javax .servlet .Filter ;
25
+
26
+ import java .nio .charset .Charset ;
27
+
28
+ import static org .mockito .Mockito .doReturn ;
29
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
30
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
31
+
32
+ /**
33
+ * Created by Rimantas Jacikevičius on 19.2.14.
34
+ */
35
+ @ RunWith (SpringRunner .class )
36
+ @ WebAppConfiguration
37
+ @ SpringBootTest (classes = SpringPipelineDemoApplication .class )
38
+ @ TestPropertySource (properties = "testContext=true" )
39
+ @ DirtiesContext
40
+ public class AdminControllerTest {
41
+
42
+ @ MockBean
43
+ UserService userService ;
44
+
45
+ @ Autowired
46
+ PasswordEncoder encoder ;
47
+ @ Autowired
48
+ private WebApplicationContext webApplicationContext ;
49
+ @ Autowired
50
+ private Filter springSecurityFilterChain ;
51
+
52
+ private MockMvc mockMvc ;
53
+ private ObjectMapper objectMapper = new ObjectMapper ();
54
+
55
+ @ Before
56
+ public void setup () throws Exception {
57
+ mockMvc = MockMvcBuilders .webAppContextSetup (webApplicationContext )
58
+ .addFilter (springSecurityFilterChain )
59
+ .build ();
60
+ }
61
+
62
+ @ Test
63
+ public void home () throws Exception {
64
+ String username =
"[email protected] " ;
65
+ String pass = "pass123" ;
66
+
67
+ User user = new User (username , encoder .encode (pass ));
68
+ doReturn (user ).when (userService ).loadUserByUsername (username );
69
+
70
+ String auth = username +":" +pass ;
71
+
72
+ String token = "Basic " + new String (Base64 .encodeBase64 (
73
+ auth .getBytes (Charset .forName ("US-ASCII" ))));
74
+
75
+ mockMvc .perform (
76
+ get ("/admin/home" )
77
+ .header (HttpHeaders .AUTHORIZATION , token ))
78
+ .andExpect (
79
+ status ().isOk ());
80
+ }
81
+
82
+ @ Test
83
+ public void home_unauthorised () throws Exception {
84
+ mockMvc .perform (
85
+ get ("/admin/home" ))
86
+ .andExpect (
87
+ status ().isUnauthorized ());
88
+ }
89
+ }
0 commit comments