from unittest.mock import call
from unittest.mock import Mock
from unittest.mock import patch

from osfclient import cli


@patch('osfclient.cli.os.path.exists', return_value=True)
@patch('osfclient.cli.configparser.ConfigParser')
def test_config_file(MockConfigParser, os_path_exists):
    MockConfigParser().__getitem__ = Mock(return_value={'project': '1234'})

    config = cli.config_from_file()

    assert config == {'project': '1234'}

    assert call.read('.osfcli.config') in MockConfigParser().mock_calls
    assert call('osf') in MockConfigParser().__getitem__.mock_calls


def test_config_from_env_replace_username():
    def simple_getenv(key):
        if key == 'OSF_USERNAME':
            return 'theusername'

    with patch('osfclient.cli.os.getenv', side_effect=simple_getenv):
        config = cli.config_from_env({'username': 'notusername'})

    assert config == {'username': 'theusername'}


def test_config_from_env_username():
    def simple_getenv(key):
        if key == 'OSF_USERNAME':
            return None

    with patch('osfclient.cli.os.getenv', side_effect=simple_getenv):
        config = cli.config_from_env({'username': 'theusername'})

    assert config == {'username': 'theusername'}


def test_config_from_env_replace_project():
    def simple_getenv(key):
        if key == 'OSF_PROJECT':
            return 'theproject'

    with patch('osfclient.cli.os.getenv', side_effect=simple_getenv):
        config = cli.config_from_env({'project': 'notproject'})

    assert config == {'project': 'theproject'}


def test_config_from_env_project():
    def simple_getenv(key):
        if key == 'OSF_PROJECT':
            return None

    with patch('osfclient.cli.os.getenv', side_effect=simple_getenv):
        config = cli.config_from_env({'project': 'theproject'})

    assert config == {'project': 'theproject'}

from unittest.mock import call
from unittest.mock import Mock
from unittest.mock import patch

import pytest

from osfclient.tests.mocks import MockArgs

from osfclient import cli


@patch('osfclient.cli.os.path.exists', return_value=True)
@patch('osfclient.cli.configparser.ConfigParser')
def test_config_file(MockConfigParser, os_path_exists):
    MockConfigParser().__getitem__ = Mock(return_value={'project': '1234'})

    config = cli.config_from_file()

    assert config == {'project': '1234'}

    assert call.read('.osfcli.config') in MockConfigParser().mock_calls
    assert call('osf') in MockConfigParser().__getitem__.mock_calls


def test_config_from_env_replace_username():
    def simple_getenv(key):
        if key == 'OSF_USERNAME':
            return 'theusername'

    with patch('osfclient.cli.os.getenv', side_effect=simple_getenv):
        config = cli.config_from_env({'username': 'notusername'})

    assert config == {'username': 'theusername'}


def test_config_from_env_username():
    def simple_getenv(key):
        if key == 'OSF_USERNAME':
            return None

    with patch('osfclient.cli.os.getenv', side_effect=simple_getenv):
        config = cli.config_from_env({'username': 'theusername'})

    assert config == {'username': 'theusername'}


def test_config_from_env_replace_project():
    def simple_getenv(key):
        if key == 'OSF_PROJECT':
            return 'theproject'

    with patch('osfclient.cli.os.getenv', side_effect=simple_getenv):
        config = cli.config_from_env({'project': 'notproject'})

    assert config == {'project': 'theproject'}


def test_config_from_env_project():
    def simple_getenv(key):
        if key == 'OSF_PROJECT':
            return None

    with patch('osfclient.cli.os.getenv', side_effect=simple_getenv):
        config = cli.config_from_env({'project': 'theproject'})

    assert config == {'project': 'theproject'}


def test_config_project(capsys):
    # No project in args or the config, should sys.exit(1)
    args = MockArgs(project=None)

    def simple_config(key):
        return {}

    with patch('osfclient.cli.config_from_env', side_effect=simple_config):
        with pytest.raises(SystemExit):
            cli._setup_osf(args)

    out, err = capsys.readouterr()
    expected = ('specify a project ID via the command line, configuration '
                'file or environment variable')
    assert expected in out

Add test for error message when no project configured