# Path: tibanna/utils.py
# def create_jobid():
#     return randomword(12)    # date+random_string
# 
# def upload(filepath, bucket, prefix='', public=True, encrypt_s3_upload=False, kms_key_id=None):
#     """ Uploads a file to S3 under a prefix.
#         The original directory structure is removed
#         and only the filename is preserved.
#         If filepath is none, upload an empty file with prefix
#         itself as key
#         If encrypt_s3_upload is True, default Server-Side encryption is enabled
#         If kms_key_id is True as well, Server-Side encryption with the given key_id is enabled
#     """
#     if public:
#         acl = 'public-read'
#     else:
#         acl = 'private'
#     s3 = boto3.client('s3')
#     if encrypt_s3_upload:
#         upload_extra_args = {'ServerSideEncryption': 'aws:kms'}
#         if kms_key_id:
#             upload_extra_args['SSEKMSKeyId'] = kms_key_id
#     else:
#         upload_extra_args = {}
#     if filepath:
#         dirname, filename = os.path.split(filepath)
#         key = os.path.join(prefix, filename)
#         logger.debug('filepath=%s, filename=%s, key=%s' % (filepath, filename, key))
#         content_type = mimetypes.guess_type(filename)[0]
#         if content_type is None:
#             content_type = 'binary/octet-stream'
#         upload_extra_args.update({'ContentType': content_type})
#         try:
#             upload_extra_args.update({'ACL': acl})
#             s3.upload_file(filepath, bucket, key, ExtraArgs=upload_extra_args)
#         except Exception as e:
#             upload_extra_args.update({'ACL': 'private'})
#             s3.upload_file(filepath, bucket, key, ExtraArgs=upload_extra_args)
#     else:
#         try:
#             s3.put_object(Body=b'', Bucket=bucket, Key=prefix, ACL=acl, **upload_extra_args)
#         except Exception as e:
#             s3.put_object(Body=b'', Bucket=bucket, Key=prefix, ACL='private', **upload_extra_args)


import pytest
import os
import shutil
import boto3
from tibanna.utils import (
    create_jobid,
    upload
)


def test_upload():
    randomstr = 'test-' + create_jobid()
    os.mkdir(randomstr)
    filepath = os.path.join(os.path.abspath(randomstr), randomstr)
    with open(filepath, 'w') as f:
        f.write('haha')
    upload(filepath, 'tibanna-output', 'uploadtest')
    s3 = boto3.client('s3')
    res = s3.get_object(Bucket='tibanna-output', Key='uploadtest/' + randomstr)
    assert res
    res = s3.head_object(Bucket='tibanna-output', Key='uploadtest/' + randomstr)
    assert "ServerSideEncryption" not in res
    # cleanup afterwards
    shutil.rmtree(randomstr)
    s3.delete_objects(Bucket='tibanna-output',
                      Delete={'Objects': [{'Key': 'uploadtest/' + randomstr}]})


def test_upload_encrypt_s3_upload():
    randomstr = 'test-' + create_jobid()
    os.mkdir(randomstr)
    filepath = os.path.join(os.path.abspath(randomstr), randomstr)
    with open(filepath, 'w') as f:
        f.write('haha')

# Path: tibanna/utils.py
# def create_jobid():
#     return randomword(12)    # date+random_string
# 
# def upload(filepath, bucket, prefix='', public=True, encrypt_s3_upload=False, kms_key_id=None):
#     """ Uploads a file to S3 under a prefix.
#         The original directory structure is removed
#         and only the filename is preserved.
#         If filepath is none, upload an empty file with prefix
#         itself as key
#         If encrypt_s3_upload is True, default Server-Side encryption is enabled
#         If kms_key_id is True as well, Server-Side encryption with the given key_id is enabled
#     """
#     if public:
#         acl = 'public-read'
#     else:
#         acl = 'private'
#     s3 = boto3.client('s3')
#     if encrypt_s3_upload:
#         upload_extra_args = {'ServerSideEncryption': 'aws:kms'}
#         if kms_key_id:
#             upload_extra_args['SSEKMSKeyId'] = kms_key_id
#     else:
#         upload_extra_args = {}
#     if filepath:
#         dirname, filename = os.path.split(filepath)
#         key = os.path.join(prefix, filename)
#         logger.debug('filepath=%s, filename=%s, key=%s' % (filepath, filename, key))
#         content_type = mimetypes.guess_type(filename)[0]
#         if content_type is None:
#             content_type = 'binary/octet-stream'
#         upload_extra_args.update({'ContentType': content_type})
#         try:
#             upload_extra_args.update({'ACL': acl})
#             s3.upload_file(filepath, bucket, key, ExtraArgs=upload_extra_args)
#         except Exception as e:
#             upload_extra_args.update({'ACL': 'private'})
#             s3.upload_file(filepath, bucket, key, ExtraArgs=upload_extra_args)
#     else:
#         try:
#             s3.put_object(Body=b'', Bucket=bucket, Key=prefix, ACL=acl, **upload_extra_args)
#         except Exception as e:
#             s3.put_object(Body=b'', Bucket=bucket, Key=prefix, ACL='private', **upload_extra_args)


# Path: tests/tibanna/unicorn/test_utils.py
import pytest
import os
import shutil
import boto3
from tibanna.utils import (
    create_jobid,
    upload
)



def test_upload():
    randomstr = 'test-' + create_jobid()
    os.mkdir(randomstr)
    filepath = os.path.join(os.path.abspath(randomstr), randomstr)
    with open(filepath, 'w') as f:
        f.write('haha')
    upload(filepath, 'tibanna-output', 'uploadtest')
    s3 = boto3.client('s3')
    res = s3.get_object(Bucket='tibanna-output', Key='uploadtest/' + randomstr)
    assert res
    res = s3.head_object(Bucket='tibanna-output', Key='uploadtest/' + randomstr)
    assert "ServerSideEncryption" not in res
    # cleanup afterwards
    shutil.rmtree(randomstr)
    s3.delete_objects(Bucket='tibanna-output',
                      Delete={'Objects': [{'Key': 'uploadtest/' + randomstr}]})


def test_upload_encrypt_s3_upload():
    randomstr = 'test-' + create_jobid()
    os.mkdir(randomstr)
    filepath = os.path.join(os.path.abspath(randomstr), randomstr)
    with open(filepath, 'w') as f:
        f.write('haha')

    upload(filepath, 'tibanna-output', 'uploadtest', encrypt_s3_upload=True)