# -*- coding: utf-8 -*-
from homehub_backend.testing import services as testing_services


class Test_WebUseService:
    async def test_unknown_service_instance(self, homehub_client):
        response = await homehub_client.get('/services/use', params={
            'kind': 'testing.FakeService',
            'instance': 'idontexist',
            'capability': 'capability',
            'params': 'It works!',
        })
        assert response.status == 400

        data = await response.text()
        assert data == 'Unknown service: idontexist of testing.FakeService'

    async def test_unknown_service_kind(self, homehub_client):
        response = await homehub_client.get('/services/use', params={
            'kind': 'IDontExist',
            'instance': 'fake_instance',
            'capability': 'capability',
            'params': 'It works!',
        })
        assert response.status == 400

        data = await response.text()
        assert data == 'Unknown service: IDontExist'

    async def test_unknown_capability(self, homehub_client):
        service = testing_services.FakeService(
            homehub_client.server.app, 'fake_instance', {},
        )
        homehub_client.server.app['SERVICES'].update({
            'testing.FakeService.fake_instance': service,
        })

        response = await homehub_client.get('/services/use', params={
            'kind': 'testing.FakeService',
            'instance': 'fake_instance',
            'capability': 'idontexist',
            'params': 'It works!',
        })
        assert response.status == 400

        data = await response.text()
        assert data == 'Unknown capability: testing.FakeService.idontexist'

    async def test_capability_not_public(self, homehub_client):
        service = testing_services.FakeService(
            homehub_client.server.app, 'fake_instance', {},
        )
        service.register_capability('capability', service.capability)

        homehub_client.server.app['SERVICES'].update({
            'testing.FakeService.fake_instance': service,
        })

        response = await homehub_client.get('/services/use', params={
            'kind': 'testing.FakeService',
            'instance': 'fake_instance',
            'capability': 'capability',
            'params': 'It works!',
        })
        assert response.status == 400

        data = await response.text()
        assert data == 'Unknown capability: testing.FakeService.capability'

    async def test_ok(self, homehub_client):
        service = testing_services.FakeService(
            homehub_client.server.app, 'fake_instance', {},
        )
        service.register_capability(
            'capability',
            service.capability,
            public=True,
            public_content_type='text/plain',
        )

        homehub_client.server.app['SERVICES'].update({
            'testing.FakeService.fake_instance': service,
        })

        response = await homehub_client.get('/services/use', params={
            'kind': 'testing.FakeService',
            'instance': 'fake_instance',
            'capability': 'capability',
            'params': 'It works!',
        })
        assert response.status == 200

        data = await response.text()
        assert data == 'It works!'
