Skip to content

Commit 9ccf33d

Browse files
committed
Added test for not allowed files
1 parent 58f79a8 commit 9ccf33d

4 files changed

Lines changed: 38 additions & 10 deletions

File tree

app/mod_photos/controllers.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,43 @@
77
import uuid
88
import os
99

10+
ALLOWED_EXTENSIONS=set(['jpg','jpeg','bmp','png'])
11+
1012
mod_photos = Blueprint('photos',__name__,url_prefix='/photos/v1.0')
1113

14+
def allowed_file(filename):
15+
return '.' in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS
16+
17+
1218
@mod_photos.route('/photos',methods=['POST'])
1319
def photos():
1420
app.logger.debug("Applying post photo...")
1521

1622
data = None
1723
if request.method == 'POST':
1824
file = request.files['file']
25+
if file and not allowed_file(file.filename):
26+
resp = jsonify()
27+
resp.content_type='application/json'
28+
resp.status_code = 202
29+
return resp
30+
1931
extension = os.path.splitext(file.filename)[1]
2032
f_name=str(uuid.uuid4()) + extension
21-
file.save(os.path.join(app.config['UPLOAD_FOLDER'], f_name))
33+
filepath=os.path.join(app.config['UPLOAD_FOLDER'], f_name)
34+
photo = Photo(f_name,filepath)
35+
data ={'uuid':f_name, 'filepath': filepath}
36+
Photo.create(**data)
37+
file.save(filepath)
2238
data = {'filename':f_name}
2339
#TODO refactor to set up necessary methods to create responses
2440
if data == None:
2541
resp = jsonify()
42+
resp.status_code = 200
2643
else:
2744
resp = jsonify(data)
45+
resp.status_code = 201
46+
#TODO add diferent type of constructors
2847
resp.content_type='application/json'
29-
resp.status_code == 200
3048
return resp
3149

app/mod_photos/models.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ class Base(db.Model):
88

99
class Photo(Base,CRUDMixin):
1010
__tablename__='photo'
11-
name = db.Column(db.String(128), nullable=False)
11+
uuid = db.Column(db.String(128), nullable=False)
12+
filepath = db.Column(db.String(500), nullable=False)
1213

13-
def __init__(self,name):
14-
self.name = name
14+
def __init__(self,uuid,filepath):
15+
self.uuid = uuid
16+
self.filepath = filepath
1517

1618
def get_id(self):
1719
return self.id
1820

1921
def __repr__(self):
20-
return '<photo %r>' % (self.name)
22+
return '<photo uuid=%r filepath=%r>' % (self.name,self.uuid,self.filepath)
2123

2224

app/mod_photos/tests.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010
class TestModelPhoto(BaseTestCase):
1111
def test_new_photo(self):
12-
photo = Photo('carlos')
13-
self.assertTrue(photo.name,'carlos')
12+
photo = Photo('1','/test/photo.png')
13+
self.assertTrue(photo.uuid,'1')
14+
self.assertTrue(photo.filepath,'/test/photo.png')
1415

1516
class TestPhotosViews(BaseTestCase):
1617
def test_post_photo_correct(self):
@@ -22,6 +23,13 @@ def test_post_photo_correct(self):
2223
print dir(response)
2324
self.app.logger.debug(response.json)
2425
#TODO add assert for correct response
25-
self.assert_200(response)
26+
self.assertTrue(response.status_code == 201)
2627
def post_photo_not_allowed_file(self):
27-
pass
28+
with self.app.open_resource("test_resources/photo.txt") as fp:
29+
with self.client:
30+
response = self.client.post("/photos/v1.0/photos",data={'file':fp});
31+
app.logger.debug(response)
32+
#self.app.logger.debug(response.json)
33+
#TODO add assert for correct response
34+
self.assertTrue(response.status_code == 202)
35+

app/test_resources/photo.txt

24 KB
Binary file not shown.

0 commit comments

Comments
 (0)