Skip to content

Commit

Permalink
cleanup tests, add tests for health, no cache, home redirect, favicon
Browse files Browse the repository at this point in the history
  • Loading branch information
jinpark committed Mar 29, 2015
1 parent 75add02 commit 17b292f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def convert(url):
# img.transform("{}x{}".format(width,height))


@app.route('/health')
@app.route('/health/')
@nocache
def health_check():
return jsonify({'health': 'ok', 'commit_hash': os.environ.get('COMMIT_HASH')})
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# jinageresizer (image resizer by jin)

[![Build Status](https://travis-ci.org/jinpark/imageresizer.svg?branch=master)](https://travis-ci.org/jinpark/imageresizer) [![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy)
[![Build Status](https://travis-ci.org/jinpark/imageresizer.svg?branch=master)](https://travis-ci.org/jinpark/imageresizer)
[![Coverage Status](https://coveralls.io/repos/jinpark/imageresizer/badge.svg?branch=master)](https://coveralls.io/r/jinpark/imageresizer?branch=master)
[![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy)


jinageresizer is a image resizing service using flask, imagemagick and [wand](http://docs.wand-py.org/en/0.4.0/) inspired by [firesize](http://firesize.com)
Expand Down
50 changes: 31 additions & 19 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,79 @@
import sys
import unittest
import requests
from freezegun import freeze_time
from datetime import datetime
from StringIO import StringIO
from wand.image import Image
from mock import MagicMock


topdir = os.path.join(os.path.dirname(__file__), "..")
sys.path.append(topdir)
import app


def get_stub(*args, **kwargs):
def requests_stub(*args, **kwargs):
response = requests.get.return_value
with open('tests/images/50x200_pre.jpeg', 'r') as f:
response.content = f.read()
response.headers = {'content-type': 'image/jpeg'}
return response

requests.get = MagicMock(side_effect=get_stub)

requests.get = MagicMock(side_effect=requests_stub)

class TestImageResizer(unittest.TestCase):
def setUp(self):
self.app = app.app.test_client()

def test_resize_width(self):
expected_image = open('tests/images/50x200.jpeg').read()

rv = self.app.get('/http://placekitten.com/g/50/200/?rwidth=100', follow_redirects=True)
with Image(file=StringIO(rv.data)) as img:
self.assertEqual(img.width, 100)
self.assertEqual(img.height, 400)

def test_resize_height(self):
expected_image = open('tests/images/50x200.jpeg').read()

rv = self.app.get('/http://placekitten.com/g/50/200/?rheight=100', follow_redirects=True)
with Image(file=StringIO(rv.data)) as img:
self.assertEqual(img.width, 25)
self.assertEqual(img.height, 100)

def test_resize_width_and_height(self):
expected_image = open('tests/images/50x200.jpeg').read()

rv = self.app.get('/http://placekitten.com/g/50/200/?rheight=100&rwidth=100', follow_redirects=True)
with Image(file=StringIO(rv.data)) as img:
self.assertEqual(img.width, 100)
self.assertEqual(img.height, 100)

def test_resize_width_and_height(self):
expected_image = open('tests/images/50x200.jpeg').read()

rv = self.app.get('/http://placekitten.com/g/50/200/?rheight=100&rwidth=100', follow_redirects=True)
with Image(file=StringIO(rv.data)) as img:
self.assertEqual(img.width, 100)
self.assertEqual(img.height, 100)

def test_convert_type(self):
expected_image = open('tests/images/50x200.jpeg').read()

rv = self.app.get('/http://placekitten.com/g/50/200/?type=png', follow_redirects=True)
with Image(file=StringIO(rv.data)) as img:
self.assertEqual(img.mimetype, "image/png")

class TestImageResizerSide(unittest.TestCase):
def setUp(self):
self.app = app.app.test_client()

def test_home_redirect(self):
rv = self.app.get('/', follow_redirects=False)
self.assertEqual(rv.location, "https://github.com/jinpark/imageresizer")

def test_health_check(self):
rv = self.app.get('/health/')
self.assertEqual(rv.data, '{\n "commit_hash": null, \n "health": "ok"\n}')

@freeze_time("2015-03-29")
def test_health_check_uncached(self):
rv = self.app.get('/health/')
headers = rv.headers
datetime_now = datetime(2015, 3, 29)
self.assertEqual(headers['Last-Modified'], str(datetime_now))
self.assertEqual(headers['Cache-Control'], 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0')
self.assertEqual(headers['Pragma'], 'no-cache')
self.assertEqual(headers['Expires'], '-1')

def test_favicon(self):
rv = self.app.get('/favicon.ico/')
self.assertEqual(rv.mimetype, 'image/png')

if __name__ == '__main__':
unittest.main()

0 comments on commit 17b292f

Please sign in to comment.