test_amazon.py
2.24 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
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2016 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/>.
import json
from mock import patch, MagicMock
from default import flask_app
from pybossa.s3_client import NoSuchBucket, PrivateBucket
class TestAmazonS3API(object):
@patch('pybossa.view.amazon.S3Client')
def test_buckets_with_specific_bucket_lists_its_content(self, S3Client):
objects = ['test.pdf', 'sunset.png']
bucket_name = 'Bucket1'
client_instance = MagicMock()
S3Client.return_value = client_instance
client_instance.objects.return_value = objects
resp = flask_app.test_client().get('/amazon/bucket/%s' % bucket_name)
client_instance.objects.assert_called_with(bucket_name)
assert resp.data == json.dumps(objects), resp.data
@patch('pybossa.view.amazon.S3Client')
def test_buckets_with_non_existing_bucket_returns_error(self, S3Client):
client_instance = MagicMock()
S3Client.return_value = client_instance
client_instance.objects.side_effect = NoSuchBucket('Bucket "noSuchBucket" does not exist')
resp = flask_app.test_client().get('/amazon/bucket/noSuchBucket')
assert resp.status_code == 404, resp
@patch('pybossa.view.amazon.S3Client')
def test_buckets_with_private_bucket_returns_error(self, S3Client):
client_instance = MagicMock()
S3Client.return_value = client_instance
client_instance.objects.side_effect = PrivateBucket('Bucket "noSuchBucket" is private')
resp = flask_app.test_client().get('/amazon/bucket/privateBucket')
assert resp.status_code == 403, resp