from unittest.mock import patch, MagicMock

from pdchaosazure.webapp.actions import stop, restart, delete
from tests.data import config_provider, secrets_provider, webapp_provider


@patch('pdchaosazure.webapp.actions.fetch_webapps', autospec=True)
@patch('pdchaosazure.webapp.actions.client.init', autospec=True)
def test_happily_stop_webapp(init, fetch):
    config = config_provider.provide_default_config()
    secrets = secrets_provider.provide_secrets_public()
    webapp = webapp_provider.default()

    client = MagicMock()
    init.return_value = client
    resource_list = [webapp]
    fetch.return_value = resource_list

    f = "where resourceGroup=~'rg'"
    stop(f, config, secrets)

    fetch.assert_called_with(f, config, secrets)
    client.web_apps.stop.assert_called_with(webapp['resourceGroup'], webapp['name'])


@patch('pdchaosazure.webapp.actions.fetch_webapps', autospec=True)
@patch('pdchaosazure.webapp.actions.client.init', autospec=True)
def test_happily_restart_webapp(init, fetch):
    config = config_provider.provide_default_config()
    secrets = secrets_provider.provide_secrets_public()
    webapp = webapp_provider.default()

    client = MagicMock()
    init.return_value = client
    resource_list = [webapp]
    fetch.return_value = resource_list

    f = "where resourceGroup=~'rg'"
    restart(f, config, secrets)

    fetch.assert_called_with(f, config, secrets)
    client.web_apps.restart.assert_called_with(webapp['resourceGroup'], webapp['name'])


@patch('pdchaosazure.webapp.actions.fetch_webapps', autospec=True)
@patch('pdchaosazure.webapp.actions.client.init', autospec=True)
def test_happily_delete_webapp(init, fetch):
    webapp = webapp_provider.default()
    config = config_provider.provide_default_config()
    secrets = secrets_provider.provide_secrets_public()

    client = MagicMock()
    init.return_value = client
    resource_list = [webapp]
    fetch.return_value = resource_list

    f = "where resourceGroup=~'rg'"
    delete(f, config, secrets)

    fetch.assert_called_with(f, config, secrets)
    client.web_apps.delete.assert_called_with(webapp['resourceGroup'], webapp['name'])

[(7, 'unittest.mock.patch', 'patch', (['"""pdchaosazure.webapp.actions.fetch_webapps"""'], {'autospec': '(True)'}), False, 'from unittest.mock import patch, MagicMock\n'), (8, 'unittest.mock.patch', 'patch', (['"""pdchaosazure.webapp.actions.client.init"""'], {'autospec': '(True)'}), False, 'from unittest.mock import patch, MagicMock\n'), (26, 'unittest.mock.patch', 'patch', (['"""pdchaosazure.webapp.actions.fetch_webapps"""'], {'autospec': '(True)'}), False, 'from unittest.mock import patch, MagicMock\n'), (27, 'unittest.mock.patch', 'patch', (['"""pdchaosazure.webapp.actions.client.init"""'], {'autospec': '(True)'}), False, 'from unittest.mock import patch, MagicMock\n'), (45, 'unittest.mock.patch', 'patch', (['"""pdchaosazure.webapp.actions.fetch_webapps"""'], {'autospec': '(True)'}), False, 'from unittest.mock import patch, MagicMock\n'), (46, 'unittest.mock.patch', 'patch', (['"""pdchaosazure.webapp.actions.client.init"""'], {'autospec': '(True)'}), False, 'from unittest.mock import patch, MagicMock\n'), (10, 'tests.data.config_provider.provide_default_config', 'config_provider.provide_default_config', ([], {}), False, 'from tests.data import config_provider, secrets_provider, webapp_provider\n'), (11, 'tests.data.secrets_provider.provide_secrets_public', 'secrets_provider.provide_secrets_public', ([], {}), False, 'from tests.data import config_provider, secrets_provider, webapp_provider\n'), (12, 'tests.data.webapp_provider.default', 'webapp_provider.default', ([], {}), False, 'from tests.data import config_provider, secrets_provider, webapp_provider\n'), (14, 'unittest.mock.MagicMock', 'MagicMock', ([], {}), False, 'from unittest.mock import patch, MagicMock\n'), (20, 'pdchaosazure.webapp.actions.stop', 'stop', (['f', 'config', 'secrets'], {}), False, 'from pdchaosazure.webapp.actions import stop, restart, delete\n'), (29, 'tests.data.config_provider.provide_default_config', 'config_provider.provide_default_config', ([], {}), False, 'from tests.data import config_provider, secrets_provider, webapp_provider\n'), (30, 'tests.data.secrets_provider.provide_secrets_public', 'secrets_provider.provide_secrets_public', ([], {}), False, 'from tests.data import config_provider, secrets_provider, webapp_provider\n'), (31, 'tests.data.webapp_provider.default', 'webapp_provider.default', ([], {}), False, 'from tests.data import config_provider, secrets_provider, webapp_provider\n'), (33, 'unittest.mock.MagicMock', 'MagicMock', ([], {}), False, 'from unittest.mock import patch, MagicMock\n'), (39, 'pdchaosazure.webapp.actions.restart', 'restart', (['f', 'config', 'secrets'], {}), False, 'from pdchaosazure.webapp.actions import stop, restart, delete\n'), (48, 'tests.data.webapp_provider.default', 'webapp_provider.default', ([], {}), False, 'from tests.data import config_provider, secrets_provider, webapp_provider\n'), (49, 'tests.data.config_provider.provide_default_config', 'config_provider.provide_default_config', ([], {}), False, 'from tests.data import config_provider, secrets_provider, webapp_provider\n'), (50, 'tests.data.secrets_provider.provide_secrets_public', 'secrets_provider.provide_secrets_public', ([], {}), False, 'from tests.data import config_provider, secrets_provider, webapp_provider\n'), (52, 'unittest.mock.MagicMock', 'MagicMock', ([], {}), False, 'from unittest.mock import patch, MagicMock\n'), (58, 'pdchaosazure.webapp.actions.delete', 'delete', (['f', 'config', 'secrets'], {}), False, 'from pdchaosazure.webapp.actions import stop, restart, delete\n')]