Commit bbbf7911 by Mariusz Kupidura Committed by GitHub

Refactor tests (#332)

* Move tests to tests/ directory
* Remove unused `tox.ini` file
* Add flake8 compliance
* Add `lint` target to `tests` target in the Makefile
parent 550bcf44
......@@ -10,4 +10,4 @@ if [[ "$(uname -s)" == "Darwin" ]]; then
source ~/.venv/bin/activate
fi
make tests lint
\ No newline at end of file
make lint tests
\ No newline at end of file
......@@ -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)
......
from routersploit.test.test_case import RoutersploitTestCase
import unittest
import logging
import unittest
from routersploit.utils import NonStringIterable
logging.getLogger().addHandler(logging.NullHandler())
......@@ -17,7 +16,8 @@ class RoutersploitTestCase(unittest.TestCase):
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):
......
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))
......@@ -32,7 +34,9 @@ class RoutersploitCompleterTest(RoutersploitTestCase):
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")
......@@ -249,7 +253,8 @@ class RoutersploitCompleterTest(RoutersploitTestCase):
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')
)
......
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):
......@@ -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):
......@@ -66,7 +65,8 @@ class OptionTest(RoutersploitTestCase):
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'
......@@ -85,9 +85,11 @@ class OptionTest(RoutersploitTestCase):
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'])
......
import unittest
from routersploit.utils import iter_modules
from routersploit.test import RoutersploitTestCase
from tests.test_case import RoutersploitTestCase
class ModuleTest(RoutersploitTestCase):
......@@ -17,7 +17,8 @@ class ModuleTest(RoutersploitTestCase):
self.module = module
def __str__(self):
return " ".join([super(ModuleTest, self).__str__(), self.module.__module__])
return " ".join(
[super(ModuleTest, self).__str__(), self.module.__module__])
@property
def module_metadata(self):
......
from __future__ import absolute_import
import unittest
try:
import unittest.mock as mock
except ImportError:
import mock
import mock
from .. import utils
from . import RoutersploitTestCase
from routersploit import utils
from tests.test_case import RoutersploitTestCase
class UtilsTest(RoutersploitTestCase):
@mock.patch('os.walk')
def test_load_modules_01(self, mock_walk):
mock_walk.return_value = (
('/Abs/Path/routersploit/routersploit/modules', ['asmax', 'creds'], ['__init__.py', '__init__.pyc']),
('/Abs/Path/routersploit/routersploit/modules/creds', [], ['__init__.py', '__init__.pyc', 'ftp_bruteforce.py', 'ftp_bruteforce.pyc']),
('/Abs/Path/routersploit/routersploit/modules/exploits/asmax', [], ['__init__.py', '__init__.pyc', 'asmax_exploit.py', 'asmax_exploit.pyc']),
('/Abs/Path/routersploit/routersploit/modules', ['asmax', 'creds'],
['__init__.py', '__init__.pyc']),
('/Abs/Path/routersploit/routersploit/modules/creds', [],
['__init__.py', '__init__.pyc', 'ftp_bruteforce.py',
'ftp_bruteforce.pyc']),
('/Abs/Path/routersploit/routersploit/modules/exploits/asmax', [],
['__init__.py', '__init__.pyc', 'asmax_exploit.py',
'asmax_exploit.pyc']),
)
path = 'path/to/module'
......@@ -35,9 +35,14 @@ class UtilsTest(RoutersploitTestCase):
@mock.patch('os.walk')
def test_load_modules_import_error_02(self, mock_walk):
mock_walk.return_value = (
('/Abs/Path/routersploit/routersploit/modules', ['asmax', 'creds'], ['__init__.py', '__init__.pyc']),
('/Abs/Path/routersploit/routersploit/modules/creds', [], ['__init__.py', '__init__.pyc', 'ftp_bruteforce.py', 'ftp_bruteforce.pyc']),
('/Abs/Path/routersploit/routersploit/modules/exploits/asmax', [], ['__init__.py', '__init__.pyc', 'asmax_exploit.py', 'asmax_exploit.pyc', 'asmax_multi.py', 'asmax_multi.pyc']),
('/Abs/Path/routersploit/routersploit/modules', ['asmax', 'creds'],
['__init__.py', '__init__.pyc']),
('/Abs/Path/routersploit/routersploit/modules/creds', [],
['__init__.py', '__init__.pyc', 'ftp_bruteforce.py',
'ftp_bruteforce.pyc']),
('/Abs/Path/routersploit/routersploit/modules/exploits/asmax', [],
['__init__.py', '__init__.pyc', 'asmax_exploit.py',
'asmax_exploit.pyc', 'asmax_multi.py', 'asmax_multi.pyc']),
)
path = 'path/to/module'
......
import unittest
try:
import unittest.mock as mock
except ImportError:
import mock
import mock
from routersploit.test import RoutersploitTestCase
from routersploit import validators
from routersploit.exceptions import OptionValidationError
from tests.test_case import RoutersploitTestCase
class ValidatorsTest(RoutersploitTestCase):
......@@ -15,10 +12,12 @@ class ValidatorsTest(RoutersploitTestCase):
self.assertEqual(validators.url("127.0.0.1"), "http://127.0.0.1")
def test_url_already_with_http_prefix(self):
self.assertEqual(validators.url("http://127.0.0.1"), "http://127.0.0.1")
self.assertEqual(validators.url("http://127.0.0.1"),
"http://127.0.0.1")
def test_url_already_with_https_prefix(self):
self.assertEqual(validators.url("https://127.0.0.1"), "https://127.0.0.1")
self.assertEqual(validators.url("https://127.0.0.1"),
"https://127.0.0.1")
def test_ipv4_valid_address(self):
address = "127.0.0.1"
......@@ -139,7 +138,8 @@ class ValidatorsTest(RoutersploitTestCase):
def test_choice_1(self):
valid_values = ["test1", "test2"]
selected_value = "test1"
self.assertEqual(validators.choice(valid_values)(selected_value), selected_value)
self.assertEqual(validators.choice(valid_values)(selected_value),
selected_value)
def test_choice_2(self):
valid_values = ["test1", "test2"]
......
[tox]
skipsdist = True
envlist = py27
[testenv]
deps =
mock
pexpect
-r{toxinidir}/requirements.txt
commands=python -m unittest discover
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment