Not even sure how to phrase this exactly.
I have the two functions you can see below. checkDescription works and returns as expected and then rejects the promise. The second block "checkAwsVsSgs" also executes as expected, but never rejects the promise or even executes the test console.log section.
// Library Module, Public functions
module.exports = {
checkSG: function(block) {
return new Promise((resolve, reject) => {
let found = false;
Object.keys(awsSecurityGroups).forEach( (key)=> {
let group = awsSecurityGroups[key];
if (block.local_action.name === group.GroupName) {
found = true;
if (!checkDescription(group, block)) { reject("Description doesn't match.") }
if (!checkAwsVsSgs(group, block)) { console.log("!!!!! I'm never called"); reject("Found rules in aws that are not in sgs") }
}
});
resolve(found);
});
},
};
// Private functions
function checkDescription(group, block) {
return group.Description === block.local_action.description
}
function checkAwsVsSgs(group, block) { // Look for rules that are in AWS but not sgs
group.IpPermissions.forEach( (awsRule) => {
let foundRuleMatch = false;
block.local_action.rules.forEach ( (blockRule) => {
//console.log("Checking FromPort %s vs %s and ToPort %s vs %s for %s", awsRule.FromPort, blockRule.from_port, awsRule.ToPort, blockRule.to_port, group.GroupName)
if (awsRule.ToPort === blockRule.to_port && awsRule.FromPort === blockRule.from_port) { foundRuleMatch = true }
});
if (!foundRuleMatch) {
console.log("Error: Could not find matching rule for %s FromPort: %s ToPort: %s", group.GroupName, awsRule.FromPort, awsRule.ToPort);
return false;
}
});
return true;
}
It's because you're always returning true in checkAwsVsSgs. The return false; that you have inside the forEach is for the forEach function, not for the outside function. You need to do something like this:
function checkAwsVsSgs(group, block) { // Look for rules that are in AWS but not sgs
var found = true;
group.IpPermissions.forEach( (awsRule) => {
let foundRuleMatch = false;
block.local_action.rules.forEach ( (blockRule) => {
//console.log("Checking FromPort %s vs %s and ToPort %s vs %s for %s", awsRule.FromPort, blockRule.from_port, awsRule.ToPort, blockRule.to_port, group.GroupName)
if (awsRule.ToPort === blockRule.to_port && awsRule.FromPort === blockRule.from_port) { foundRuleMatch = true }
});
if (!foundRuleMatch) {
console.log("Error: Could not find matching rule for %s FromPort: %s ToPort: %s", group.GroupName, awsRule.FromPort, awsRule.ToPort);
found = false;
return;
}
});
return found;
}
Also, if you want to test whether some element in the array passes certain condition, consider using Array.prototype.some() or Array.prototype.every() for all elements.