|
| 1 | +const expect = require('chai').expect; |
| 2 | +const emptyGroups = require('./emptyGroups'); |
| 3 | + |
| 4 | +const listGroups = [ |
| 5 | + { |
| 6 | + "Path": "/", |
| 7 | + "GroupName": "test-cp", |
| 8 | + "GroupId": "AGPAYE32SRU5ROTM7JUA3", |
| 9 | + "Arn": "arn:aws:iam::123456654321:group/test-cp", |
| 10 | + "CreateDate": "2020-09-20T22:20:25Z" |
| 11 | + }, |
| 12 | + { |
| 13 | + "Path": "/", |
| 14 | + "GroupName": "test-cs-1", |
| 15 | + "GroupId": "AGPAYE32SRU5TOSRN5ZC5", |
| 16 | + "Arn": "arn:aws:iam::123456654321:group/test-cs-1", |
| 17 | + "CreateDate": "2020-09-20T22:24:49Z" |
| 18 | + } |
| 19 | +]; |
| 20 | + |
| 21 | +const getGroup = [ |
| 22 | + { |
| 23 | + "Users": [], |
| 24 | + "Group": { |
| 25 | + "Path": "/", |
| 26 | + "GroupName": "test-cp", |
| 27 | + "GroupId": "AGPAYE32SRU5ROTM7JUA3", |
| 28 | + "Arn": "arn:aws:iam::123456654321:group/test-cp", |
| 29 | + "CreateDate": "2020-09-20T22:20:25Z" |
| 30 | + } |
| 31 | + }, |
| 32 | + { |
| 33 | + "Users": [ |
| 34 | + { |
| 35 | + "Path": "/", |
| 36 | + "UserName": "cloudsploit", |
| 37 | + "UserId": "AIDAYE32SRU57PAYVNPEI", |
| 38 | + "Arn": "arn:aws:iam::123456654321:user/cloudsploit", |
| 39 | + "CreateDate": "2020-09-12T16:58:32Z", |
| 40 | + "PasswordLastUsed": "2020-09-20T18:58:07Z" |
| 41 | + } |
| 42 | + ], |
| 43 | + "Group": { |
| 44 | + "Path": "/", |
| 45 | + "GroupName": "test-cs-1", |
| 46 | + "GroupId": "AGPAYE32SRU5TOSRN5ZC5", |
| 47 | + "Arn": "arn:aws:iam::123456654321:group/test-cs-1", |
| 48 | + "CreateDate": "2020-09-20T22:24:49Z" |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | +]; |
| 53 | + |
| 54 | +const createCache = (listGroups, getGroup) => { |
| 55 | + var groupName = (listGroups && listGroups.length) ? listGroups[0].GroupName : null; |
| 56 | + return { |
| 57 | + iam: { |
| 58 | + listGroups: { |
| 59 | + 'us-east-1': { |
| 60 | + data: listGroups, |
| 61 | + }, |
| 62 | + }, |
| 63 | + getGroup: { |
| 64 | + 'us-east-1': { |
| 65 | + [groupName]: { |
| 66 | + data: getGroup |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + }; |
| 72 | +}; |
| 73 | + |
| 74 | +const createErrorCache = () => { |
| 75 | + return { |
| 76 | + iam: { |
| 77 | + listGroups: { |
| 78 | + 'us-east-1': { |
| 79 | + err: { |
| 80 | + message: 'error listing IAM groups' |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }; |
| 86 | +}; |
| 87 | + |
| 88 | +const createNullCache = () => { |
| 89 | + return { |
| 90 | + iam: { |
| 91 | + listGroups: { |
| 92 | + 'us-east-1': null, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }; |
| 96 | +}; |
| 97 | + |
| 98 | +describe('emptyGroups', function () { |
| 99 | + describe('run', function () { |
| 100 | + it('should WARN if IAM group does not contain any users', function (done) { |
| 101 | + const cache = createCache([listGroups[0]], getGroup[0]); |
| 102 | + emptyGroups.run(cache, {}, (err, results) => { |
| 103 | + expect(results.length).to.equal(1); |
| 104 | + expect(results[0].status).to.equal(1); |
| 105 | + done(); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should PASS if no IAM groups found', function (done) { |
| 110 | + const cache = createCache([]); |
| 111 | + emptyGroups.run(cache, {}, (err, results) => { |
| 112 | + expect(results.length).to.equal(1); |
| 113 | + expect(results[0].status).to.equal(0); |
| 114 | + done(); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should PASS if IAM group contains user(s)', function (done) { |
| 119 | + const cache = createCache([listGroups[1]], getGroup[1]); |
| 120 | + emptyGroups.run(cache, {}, (err, results) => { |
| 121 | + expect(results.length).to.equal(1); |
| 122 | + expect(results[0].status).to.equal(0); |
| 123 | + done(); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should UNKNOWN if there was an error querying for IAM groups', function (done) { |
| 128 | + const cache = createErrorCache(); |
| 129 | + emptyGroups.run(cache, {}, (err, results) => { |
| 130 | + expect(results.length).to.equal(1); |
| 131 | + expect(results[0].status).to.equal(3); |
| 132 | + done(); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should not return any results if unable to query for IAM groups', function (done) { |
| 137 | + const cache = createNullCache(); |
| 138 | + emptyGroups.run(cache, {}, (err, results) => { |
| 139 | + expect(results.length).to.equal(0); |
| 140 | + done(); |
| 141 | + }); |
| 142 | + }); |
| 143 | + }); |
| 144 | +}); |
0 commit comments