Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

[removed] Set the test suite regardless of actual cloud connection. #219

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ repos:
- django-storages
- boto3
- google-cloud-storage
- moto
- repo: https://github.com/mgedmin/check-manifest
rev: "0.47"
hooks:
Expand Down
3 changes: 1 addition & 2 deletions collectfast/tests/command/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.test import override_settings as override_django_settings

from collectfast.management.commands.collectstatic import Command
from collectfast.tests.command.utils import call_collectstatic
from collectfast.tests.utils import clean_static_dir
from collectfast.tests.utils import create_static_file
from collectfast.tests.utils import live_test
Expand All @@ -13,8 +14,6 @@
from collectfast.tests.utils import override_storage_attr
from collectfast.tests.utils import test_many

from .utils import call_collectstatic

aws_backend_confs = {
"boto3": override_django_settings(
STATICFILES_STORAGE="storages.backends.s3boto3.S3Boto3Storage",
Expand Down
3 changes: 1 addition & 2 deletions collectfast/tests/command/test_disable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@

from django.test import override_settings as override_django_settings

from collectfast.tests.command.utils import call_collectstatic
from collectfast.tests.utils import clean_static_dir
from collectfast.tests.utils import create_static_file
from collectfast.tests.utils import live_test
from collectfast.tests.utils import make_test
from collectfast.tests.utils import override_setting

from .utils import call_collectstatic


@make_test
@override_django_settings(
Expand Down
10 changes: 6 additions & 4 deletions collectfast/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@
AWS_PRELOAD_METADATA = True
AWS_STORAGE_BUCKET_NAME = "collectfast"
AWS_IS_GZIPPED = False
AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID", "").strip()
AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY", "").strip()
AWS_S3_REGION_NAME = "eu-central-1"
AWS_SECURITY_TOKEN = "testing"
AWS_SESSION_TOKEN = "testing"
AWS_ACCESS_KEY_ID = "testing"
AWS_SECRET_ACCESS_KEY = "testing"
AWS_S3_REGION_NAME = "ap-northeast-2"
AWS_S3_SIGNATURE_VERSION = "s3v4"
AWS_QUERYSTRING_AUTH = False
AWS_DEFAULT_ACL = None
S3_USE_SIGV4 = True
AWS_S3_HOST = "s3.eu-central-1.amazonaws.com"
AWS_S3_HOST = "s3.ap-northeast-2.amazonaws.com"

# Google Cloud
gcloud_credentials_json = os.environ.get("GCLOUD_CREDENTIALS", "").strip()
Expand Down
40 changes: 39 additions & 1 deletion collectfast/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
import os
import pathlib
import random
import unittest
import unittest.mock
import uuid
from typing import Any
from typing import Callable
from typing import Type
from typing import TypeVar
from typing import cast

import boto3
import moto
import pytest
from django.conf import settings as django_settings
from django.utils.module_loading import import_string
Expand Down Expand Up @@ -38,6 +40,8 @@ def make_test(func: F) -> Type[unittest.TestCase]:
"""
case = type(func.__name__, (unittest.TestCase,), {func.__name__: func})
case.__module__ = func.__module__
case.setUp = setUp
case.tearDown = tearDown
return case


Expand All @@ -59,6 +63,8 @@ def test(func: F) -> Type[unittest.TestCase]:

case = type(func.__name__, (unittest.TestCase,), case_dict)
case.__module__ = func.__module__
case.setUp = setUp
case.tearDown = tearDown
return case

return test
Expand Down Expand Up @@ -109,3 +115,35 @@ def wrapper(*args, **kwargs):
return cast(F, wrapper)

return decorator


def create_bucket() -> None:
s3 = boto3.client("s3", region_name=django_settings.AWS_S3_REGION_NAME)
location = {"LocationConstraint": django_settings.AWS_S3_REGION_NAME}
s3.create_bucket(
Bucket=django_settings.AWS_STORAGE_BUCKET_NAME,
CreateBucketConfiguration=location,
)


def delete_bucket() -> None:
s3 = boto3.resource("s3", region_name=django_settings.AWS_S3_REGION_NAME)
bucket = s3.Bucket(django_settings.AWS_STORAGE_BUCKET_NAME)
bucket.objects.delete()
bucket.delete()


def setUp(klass: Type[unittest.TestCase]) -> None:
klass.mock_s3 = moto.mock_s3()
klass.mock_s3.start()
create_bucket()
klass.gc = unittest.mock.patch("storages.backends.gcloud.GoogleCloudStorage")
klass.mock_instance = klass.gc.start().return_value # type: ignore[assignment]
klass.mock_instance._bucket = unittest.mock.MagicMock()
klass.mock_instance.bucket.get_blob.return_value = None


def tearDown(klass: Type[unittest.TestCase]) -> None:
delete_bucket()
klass.mock_s3.stop()
klass.gc.stop()
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ boto3
google-cloud-storage
pytest
pytest-django
moto[s3]