|
1 | 1 | import pytest |
| 2 | +import os |
2 | 3 | import yaml |
3 | 4 |
|
4 | 5 | from datetime import datetime, timezone |
| 6 | +from unittest import mock |
5 | 7 | from app.utility.base_world import BaseWorld |
6 | 8 |
|
7 | 9 |
|
@@ -91,3 +93,45 @@ def test_is_not_base64(self): |
91 | 93 | def test_is_base64(self): |
92 | 94 | b64str = 'aGVsbG8gd29ybGQgZnJvbSB1bml0IHRlc3QgbGFuZAo=' |
93 | 95 | assert BaseWorld.is_base64(b64str) |
| 96 | + |
| 97 | + @mock.patch.object(os, 'listdir', return_value=['stockpile', 'testplugin', 'dummy']) |
| 98 | + @mock.patch.object(os.path, 'isdir', return_value=True) |
| 99 | + def test_verify_module(self, mock_isdir, mock_listdir): |
| 100 | + def _mock_isfile(path): |
| 101 | + return path in [ |
| 102 | + 'plugins/stockpile/app/requirements/test_req.py', |
| 103 | + 'plugins/testplugin/app/obfuscators/test_obf.py', |
| 104 | + 'app/planners/test_planner.py', |
| 105 | + 'app/parsers/test_parser.py', |
| 106 | + 'app/learning/learning_parser.py', |
| 107 | + ] |
| 108 | + |
| 109 | + with mock.patch.object(os.path, 'isfile', side_effect=_mock_isfile): |
| 110 | + BaseWorld.verify_module('plugins.stockpile.app.requirements.test_req', 'requirements') |
| 111 | + BaseWorld.verify_module('plugins.testplugin.app.obfuscators.test_obf', 'obfuscators') |
| 112 | + BaseWorld.verify_module('app.planners.test_planner', 'planners') |
| 113 | + BaseWorld.verify_module('app.parsers.test_parser', 'parsers') |
| 114 | + BaseWorld.verify_module('app.learning.learning_parser', 'parsers', ['app/learning']) |
| 115 | + |
| 116 | + allowed_paths_str = str([ |
| 117 | + 'app/parsers/myparser.py', |
| 118 | + 'plugins/stockpile/parsers/myparser.py', |
| 119 | + 'plugins/testplugin/parsers/myparser.py', |
| 120 | + 'plugins/dummy/parsers/myparser.py' |
| 121 | + ]) |
| 122 | + expected_err = f'Module data.payloads.myparser does not align with allowed paths for this module type. Allowed paths for this module: {allowed_paths_str}' |
| 123 | + with pytest.raises(ModuleNotFoundError, match=expected_err): |
| 124 | + BaseWorld.verify_module('data.payloads.myparser', 'parsers') |
| 125 | + allowed_paths_str = str([ |
| 126 | + 'otherdir/myparser.py', |
| 127 | + 'app/parsers/myparser.py', |
| 128 | + 'plugins/stockpile/parsers/myparser.py', |
| 129 | + 'plugins/testplugin/parsers/myparser.py', |
| 130 | + 'plugins/dummy/parsers/myparser.py' |
| 131 | + ]) |
| 132 | + expected_err = f'Module plugins.dne.myparser does not align with allowed paths for this module type. Allowed paths for this module: {allowed_paths_str}' |
| 133 | + with pytest.raises(ModuleNotFoundError, match=expected_err): |
| 134 | + BaseWorld.verify_module('data.payloads.myparser', 'parsers', ['otherdir']) |
| 135 | + expected_err = 'Module plugins.stockpile.app.obfuscators.dne with path plugins/stockpile/app/obfuscators/dne was not found on disk.' |
| 136 | + with pytest.raises(ModuleNotFoundError, match=expected_err): |
| 137 | + BaseWorld.verify_module('plugins.stockpile.app.obfuscators.dne', 'obfuscators', ['otherdir']) |
0 commit comments