1
- package test . com .qiniu . iam .apis ;
1
+ package com .qiniu .apis ;
2
2
3
3
import com .qiniu .common .QiniuException ;
4
4
import com .qiniu .iam .apis .*;
5
5
import com .qiniu .storage .Api ;
6
6
import org .junit .jupiter .api .Assertions ;
7
- import org .junit .jupiter .api .Disabled ;
8
7
import org .junit .jupiter .api .Tag ;
9
8
import org .junit .jupiter .api .Test ;
10
- import test .com .qiniu .TestConfig ;
11
9
12
10
import static org .junit .jupiter .api .Assertions .*;
13
11
@@ -22,7 +20,7 @@ public class UserApiTest {
22
20
23
21
@ Test
24
22
@ Tag ("IntegrationTest" )
25
- public void testUsers () {
23
+ void testUsers () {
26
24
// 先删除,流程开始先清理历史数据
27
25
try {
28
26
ApiDeleteUser .Request deleteUserRequest = new ApiDeleteUser .Request (baseUrl , userAlias );
@@ -124,7 +122,7 @@ public void testUsers() {
124
122
125
123
@ Test
126
124
@ Tag ("IntegrationTest" )
127
- public void testUsersKeyPairs () {
125
+ void testUsersKeyPairs () {
128
126
129
127
// 先删除,流程开始先清理历史数据
130
128
try {
@@ -238,7 +236,7 @@ public void testUsersKeyPairs() {
238
236
239
237
@ Test
240
238
@ Tag ("IntegrationTest" )
241
- public void testUsersGroups () {
239
+ void testUsersGroups () {
242
240
try {
243
241
ApiDeleteUser .Request deleteUserRequest = new ApiDeleteUser .Request (baseUrl , userAlias );
244
242
ApiDeleteUser deleteUserApi = new ApiDeleteUser (null , config );
@@ -309,7 +307,7 @@ public void testUsersGroups() {
309
307
310
308
@ Test
311
309
@ Tag ("IntegrationTest" )
312
- public void testUsersPolicies () {
310
+ void testUsersPolicies () {
313
311
try {
314
312
ApiDeleteUser .Request deleteUserRequest = new ApiDeleteUser .Request (baseUrl , userAlias );
315
313
ApiDeleteUser deleteUserApi = new ApiDeleteUser (null , config );
@@ -326,7 +324,22 @@ public void testUsersPolicies() {
326
324
}
327
325
328
326
try {
329
- // 0 创建用户的
327
+ ApiDeletePolicy .Request deleteRequest = new ApiDeletePolicy .Request (baseUrl , policyAlias + "New" );
328
+ ApiDeletePolicy deleteApi = new ApiDeletePolicy (null , config );
329
+ deleteApi .request (deleteRequest );
330
+ } catch (QiniuException e ) {
331
+ }
332
+
333
+ try {
334
+ ApiDeleteGroup .Request deleteGroupRequest = new ApiDeleteGroup .Request (baseUrl , groupAlias );
335
+ ApiDeleteGroup deleteGroupApi = new ApiDeleteGroup (null , config );
336
+ deleteGroupApi .request (deleteGroupRequest );
337
+ } catch (QiniuException e ) {
338
+ // 删除失败时预期的
339
+ }
340
+
341
+ try {
342
+ // 0 创建用户
330
343
ApiCreateUser .Request .CreateIamUserParam createUserParam = new ApiCreateUser .Request .CreateIamUserParam ();
331
344
createUserParam .setAlias (userAlias );
332
345
createUserParam .setPassword (userPWD );
@@ -337,7 +350,7 @@ public void testUsersPolicies() {
337
350
assertEquals (createUserResponse .isOK (), true , "0.1 创建 User 失败:" + createUserResponse );
338
351
339
352
// 1 创建用户的 Policy
340
- String policyDesc = policyAlias + "Desc" ;
353
+ String policyDesc = policyAlias ;
341
354
String policyAction = "cdn/DownloadCDNLog" ;
342
355
String policyEffect = "Allow" ;
343
356
String policyResource = "qrn:product:::/a/b/c.txt" ;
@@ -357,20 +370,133 @@ public void testUsersPolicies() {
357
370
assertTrue (createPolicyResponse .isOK (), "1.1 创建 Policy 失败:" + createPolicyResponse );
358
371
359
372
// 2 用户添加至 Policy
373
+ ApiModifyUserPolicies .Request .ModifiedIamUserPoliciesParam modifyUserPoliciesParam = new ApiModifyUserPolicies .Request .ModifiedIamUserPoliciesParam ();
374
+ modifyUserPoliciesParam .setPolicyAliases (new String []{policyAlias });
375
+ ApiModifyUserPolicies .Request modifyUserPoliciesRequest = new ApiModifyUserPolicies .Request (baseUrl , userAlias , modifyUserPoliciesParam );
376
+ ApiModifyUserPolicies modifyUserPoliciesApi = new ApiModifyUserPolicies (null , config );
377
+ ApiModifyUserPolicies .Response modifyUserPoliciesResponse = modifyUserPoliciesApi .request (modifyUserPoliciesRequest );
378
+ assertNotNull (modifyUserPoliciesResponse , "4. 更新用户添加的 Policy 失败:" + modifyUserPoliciesResponse );
379
+ assertTrue (modifyUserPoliciesResponse .isOK (), "4.1 更新用户添加的 Policy 失败:" + modifyUserPoliciesResponse );
380
+
360
381
361
382
// 3 获取用户 Policy
383
+ ApiGetUserPolicies .Request getUserPoliciesRequest = new ApiGetUserPolicies .Request (baseUrl , userAlias );
384
+ getUserPoliciesRequest .setPage (1 );
385
+ getUserPoliciesRequest .setPageSize (1 );
386
+ ApiGetUserPolicies getUserPoliciesApi = new ApiGetUserPolicies (null , config );
387
+ ApiGetUserPolicies .Response getUserPoliciesResponse = getUserPoliciesApi .request (getUserPoliciesRequest );
388
+ assertNotNull (getUserPoliciesResponse , "3 获取用户 Policy 失败:" + getUserPoliciesResponse );
389
+ assertTrue (getUserPoliciesResponse .isOK (), "3.1 获取用户 Policy 失败:" + getUserPoliciesResponse );
390
+
391
+ ApiGetUserPolicies .Response .GetIamUserPolicy getUserPoliciesResponseData = getUserPoliciesResponse .getData ().getData ().getList ()[0 ];
392
+ assertNotNull (getUserPoliciesResponseData .getId (), "3.2 获取用户 Policy 失败:" + createPolicyResponse );
393
+ assertNotNull (getUserPoliciesResponseData .getRootUid (), "3.3 获取用户 Policy 失败:" + createPolicyResponse );
394
+ assertEquals (getUserPoliciesResponseData .getAlias (), policyAlias , "3.4 获取用户 Policy 失败:" + createPolicyResponse );
395
+ assertEquals (getUserPoliciesResponseData .getDescription (), policyDesc , "3.5 获取用户 Policy 失败:" + createPolicyResponse );
396
+ assertEquals (getUserPoliciesResponseData .getEnabled (), true , "3.6 获取用户 Policy 失败:" + createPolicyResponse );
397
+ assertNotNull (getUserPoliciesResponseData .getUpdatedAt (), "3.7 获取用户 Policy 失败:" + createPolicyResponse );
398
+ assertNotNull (getUserPoliciesResponseData .getCreatedAt (), "3.8 获取用户 Policy 失败:" + createPolicyResponse );
399
+ assertEquals (getUserPoliciesResponseData .getStatement ().length , 1 , "3.9 获取用户 Policy 失败:" + createPolicyResponse );
400
+ ApiGetUserPolicies .Response .Statement getUserPolicyResponseStatement = getUserPoliciesResponseData .getStatement ()[0 ];
401
+ assertEquals (getUserPolicyResponseStatement .getEffect (), policyEffect , "3.10 获取用户 Policy 失败:" + createPolicyResponse );
402
+ assertEquals (getUserPolicyResponseStatement .getActions ()[0 ], policyAction , "3.11 获取用户 Policy 失败:" + createPolicyResponse );
403
+ assertEquals (getUserPolicyResponseStatement .getResources ()[0 ], policyResource , "3.12 获取用户 Policy 失败:" + createPolicyResponse );
404
+
405
+ // 4 创建 Policy
406
+ policyAlias = policyAlias + "New" ;
407
+ policyDesc = policyDesc + "New" ;
408
+ policyAction = "cdn/DownloadCDNLog" ;
409
+ policyEffect = "Allow" ;
410
+ policyResource = "qrn:product:::/a/b/c.txt" ;
411
+ createStatement = new ApiCreatePolicy .Request .CreateStatement ();
412
+ createStatement .setActions (new String []{policyAction });
413
+ createStatement .setEffect (policyEffect );
414
+ createStatement .setResources (new String []{policyResource });
415
+ createPolicyRequestParam = new ApiCreatePolicy .Request .CreatePolicyParam ();
416
+ createPolicyRequestParam .setEditType (1 );
417
+ createPolicyRequestParam .setAlias (policyAlias );
418
+ createPolicyRequestParam .setDescription (policyDesc );
419
+ createPolicyRequestParam .setStatement (new ApiCreatePolicy .Request .CreateStatement []{createStatement });
420
+ createPolicyRequest = new ApiCreatePolicy .Request (baseUrl , createPolicyRequestParam );
421
+ createPolicyApi = new ApiCreatePolicy (null , config );
422
+ createPolicyResponse = createPolicyApi .request (createPolicyRequest );
423
+ assertNotNull (createPolicyResponse , "1. 创建 Policy 失败:" + createPolicyResponse );
424
+ assertTrue (createPolicyResponse .isOK (), "1.1 创建 Policy 失败:" + createPolicyResponse );
362
425
363
- // 4 更新用户添加至 Policy
426
+ // 4 更新用户添加的 Policy
427
+ ApiUpdateUserPolicies .Request .UpdatedIamUserPoliciesParam updateUserPoliciesRequestParam = new ApiUpdateUserPolicies .Request .UpdatedIamUserPoliciesParam ();
428
+ updateUserPoliciesRequestParam .setPolicyAliases (new String []{policyAlias });
429
+ ApiUpdateUserPolicies .Request updateUserPoliciesRequest = new ApiUpdateUserPolicies .Request (baseUrl , userAlias , updateUserPoliciesRequestParam );
430
+ ApiUpdateUserPolicies updateUserPoliciesApi = new ApiUpdateUserPolicies (null , config );
431
+ ApiUpdateUserPolicies .Response updateUserPoliciesResponse = updateUserPoliciesApi .request (updateUserPoliciesRequest );
432
+ assertNotNull (updateUserPoliciesResponse , "2. 用户添加至 Policy 失败:" + updateUserPoliciesResponse );
433
+ assertTrue (updateUserPoliciesResponse .isOK (), "2.1 用户添加至 Policy 失败:" + updateUserPoliciesResponse );
364
434
365
435
// 5 获取用户的 Policy(验证)
436
+ getUserPoliciesRequest = new ApiGetUserPolicies .Request (baseUrl , userAlias );
437
+ getUserPoliciesRequest .setPage (1 );
438
+ getUserPoliciesRequest .setPageSize (1 );
439
+ getUserPoliciesApi = new ApiGetUserPolicies (null , config );
440
+ getUserPoliciesResponse = getUserPoliciesApi .request (getUserPoliciesRequest );
441
+ assertNotNull (getUserPoliciesResponse , "5 获取用户 Policy 失败:" + getUserPoliciesResponse );
442
+ assertTrue (getUserPoliciesResponse .isOK (), "5.1 获取用户 Policy 失败:" + getUserPoliciesResponse );
443
+
444
+ getUserPoliciesResponseData = getUserPoliciesResponse .getData ().getData ().getList ()[0 ];
445
+ assertNotNull (getUserPoliciesResponseData .getId (), "5.2 获取用户 Policy 失败:" + createPolicyResponse );
446
+ assertNotNull (getUserPoliciesResponseData .getRootUid (), "5.3 获取用户 Policy 失败:" + createPolicyResponse );
447
+ assertEquals (getUserPoliciesResponseData .getAlias (), policyAlias , "5.4 获取用户 Policy 失败:" + createPolicyResponse );
448
+ assertEquals (getUserPoliciesResponseData .getDescription (), policyDesc , "5.5 获取用户 Policy 失败:" + createPolicyResponse );
449
+ assertEquals (getUserPoliciesResponseData .getEnabled (), true , "5.6 获取用户 Policy 失败:" + createPolicyResponse );
450
+ assertNotNull (getUserPoliciesResponseData .getUpdatedAt (), "5.7 获取用户 Policy 失败:" + createPolicyResponse );
451
+ assertNotNull (getUserPoliciesResponseData .getCreatedAt (), "5.8 获取用户 Policy 失败:" + createPolicyResponse );
452
+ assertEquals (getUserPoliciesResponseData .getStatement ().length , 1 , "5.9 获取用户 Policy 失败:" + createPolicyResponse );
453
+ getUserPolicyResponseStatement = getUserPoliciesResponseData .getStatement ()[0 ];
454
+ assertEquals (getUserPolicyResponseStatement .getEffect (), policyEffect , "5.10 获取用户 Policy 失败:" + createPolicyResponse );
455
+ assertEquals (getUserPolicyResponseStatement .getActions ()[0 ], policyAction , "5.11 获取用户 Policy 失败:" + createPolicyResponse );
456
+ assertEquals (getUserPolicyResponseStatement .getResources ()[0 ], policyResource , "5.12 获取用户 Policy 失败:" + createPolicyResponse );
366
457
367
458
// 6 删除用户的 Policy
459
+ ApiDeleteUserPolicy .Request .DeletedIamUserPoliciesParam deleteUserPolicyRequestParam = new ApiDeleteUserPolicy .Request .DeletedIamUserPoliciesParam ();
460
+ deleteUserPolicyRequestParam .setPolicyAliases (new String []{policyAlias });
461
+ ApiDeleteUserPolicy .Request deleteUserPolicyRequest = new ApiDeleteUserPolicy .Request (baseUrl , userAlias , deleteUserPolicyRequestParam );
462
+ ApiDeleteUserPolicy deleteUserPolicyApi = new ApiDeleteUserPolicy (null , config );
463
+ ApiDeleteUserPolicy .Response deleteUserPolicyResponse = deleteUserPolicyApi .request (deleteUserPolicyRequest );
464
+ assertNotNull (getUserPoliciesResponse , "6 获取用户 Policy 失败:" + getUserPoliciesResponse );
465
+ assertTrue (getUserPoliciesResponse .isOK (), "6.1 获取用户 Policy 失败:" + getUserPoliciesResponse );
368
466
369
467
// 7 获取用户的 Policy(验证)
468
+ getUserPoliciesRequest = new ApiGetUserPolicies .Request (baseUrl , userAlias );
469
+ getUserPoliciesRequest .setPage (1 );
470
+ getUserPoliciesRequest .setPageSize (1 );
471
+ getUserPoliciesApi = new ApiGetUserPolicies (null , config );
472
+ getUserPoliciesResponse = getUserPoliciesApi .request (getUserPoliciesRequest );
473
+ assertNotNull (getUserPoliciesResponse , "7 获取用户 Policy 失败:" + getUserPoliciesResponse );
474
+ assertTrue (getUserPoliciesResponse .isOK (), "7.1 获取用户 Policy 失败:" + getUserPoliciesResponse );
475
+ assertEquals (0 , (int ) getUserPoliciesResponse .getData ().getData ().getCount (), "7.2 获取用户 Policy 失败:" + getUserPoliciesResponse );
370
476
371
477
// 8 获取用户的 Service
478
+ ApiGetUserAvailableServices .Request getUserAvailableServicesRequest = new ApiGetUserAvailableServices .Request (baseUrl , userAlias );
479
+ ApiGetUserAvailableServices getUserAvailableServicesApi = new ApiGetUserAvailableServices (null , config );
480
+ ApiGetUserAvailableServices .Response getUserAvailableServicesResponse = getUserAvailableServicesApi .request (getUserAvailableServicesRequest );
481
+ assertNotNull (getUserAvailableServicesResponse , "8 获取用户的 Service 失败:" + getUserAvailableServicesResponse );
482
+ assertTrue (getUserAvailableServicesResponse .isOK (), "8.1 获取用户的 Service 失败:" + getUserAvailableServicesResponse );
483
+ assertNotNull (getUserAvailableServicesResponse .getData ().getData (), "8.2 获取用户的 Service 失败:" + getUserPoliciesResponse );
372
484
373
485
// 9 列举子用户指定服务操作下的可访问资源
486
+ String service = "cdn" ;
487
+ String actionAlias = "DownloadCDNLog" ;
488
+ ApiGetUserServiceActionResources .Request getUserServiceActionResourcesRequest = new ApiGetUserServiceActionResources .Request (baseUrl , userAlias , service , actionAlias );
489
+ ApiGetUserServiceActionResources getUserServiceActionResourcesApi = new ApiGetUserServiceActionResources (null , config );
490
+ ApiGetUserServiceActionResources .Response getUserServiceActionResourcesResponse = getUserServiceActionResourcesApi .request (getUserServiceActionResourcesRequest );
491
+ assertNotNull (getUserServiceActionResourcesResponse , "9 列举子用户指定服务操作下的可访问资源失败:" + getUserServiceActionResourcesResponse );
492
+ assertTrue (getUserServiceActionResourcesResponse .isOK (), "9.1 列举子用户指定服务操作下的可访问资源失败:" + getUserServiceActionResourcesResponse );
493
+
494
+ ApiGetUserServiceActionResources .Response .GetIamUserServiceActionResources getUserServiceActionResources = getUserServiceActionResourcesResponse .getData ().getData ();
495
+ assertNotNull (getUserServiceActionResources , "9.2 列举子用户指定服务操作下的可访问资源失败:" + getUserServiceActionResourcesResponse );
496
+ assertNotNull (getUserServiceActionResources .getAllowedResources (), "9.3 列举子用户指定服务操作下的可访问资源失败:" + getUserServiceActionResourcesResponse );
497
+ assertNotNull (getUserServiceActionResources .getDeniedResources (), "9.4 列举子用户指定服务操作下的可访问资源失败:" + getUserServiceActionResourcesResponse );
498
+
499
+
374
500
} catch (QiniuException e ) {
375
501
fail (e );
376
502
}
0 commit comments