# -*- coding: utf-8 -*-
from unittest import mock

from aiohttp.web import Request
import pytest

from homehub_backend.lib import application


@pytest.fixture
def fake_request(homehub_app):
    result = mock.Mock(spec=Request)
    result.app = homehub_app
    result.host = 'homehub.test'

    return result


class Test_HomeHubJSONRPCView:
    async def test_can_call_public_method(self, fake_request):
        # Given
        jsonrpc_view = application.HomeHubJSONRPCView()

        # When
        result = await jsonrpc_view.can_call(
            fake_request, 'system.list_methods', [], {},
        )

        # Then
        assert result is True

    async def test_can_call_admin_hosts_wildcard(self, fake_request):
        # Given
        fake_request.app['SETTINGS'].ADMIN_HOSTS = '*'

        jsonrpc_view = application.HomeHubJSONRPCView()

        # When
        result = await jsonrpc_view.can_call(
            fake_request, 'services.stop', [], {},
        )

        # Then
        assert result is True

    async def test_can_call_host_in_admin_hosts(self, fake_request):
        # Given
        fake_request.app['SETTINGS'].ADMIN_HOSTS = ['homehub.test']

        jsonrpc_view = application.HomeHubJSONRPCView()

        # When
        result = await jsonrpc_view.can_call(
            fake_request, 'services.stop', [], {},
        )

        # Then
        assert result is True

    async def test_can_call_False(self, fake_request):
        # Given
        fake_request.app['SETTINGS'].ADMIN_HOSTS = ['admin.homehub.test']

        jsonrpc_view = application.HomeHubJSONRPCView()

        # When
        result = await jsonrpc_view.can_call(
            fake_request, 'services.stop', [], {},
        )

        # Then
        assert result is False
