Ejemplo n.º 1
0
 def setUp(self):
     Config.init_from_args_dict(
         {
             "verbose": True,
             "path": os.path.join(os.getcwd(), "extras", "sample"),
         }
     )
Ejemplo n.º 2
0
    def on_any_event(self, event):
        Log.info("Directory changed, rebuilding mapping settings...\n"
                 "Path: %s" % event.src_path +
                 "\nEvent type: %s" % event.event_type)

        Config.reload_sys_path_list()

        self.mapping_manager.parse_yaml_files()

        Log.ok("Mapping settings rebuilt successfully")
Ejemplo n.º 3
0
def main():
    # initialize
    colorama.init()
    mimetypes.init()

    # command line params
    parser = Args.create_parser()
    args = parser.parse_args()

    # apply config
    Config.init_from_args(args)
    Core.run(args)
Ejemplo n.º 4
0
    def test_return_correct_header(self):
        Config.init_from_args_dict({
            "verbose":
            True,
            "path":
            os.path.join(os.getcwd(), "extras", "sample"),
        })

        mapper = MappingItemsManager()

        mock_mapping_request = Mock(url="http://localhost:9000/pymocky_json_1",
                                    method="get")

        response = mapper.response_for_mapping_request(mock_mapping_request)[0]

        self.assertEqual("application/json", response.headers["Content-Type"])
Ejemplo n.º 5
0
    def test_return_status_code(self):
        Config.init_from_args_dict({
            "verbose":
            True,
            "path":
            os.path.join(os.getcwd(), "extras", "sample"),
        })

        mapper = MappingItemsManager()

        mock_mapping_request = Mock(
            url="http://localhost:9000/pymocky_hello_world?name=test",
            method="get")

        response = mapper.response_for_mapping_request(mock_mapping_request)[0]

        self.assertEqual(200, response.status)
Ejemplo n.º 6
0
    def test_returns_all_matching_requests_for_default_scenario(self):
        Config.init_from_args_dict({
            "verbose":
            True,
            "path":
            os.path.join(os.getcwd(), "extras", "sample"),
            "scenario":
            Constants.DEFAULT_SCENARIO,
        })

        mapper = MappingItemsManager()

        mock_mapping_request = Mock(url="http://localhost/pymocky_login",
                                    method="post")

        self.assertEqual(
            len(mapper.mapping_item_for_mapping_request(mock_mapping_request)),
            1)
Ejemplo n.º 7
0
    def test_display_name_is_correct(self):
        Config.init_from_args_dict({
            "verbose":
            True,
            "path":
            os.path.join(os.getcwd(), "extras", "sample"),
            "watch":
            True,
        })

        mapper = MappingItemsManager()

        names = [mapping.display_name for mapping in mapper.mappings]

        self.assertIn("hello-world", names)
        self.assertIn("images", names)
        self.assertIn("files", names)
        self.assertIn("json", names)
        self.assertIn("scenario", names)
Ejemplo n.º 8
0
    def test_empty_dict(self):
        cmd_line = Config.empty_dict()

        self.assertEqual(len(cmd_line), 8)
        self.assertEqual(Config.host, "")
        self.assertEqual(Config.port, 0)
        self.assertEqual(Config.path, "")
        self.assertEqual(Config.delay, 0)
        self.assertEqual(Config.scenario, Constants.DEFAULT_SCENARIO)
        self.assertEqual(Config.watch, False)
        self.assertEqual(Config.cors, False)
Ejemplo n.º 9
0
    def test_init_from_args(self):
        cmd_line = (
            "--verbose --host=127.0.0.1 --port=9000 --path=/my-path --delay=1 --scenario=my-scenario --watch "
            "--cors"
        )

        parser = Args.create_parser()
        argv = cmd_line.split()
        args = parser.parse_args(argv)

        Config.init_from_args(args)

        self.assertEqual(len(argv), 8)
        self.assertEqual(Config.host, "127.0.0.1")
        self.assertEqual(Config.port, Constants.PORT)
        self.assertEqual(Config.path, "/my-path")
        self.assertEqual(Config.delay, 1)
        self.assertEqual(Config.scenario, "my-scenario")
        self.assertEqual(Config.watch, True)
        self.assertEqual(Config.cors, True)
