def test_backend_module_name_nonexistent(self):
     cfg = {
         "module": __name__,
         "module_class": "NonexistentClassName",
     }
     with pytest.raises(AttributeError):
         medallion.connect_to_backend(cfg)
 def test_backend_module_path_nonexistent(self):
     cfg = {
         "module": "nonexistent.module.path",
         "module_class": mock.sentinel.class_name,
     }
     with pytest.raises(ImportError):
         medallion.connect_to_backend(cfg)
 def test_backend_lookup_unknown(self):
     cfg = {"module_class": mock.sentinel.class_name}
     with mock.patch(
             "medallion.backends.base.BackendRegistry.get",
             side_effect=KeyError,
     ) as mock_find:
         with pytest.raises(ValueError):
             medallion.connect_to_backend(cfg)
     mock_find.assert_called_once_with(mock.sentinel.class_name)
    def test_backend_instantiation_raises(self):
        class SentinelError(BaseException):
            pass

        cfg = {"module_class": mock.sentinel.class_name}
        with mock.patch(
                "medallion.backends.base.BackendRegistry.get",
                return_value=mock.MagicMock(side_effect=SentinelError),
        ) as mock_find:
            with pytest.raises(SentinelError):
                medallion.connect_to_backend(cfg)
        mock_find.assert_called_once_with(mock.sentinel.class_name)
Ejemplo n.º 5
0
def main():
    medallion_parser = _get_argparser()
    medallion_args = medallion_parser.parse_args()
    # Configuration checking sets up debug logging and does not run the app
    if medallion_args.conf_check:
        medallion_args.log_level = logging.DEBUG
    log.setLevel(medallion_args.log_level)

    configuration = medallion.config.load_config(
        medallion_args.CONFIG_PATH or medallion_args.conf_file,
        medallion_args.conf_dir if not medallion_args.no_conf_dir else None,
    )

    set_config(APPLICATION_INSTANCE, "users", configuration)
    set_config(APPLICATION_INSTANCE, "taxii", configuration)
    set_config(APPLICATION_INSTANCE, "backend", configuration)

    APPLICATION_INSTANCE.medallion_backend = connect_to_backend(
        get_application_instance_config_values(APPLICATION_INSTANCE,
                                               "backend"))
    if (not APPLICATION_INSTANCE.blueprints):
        register_blueprints(APPLICATION_INSTANCE)

    if not medallion_args.conf_check:
        APPLICATION_INSTANCE.run(
            host=medallion_args.host,
            port=medallion_args.port,
            debug=medallion_args.debug_mode,
        )
 def test_backend_instantiation(self):
     cfg = {"module_class": mock.sentinel.class_name}
     with mock.patch(
             "medallion.backends.base.BackendRegistry.get", ) as mock_find:
         be_obj = medallion.connect_to_backend(cfg)
     mock_find.assert_called_once_with(mock.sentinel.class_name)
     assert be_obj is mock_find.return_value()
 def test_backend_module_path(self):
     cfg = {
         "module": __name__,
         "module_class": "SavesArgs",
     }
     with mock.patch(
             "medallion.backends.base.BackendRegistry.get", ) as mock_find:
         be_obj = medallion.connect_to_backend(cfg)
     assert mock_find.call_count == 0
     assert isinstance(be_obj, SavesArgs)
     assert be_obj.args == tuple()
     assert be_obj.kwargs == cfg
Ejemplo n.º 8
0
 def setUp(self, start_threads=True):
     self.__name__ = self.type
     self.app = APPLICATION_INSTANCE
     self.app_context = APPLICATION_INSTANCE.app_context()
     self.app_context.push()
     self.app.testing = True
     if not self.app.blueprints:
         register_blueprints(self.app)
     if self.type == "mongo":
         self.configuration = self.mongodb_config
     elif self.type == "memory":
         self.configuration = self.memory_config
     elif self.type == "memory_no_config":
         self.configuration = self.no_config
     elif self.type == "no_taxii":
         self.configuration = self.config_no_taxii
     elif self.type == "no_auth":
         self.configuration = self.config_no_auth
     elif self.type == "no_backend":
         self.configuration = self.config_no_backend
     else:
         raise RuntimeError("Unknown backend!")
     set_config(self.app, "backend", self.configuration)
     set_config(self.app, "users", self.configuration)
     set_config(self.app, "taxii", self.configuration)
     if not start_threads:
         self.app.backend_config["run_cleanup_threads"] = False
     APPLICATION_INSTANCE.medallion_backend = connect_to_backend(
         get_application_instance_config_values(APPLICATION_INSTANCE,
                                                "backend"),
         clear_db=True)
     self.client = APPLICATION_INSTANCE.test_client()
     if self.type == "memory_no_config" or self.type == "no_auth":
         encoded_auth = "Basic " + \
             base64.b64encode(b"user:pass").decode("ascii")
     elif self.type == "mongo":
         encoded_auth = "Basic " + \
             base64.b64encode(b"root:example").decode("ascii")
     else:
         encoded_auth = "Basic " + \
             base64.b64encode(b"admin:Password0").decode("ascii")
     self.headers = {
         "Accept": "application/taxii+json;version=2.1",
         "Authorization": encoded_auth
     }
     self.post_headers = {
         "Content-Type": "application/taxii+json;version=2.1",
         "Accept": "application/taxii+json;version=2.1",
         "Authorization": encoded_auth
     }
 def test_builtin_backend(self):
     be_obj = medallion.connect_to_backend(
         dict(module_class="MemoryBackend", ))
     assert isinstance(be_obj, mbe_mem.MemoryBackend)
 def test_backend_lookup(self):
     cfg = {"module_class": mock.sentinel.class_name}
     with mock.patch(
             "medallion.backends.base.BackendRegistry.get", ) as mock_find:
         medallion.connect_to_backend(cfg)
     mock_find.assert_called_once_with(mock.sentinel.class_name)
 def test_backend_without_name(self):
     with pytest.raises(ValueError):
         medallion.connect_to_backend(dict())