Repository: salesforce/policy_sentry
Pull request #262 (2020-10-20 01:45:43)

Added click unit tests
## What does this PR do?

I just added some Click unit tests because (1) It was fun to learn that, (2) I am maintainer and I say so (lol jk but only kinda), and (3) someone asked for click unit tests for their upcoming change so I thought I'd give a demo of how it would work here.

I also changed a value in the examples/yml/crud-cases/3-wildcard-only-single-actions.yml file because secretsmanager:CreateSecret supports resource ARN constraints now.

## What gif best describes this PR or how it makes you feel?

![image](https://user-images.githubusercontent.com/3422255/96529847-2ad3ab80-1254-11eb-816b-88b61db34c18.png)

## Completion checklist

- [x] CHANGELOG.md has been updated to reflect the changes
- [x] Additions and changes have unit tests
- [x] [Unit tests, Pylint, security testing, and Integration tests are passing.](https://github.com/salesforce/policy_sentry/actions). GitHub actions does this automatically
- [x] The pull request has been appropriately labeled using the provided PR labels



Patch:
diff --git a/examples/yml/crud-cases/3-wildcard-only-single-actions.yml b/examples/yml/crud-cases/3-wildcard-only-single-actions.yml
index 958f4e49..dc23900c 100644
--- a/examples/yml/crud-cases/3-wildcard-only-single-actions.yml
+++ b/examples/yml/crud-cases/3-wildcard-only-single-actions.yml
@@ -1,4 +1,4 @@
 mode: crud
 wildcard-only:
     single-actions:
-    - secretsmanager:CreateSecret
+    - secretsmanager:ListSecrets


Test patch:
diff --git a/test/command/test_write_policy_click.py b/test/command/test_write_policy_click.py
new file mode 100644
index 00000000..7ffec40c
--- /dev/null
+++ b/test/command/test_write_policy_click.py
@@ -0,0 +1,57 @@
+import os
+import json
+import unittest
+from click.testing import CliRunner
+from policy_sentry.command.write_policy import write_policy
+from policy_sentry.util.policy_files import get_sid_names_from_policy
+
+test_file_directory = os.path.join(
+    os.path.dirname(__file__),
+    os.path.pardir,
+    os.path.pardir,
+    "examples",
+    "yml"
+)
+
+
+class PolicySentryClickUnitTests(unittest.TestCase):
+    def setUp(self):
+        self.runner = CliRunner()
+
+    def test_write_policy_basic_with_click(self):
+        """command.write_policy: using crud.yml should return exit code 0"""
+        template_file = os.path.join(test_file_directory, "crud.yml")
+        result = self.runner.invoke(write_policy, ["--input-file", template_file])
+        self.assertTrue(result.exit_code == 0)
+
+    def test_click_crud_case_1(self):
+        """write_policy: using 1-ssm-read.yml"""
+        template_file = os.path.join(test_file_directory, "crud-cases", "1-ssm-read.yml")
+        result = self.runner.invoke(write_policy, ["--input-file", template_file])
+        result_json = json.loads(result.output)
+        self.assertListEqual(get_sid_names_from_policy(result_json), ["SsmReadParameter"])
+        self.assertTrue(result.exit_code == 0)
+
+    def test_click_crud_case_2(self):
+        """write_policy: using 2-skip-resource-constraints.yml"""
+        template_file = os.path.join(test_file_directory, "crud-cases", "2-skip-resource-constraints.yml")
+        result = self.runner.invoke(write_policy, ["--input-file", template_file])
+        result_json = json.loads(result.output)
+        self.assertListEqual(get_sid_names_from_policy(result_json), ["SkipResourceConstraints"])
+        self.assertTrue(result.exit_code == 0)
+
+    def test_click_crud_case_3(self):
+        """write_policy: using 3-wildcard-only-single-actions.yml"""
+        template_file = os.path.join(test_file_directory, "crud-cases", "3-wildcard-only-single-actions.yml")
+        result = self.runner.invoke(write_policy, ["--input-file", template_file])
+        result_json = json.loads(result.output)
+        self.assertListEqual(get_sid_names_from_policy(result_json), ["MultMultNone"])
+        self.assertTrue(result.exit_code == 0)
+
+    def test_click_crud_case_4(self):
+        """write_policy: using 4-wildcard-only-bulk-selection.yml"""
+        template_file = os.path.join(test_file_directory, "crud-cases", "4-wildcard-only-bulk-selection.yml")
+        result = self.runner.invoke(write_policy, ["--input-file", template_file])
+        result_json = json.loads(result.output)
+        self.assertListEqual(get_sid_names_from_policy(result_json), ["MultMultNone"])
+        self.assertTrue(result.exit_code == 0)