1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import base64
from unittest import TestCase
import sys
import os
# this block resolves issues with pytest/tox, overall project dir structure
# should be updated, some hints at can be found here:
# https://stackoverflow.com/questions/55737714/how-does-a-tox-environment-set-its-sys-path
print(f'sys.path={sys.path}')
this_script_dir=os.path.dirname(os.path.abspath(__file__))
extra_path=os.path.join(this_script_dir, '..', 'awscurl')
if not os.path.exists(extra_path):
print(f'extra_path does not exist: {extra_path}')
sys.path.append(extra_path)
print(f'sys.path2={sys.path}')
from awscurl.awscurl import make_request, inner_main # nopep8: E402
__author__ = 'iokulist'
class TestMakeRequestWithToken(TestCase):
maxDiff = None
def test_make_request(self, *args, **kvargs):
headers = {}
access_key = base64.b64decode('QUtJQUkyNkxPQU5NSlpLNVNQWUE=').decode("utf-8")
secret_key = base64.b64decode('ekVQbE9URjU0Mys5M0l6UlNnNEVCOEd4cjFQV2NVa1p0TERWSmY4ag==').decode("utf-8")
params = {'method': 'GET',
'service': 's3',
'region': 'us-east-1',
'uri': 'https://awscurl-sample-bucket.s3.amazonaws.com/awscurl-sample-file:.txt?a=b',
'headers': headers,
'data': '',
'access_key': access_key,
'secret_key': secret_key,
'security_token': None,
'data_binary': False}
r = make_request(**params)
self.assertEqual(r.status_code, 200)
class TestMakeRequestWithTokenAndBinaryData(TestCase):
maxDiff = None
def test_make_request(self, *args, **kvargs):
headers = {}
access_key = base64.b64decode('QUtJQUkyNkxPQU5NSlpLNVNQWUE=').decode("utf-8")
secret_key = base64.b64decode('ekVQbE9URjU0Mys5M0l6UlNnNEVCOEd4cjFQV2NVa1p0TERWSmY4ag==').decode("utf-8")
params = {'method': 'GET',
'service': 's3',
'region': 'us-east-1',
'uri': 'https://awscurl-sample-bucket.s3.amazonaws.com/awscurl-sample-file:.txt?a=b',
'headers': headers,
'data': b'C\xcfI\x91\xc1\xd0\tw<\xa8\x13\x06{=\x9b\xb3\x1c\xfcl\xfe\xb9\xb18zS\xf4%i*Q\xc9v',
'access_key': access_key,
'secret_key': secret_key,
'security_token': None,
'data_binary': True}
r = make_request(**params)
self.assertEqual(r.status_code, 200)
class TestMakeRequestWithTokenAndEnglishData(TestCase):
maxDiff = None
def test_make_request(self, *args, **kvargs):
headers = {}
access_key = base64.b64decode('QUtJQUkyNkxPQU5NSlpLNVNQWUE=').decode("utf-8")
secret_key = base64.b64decode('ekVQbE9URjU0Mys5M0l6UlNnNEVCOEd4cjFQV2NVa1p0TERWSmY4ag==').decode("utf-8")
params = {'method': 'GET',
'service': 's3',
'region': 'us-east-1',
'uri': 'https://awscurl-sample-bucket.s3.amazonaws.com/awscurl-sample-file:.txt?a=b',
'headers': headers,
'data': 'Test',
'access_key': access_key,
'secret_key': secret_key,
'security_token': None,
'data_binary': False}
r = make_request(**params)
self.assertEqual(r.status_code, 200)
class TestMakeRequestWithTokenAndNonEnglishData(TestCase):
maxDiff = None
def test_make_request(self, *args, **kvargs):
headers = {}
access_key = base64.b64decode('QUtJQUkyNkxPQU5NSlpLNVNQWUE=').decode("utf-8")
secret_key = base64.b64decode('ekVQbE9URjU0Mys5M0l6UlNnNEVCOEd4cjFQV2NVa1p0TERWSmY4ag==').decode("utf-8")
params = {'method': 'GET',
'service': 's3',
'region': 'us-east-1',
'uri': 'https://awscurl-sample-bucket.s3.amazonaws.com/awscurl-sample-file:.txt?a=b',
'headers': headers,
'data': u'ใในใ',
'access_key': access_key,
'secret_key': secret_key,
'security_token': None,
'data_binary': False}
r = make_request(**params)
self.assertEqual(r.status_code, 200)
class TestInnerMainMethod(TestCase):
maxDiff = None
def test_exit_code_without_fail_option(self, *args, **kwargs):
self.assertEqual(
inner_main(['--verbose', '--service', 's3', 'https://awscurl-sample-bucket.s3.amazonaws.com']),
0
)
def test_exit_code_with_fail_option(self, *args, **kwargs):
self.assertEqual(
inner_main(['--verbose', '--fail-with-body', '--service', 's3', 'https://awscurl-sample-bucket.s3.amazonaws.com']),
22
)
class TestInnerMainMethodEmptyCredentials(TestCase):
maxDiff = None
def test_exit_code_without_fail_option(self, *args, **kwargs):
self.assertEqual(
inner_main(['--verbose', '--access_key', '', '--secret_key', '', '--session_token', '', '--service', 's3',
'https://awscurl-sample-bucket.s3.amazonaws.com']),
0
)
def test_exit_code_with_fail_option(self, *args, **kwargs):
self.assertEqual(
inner_main(['--verbose', '--fail-with-body', '--access_key', '', '--secret_key', '', '--session_token', '', '--service', 's3',
'https://awscurl-sample-bucket.s3.amazonaws.com']),
22
)
|