From a51448f67fc199e8a2a36deceed1aa0a4e52a2ac Mon Sep 17 00:00:00 2001 From: rjt-gupta Date: Wed, 13 Mar 2019 14:08:30 +0530 Subject: [PATCH 1/4] increased coverage --- .gitignore | 1 + snare/cloner.py | 2 +- snare/tests/test_add_meta_tag.py | 3 +++ snare/tests/test_parse_timeout.py | 23 +++++++++++++++++++++++ snare/utils/snare_helpers.py | 6 +++--- 5 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 snare/tests/test_parse_timeout.py diff --git a/.gitignore b/.gitignore index 1d99689b..ffbdcaea 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ __pycache__/ *.py[cod] *$py.class +.coverage # Sphinx documentation docs/_build/ diff --git a/snare/cloner.py b/snare/cloner.py index 8ed2e8f6..4c4e43f5 100644 --- a/snare/cloner.py +++ b/snare/cloner.py @@ -19,7 +19,7 @@ def __init__(self, root, max_depth, css_validate): self.max_depth = max_depth self.moved_root = None if len(self.root.host) < 4: - sys.exit('invalid taget {}'.format(self.root.host)) + sys.exit('invalid target {}'.format(self.root.host)) self.target_path = '/opt/snare/pages/{}'.format(self.root.host) if not os.path.exists(self.target_path): diff --git a/snare/tests/test_add_meta_tag.py b/snare/tests/test_add_meta_tag.py index df28b88a..697993b8 100644 --- a/snare/tests/test_add_meta_tag.py +++ b/snare/tests/test_add_meta_tag.py @@ -28,5 +28,8 @@ def test_add_meta_tag(self): assert(soup.find("meta", attrs={"name": "google-site-verification"}) and soup.find("meta", attrs={"name": "msvalidate.01"})) + config['WEB-TOOLS'] = dict(google='', bing='') + assert add_meta_tag(self.page_dir, self.index_page, config) is None + def tearDown(self): shutil.rmtree(self.main_page_path) diff --git a/snare/tests/test_parse_timeout.py b/snare/tests/test_parse_timeout.py new file mode 100644 index 00000000..233d5101 --- /dev/null +++ b/snare/tests/test_parse_timeout.py @@ -0,0 +1,23 @@ +import unittest +from snare.utils.snare_helpers import parse_timeout, str_to_bool + + +class TestParseTimeout(unittest.TestCase): + + def setUp(self): + self.v = None + + def test_parse_timeout(self): + + assert parse_timeout('20H') == 72000 + assert parse_timeout('10M') == 600 + assert parse_timeout('1D') == 86400 + + assert parse_timeout('24Y') == 86400 # Default 24H format is used. + + def test_str_to_bool(self): + + self.v = 'true' + assert str_to_bool(self.v) is True + self.v = 'false' + assert str_to_bool(self.v) is False diff --git a/snare/utils/snare_helpers.py b/snare/utils/snare_helpers.py index 98855847..1eae2dcd 100644 --- a/snare/utils/snare_helpers.py +++ b/snare/utils/snare_helpers.py @@ -63,12 +63,12 @@ def add_meta_tag(page_dir, index_page, config): main_page = main.read() soup = BeautifulSoup(main_page, 'html.parser') - if (google_content and soup.find("meta", attrs={"name": "google-site-verification"}) is None): + if google_content and soup.find("meta", attrs={"name": "google-site-verification"}) is None: google_meta = soup.new_tag('meta') google_meta.attrs['name'] = 'google-site-verification' google_meta.attrs['content'] = google_content soup.head.append(google_meta) - if (bing_content and soup.find("meta", attrs={"name": "msvalidate.01"}) is None): + if bing_content and soup.find("meta", attrs={"name": "msvalidate.01"}) is None: bing_meta = soup.new_tag('meta') bing_meta.attrs['name'] = 'msvalidate.01' bing_meta.attrs['content'] = bing_content @@ -90,7 +90,7 @@ def parse_timeout(timeout): form = timeout[-1] if form not in timeouts_coeff.keys(): print('Bad timeout format, default will be used') - parse_timeout('24H') + result = parse_timeout('24H') else: result = int(timeout[:-1]) result *= timeouts_coeff[form] From 3a333cd846c3e02b963f73e8a74795fa865f42ef Mon Sep 17 00:00:00 2001 From: rjt-gupta Date: Wed, 13 Mar 2019 20:23:05 +0530 Subject: [PATCH 2/4] test ArgumentTypeError --- snare/cloner.py | 2 +- snare/tests/test_parse_timeout.py | 13 +------------ snare/tests/test_str_to_bool.py | 18 ++++++++++++++++++ snare/utils/snare_helpers.py | 4 ++-- 4 files changed, 22 insertions(+), 15 deletions(-) create mode 100644 snare/tests/test_str_to_bool.py diff --git a/snare/cloner.py b/snare/cloner.py index 4c4e43f5..8ed2e8f6 100644 --- a/snare/cloner.py +++ b/snare/cloner.py @@ -19,7 +19,7 @@ def __init__(self, root, max_depth, css_validate): self.max_depth = max_depth self.moved_root = None if len(self.root.host) < 4: - sys.exit('invalid target {}'.format(self.root.host)) + sys.exit('invalid taget {}'.format(self.root.host)) self.target_path = '/opt/snare/pages/{}'.format(self.root.host) if not os.path.exists(self.target_path): diff --git a/snare/tests/test_parse_timeout.py b/snare/tests/test_parse_timeout.py index 233d5101..58829077 100644 --- a/snare/tests/test_parse_timeout.py +++ b/snare/tests/test_parse_timeout.py @@ -1,23 +1,12 @@ import unittest -from snare.utils.snare_helpers import parse_timeout, str_to_bool +from snare.utils.snare_helpers import parse_timeout class TestParseTimeout(unittest.TestCase): - def setUp(self): - self.v = None - def test_parse_timeout(self): - assert parse_timeout('20H') == 72000 assert parse_timeout('10M') == 600 assert parse_timeout('1D') == 86400 assert parse_timeout('24Y') == 86400 # Default 24H format is used. - - def test_str_to_bool(self): - - self.v = 'true' - assert str_to_bool(self.v) is True - self.v = 'false' - assert str_to_bool(self.v) is False diff --git a/snare/tests/test_str_to_bool.py b/snare/tests/test_str_to_bool.py new file mode 100644 index 00000000..bc9cd1e3 --- /dev/null +++ b/snare/tests/test_str_to_bool.py @@ -0,0 +1,18 @@ +import unittest +from argparse import ArgumentTypeError +from snare.utils.snare_helpers import str_to_bool + + +class TestStrToBool(unittest.TestCase): + + def setUp(self): + self.v = None + + def test_str_to_bool(self): + self.v = 'true' + assert str_to_bool(self.v) is True + self.v = 'false' + assert str_to_bool(self.v) is False + + with self.assertRaises(ArgumentTypeError): + str_to_bool('twz') diff --git a/snare/utils/snare_helpers.py b/snare/utils/snare_helpers.py index 1eae2dcd..24998021 100644 --- a/snare/utils/snare_helpers.py +++ b/snare/utils/snare_helpers.py @@ -63,12 +63,12 @@ def add_meta_tag(page_dir, index_page, config): main_page = main.read() soup = BeautifulSoup(main_page, 'html.parser') - if google_content and soup.find("meta", attrs={"name": "google-site-verification"}) is None: + if (google_content and soup.find("meta", attrs={"name": "google-site-verification"}) is None): google_meta = soup.new_tag('meta') google_meta.attrs['name'] = 'google-site-verification' google_meta.attrs['content'] = google_content soup.head.append(google_meta) - if bing_content and soup.find("meta", attrs={"name": "msvalidate.01"}) is None: + if (bing_content and soup.find("meta", attrs={"name": "msvalidate.01"}) is None): bing_meta = soup.new_tag('meta') bing_meta.attrs['name'] = 'msvalidate.01' bing_meta.attrs['content'] = bing_content From 813e44dc93dd42e81a4b0125922a3b27201fee67 Mon Sep 17 00:00:00 2001 From: rjt-gupta Date: Thu, 14 Mar 2019 12:52:17 +0530 Subject: [PATCH 3/4] requested changes --- snare/tests/test_add_meta_tag.py | 2 ++ snare/tests/test_parse_timeout.py | 8 ++++---- snare/tests/test_str_to_bool.py | 8 ++++++-- snare/utils/snare_helpers.py | 1 - 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/snare/tests/test_add_meta_tag.py b/snare/tests/test_add_meta_tag.py index 697993b8..5c3bea59 100644 --- a/snare/tests/test_add_meta_tag.py +++ b/snare/tests/test_add_meta_tag.py @@ -28,6 +28,8 @@ def test_add_meta_tag(self): assert(soup.find("meta", attrs={"name": "google-site-verification"}) and soup.find("meta", attrs={"name": "msvalidate.01"})) + def test_add_meta_tag_with_empty_tags(self): + config = configparser.ConfigParser() config['WEB-TOOLS'] = dict(google='', bing='') assert add_meta_tag(self.page_dir, self.index_page, config) is None diff --git a/snare/tests/test_parse_timeout.py b/snare/tests/test_parse_timeout.py index 58829077..d6d29b66 100644 --- a/snare/tests/test_parse_timeout.py +++ b/snare/tests/test_parse_timeout.py @@ -5,8 +5,8 @@ class TestParseTimeout(unittest.TestCase): def test_parse_timeout(self): - assert parse_timeout('20H') == 72000 - assert parse_timeout('10M') == 600 - assert parse_timeout('1D') == 86400 + assert parse_timeout('20H') == 20*60*60 + assert parse_timeout('10M') == 10*60 + assert parse_timeout('1D') == 1*86400 - assert parse_timeout('24Y') == 86400 # Default 24H format is used. + assert parse_timeout('24Y') == 24*60*60 # Default 24H format is used. diff --git a/snare/tests/test_str_to_bool.py b/snare/tests/test_str_to_bool.py index bc9cd1e3..69a472e8 100644 --- a/snare/tests/test_str_to_bool.py +++ b/snare/tests/test_str_to_bool.py @@ -8,11 +8,15 @@ class TestStrToBool(unittest.TestCase): def setUp(self): self.v = None - def test_str_to_bool(self): + def test_str_to_bool_true(self): self.v = 'true' assert str_to_bool(self.v) is True + + def test_str_to_bool_false(self): self.v = 'false' assert str_to_bool(self.v) is False + def test_str_to_bool_error(self): + self.v = 'twz' with self.assertRaises(ArgumentTypeError): - str_to_bool('twz') + str_to_bool(self.v) diff --git a/snare/utils/snare_helpers.py b/snare/utils/snare_helpers.py index 24998021..1c0f6528 100644 --- a/snare/utils/snare_helpers.py +++ b/snare/utils/snare_helpers.py @@ -80,7 +80,6 @@ def add_meta_tag(page_dir, index_page, config): def parse_timeout(timeout): - result = None timeouts_coeff = { 'M': 60, 'H': 3600, From 58d2c6c8bfc7eb2d25b9e8eb21ba379a13b074a7 Mon Sep 17 00:00:00 2001 From: rjt-gupta Date: Thu, 14 Mar 2019 17:56:36 +0530 Subject: [PATCH 4/4] minor change --- snare/tests/test_parse_timeout.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snare/tests/test_parse_timeout.py b/snare/tests/test_parse_timeout.py index d6d29b66..b4a3de5f 100644 --- a/snare/tests/test_parse_timeout.py +++ b/snare/tests/test_parse_timeout.py @@ -7,6 +7,6 @@ class TestParseTimeout(unittest.TestCase): def test_parse_timeout(self): assert parse_timeout('20H') == 20*60*60 assert parse_timeout('10M') == 10*60 - assert parse_timeout('1D') == 1*86400 + assert parse_timeout('1D') == 24*60*60 assert parse_timeout('24Y') == 24*60*60 # Default 24H format is used.