Skip to content

Commit

Permalink
Refactor tests (threat9#332)
Browse files Browse the repository at this point in the history
* Move tests to tests/ directory
* Remove unused `tox.ini` file
* Add flake8 compliance
* Add `lint` target to `tests` target in the Makefile
  • Loading branch information
fwkz authored Oct 21, 2017
1 parent 550bcf4 commit bbbf791
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 136 deletions.
2 changes: 1 addition & 1 deletion .travis/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ if [[ "$(uname -s)" == "Darwin" ]]; then
source ~/.venv/bin/activate
fi

make tests lint
make lint tests
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ run:
docker run -it --rm $(RSF_IMAGE)

lint:
flake8 --exclude=__init__.py --ignore=$(FLAKE8_IGNORED_RULES) $(MODULES)
flake8 --exclude=__init__.py --ignore=$(FLAKE8_IGNORED_RULES) tests $(MODULES)

tests: clean
ifeq ($(MODULES), routersploit)
Expand Down
1 change: 0 additions & 1 deletion routersploit/test/__init__.py

This file was deleted.

Empty file added tests/__init__.py
Empty file.
6 changes: 3 additions & 3 deletions routersploit/test/test_case.py → tests/test_case.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import unittest
import logging
import unittest

from routersploit.utils import NonStringIterable


logging.getLogger().addHandler(logging.NullHandler())


Expand All @@ -17,7 +16,8 @@ def assertIsDecorated(self, function, decorator_name):
self.assertIn(
decorator_name,
decorator_list,
msg="'{}' method should be decorated with 'module_required'".format(function.__name__)
msg="'{}' method should be decorated "
"with 'module_required'".format(function.__name__)
)

def assertIsSubset(self, subset, container):
Expand Down
19 changes: 12 additions & 7 deletions routersploit/test/test_completer.py → tests/test_completer.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import unittest
import os
import unittest

import pexpect

from routersploit.test import RoutersploitTestCase
from tests.test_case import RoutersploitTestCase


class RoutersploitCompleterTest(RoutersploitTestCase):

def __init__(self, methodName='runTest'):
super(RoutersploitCompleterTest, self).__init__(methodName)
self.cli_path = os.path.abspath(os.path.join(__file__, os.pardir, os.pardir, os.pardir, 'rsf.py'))
self.cli_path = os.path.abspath(
os.path.join(__file__, os.pardir, os.pardir, 'rsf.py')
)
self.raw_prompt = "\033[4mrsf\033[0m > "
self.module_prompt = lambda x: "\033[4mrsf\033[0m (\033[91m{}\033[0m) > ".format(x)
self.module_prompt = lambda \
x: "\033[4mrsf\033[0m (\033[91m{}\033[0m) > ".format(x)

def setUp(self):
self.rsf = pexpect.spawn('python {}'.format(self.cli_path))
Expand All @@ -32,7 +34,9 @@ def set_module(self):

def test_raw_commands_no_module(self):
self.rsf.send("\t\t")
self.assertPrompt('exec exit help search show use \r\n', self.raw_prompt)
self.assertPrompt(
'exec exit help search show use \r\n',
self.raw_prompt)

def test_complete_use_raw(self):
self.rsf.send("u\t\t")
Expand Down Expand Up @@ -249,7 +253,8 @@ def test_complete_show(self):
self.set_module()
self.rsf.send("show \t\t")
self.assertPrompt(
'all creds devices exploits info options scanners\r\n',
'all creds devices exploits '
'info options scanners\r\n',
self.module_prompt('FTP Bruteforce')
)

Expand Down
26 changes: 14 additions & 12 deletions routersploit/test/test_exploits.py → tests/test_exploits.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import unittest
import os
import unittest

try:
import unittest.mock as mock
except ImportError:
import mock
import mock

from routersploit.test import RoutersploitTestCase
from routersploit.exploits import Exploit, Option, GLOBAL_OPTS
from routersploit.exploits import Exploit, GLOBAL_OPTS, Option
from tests.test_case import RoutersploitTestCase


def suffix(x):
Expand All @@ -29,8 +26,10 @@ class TestExploitBar(Exploit):


class TestExploitWithValidators(Exploit):
doo = Option(default="default_value", description="description_three", validators=suffix)
paa = Option(default="default_value", description="description_three", validators=(suffix, SUFFIX))
doo = Option(default="default_value", description="description_three",
validators=suffix)
paa = Option(default="default_value", description="description_three",
validators=(suffix, SUFFIX))


class OptionTest(RoutersploitTestCase):
Expand Down Expand Up @@ -66,7 +65,8 @@ def test_if_validator_is_applied_after_setting_value(self):

def test_if_validator_is_applied_in_specific_order(self):
self.exploit_with_validators.paa = "new_value"
self.assertEqual(self.exploit_with_validators.paa, "new_value_suffix_SUFFIX")
self.assertEqual(self.exploit_with_validators.paa,
"new_value_suffix_SUFFIX")

def test_if_exploit_option_is_picked_up_before_global(self):
GLOBAL_OPTS['doo'] = 'global_doo'
Expand All @@ -85,9 +85,11 @@ def test_if_validators_are_applied_on_global_options(self):
self.assertEqual(self.exploit_with_validators.doo, 'global_doo_suffix')

def test_str_representation(self):
with mock.patch.object(TestExploitFoo, "__module__", new_callable=mock.PropertyMock) as mock_module:
with mock.patch.object(TestExploitFoo, "__module__",
new_callable=mock.PropertyMock) as mock_module:
mock_module.return_value = "routersploit.modules.exploits.foo.bar"
self.assertEqual(str(TestExploitFoo()), os.path.join('exploits', 'foo', 'bar'))
self.assertEqual(str(TestExploitFoo()),
os.path.join('exploits', 'foo', 'bar'))

def test_exploit_options_property(self):
self.assertEqual(self.exploit_bar.options, ['paa', 'target', 'doo'])
Expand Down
Loading

0 comments on commit bbbf791

Please sign in to comment.