From 5e63db30a5102c373281f5b36e380a50ce7d3c2b Mon Sep 17 00:00:00 2001 From: mode9 Date: Sun, 16 Jan 2022 03:39:13 +0900 Subject: [PATCH 1/4] Apply moto --- collectfast/tests/command/test_command.py | 3 +-- collectfast/tests/command/test_disable.py | 2 +- collectfast/tests/command/utils.py | 1 + collectfast/tests/settings.py | 10 ++++--- collectfast/tests/utils.py | 33 +++++++++++++++++++++++ test-requirements.txt | 1 + 6 files changed, 43 insertions(+), 7 deletions(-) diff --git a/collectfast/tests/command/test_command.py b/collectfast/tests/command/test_command.py index 858f1690..c3d434b1 100644 --- a/collectfast/tests/command/test_command.py +++ b/collectfast/tests/command/test_command.py @@ -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 @@ -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", diff --git a/collectfast/tests/command/test_disable.py b/collectfast/tests/command/test_disable.py index 1dd439aa..ef927237 100644 --- a/collectfast/tests/command/test_disable.py +++ b/collectfast/tests/command/test_disable.py @@ -9,7 +9,7 @@ from collectfast.tests.utils import make_test from collectfast.tests.utils import override_setting -from .utils import call_collectstatic +from collectfast.tests.command.utils import call_collectstatic @make_test diff --git a/collectfast/tests/command/utils.py b/collectfast/tests/command/utils.py index 9fe9bff1..3722e9e8 100644 --- a/collectfast/tests/command/utils.py +++ b/collectfast/tests/command/utils.py @@ -2,6 +2,7 @@ from typing import Any from django.core.management import call_command +from moto import mock_s3 def call_collectstatic(*args: Any, **kwargs: Any) -> str: diff --git a/collectfast/tests/settings.py b/collectfast/tests/settings.py index 30f012f6..73b697cf 100644 --- a/collectfast/tests/settings.py +++ b/collectfast/tests/settings.py @@ -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() diff --git a/collectfast/tests/utils.py b/collectfast/tests/utils.py index 9ad5801e..03aeeaa4 100644 --- a/collectfast/tests/utils.py +++ b/collectfast/tests/utils.py @@ -10,6 +10,8 @@ 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 @@ -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 @@ -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 @@ -109,3 +115,30 @@ 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: unittest.TestCase) -> None: + klass.mock_s3 = moto.mock_s3() + klass.mock_s3.start() + create_bucket() + + +def tearDown(klass: unittest.TestCase) -> None: + delete_bucket() + klass.mock_s3.stop() diff --git a/test-requirements.txt b/test-requirements.txt index 0a194dc5..ac943cf2 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -6,3 +6,4 @@ boto3 google-cloud-storage pytest pytest-django +moto[s3] \ No newline at end of file From 3991bde9604cbac115c56cb9278c9d379fe4dcd0 Mon Sep 17 00:00:00 2001 From: mode9 Date: Sun, 16 Jan 2022 16:32:19 +0900 Subject: [PATCH 2/4] Mocking google cloud storage client --- collectfast/tests/utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/collectfast/tests/utils.py b/collectfast/tests/utils.py index 03aeeaa4..5c71d226 100644 --- a/collectfast/tests/utils.py +++ b/collectfast/tests/utils.py @@ -2,7 +2,7 @@ import os import pathlib import random -import unittest +import unittest.mock import uuid from typing import Any from typing import Callable @@ -137,8 +137,13 @@ def setUp(klass: 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: unittest.TestCase) -> None: delete_bucket() klass.mock_s3.stop() + klass.gc.stop() From e77d23bed1d2359ef880a0e70b308534d47c773d Mon Sep 17 00:00:00 2001 From: mode9 Date: Sun, 16 Jan 2022 17:22:23 +0900 Subject: [PATCH 3/4] chore: remove unnecessray import --- collectfast/tests/command/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/collectfast/tests/command/utils.py b/collectfast/tests/command/utils.py index 3722e9e8..9fe9bff1 100644 --- a/collectfast/tests/command/utils.py +++ b/collectfast/tests/command/utils.py @@ -2,7 +2,6 @@ from typing import Any from django.core.management import call_command -from moto import mock_s3 def call_collectstatic(*args: Any, **kwargs: Any) -> str: From ddb9ccaf7d467d3cf1212bbe13cad6130173adf1 Mon Sep 17 00:00:00 2001 From: mode9 Date: Sun, 16 Jan 2022 17:35:16 +0900 Subject: [PATCH 4/4] style: fix lint error --- .pre-commit-config.yaml | 1 + collectfast/tests/command/test_disable.py | 3 +-- collectfast/tests/settings.py | 4 ++-- collectfast/tests/utils.py | 14 +++++++------- test-requirements.txt | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 99a0153c..5af17b92 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -62,6 +62,7 @@ repos: - django-storages - boto3 - google-cloud-storage + - moto - repo: https://github.com/mgedmin/check-manifest rev: "0.47" hooks: diff --git a/collectfast/tests/command/test_disable.py b/collectfast/tests/command/test_disable.py index ef927237..2ae72d25 100644 --- a/collectfast/tests/command/test_disable.py +++ b/collectfast/tests/command/test_disable.py @@ -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 collectfast.tests.command.utils import call_collectstatic - @make_test @override_django_settings( diff --git a/collectfast/tests/settings.py b/collectfast/tests/settings.py index 73b697cf..261d247b 100644 --- a/collectfast/tests/settings.py +++ b/collectfast/tests/settings.py @@ -39,8 +39,8 @@ AWS_IS_GZIPPED = False AWS_SECURITY_TOKEN = "testing" AWS_SESSION_TOKEN = "testing" -AWS_ACCESS_KEY_ID = 'testing' -AWS_SECRET_ACCESS_KEY = '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 diff --git a/collectfast/tests/utils.py b/collectfast/tests/utils.py index 5c71d226..19054cf6 100644 --- a/collectfast/tests/utils.py +++ b/collectfast/tests/utils.py @@ -118,32 +118,32 @@ def wrapper(*args, **kwargs): 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 = 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 + CreateBucketConfiguration=location, ) def delete_bucket() -> None: - s3 = boto3.resource('s3', region_name=django_settings.AWS_S3_REGION_NAME) + 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: unittest.TestCase) -> None: +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.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: unittest.TestCase) -> None: +def tearDown(klass: Type[unittest.TestCase]) -> None: delete_bucket() klass.mock_s3.stop() klass.gc.stop() diff --git a/test-requirements.txt b/test-requirements.txt index ac943cf2..88f829c5 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -6,4 +6,4 @@ boto3 google-cloud-storage pytest pytest-django -moto[s3] \ No newline at end of file +moto[s3]