Ejemplo n.º 1
0
    def test_reloading_config_changes_token(self) -> None:
        """Test that reloading configuration changes token."""
        configuration = Configuration()
        original_token = configuration.token

        configuration.determine_values_from_environment()

        self.assertNotEqual(original_token, configuration.token)
Ejemplo n.º 2
0
    def test_when_all_ports_taken_it_fails(
        self,
        mock_socket_bind: MagicMock,
    ) -> None:
        """Test fail when all ports taken."""
        mock_socket_bind.configure_mock(side_effect=socket.error)

        with self.assertRaises(NotFoundException):
            configuration = Configuration()
            configuration.determine_values_from_environment()
Ejemplo n.º 3
0
    def test_changing_server_port_to_already_taken_fails(
        self,
        mock_socket_bind: MagicMock,
    ) -> None:
        """Test fail during attempting to use taken port."""
        mock_socket_bind.configure_mock(side_effect=socket.error)

        with self.assertRaises(NotFoundException):
            configuration = Configuration()
            configuration.determine_values_from_environment()
Ejemplo n.º 4
0
    def test_changing_server_and_gui_port(
        self,
        mock_secrets_token_hex: MagicMock,
    ) -> None:
        """Test changing API and GUI ports."""
        mock_secrets_token_hex.return_value = "this is a mocked token value"

        configuration = Configuration()
        configuration.determine_values_from_environment()

        self.assertEqual(1234, configuration.server_port)
        self.assertEqual(5678, configuration.gui_port)
        self.assertEqual(
            "http://localhost:5678/?token=this is a mocked token value",
            configuration.get_url(),
        )
Ejemplo n.º 5
0
    def test_defaults(
        self,
        mock_secrets_token_hex: MagicMock,
    ) -> None:
        """Test default values."""
        mock_secrets_token_hex.return_value = "this is a mocked token value"

        configuration = Configuration()
        configuration.determine_values_from_environment()

        self.assertEqual(5000, configuration.server_port)
        self.assertEqual(5000, configuration.gui_port)
        self.assertEqual(logging.CRITICAL, configuration.log_level)
        self.assertEqual("localhost", configuration.server_address)
        self.assertEqual("http", configuration.scheme)
        self.assertEqual("this is a mocked token value", configuration.token)
        self.assertEqual(
            "http://localhost:5000/?token=this is a mocked token value",
            configuration.get_url(),
        )
Ejemplo n.º 6
0
def main() -> None:
    """Get parameters and initialize server."""
    try:
        configuration = Configuration()
    except NotFoundException as e:
        print(str(e))
        sys.exit(1)

    change_log_level(configuration.log_level)
    print(f"Visit {configuration.get_url()} in your browser to access the UX.")

    run_server(configuration)
Ejemplo n.º 7
0
def main() -> None:
    """Get parameters and initialize server."""
    try:
        configuration = Configuration()
    except NotFoundException as e:
        print(str(e))
        sys.exit(1)

    address = configuration.ip
    port = configuration.port
    token = uuid.uuid4().hex

    print(
        f"Visit http://{address}:{port}/?token={token} in your browser to access the UX.",
    )

    run_server(address, port, token)
Ejemplo n.º 8
0
def main() -> None:
    """Get parameters and initialize server."""
    try:
        configuration = Configuration()
    except NotFoundException as e:
        print(str(e))
        sys.exit(1)

    change_log_level(configuration.log_level)
    print(
        "LPOT UX Server started.\n"
        "Setup port forwarding from "
        f"your local port {configuration.gui_port} to "
        f"{configuration.server_port} on this machine.\n"
        f"Then open address {configuration.get_url()}",
    )

    run_server(configuration)
Ejemplo n.º 9
0
def run_server(configuration: Configuration) -> None:
    """Run webserver on specified scheme, address and port."""
    addr = configuration.ip
    port = configuration.port
    token = configuration.token

    cors_allowed_origins = f"{configuration.scheme}://{addr}:{port}"
    if is_development_env():
        cors_allowed_origins = "*"

    app.secret_key = token
    CORS(app, origins=cors_allowed_origins)
    socketio.init_app(
        app,
        cors_allowed_origins=cors_allowed_origins,
        max_http_buffer_size=2000,
    )

    args = {}
    if configuration.is_tls_used():
        args["certfile"] = configuration.tls_certificate
        args["keyfile"] = configuration.tls_key

    socketio.run(app, host=addr, port=port, **args)
Ejemplo n.º 10
0
    def test_many_instances_are_the_same(self) -> None:
        """Test that all instances references same object."""
        original_configuration = Configuration()
        new_configuration = Configuration()

        self.assertTrue(original_configuration is new_configuration)
Ejemplo n.º 11
0
    def test_changing_log_level_to_not_defined_one(self) -> None:
        """Test changing log level to unknown one."""
        configuration = Configuration()
        configuration.determine_values_from_environment()

        self.assertEqual(logging.DEBUG, configuration.log_level)