test_s3_client.py
5.42 KB
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
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2015 SciFabric LTD.
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa. If not, see <http://www.gnu.org/licenses/>.
from mock import patch, MagicMock
from nose.tools import assert_raises
import json
from pybossa.s3_client import S3Client, NoSuchBucket, PrivateBucket
class TestS3Client(object):
def make_response(self, text, status_code=200):
fake_response = MagicMock()
fake_response.text = text
fake_response.status_code = status_code
return fake_response
bucket_with_content = (
"""<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Name>test-pybossa</Name>
<Prefix></Prefix>
<Marker></Marker>
<MaxKeys>1000</MaxKeys>
<IsTruncated>false</IsTruncated>
<Contents>
<Key>16535035993_1080p.mp4</Key>
<LastModified>2016-01-29T08:55:41.000Z</LastModified>
<ETag>"10055dfebe62cf30e34d87fd27b28efc"</ETag>
<Size>11801468</Size>
<StorageClass>STANDARD</StorageClass>
</Contents>
<Contents>
<Key>BFI-demo.mp4</Key>
<LastModified>2016-01-29T08:55:38.000Z</LastModified>
<ETag>"b24442a1484b6b8f2b4e08c43e0abd3f"</ETag>
<Size>27063915</Size>
<StorageClass>STANDARD</StorageClass>
</Contents>
</ListBucketResult>
""")
empty_bucket = (
"""<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Name>test-pybossa</Name>
<Prefix></Prefix>
<Marker></Marker>
<MaxKeys>1000</MaxKeys>
<IsTruncated>false</IsTruncated>
</ListBucketResult>
""")
no_such_bucket = (
"""<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>NoSuchBucket</Code>
<Message>The specified bucket does not exist</Message>
<BucketName>test-pybosa</BucketName>
<RequestId>5DB95818E2273F2A</RequestId>
<HostId>2xqg6pMK20zocCIN0DpqzDVEmbNkqKdTrp0BT/K2EUBbSIek5+7333DjDVuvpN0fFR/Pp/+IkM8=</HostId>
</Error>
""")
bucket_with_folder = (
"""<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Name>test-pybossa</Name>
<Prefix></Prefix>
<Marker></Marker>
<MaxKeys>1000</MaxKeys>
<IsTruncated>false</IsTruncated>
<Contents>
<Key>myfolder/</Key>
<LastModified>2016-01-29T08:56:15.000Z</LastModified>
<ETag>"d41d8cd98f00b204e9800998ecf8427e"</ETag>
<Size>0</Size>
<StorageClass>STANDARD</StorageClass>
</Contents>
</ListBucketResult>
""")
private_bucket = (
"""<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>0C189C667703869B</RequestId>
<HostId>e6HNleTSx+vQHCXsjphJNLumbwd2YfYfZMrEBEkGOF/0jCMDZf6RIrgUAooa+HT86f0Azr27/h4=</HostId>
</Error>
""")
@patch('pybossa.s3_client.requests')
def test_objects_return_empty_list_for_an_empty_bucket(self, requests):
resp = self.make_response(self.empty_bucket, 200)
requests.get.return_value = resp
objects = S3Client().objects('test-pybossa')
assert objects == [], objects
@patch('pybossa.s3_client.requests')
def test_objects_return_list_of_object_names_in_a_bucket(self, requests):
resp = self.make_response(self.bucket_with_content, 200)
requests.get.return_value = resp
objects = S3Client().objects('test-pybossa')
assert objects == [u'16535035993_1080p.mp4', u'BFI-demo.mp4'], objects
@patch('pybossa.s3_client.requests')
def test_objects_not_returns_folders_inside_bucket(self, requests):
resp = self.make_response(self.bucket_with_folder, 200)
requests.get.return_value = resp
objects = S3Client().objects('test-pybossa')
assert objects == [], objects
@patch('pybossa.s3_client.requests')
def test_objects_raises_NoSuchBucket_if_bucket_does_not_exist(self, requests):
resp = self.make_response(self.no_such_bucket, 404)
requests.get.return_value = resp
assert_raises(NoSuchBucket, S3Client().objects, 'test-pybossa')
@patch('pybossa.s3_client.requests')
def test_objects_raises_PrivateBucket_if_bucket_is_private(self, requests):
resp = self.make_response(self.no_such_bucket, 403)
requests.get.return_value = resp
assert_raises(PrivateBucket, S3Client().objects, 'test-pybossa')