Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking s3 in tests properly with moto #937

Closed
Ouradze opened this issue Sep 17, 2020 · 1 comment
Closed

Mocking s3 in tests properly with moto #937

Ouradze opened this issue Sep 17, 2020 · 1 comment

Comments

@Ouradze
Copy link

Ouradze commented Sep 17, 2020

Hi, I am using django-storages to upload some data. I would like to know if there is a proper way to mock s3 using moto in the tests. At the moment, I managed to mock the upload with boto3 only but when using it with the storage as I have no endpoint defined, it fails. I am not sure if I defined my fixtures with pytest correctly. If anyone has already tried that, I would be thankful for the help. If my question is unclear, please tell me.

Thanks in advance.

conftest.py

@pytest.fixture
def aws_credentials():
    """S3 mock client"""
    os.environ.setdefault("AWS_ACCESS_KEY_ID", "testing")
    os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "testing")


@pytest.fixture
def s3_client(aws_credentials):
    """S3 mock client"""
    with mock_s3():
        conn = boto3.client("s3", region_name="us-east-1")
        yield conn

storage.py

class DatasetUploadStorage(S3Boto3Storage):
    location = settings.DATASET_UPLOAD_PATH
    object_parameters = {"ACL": "public-read"}

utils.py

def upload_file(filename, storage, file_obj):
    storage.save(filename, file_obj)
    file_url = storage.url(filename)
    return file_url

test.py

@contextmanager
def s3_setup(s3_client):
    s3_client.create_bucket(Bucket="bucket")
    yield


def test_upload_file(s3_client):
    dataset_storage = DatasetUploadStorage()
    with open("sample_dataset.csv") as fp:
        with s3_setup(s3_client):
            file_url = upload_file("dataset.csv", dataset_storage, fp)
    # some assert :)
@jschneier
Copy link
Owner

An example was merged in #911

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants