# Path: notifications_utils/s3.py
# class S3ObjectNotFound(botocore.exceptions.ClientError):
#     pass
# 
# def s3download(bucket_name, filename):
#     try:
#         s3 = resource('s3')
#         key = s3.Object(bucket_name, filename)
#         return key.get()['Body']
#     except botocore.exceptions.ClientError as error:
#         raise S3ObjectNotFound(error.response, error.operation_name)
# 
# def s3upload(
#     filedata,
#     region,
#     bucket_name,
#     file_location,
#     content_type='binary/octet-stream',
#     tags=None,
#     metadata=None,
# ):
#     _s3 = resource('s3')
# 
#     key = _s3.Object(bucket_name, file_location)
# 
#     put_args = {
#         'Body': filedata,
#         'ServerSideEncryption': 'AES256',
#         'ContentType': content_type
#     }
# 
#     if tags:
#         tags = urllib.parse.urlencode(tags)
#         put_args['Tagging'] = tags
# 
#     if metadata:
#         metadata = put_args['Metadata'] = metadata
# 
#     try:
#         key.put(**put_args)
#     except botocore.exceptions.ClientError as e:
#         current_app.logger.error(
#             "Unable to upload file to S3 bucket {}".format(bucket_name)
#         )
#         raise e


from urllib.parse import parse_qs
from notifications_utils.s3 import S3ObjectNotFound, s3download, s3upload
import botocore
import pytest
        bucket_name=bucket,
        file_location=location,
        tags={'a': '1/2', 'b': 'x y'},
    )
    mocked_put = mocked.return_value.Object.return_value.put

    # make sure tags were a urlencoded query string
    encoded_tags = mocked_put.call_args[1]['Tagging']
    assert parse_qs(encoded_tags) == {'a': ['1/2'], 'b': ['x y']}


def test_s3upload_save_file_to_bucket_with_metadata(mocker):
    mocked = mocker.patch('notifications_utils.s3.resource')
    s3upload(
        filedata=contents,
        region=region,
        bucket_name=bucket,
        file_location=location,
        metadata={'status': 'valid', 'pages': '5'}
    )
    mocked_put = mocked.return_value.Object.return_value.put

    metadata = mocked_put.call_args[1]['Metadata']
    assert metadata == {'status': 'valid', 'pages': '5'}


def test_s3download_gets_file(mocker):
    mocked = mocker.patch('notifications_utils.s3.resource')
    mocked_object = mocked.return_value.Object
    mocked_get = mocked.return_value.Object.return_value.get

# Path: notifications_utils/s3.py
# class S3ObjectNotFound(botocore.exceptions.ClientError):
#     pass
# 
# def s3download(bucket_name, filename):
#     try:
#         s3 = resource('s3')
#         key = s3.Object(bucket_name, filename)
#         return key.get()['Body']
#     except botocore.exceptions.ClientError as error:
#         raise S3ObjectNotFound(error.response, error.operation_name)
# 
# def s3upload(
#     filedata,
#     region,
#     bucket_name,
#     file_location,
#     content_type='binary/octet-stream',
#     tags=None,
#     metadata=None,
# ):
#     _s3 = resource('s3')
# 
#     key = _s3.Object(bucket_name, file_location)
# 
#     put_args = {
#         'Body': filedata,
#         'ServerSideEncryption': 'AES256',
#         'ContentType': content_type
#     }
# 
#     if tags:
#         tags = urllib.parse.urlencode(tags)
#         put_args['Tagging'] = tags
# 
#     if metadata:
#         metadata = put_args['Metadata'] = metadata
# 
#     try:
#         key.put(**put_args)
#     except botocore.exceptions.ClientError as e:
#         current_app.logger.error(
#             "Unable to upload file to S3 bucket {}".format(bucket_name)
#         )
#         raise e


# Path: tests/test_s3.py
from urllib.parse import parse_qs
from notifications_utils.s3 import S3ObjectNotFound, s3download, s3upload
import botocore
import pytest

        bucket_name=bucket,
        file_location=location,
        tags={'a': '1/2', 'b': 'x y'},
    )
    mocked_put = mocked.return_value.Object.return_value.put

    # make sure tags were a urlencoded query string
    encoded_tags = mocked_put.call_args[1]['Tagging']
    assert parse_qs(encoded_tags) == {'a': ['1/2'], 'b': ['x y']}


def test_s3upload_save_file_to_bucket_with_metadata(mocker):
    mocked = mocker.patch('notifications_utils.s3.resource')
    s3upload(
        filedata=contents,
        region=region,
        bucket_name=bucket,
        file_location=location,
        metadata={'status': 'valid', 'pages': '5'}
    )
    mocked_put = mocked.return_value.Object.return_value.put

    metadata = mocked_put.call_args[1]['Metadata']
    assert metadata == {'status': 'valid', 'pages': '5'}


def test_s3download_gets_file(mocker):
    mocked = mocker.patch('notifications_utils.s3.resource')
    mocked_object = mocked.return_value.Object
    mocked_get = mocked.return_value.Object.return_value.get

    s3download('bucket', 'location.file')