# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright 2016 Continuum Analytics, Inc.
#
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
# -----------------------------------------------------------------------------

import pytest

from anaconda_enterprise.cli.api import ApiClient, create_client
from anaconda_enterprise.cli.internal import ClientError


def _setUp(mocker, expected_acls):
    mocker.patch.object(ApiClient, 'check')
    ApiClient.check.return_value = None

    mocker.patch.object(ApiClient, 'channels')
    ApiClient.channels.return_value = [{'id': 1, 'attributes': {'name': 'foo', 'data': 1}}]

    mocker.patch.object(ApiClient, '_json_api')
    ApiClient._json_api.return_value = expected_acls


def test_get_channel_acls(config_manager, mocker):
    expected_acls = [{'id': 1, 'attributes': {'name': 'mike', 'namespace': 'users'}}]
    _setUp(mocker, expected_acls)

    client = create_client(config_manager)
    acls = client.get_channel_acls('foo')

    assert acls is not None
    assert acls[0]['id'] == 1


def test_get_channel_acls_not_found(config_manager, mocker):
    expected_acls = [{'id': 1, 'attributes': {'name': 'mike', 'namespace': 'users'}}]
    _setUp(mocker, expected_acls)

    client = create_client(config_manager)
    acls = client.get_channel_acls('bar')

    assert acls is None


def test_find_channel_acl(config_manager, mocker):
    expected_acls = [{'id': 1, 'attributes': {'name': 'mike', 'namespace': 'users'}}]
    _setUp(mocker, expected_acls)

    client = create_client(config_manager)
    acl = client.find_channel_acl('foo', 'mike', 'users')

    assert acl is not None
    assert acl['id'] == expected_acls[0]['id']
    assert acl['attributes']['name'] == expected_acls[0]['attributes']['name']
    assert acl['attributes']['namespace'] == expected_acls[0]['attributes']['namespace']


def test_find_channel_acl_not_found(config_manager, mocker):
    expected_acls = [{'id': 1, 'attributes': {'name': 'mike', 'namespace': 'users'}}]
    _setUp(mocker, expected_acls)

    client = create_client(config_manager)
    acl = client.find_channel_acl('foo', 'rami', 'users')

    assert acl is None


def test_remove_shared_channel(config_manager, mocker):
    expected_acls = [{'id': 1, 'attributes': {'name': 'mike', 'namespace': 'users'}}]
    _setUp(mocker, expected_acls)

    client = create_client(config_manager)
    client.remove_shared_channel('foo', 'mike', 'users')

    ApiClient._json_api.return_value = []

    acl = client.find_channel_acl('foo', 'mike', 'users')

    assert acl is None


def test_remove_shared_channel_not_found(config_manager, mocker):
    expected_acls = [{'id': 1, 'attributes': {'name': 'mike', 'namespace': 'users'}}]
    _setUp(mocker, expected_acls)

    client = create_client(config_manager)
    with pytest.raises(ClientError):
        client.remove_shared_channel('foo', 'rami', 'users')

    acl = client.find_channel_acl('foo', 'mike', 'users')
    assert acl is not None