Ejemplo n.º 10
0
    def test_init_from_args_dict(self):
        cmd_line = {
            "verbose": True,
            "host": "127.0.0.1",
            "port": 9000,
            "path": "/my-path",
            "delay": 1,
            "scenario": "my-scenario",
            "watch": True,
            "cors": True,
        }

        Config.init_from_args_dict(cmd_line)

        self.assertEqual(len(cmd_line), 8)
        self.assertEqual(Config.host, "127.0.0.1")
        self.assertEqual(Config.port, Constants.PORT)
        self.assertEqual(Config.path, "/my-path")
        self.assertEqual(Config.delay, 1)
        self.assertEqual(Config.scenario, "my-scenario")
        self.assertEqual(Config.watch, True)
        self.assertEqual(Config.cors, True)
Ejemplo n.º 11
0
    def test_event_listener(self):
        with patch("sys.stdout", new=StringIO()) as output:
            path = os.path.join(os.getcwd(), "extras", "sample")

            Config.init_from_args_dict(
                {
                    "verbose": True,
                    "path": path,
                }
            )

            handler = MappingItemsManager()
            event_handler = MapperDirectoryListener(handler)

            observer = Observer()
            observer.schedule(event_handler, path, recursive=True)
            observer.start()

            file_path = os.path.join(path, "files.yml")
            content = File.get_file_content(file_path)
            File.write_to_file(path, "files.yml", content)

            self.assertIn("Mappings loaded: 13", output.getvalue().strip())
Ejemplo n.º 12
0
    def test_returns_all_matching_requests(self):
        Config.init_from_args_dict({
            "verbose":
            True,
            "path":
            os.path.join(os.getcwd(), "extras", "sample"),
        })

        mapper = MappingItemsManager()

        # two matches
        mock_mapping_request = Mock(url="http://localhost/pymocky_json_1",
                                    method="get")

        self.assertEqual(
            len(mapper.response_for_mapping_request(mock_mapping_request)), 2)

        # normal - get
        mock_mapping_request = Mock(url="http://localhost/pymocky_hello_world",
                                    method="get")

        self.assertEqual(
            len(mapper.response_for_mapping_request(mock_mapping_request)), 1)

        # normal - post
        mock_mapping_request = Mock(url="http://localhost/pymocky_hello_world",
                                    method="post")

        self.assertEqual(
            len(mapper.response_for_mapping_request(mock_mapping_request)), 0)

        # normal - get - with params
        mock_mapping_request = Mock(
            url="http://localhost/pymocky_hello_world?name=test", method="get")

        self.assertEqual(
            len(mapper.response_for_mapping_request(mock_mapping_request)), 1)
Ejemplo n.º 13
0
    def test_returns_all_matching_requests_for_method(self):
        Config.init_from_args_dict({
            "verbose":
            True,
            "path":
            os.path.join(os.getcwd(), "extras", "sample"),
        })

        mapper = MappingItemsManager()

        mock_mapping_request = Mock(
            url="http://localhost/pymocky_hello_world?test1", method="Get")
        self.assertEqual(
            len(mapper.response_for_mapping_request(mock_mapping_request)), 1)

        mock_mapping_request = Mock(
            url="http://localhost/pymocky_hello_world?test2", method="gEt")
        self.assertEqual(
            len(mapper.response_for_mapping_request(mock_mapping_request)), 1)

        mock_mapping_request = Mock(
            url="http://localhost/pymocky_hello_world?test3", method="post")
        self.assertEqual(
            len(mapper.response_for_mapping_request(mock_mapping_request)), 0)