<commit_before>import getpass
import os

from unittest import TestCase

from mock import patch, mock_open
import pytest

from dvc.remote.ssh import RemoteSSH


class TestRemoteSSH(TestCase):
    def test_url(self):
        user = "test"
        host = "172.16.58.3"
        port = 1234
        path = "/path/to/dir"

        # URL ssh://[user@]host.xz[:port]/path
        url = "ssh://{}@{}:{}{}".format(user, host, port, path)
        config = {"url": url}

        remote = RemoteSSH(None, config)

        self.assertEqual(remote.user, user)
        self.assertEqual(remote.host, host)
        self.assertEqual(remote.port, port)
        self.assertEqual(remote.prefix, path)

        # SCP-like URL ssh://[user@]host.xz:/absolute/path
        url = "ssh://{}@{}:{}".format(user, host, path)
        config = {"url": url}

        remote = RemoteSSH(None, config)

        self.assertEqual(remote.user, user)
        self.assertEqual(remote.host, host)
        self.assertEqual(remote.port, remote.DEFAULT_PORT)
        self.assertEqual(remote.prefix, path)

    def test_no_path(self):
        config = {"url": "ssh://127.0.0.1"}
        remote = RemoteSSH(None, config)
        self.assertEqual(remote.prefix, "/")


mock_ssh_config = """
Host example.com
   User ubuntu
   HostName 1.2.3.4
   Port 1234
   IdentityFile ~/.ssh/not_default.key
"""


@pytest.mark.parametrize(
    "config,expected_host",
    [
        ({"url": "ssh://example.com"}, "1.2.3.4"),
        ({"url": "ssh://not_in_ssh_config.com"}, "not_in_ssh_config.com"),
    ],
)
@patch("os.path.exists", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data=mock_ssh_config)
def test_ssh_host_override_from_config(
    mock_exists, mock_file, config, expected_host
):
    remote = RemoteSSH(None, config)

    mock_file.assert_called_with(os.path.expanduser("~/.ssh/config"))
    assert remote.host == expected_host


@pytest.mark.parametrize(
    "config,expected_user",
    [
        ({"url": "ssh://test1@example.com"}, "test1"),
        ({"url": "ssh://example.com", "user": "test2"}, "test2"),
        ({"url": "ssh://example.com"}, "ubuntu"),
        ({"url": "ssh://test1@not_in_ssh_config.com"}, "test1"),
        (
            {"url": "ssh://test1@not_in_ssh_config.com", "user": "test2"},
            "test2",
        ),
        ({"url": "ssh://not_in_ssh_config.com"}, getpass.getuser()),
    ],
)
@patch("os.path.exists", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data=mock_ssh_config)
def test_ssh_user(mock_exists, mock_file, config, expected_user):
    remote = RemoteSSH(None, config)

    mock_file.assert_called_with(os.path.expanduser("~/.ssh/config"))
    assert remote.user == expected_user


@pytest.mark.parametrize(
    "config,expected_port",
    [
        ({"url": "ssh://example.com:2222"}, 2222),
        ({"url": "ssh://example.com"}, 1234),
        ({"url": "ssh://example.com", "port": 4321}, 4321),
        ({"url": "ssh://not_in_ssh_config.com"}, RemoteSSH.DEFAULT_PORT),
        ({"url": "ssh://not_in_ssh_config.com:2222"}, 2222),
        ({"url": "ssh://not_in_ssh_config.com:2222", "port": 4321}, 4321),
    ],
)
@patch("os.path.exists", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data=mock_ssh_config)
def test_ssh_port(mock_exists, mock_file, config, expected_port):
    remote = RemoteSSH(None, config)

    mock_file.assert_called_with(os.path.expanduser("~/.ssh/config"))
    assert remote.port == expected_port


@pytest.mark.parametrize(
    "config,expected_keyfile",
    [
        (
            {"url": "ssh://example.com", "keyfile": "dvc_config.key"},
            "dvc_config.key",
        ),
        (
            {"url": "ssh://example.com"},
            os.path.expanduser("~/.ssh/not_default.key"),
        ),
        (
            {
                "url": "ssh://not_in_ssh_config.com",
                "keyfile": "dvc_config.key",
            },
            "dvc_config.key",
        ),
        ({"url": "ssh://not_in_ssh_config.com"}, None),
    ],
)
@patch("os.path.exists", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data=mock_ssh_config)
def test_ssh_keyfile(mock_exists, mock_file, config, expected_keyfile):
    remote = RemoteSSH(None, config)

    mock_exists.assert_called_with(os.path.expanduser("~/.ssh/config"))
    mock_file.assert_called_with(os.path.expanduser("~/.ssh/config"))
    assert remote.keyfile == expected_keyfile<commit_msg>test: ssh: fixes wrong order of mocks<commit_after>import getpass
import os

from unittest import TestCase

from mock import patch, mock_open
import pytest

from dvc.remote.ssh import RemoteSSH


