@@ -14,6 +14,7 @@ const { TMP_PATH, generate_nsfs_account, get_new_buckets_path_by_test_env, gener
1414const { ListUsersCommand, CreateUserCommand, GetUserCommand, UpdateUserCommand, DeleteUserCommand,
1515 ListAccessKeysCommand, CreateAccessKeyCommand, GetAccessKeyLastUsedCommand,
1616 UpdateAccessKeyCommand, DeleteAccessKeyCommand,
17+ ListUserPoliciesCommand, PutUserPolicyCommand, DeleteUserPolicyCommand, GetUserPolicyCommand,
1718 ListGroupsForUserCommand, ListAccountAliasesCommand, ListAttachedGroupPoliciesCommand,
1819 ListAttachedRolePoliciesCommand, ListAttachedUserPoliciesCommand, ListEntitiesForPolicyCommand,
1920 ListGroupPoliciesCommand, ListGroupsCommand, ListInstanceProfilesCommand,
@@ -22,7 +23,7 @@ const { ListUsersCommand, CreateUserCommand, GetUserCommand, UpdateUserCommand,
2223 ListPoliciesCommand, ListPolicyTagsCommand, ListPolicyVersionsCommand, ListRolesCommand,
2324 ListRoleTagsCommand, ListSAMLProvidersCommand, ListServerCertificatesCommand,
2425 ListServerCertificateTagsCommand, ListServiceSpecificCredentialsCommand,
25- ListSigningCertificatesCommand, ListSSHPublicKeysCommand, ListUserPoliciesCommand ,
26+ ListSigningCertificatesCommand, ListSSHPublicKeysCommand,
2627 ListUserTagsCommand, ListVirtualMFADevicesCommand } = require ( '@aws-sdk/client-iam' ) ;
2728const { ACCESS_KEY_STATUS_ENUM } = require ( '../../../../endpoint/iam/iam_constants' ) ;
2829const IamError = require ( '../../../../endpoint/iam/iam_errors' ) . IamError ;
@@ -257,6 +258,107 @@ mocha.describe('IAM basic integration tests - happy path', async function() {
257258 } ) ;
258259 } ) ;
259260
261+ mocha . describe ( 'IAM User Policy API' , async function ( ) {
262+ if ( is_nc_coretest ) this . skip ( ) ; // eslint-disable-line no-invalid-this
263+ const username3 = 'Kai' ;
264+ const policy_name = 'AllAccessPolicy' ;
265+ const iam_user_inline_policy_document = '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"*","Resource":"*"}]}' ;
266+
267+ mocha . before ( async ( ) => {
268+ // create a user
269+ const input = {
270+ UserName : username3
271+ } ;
272+ const command = new CreateUserCommand ( input ) ;
273+ const response = await iam_account . send ( command ) ;
274+ _check_status_code_ok ( response ) ;
275+ } ) ;
276+
277+ mocha . after ( async ( ) => {
278+ // delete a user
279+ const input = {
280+ UserName : username3
281+ } ;
282+ const command = new DeleteUserCommand ( input ) ;
283+ const response = await iam_account . send ( command ) ;
284+ _check_status_code_ok ( response ) ;
285+ // note: if somehow the delete user policy would fail, then deleting the user would also fail
286+ // (as we can delete a user only after its user policies were deleted)
287+ } ) ;
288+
289+ mocha . it ( 'list user policies for non existing user - should throw an error' , async function ( ) {
290+ try {
291+ const input = {
292+ UserName : 'non-existing-user'
293+ } ;
294+ const command = new ListUserPoliciesCommand ( input ) ;
295+ await iam_account . send ( command ) ;
296+ assert . fail ( 'list user policies for non existing user - should throw an error' ) ;
297+ } catch ( err ) {
298+ const err_code = err . Error . Code ;
299+ assert . equal ( err_code , IamError . NoSuchEntity . code ) ;
300+ }
301+ } ) ;
302+
303+ mocha . it ( 'list user policies for user - should be empty' , async function ( ) {
304+ const input = {
305+ UserName : username3
306+ } ;
307+ const command = new ListUserPoliciesCommand ( input ) ;
308+ const response = await iam_account . send ( command ) ;
309+ _check_status_code_ok ( response ) ;
310+ assert . equal ( response . PolicyNames . length , 0 ) ;
311+ } ) ;
312+
313+ mocha . it ( 'put user policy' , async function ( ) {
314+ const input = {
315+ UserName : username3 ,
316+ PolicyName : policy_name ,
317+ PolicyDocument : iam_user_inline_policy_document
318+ } ;
319+ const command = new PutUserPolicyCommand ( input ) ;
320+ const response = await iam_account . send ( command ) ;
321+ _check_status_code_ok ( response ) ;
322+
323+ // verify it using list user policies
324+ const input2 = {
325+ UserName : username3
326+ } ;
327+ const command2 = new ListUserPoliciesCommand ( input2 ) ;
328+ const response2 = await iam_account . send ( command2 ) ;
329+ _check_status_code_ok ( response2 ) ;
330+ assert . equal ( response2 . PolicyNames . length , 1 ) ;
331+ assert . equal ( response2 . PolicyNames [ 0 ] , policy_name ) ;
332+ } ) ;
333+
334+ mocha . it ( 'get user policy' , async function ( ) {
335+ const input = {
336+ UserName : username3 ,
337+ PolicyName : policy_name
338+ } ;
339+ const command = new GetUserPolicyCommand ( input ) ;
340+ const response = await iam_account . send ( command ) ;
341+ _check_status_code_ok ( response ) ;
342+ assert . equal ( response . UserName , username3 ) ;
343+ assert . equal ( response . PolicyName , policy_name ) ;
344+ assert ( response . PolicyDocument !== undefined ) ;
345+ const response_policy_document_json = JSON . parse ( response . PolicyDocument ) ;
346+ assert . equal ( response_policy_document_json . Version , '2012-10-17' ) ;
347+ assert ( Array . isArray ( response_policy_document_json . Statement ) ) ;
348+ assert . deepEqual ( response_policy_document_json . Statement [ 0 ] , { "Effect" : "Allow" , "Action" : "*" , "Resource" : "*" } ) ;
349+ } ) ;
350+
351+ mocha . it ( 'delete user policy' , async function ( ) {
352+ const input = {
353+ UserName : username3 ,
354+ PolicyName : policy_name
355+ } ;
356+ const command = new DeleteUserPolicyCommand ( input ) ;
357+ const response = await iam_account . send ( command ) ;
358+ _check_status_code_ok ( response ) ;
359+ } ) ;
360+ } ) ;
361+
260362 mocha . describe ( 'IAM other APIs (currently returns empty value)' , async function ( ) {
261363 const username3 = 'Emi' ;
262364 const group_name = 'my_group' ;
0 commit comments