18
18
19
19
import com .alibaba .nacos .auth .config .AuthConfigs ;
20
20
import com .alibaba .nacos .common .model .RestResult ;
21
+ import com .alibaba .nacos .core .context .RequestContextHolder ;
21
22
import com .alibaba .nacos .persistence .model .Page ;
22
23
import com .alibaba .nacos .plugin .auth .api .IdentityContext ;
23
24
import com .alibaba .nacos .plugin .auth .exception .AccessException ;
33
34
import com .alibaba .nacos .sys .env .EnvUtil ;
34
35
import com .fasterxml .jackson .databind .JsonNode ;
35
36
import com .fasterxml .jackson .databind .node .ObjectNode ;
37
+ import org .junit .jupiter .api .AfterEach ;
36
38
import org .junit .jupiter .api .BeforeEach ;
37
39
import org .junit .jupiter .api .Test ;
38
40
import org .junit .jupiter .api .extension .ExtendWith ;
@@ -105,6 +107,12 @@ void setUp() throws Exception {
105
107
AuthConstants .DEFAULT_TOKEN_EXPIRE_SECONDS .toString ());
106
108
107
109
EnvUtil .setEnvironment (mockEnvironment );
110
+ RequestContextHolder .getContext ().getAuthContext ().setIdentityContext (new IdentityContext ());
111
+ }
112
+
113
+ @ AfterEach
114
+ public void tearDown () {
115
+ RequestContextHolder .removeContext ();
108
116
}
109
117
110
118
@ Test
@@ -123,20 +131,26 @@ void testLoginWithAuthedUser() throws AccessException, IOException {
123
131
124
132
@ Test
125
133
void testCreateUser1 () {
126
- when (userDetailsService .getUserFromDatabase ("nacos " )).thenReturn (null );
127
- RestResult <String > result = (RestResult <String >) userController .createUser ("nacos " , "test" );
134
+ when (userDetailsService .getUserFromDatabase ("test " )).thenReturn (null );
135
+ RestResult <String > result = (RestResult <String >) userController .createUser ("test " , "test" );
128
136
assertEquals (200 , result .getCode ());
129
137
130
138
}
131
139
132
140
@ Test
133
141
void testCreateUser2 () {
134
- when (userDetailsService .getUserFromDatabase ("nacos " )).thenReturn (new User ());
142
+ when (userDetailsService .getUserFromDatabase ("test " )).thenReturn (new User ());
135
143
assertThrows (IllegalArgumentException .class , () -> {
136
- userController .createUser ("nacos " , "test" );
144
+ userController .createUser ("test " , "test" );
137
145
});
138
146
}
139
147
148
+ @ Test
149
+ void testCreateUserNamedNacos () {
150
+ RestResult <String > result = (RestResult <String >) userController .createUser ("nacos" , "test" );
151
+ assertEquals (409 , result .getCode ());
152
+ }
153
+
140
154
@ Test
141
155
void testCreateAdminUser1 () {
142
156
when (authConfigs .getNacosAuthSystemType ()).thenReturn (AuthSystemTypes .NACOS .name ());
@@ -221,7 +235,7 @@ void testUpdateUser2() {
221
235
222
236
@ Test
223
237
void testUpdateUser3 () throws IOException {
224
-
238
+ RequestContextHolder . getContext (). getAuthContext (). setIdentityContext ( null );
225
239
when (authConfigs .isAuthEnabled ()).thenReturn (true );
226
240
MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest ();
227
241
MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse ();
@@ -234,15 +248,11 @@ void testUpdateUser3() throws IOException {
234
248
235
249
@ Test
236
250
void testUpdateUser4 () throws IOException {
237
-
251
+ RequestContextHolder .getContext ().getAuthContext ().getIdentityContext ()
252
+ .setParameter (AuthConstants .NACOS_USER_KEY , user );
238
253
when (authConfigs .isAuthEnabled ()).thenReturn (true );
239
254
when (userDetailsService .getUserFromDatabase (anyString ())).thenReturn (new User ());
240
255
MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest ();
241
- IdentityContext identityContext = new IdentityContext ();
242
- identityContext .setParameter (AuthConstants .NACOS_USER_KEY , user );
243
- mockHttpServletRequest .getSession ()
244
- .setAttribute (com .alibaba .nacos .plugin .auth .constant .Constants .Identity .IDENTITY_CONTEXT ,
245
- identityContext );
246
256
MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse ();
247
257
RestResult <String > result = (RestResult <String >) userController .updateUser ("nacos" , "test" ,
248
258
mockHttpServletResponse , mockHttpServletRequest );
@@ -252,17 +262,13 @@ void testUpdateUser4() throws IOException {
252
262
253
263
@ Test
254
264
void testUpdateUser5 () throws IOException , AccessException {
255
-
265
+ RequestContextHolder .getContext ().getAuthContext ().getIdentityContext ()
266
+ .setParameter (AuthConstants .NACOS_USER_KEY , null );
256
267
when (authConfigs .isAuthEnabled ()).thenReturn (true );
257
268
when (userDetailsService .getUserFromDatabase (anyString ())).thenReturn (new User ());
258
269
when (authenticationManager .authenticate (any (MockHttpServletRequest .class ))).thenReturn (user );
259
270
260
271
MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest ();
261
- IdentityContext identityContext = new IdentityContext ();
262
- identityContext .setParameter (AuthConstants .NACOS_USER_KEY , null );
263
- mockHttpServletRequest .getSession ()
264
- .setAttribute (com .alibaba .nacos .plugin .auth .constant .Constants .Identity .IDENTITY_CONTEXT ,
265
- identityContext );
266
272
MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse ();
267
273
RestResult <String > result = (RestResult <String >) userController .updateUser ("nacos" , "test" ,
268
274
mockHttpServletResponse , mockHttpServletRequest );
@@ -272,16 +278,12 @@ void testUpdateUser5() throws IOException, AccessException {
272
278
273
279
@ Test
274
280
void testUpdateUser6 () throws IOException , AccessException {
275
-
281
+ RequestContextHolder .getContext ().getAuthContext ().getIdentityContext ()
282
+ .setParameter (AuthConstants .NACOS_USER_KEY , null );
276
283
when (authConfigs .isAuthEnabled ()).thenReturn (true );
277
284
when (authenticationManager .authenticate (any (MockHttpServletRequest .class ))).thenReturn (null );
278
285
279
286
MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest ();
280
- IdentityContext identityContext = new IdentityContext ();
281
- identityContext .setParameter (AuthConstants .NACOS_USER_KEY , null );
282
- mockHttpServletRequest .getSession ()
283
- .setAttribute (com .alibaba .nacos .plugin .auth .constant .Constants .Identity .IDENTITY_CONTEXT ,
284
- identityContext );
285
287
MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse ();
286
288
Object result = userController .updateUser ("nacos" , "test" , mockHttpServletResponse , mockHttpServletRequest );
287
289
@@ -292,17 +294,13 @@ void testUpdateUser6() throws IOException, AccessException {
292
294
293
295
@ Test
294
296
void testUpdateUser7 () throws IOException , AccessException {
295
-
297
+ RequestContextHolder .getContext ().getAuthContext ().getIdentityContext ()
298
+ .setParameter (AuthConstants .NACOS_USER_KEY , null );
296
299
when (authConfigs .isAuthEnabled ()).thenReturn (true );
297
300
when (authenticationManager .authenticate (any (MockHttpServletRequest .class ))).thenThrow (
298
301
new AccessException ("test" ));
299
302
300
303
MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest ();
301
- IdentityContext identityContext = new IdentityContext ();
302
- identityContext .setParameter (AuthConstants .NACOS_USER_KEY , null );
303
- mockHttpServletRequest .getSession ()
304
- .setAttribute (com .alibaba .nacos .plugin .auth .constant .Constants .Identity .IDENTITY_CONTEXT ,
305
- identityContext );
306
304
MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse ();
307
305
Object result = userController .updateUser ("nacos" , "test" , mockHttpServletResponse , mockHttpServletRequest );
308
306
0 commit comments