1+ package org .baeldung .methodsecurity ;
2+
3+ import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertTrue ;
5+
6+ import java .util .ArrayList ;
7+ import java .util .List ;
8+
9+ import org .baeldung .methodsecurity .service .UserRoleService ;
10+ import org .junit .Test ;
11+ import org .junit .runner .RunWith ;
12+ import org .springframework .beans .factory .annotation .Autowired ;
13+ import org .springframework .context .annotation .ComponentScan ;
14+ import org .springframework .context .annotation .Configuration ;
15+ import org .springframework .security .access .AccessDeniedException ;
16+ import org .springframework .security .authentication .AuthenticationCredentialsNotFoundException ;
17+ import org .springframework .security .test .context .support .WithAnonymousUser ;
18+ import org .springframework .security .test .context .support .WithMockUser ;
19+ import org .springframework .test .context .ContextConfiguration ;
20+ import org .springframework .test .context .junit4 .SpringRunner ;
21+
22+ @ RunWith (SpringRunner .class )
23+ @ ContextConfiguration
24+ public class TestMethodSecurity {
25+
26+ @ Autowired
27+ UserRoleService userRoleService ;
28+
29+ @ Configuration
30+ @ ComponentScan ("org.baeldung.methodsecurity.*" )
31+ public static class SpringConfig {
32+
33+ }
34+
35+ @ Test (expected =AuthenticationCredentialsNotFoundException .class )
36+ public void givenNoSecurity_whenCallGetUsername_thenReturnException (){
37+ String userName = userRoleService .getUsername ();
38+ assertEquals ("john" , userName );
39+ }
40+
41+ @ Test
42+ @ WithMockUser (username ="john" ,roles ={"VIEWER" })
43+ public void givenRoleViewer_whenCallGetUsername_thenReturnUsername (){
44+ String userName = userRoleService .getUsername ();
45+ assertEquals ("john" , userName );
46+ }
47+
48+ @ Test
49+ @ WithMockUser (username ="john" ,roles ={"EDITOR" })
50+ public void givenUsernameJohn_whenCallIsValidUsername_thenReturnTrue (){
51+ boolean isValid = userRoleService .isValidUsername ("john" );
52+ assertEquals (true , isValid );
53+ }
54+
55+ @ Test (expected = AccessDeniedException .class )
56+ @ WithMockUser (username = "john" , roles = { "ADMIN" })
57+ public void givenRoleAdmin_whenCallGetUsername_thenReturnAccessDenied () {
58+ userRoleService .getUsername ();
59+ }
60+
61+ @ Test (expected = AccessDeniedException .class )
62+ @ WithMockUser (username = "john" , roles = {"USER" })
63+ public void givenRoleUser_whenCallGetUsername2_thenReturnAccessDenied () {
64+ userRoleService .getUsername2 ();
65+ }
66+
67+ @ Test
68+ @ WithMockUser (username ="john" ,roles ={"VIEWER" ,"EDITOR" })
69+ public void givenRoleViewer_whenCallGetUsername2_thenReturnUsername (){
70+ String userName = userRoleService .getUsername2 ();
71+ assertEquals ("john" , userName );
72+ }
73+
74+ @ Test
75+ @ WithMockUser (username ="john" ,roles ={"VIEWER" })
76+ public void givenUsernameJerry_whenCallIsValidUsername2_thenReturnFalse (){
77+ boolean isValid = userRoleService .isValidUsername2 ("jerry" );
78+ assertEquals (false , isValid );
79+ }
80+
81+ @ Test
82+ @ WithMockUser (username ="JOHN" ,authorities ={"SYS_ADMIN" })
83+ public void givenAuthoritySysAdmin_whenCallGetUsernameInLowerCase_thenReturnUsername (){
84+ String username = userRoleService .getUsernameInLowerCase ();
85+ assertEquals ("john" , username );
86+ }
87+
88+ @ Test
89+ @ WithMockUser (username ="john" ,roles ={"ADMIN" ,"USER" ,"VIEWER" })
90+ public void givenUserJohn_whenCallGetMyRolesWithJohn_thenReturnRoles (){
91+ String roles = userRoleService .getMyRoles ("john" );
92+ assertEquals ("ROLE_ADMIN,ROLE_USER,ROLE_VIEWER" , roles );
93+ }
94+
95+ @ Test (expected =AccessDeniedException .class )
96+ @ WithMockUser (username ="john" ,roles ={"ADMIN" ,"USER" ,"VIEWER" })
97+ public void givenUserJane_whenCallGetMyRolesWithJane_thenAccessDenied (){
98+ userRoleService .getMyRoles ("jane" );
99+ }
100+
101+ @ Test (expected =AccessDeniedException .class )
102+ @ WithAnonymousUser
103+ public void givenAnomynousUser_whenCallGetUsername_thenAccessDenied (){
104+ userRoleService .getUsername ();
105+ }
106+
107+ @ Test
108+ @ WithMockJohnViewer
109+ public void givenMockedJohnViewer_whenCallGetUsername_thenReturnUsername (){
110+ String userName = userRoleService .getUsername ();
111+ assertEquals ("john" , userName );
112+ }
113+
114+ @ Test
115+ @ WithMockUser (username ="jane" )
116+ public void givenListContainCurrentUsername_whenJoinUsernames_thenReturnUsernames (){
117+ List <String > usernames = new ArrayList <>();
118+ usernames .add ("jane" );
119+ usernames .add ("john" );
120+ usernames .add ("jack" );
121+ String containCurrentUser = userRoleService .joinUsernames (usernames );
122+ assertEquals ("john;jack" , containCurrentUser );
123+
124+ }
125+
126+ @ Test
127+ @ WithMockUser (username ="john" )
128+ public void givenListNotContainCurrentUsername_whenCallContainCurrentUser_thenReturnAccessDenied (){
129+ List <String > usernames = new ArrayList <>();
130+ usernames .add ("jane" );
131+ usernames .add ("john" );
132+ usernames .add ("jack" );
133+
134+ List <String > roles = new ArrayList <>();
135+ roles .add ("ROLE_ADMIN" );
136+ roles .add ("ROLE_TEST" );
137+
138+ String containCurrentUser = userRoleService .joinUsernamesAndRoles (usernames ,roles );
139+ assertEquals ("jane;jack:ROLE_ADMIN;ROLE_TEST" , containCurrentUser );
140+ }
141+
142+ @ Test
143+ @ WithMockUser (username ="john" )
144+ public void givenUserJohn_whenCallGetAllUsernamesExceptCurrent_thenReturnOtherusernames (){
145+ List <String > others = userRoleService .getAllUsernamesExceptCurrent ();
146+ assertEquals (2 , others .size ());
147+ assertTrue (others .contains ("jane" ));
148+ assertTrue (others .contains ("jack" ));
149+ }
150+
151+ @ Test
152+ @ WithMockUser (username ="john" ,roles ={"VIEWER" })
153+ public void givenRoleViewer_whenCallGetUsername4_thenReturnUsername (){
154+ String userName = userRoleService .getUsername4 ();
155+ assertEquals ("john" , userName );
156+ }
157+
158+ @ Test (expected =AccessDeniedException .class )
159+ @ WithMockUser (username ="john" )
160+ public void givenDefaultRole_whenCallGetUsername4_thenAccessDenied (){
161+ userRoleService .getUsername4 ();
162+ }
163+ }
0 commit comments