"""Tests for storage backends available for use within tdpservice."""
import pytest

from tdpservice.backends import DataFilesS3Storage, StaticFilesS3Storage

LOCALSTACK_DUMMY_CREDS = 'test'
LOCALSTACK_REGION_NAME = 'us-gov-west-1'
LOCALSTACK_URL = 'http://localstack:4566'


@pytest.fixture
def datafiles_backend():
    """Return the storage backend for user uploaded Data Files."""
    return DataFilesS3Storage()


@pytest.fixture
def staticfiles_backend():
    """Return the storage backend for Django Admin staticfiles."""
    return StaticFilesS3Storage()


def test_datafiles_localstack_storage(datafiles_backend, settings):
    """Test DataFilesS3Storage uses localstack when USE_LOCALSTACK is True."""
    # USE_LOCALSTACK is True by default in test environments.
    assert settings.USE_LOCALSTACK is True

    # Confirm the backend has the appropriate overridden credentials.
    assert datafiles_backend.access_key == LOCALSTACK_DUMMY_CREDS
    assert datafiles_backend.secret_key == LOCALSTACK_DUMMY_CREDS
    assert datafiles_backend.endpoint_url == LOCALSTACK_URL
    assert datafiles_backend.region_name == LOCALSTACK_REGION_NAME

    # Confirm the client generated by the backend has the appropriate endpoint.
    datafiles_client = datafiles_backend.connection.meta.client
    assert datafiles_client._endpoint.host == LOCALSTACK_URL


def test_datafiles_and_staticfiles_storages_have_distinct_credentials(
    datafiles_backend,
    staticfiles_backend
):
    """Test that the credentials used differ between backends."""
    assert datafiles_backend.access_key != staticfiles_backend.access_key
    assert datafiles_backend.secret_key != staticfiles_backend.secret_key
