# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright 2017 Continuum Analytics, Inc.
#
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
# -----------------------------------------------------------------------------
from click.testing import CliRunner
import pytest

from anaconda_enterprise.cli.main import group


def test_set_channel(config):  # config is a bad name, but see conftests.py
    runner = CliRunner()
    result = runner.invoke(group, ['config', 'set', 'default_channel', 'channel123'])
    assert result.exit_code == 0
    assert "default_channel: channel123\n" in config.join('cli.yml').read()


def test_set_channel_config_file_doesnot_exist(
        config):  # config is a bad name, but see conftests.py
    runner = CliRunner()
    config.join('cli.yml').remove()
    result = runner.invoke(group, ['config', 'set', 'default_channel', 'channel123'])
    assert result.exit_code == 0
    assert "default_channel: channel123\n" in config.join('cli.yml').read()


def test_get_config_not_set(config):
    runner = CliRunner()
    # conf file exists, but no default_channel see conftests.py
    result = runner.invoke(group, ['config', 'get', 'default_channel'])
    assert result.exit_code != 0
    assert result.output == "Error: Option 'default_channel' is not set\n"


def test_get_channel_config_file_doesnot_exist(
        config):  # config is a bad name, but see conftests.py
    runner = CliRunner()
    config.join('cli.yml').remove()
    result = runner.invoke(group, ['config', 'get', 'default_channel'])
    assert result.exit_code != 0
    assert result.output == "Error: Option 'default_channel' is not set\n"


def test_get_existing_config(config):
    runner = CliRunner()
    # conf file exists
    config.join('cli.yml').write("default_channel: channel123\n", mode="a")
    result = runner.invoke(group, ['config', 'get', 'default_channel'])
    assert result.exit_code == 0
    assert result.output == "'channel123'\n"


def test_set_channel_with_existing_config(config):
    runner = CliRunner()
    # conf file exists
    config.join('cli.yml').write("default_channel: channel123\n", mode="a")
    result = runner.invoke(group, ['config', 'set', 'default_channel', 'channel456'])
    assert result.exit_code == 0
    assert "default_channel: channel456\n" in config.join('cli.yml').read()


def test_set_channel_invalid_chars_in_name(config):
    runner = CliRunner()
    result = runner.invoke(group, ['config', 'set', 'default_channel', 'cha!nnel1'])
    assert result.exit_code != 0
    assert result.output == \
        ("Error: validating configuration 'default_channel'"
         ", 'cha!nnel1' does not match '^[%@/0-9A-Za-z_-]+$'\n")


def test_set_channel_with_underscore(config):
    runner = CliRunner()
    result = runner.invoke(group, ['config', 'set', 'default_channel', 'channel_one'])
    assert result.exit_code == 0
    assert "default_channel: channel_one\n" in config.join('cli.yml').read()


def test_set_channel_with_dashes(config):
    runner = CliRunner()
    result = runner.invoke(group, ['config', 'set', 'default_channel', 'channel-one'])
    assert result.exit_code == 0
    assert "default_channel: channel-one\n" in config.join('cli.yml').read()


def test_set_non_existing_config(config):
    runner = CliRunner()
    result = runner.invoke(group, ['config', 'set', 'anything', 'False'])
    assert result.exit_code != 0
    assert result.output == \
        ("Error: validating configuration '', Additional properties are"
         " not allowed ('anything' was unexpected)\n")


def test_get_ssl_verify(config_v0):
    runner = CliRunner()
    result = runner.invoke(group, ['config', 'get', 'default_site'])
    assert result.exit_code == 0
    assert result.output == "'example'\n"


def test_set_config_w_multiple_types(config):
    runner = CliRunner()
    # We are using 'sites.test.ssl_verify' as attribute because ssl_verify is already
    # used as a mock value in the file created by tests coftest.py . Check that file
    # for more details
    result = runner.invoke(group, ['config', 'set', 'sites.test.ssl_verify', 'False'])
    assert result.exit_code == 0
    config_content = config.join('cli.yml').read()
    assert "ssl_verify: false\n" in config_content


def test_disallow_set_config_sites_as_string(config):
    runner = CliRunner()
    site_to_set = 'https://enterprise.demo.continuum.io/repository/api'
    assert type(site_to_set) == str
    pre_result = runner.invoke(group, ['config', 'view']).output
    result = runner.invoke(group, ['config', 'set', 'sites', site_to_set])
    assert result.exit_code != 0
    assert result.output == (
        "Error: validating configuration 'sites', '{s}' is not of type 'object'\n".format(
            s=site_to_set))
    post_result = runner.invoke(group, ['config', 'view']).output
    assert pre_result == post_result


def test_set_sites_with_dict(config):
    runner = CliRunner()
    sites = dict()
    result = runner.invoke(group, ['config', 'set', 'sites', str(sites)])
    assert result.exit_code == 0
