import os
import pytest

# Global variables
from ultru_client.utils.globals import CLI_GLOBALS, ULTRU_API_KEY

# Ultru Config options
from ultru_client.utils.config import store_username, store_password, list_config, set_store_password

# Authenication to Ultru
from ultru_client.utils.auth import authenticate

# Exceptions
from ultru_client.utils.exceptions import NotAuthorizedError


def test_empty_config(func_config):
    config = list_config()
    assert config.get('username') == ''
    assert config.get('password') == ''
    assert config.get('store_password') == False

def test_store_auth(func_config):
    test_username = 'test_user'
    test_password = '<PASSWORD>'
    store_username(test_username)
    store_password(<PASSWORD>)
    config = list_config()
    assert config.get('username') == test_username
    assert config.get('password') == <PASSWORD>

@pytest.mark.skip(reason="authenticate function needs to skip cognito for test")
def test_valid_auth(func_config):
    del os.environ["TEST"]
    test_username = '<EMAIL>'
    test_password = '<PASSWORD>!'
    store_username(test_username)
    store_password(<PASSWORD>)
    authenticate()
    assert ULTRU_API_KEY.apikey 
    assert ULTRU_API_KEY.url
    os.environ["TEST"] = 1

@pytest.mark.skip(reason="authenticate function needs to skip cognito for test")
def test_invalid_auth(func_config):
    del os.environ["TEST"]
    test_username = '<EMAIL>'
    test_password = '<PASSWORD>'
    store_username(test_username)
    store_password(<PASSWORD>)
    with pytest.raises(NotAuthorizedError):
        authenticate()
    os.environ["TEST"] = 1


import os
import pytest

# Global variables
from ultru_client.utils.globals import CLI_GLOBALS, ULTRU_API_KEY

# Ultru Config options
from ultru_client.utils.config import store_username, store_password, list_config, set_store_password

# Authenication to Ultru
from ultru_client.utils.auth import authenticate

# Exceptions
from ultru_client.utils.exceptions import NotAuthorizedError


def test_empty_config(func_config):
    config = list_config()
    assert config.get('username') == ''
    assert config.get('password') == ''
    assert config.get('store_password') == False

def test_store_auth(func_config):
    test_username = 'test_user'
    test_password = '<PASSWORD>'
    store_username(test_username)
    store_password(<PASSWORD>)
    config = list_config()
    assert config.get('username') == test_username
    assert config.get('password') == <PASSWORD>

@pytest.mark.skip(reason="authenticate function needs to skip cognito for test")
def test_valid_auth(func_config):
    del os.environ["TEST"]
    test_username = '<EMAIL>'
    test_password = '<PASSWORD>!'
    store_username(test_username)
    store_password(<PASSWORD>)
    authenticate()
    assert ULTRU_API_KEY.apikey 
    assert ULTRU_API_KEY.url
    os.environ["TEST"] = 1

@pytest.mark.skip(reason="authenticate function needs to skip cognito for test")
def test_invalid_auth(func_config):
    del os.environ["TEST"]
    test_username = '<EMAIL>'
    test_password = '<PASSWORD>'
    store_username(test_username)
    store_password(<PASSWORD>)
    with pytest.raises(NotAuthorizedError):
        authenticate()
    os.environ["TEST"] = 1


# Global variables # Ultru Config options # Authenication to Ultru # Exceptions