import click, os
import pytest
from click.testing import CliRunner
from gravitee_cli.cli import main
import gravitee_cli.environments as env
from gravitee_cli.gravitee.config import Gravitee_configuration

def test_create_config_file():
    runner = CliRunner()

    with runner.isolated_filesystem():

        result = runner.invoke(main, ['config','--user','test'])

        with open(env.GRAVITEE_CLI_CONF_FILE) as file:
            lineUser = file.readlines()[1]

        assert result.exit_code == 0
        assert lineUser == "user = test\n"
        assert os.path.isfile(env.GRAVITEE_CLI_CONF_FILE)

def test_create_env_config():
    runner = CliRunner()

    with runner.isolated_filesystem():

        result = runner.invoke(main, ['config','--user','testenv', "--env", "QLF"])

        with open(env.GRAVITEE_CLI_CONF_FILE) as file:
            lineUser = file.readlines()[7]

        assert result.exit_code == 0
        assert os.path.isfile(env.GRAVITEE_CLI_CONF_FILE)
        assert lineUser == "user = testenv\n"

def test_create_env_config():
    runner = CliRunner()

    with runner.isolated_filesystem():

        result = runner.invoke(main, ['config','--user','testenv', "--env", "QLF", "--load", "QLF"])

        with open(env.GRAVITEE_CLI_CONF_FILE) as file:
            config = Gravitee_configuration()
            user = config.user

        assert result.exit_code == 0
        assert os.path.isfile(env.GRAVITEE_CLI_CONF_FILE)
        assert user == "testenv"