class TestRemoteSSH(TestCase):
    def test_url(self):
        user = "test"
        host = "172.16.58.3"
        port = 1234
        path = "/path/to/dir"

        # URL ssh://[user@]host.xz[:port]/path
        url = "ssh://{}@{}:{}{}".format(user, host, port, path)
        config = {"url": url}

        remote = RemoteSSH(None, config)

        self.assertEqual(remote.user, user)
        self.assertEqual(remote.host, host)
        self.assertEqual(remote.port, port)
        self.assertEqual(remote.prefix, path)

        # SCP-like URL ssh://[user@]host.xz:/absolute/path
        url = "ssh://{}@{}:{}".format(user, host, path)
        config = {"url": url}

        remote = RemoteSSH(None, config)

        self.assertEqual(remote.user, user)
        self.assertEqual(remote.host, host)
        self.assertEqual(remote.port, remote.DEFAULT_PORT)
        self.assertEqual(remote.prefix, path)

    def test_no_path(self):
        config = {"url": "ssh://127.0.0.1"}
        remote = RemoteSSH(None, config)
        self.assertEqual(remote.prefix, "/")


mock_ssh_config = """
Host example.com
   User ubuntu
   HostName 1.2.3.4
   Port 1234
   IdentityFile ~/.ssh/not_default.key
"""


@pytest.mark.parametrize(
    "config,expected_host",
    [
        ({"url": "ssh://example.com"}, "1.2.3.4"),
        ({"url": "ssh://not_in_ssh_config.com"}, "not_in_ssh_config.com"),
    ],
)
@patch("os.path.exists", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data=mock_ssh_config)
def test_ssh_host_override_from_config(
    mock_file, mock_exists, config, expected_host
):
    remote = RemoteSSH(None, config)

    mock_exists.assert_called_with(os.path.expanduser("~/.ssh/config"))
    mock_file.assert_called_with(os.path.expanduser("~/.ssh/config"))
    assert remote.host == expected_host


@pytest.mark.parametrize(
    "config,expected_user",
    [
        ({"url": "ssh://test1@example.com"}, "test1"),
        ({"url": "ssh://example.com", "user": "test2"}, "test2"),
        ({"url": "ssh://example.com"}, "ubuntu"),
        ({"url": "ssh://test1@not_in_ssh_config.com"}, "test1"),
        (
            {"url": "ssh://test1@not_in_ssh_config.com", "user": "test2"},
            "test2",
        ),
        ({"url": "ssh://not_in_ssh_config.com"}, getpass.getuser()),
    ],
)
@patch("os.path.exists", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data=mock_ssh_config)
def test_ssh_user(mock_file, mock_exists, config, expected_user):
    remote = RemoteSSH(None, config)

    mock_exists.assert_called_with(os.path.expanduser("~/.ssh/config"))
    mock_file.assert_called_with(os.path.expanduser("~/.ssh/config"))
    assert remote.user == expected_user


@pytest.mark.parametrize(
    "config,expected_port",
    [
        ({"url": "ssh://example.com:2222"}, 2222),
        ({"url": "ssh://example.com"}, 1234),
        ({"url": "ssh://example.com", "port": 4321}, 4321),
        ({"url": "ssh://not_in_ssh_config.com"}, RemoteSSH.DEFAULT_PORT),
        ({"url": "ssh://not_in_ssh_config.com:2222"}, 2222),
        ({"url": "ssh://not_in_ssh_config.com:2222", "port": 4321}, 4321),
    ],
)
@patch("os.path.exists", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data=mock_ssh_config)
def test_ssh_port(mock_file, mock_exists, config, expected_port):
    remote = RemoteSSH(None, config)

    mock_exists.assert_called_with(os.path.expanduser("~/.ssh/config"))
    mock_file.assert_called_with(os.path.expanduser("~/.ssh/config"))
    assert remote.port == expected_port


@pytest.mark.parametrize(
    "config,expected_keyfile",
    [
        (
            {"url": "ssh://example.com", "keyfile": "dvc_config.key"},
            "dvc_config.key",
        ),
        (
            {"url": "ssh://example.com"},
            os.path.expanduser("~/.ssh/not_default.key"),
        ),
        (
            {
                "url": "ssh://not_in_ssh_config.com",
                "keyfile": "dvc_config.key",
            },
            "dvc_config.key",
        ),
        ({"url": "ssh://not_in_ssh_config.com"}, None),
    ],
)
@patch("os.path.exists", return_value=True)
@patch("builtins.open", new_callable=mock_open, read_data=mock_ssh_config)
def test_ssh_keyfile(mock_file, mock_exists, config, expected_keyfile):
    remote = RemoteSSH(None, config)

    mock_exists.assert_called_with(os.path.expanduser("~/.ssh/config"))
    mock_file.assert_called_with(os.path.expanduser("~/.ssh/config"))
    assert remote.keyfile == expected_keyfile
{'fraction_non_alphanumeric': 0.1768345636126791, 'fraction_numerical': 0.01715154146765089, 'mean_word_length': 3.055017605633803, 'pattern_counts': {'":': 54, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 10, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4918476', 'n_tokens_mistral': 3398, 'n_tokens_neox': 3164, 'n_words': 482}