import mock

import fastly
from oslo.config import cfg

from poppy.provider.fastly import driver
from tests.unit import base


FASTLY_OPTIONS = [
    cfg.StrOpt('apikey',
               default='123456',
               help='Fastly API Key'),
]


class TestDriver(base.TestCase):

    def setUp(self):
        super(TestDriver, self).setUp()

        self.conf = cfg.ConfigOpts()

    @mock.patch('fastly.connect')
    @mock.patch.object(driver, 'FASTLY_OPTIONS', new=FASTLY_OPTIONS)
    def test_init(self, mock_connect):
        provider = driver.CDNProvider(self.conf)
        mock_connect.assert_called_once_with(
            provider._conf['drivers:provider:fastly'].apikey)

    @mock.patch.object(driver, 'FASTLY_OPTIONS', new=FASTLY_OPTIONS)
    def test_is_alive(self):
        provider = driver.CDNProvider(self.conf)
        self.assertEqual(provider.is_alive(), True)

    @mock.patch.object(fastly, 'FastlyConnection')
    @mock.patch('fastly.connect')
    @mock.patch.object(driver, 'FASTLY_OPTIONS', new=FASTLY_OPTIONS)
    def test_get_client(self, MockConnection, mock_connect):
        mock_connect.return_value = MockConnection(None, None)
        provider = driver.CDNProvider(self.conf)
        client = provider.client()
        self.assertNotEquals(client, None)

    @mock.patch('poppy.provider.fastly.controllers.ServiceController')
    @mock.patch.object(driver, 'FASTLY_OPTIONS', new=FASTLY_OPTIONS)
    def test_service_controller(self, MockController):
        provider = driver.CDNProvider(self.conf)
        self.assertNotEquals(provider.service_controller, None)
