From ff4006fe820b3c162f7730534adc592f9f37dbd5 Mon Sep 17 00:00:00 2001 From: Kinnaird McQuade Date: Mon, 19 Oct 2020 21:48:49 -0400 Subject: [PATCH] Added click unit tests (#262) --- .../3-wildcard-only-single-actions.yml | 2 +- test/command/test_write_policy_click.py | 57 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 test/command/test_write_policy_click.py 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 958f4e49e..dc23900c6 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 diff --git a/test/command/test_write_policy_click.py b/test/command/test_write_policy_click.py new file mode 100644 index 000000000..7ffec40c1 --- /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)