<commit_before>try:
    from http import HTTPStatus
except ImportError:
    # noinspection PyUnresolvedReferences
    from httpstatus import HTTPStatus

import pytest
import requests

from cloudstorage.drivers.azure import AzureStorageDriver
from cloudstorage.exceptions import (
    CloudStorageError, IsNotEmptyError, NotFoundError,
)
from cloudstorage.helpers import file_checksum
from tests.helpers import random_container_name, uri_validator
from tests.settings import *

pytestmark = pytest.mark.skipif(
    not bool(AZURE_ACCOUNT_NAME),
    reason='settings missing account name and key.'
)


@pytest.fixture(scope='module')
def storage():
    driver = AzureStorageDriver(account_name=AZURE_ACCOUNT_NAME,
                                key=AZURE_ACCOUNT_KEY)

    yield driver

    for container in driver:  # cleanup
        if container.name.startswith(CONTAINER_PREFIX):
            for blob in container:
                blob.delete()

            container.delete()


# noinspection PyShadowingNames
def test_driver_create_container(storage):
    container_name = random_container_name()
    container = storage.create_container(container_name)
    assert container_name in storage
    assert container.name == container_name


# noinspection PyShadowingNames
def test_driver_create_container_invalid_name(storage):
    # noinspection PyTypeChecker
    with pytest.raises(CloudStorageError):
        storage.create_container('\?!<>container-name<>!?/')


# noinspection PyShadowingNames
def test_driver_get_container(storage, container):
    container_existing = storage.get_container(container.name)
    assert container_existing.name in storage
    assert container_existing == container


# noinspection PyShadowingNames
def test_driver_get_container_invalid(storage):
    container_name = random_container_name()

    # noinspection PyTypeChecker
    with pytest.raises(NotFoundError):
        storage.get_container(container_name)


# noinspection PyShadowingNames
def test_container_delete(storage):
    container_name = random_container_name()
    container = storage.create_container(container_name)
    container.delete()
    assert container.name not in storage


def test_container_delete_not_empty(container, text_blob):
    assert text_blob in container

    # noinspection PyTypeChecker
    with pytest.raises(IsNotEmptyError):
        container.delete()


def test_container_enable_cdn(container):
    assert container.enable_cdn()


def test_container_disable_cdn(container):
    assert container.disable_cdn()


def test_container_cdn_url(container):
    container.enable_cdn()
    cdn_url = container.cdn_url

    assert uri_validator(cdn_url)
    assert container.name in cdn_url


def test_container_generate_upload_url(container, binary_stream):
    form_post = container.generate_upload_url(blob_name='prefix_',
                                              **BINARY_OPTIONS)
    assert 'url' in form_post and 'fields' in form_post
    assert uri_validator(form_post['url'])

    url = form_post['url']
    headers = form_post['headers']
    multipart_form_data = {
        'file': (BINARY_FORM_FILENAME, binary_stream, 'image/png'),
    }

    # https://blogs.msdn.microsoft.com/azureossds/2015/03/30/uploading-files-to-
    # azure-storage-using-sasshared-access-signature/
    response = requests.put(url, headers=headers, files=multipart_form_data)<commit_msg>Remove tests for enabling and disabling CDN.<commit_after>try:
    from http import HTTPStatus
except ImportError:
    # noinspection PyUnresolvedReferences
    from httpstatus import HTTPStatus

import pytest
import requests

from cloudstorage.drivers.azure import AzureStorageDriver
from cloudstorage.exceptions import (
    CloudStorageError, IsNotEmptyError, NotFoundError,
)
from cloudstorage.helpers import file_checksum
from tests.helpers import random_container_name, uri_validator
from tests.settings import *

pytestmark = pytest.mark.skipif(
    not bool(AZURE_ACCOUNT_NAME),
    reason='settings missing account name and key.'
)


@pytest.fixture(scope='module')
def storage():
    driver = AzureStorageDriver(account_name=AZURE_ACCOUNT_NAME,
                                key=AZURE_ACCOUNT_KEY)

    yield driver

    for container in driver:  # cleanup
        if container.name.startswith(CONTAINER_PREFIX):
            for blob in container:
                blob.delete()

            container.delete()


# noinspection PyShadowingNames
def test_driver_create_container(storage):
    container_name = random_container_name()
    container = storage.create_container(container_name)
    assert container_name in storage
    assert container.name == container_name


# noinspection PyShadowingNames
def test_driver_create_container_invalid_name(storage):
    # noinspection PyTypeChecker
    with pytest.raises(CloudStorageError):
        storage.create_container('\?!<>container-name<>!?/')


# noinspection PyShadowingNames
def test_driver_get_container(storage, container):
    container_existing = storage.get_container(container.name)
    assert container_existing.name in storage
    assert container_existing == container


# noinspection PyShadowingNames
def test_driver_get_container_invalid(storage):
    container_name = random_container_name()

    # noinspection PyTypeChecker
    with pytest.raises(NotFoundError):
        storage.get_container(container_name)


# noinspection PyShadowingNames
def test_container_delete(storage):
    container_name = random_container_name()
    container = storage.create_container(container_name)
    container.delete()
    assert container.name not in storage


def test_container_delete_not_empty(container, text_blob):
    assert text_blob in container

    # noinspection PyTypeChecker
    with pytest.raises(IsNotEmptyError):
        container.delete()


def test_container_enable_cdn(container):
    assert not container.enable_cdn(), 'Azure Storage does not support ' \
                                       'disabling CDN. '


def test_container_disable_cdn(container):
    assert not container.disable_cdn(), 'Azure Storage does not support ' \
                                        'disabling CDN. '


def test_container_cdn_url(container):
    container.enable_cdn()
    cdn_url = container.cdn_url

    assert uri_validator(cdn_url)
    assert container.name in cdn_url


def test_container_generate_upload_url(container, binary_stream):
    form_post = container.generate_upload_url(blob_name='prefix_',
                                              **BINARY_OPTIONS)
    assert 'url' in form_post and 'fields' in form_post
    assert uri_validator(form_post['url'])

    url = form_post['url']
    headers = form_post['headers']
    multipart_form_data = {
        'file': (BINARY_FORM_FILENAME, binary_stream, 'image/png'),
    }

    # https://blogs.msdn.microsoft.com/azureossds/2015/03/30/uploading-files-to-
    # azure-storage-using-sasshared-access-signature/
    response = requests.put(url, headers=headers, files=multipart_form_data)
{'fraction_non_alphanumeric': 0.07500711642470823, 'fraction_numerical': 0.002277255906632508, 'mean_word_length': 3.945109078114004, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2239162', 'n_tokens_mistral': 2020, 'n_tokens_neox': 1960, 'n_words': 411}