Permalink
Browse files

modify render function to allow setting the status_code

  • Loading branch information...
1 parent 4d1b8f2 commit 594ee9f63bb5fecd038fc99fe7155ba07df88337 @mapleoin committed Sep 21, 2012
Showing with 135 additions and 49 deletions.
  1. +10 −4 popcorn/helpers.py
  2. +1 −0 popcorn/test/templates/test.html
  3. +92 −0 popcorn/test/test_helpers.py
  4. +32 −45 popcorn/test/test_views.py
View
@@ -43,9 +43,15 @@ def decorated_function(*args, **kwargs):
ctx = {}
elif not isinstance(ctx, dict):
return ctx
- render_type = request_wants_json()
- if render_type:
- return jsonify(ctx)
- return render_template(template, **ctx)
+
+ try:
+ status_code = ctx.pop('status_code')
+ except KeyError:
+ status_code = 200
+
+ if request_wants_json():
+ return jsonify(ctx), status_code
+
+ return render_template(template, **ctx), status_code
return decorated_function
return decorator
@@ -0,0 +1 @@
+<p>{{ test_data }}
@@ -0,0 +1,92 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2012 Ionuț Arțăriși <iartarisi@suse.cz>
+#
+# Permission is hereby granted, free of charge, to any person
+# obtaining a copy of this software and associated documentation
+# files (the "Software"), to deal in the Software without
+# restriction, including without limitation the rights to use,
+# copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following
+# conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+# OTHER DEALINGS IN THE SOFTWARE.
+
+import json
+import unittest
+
+import flask
+
+from popcorn.helpers import render
+
+class TestHelpers(unittest.TestCase):
+
+ def test_render_html(self):
+ app = flask.Flask(__name__)
+
+ @app.route('/')
+ @render(template='test.html')
+ def index():
+ return dict(test_data='foo')
+
+ c = app.test_client()
+ response = c.get('/')
+
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.data, '<p>foo')
+
+ def test_render_html_arbitrary_error_code(self):
+ app = flask.Flask(__name__)
+
+ @app.route('/')
+ @render(template='test.html')
+ def index():
+ return dict(test_data='foo', status_code=418)
+
+ c = app.test_client()
+ response = c.get('/')
+
+ self.assertEqual(response.status_code, 418)
+ self.assertEqual(response.data, '<p>foo')
+
+ def test_render_json(self):
+ app = flask.Flask(__name__)
+
+ @app.route('/')
+ @render(template='test.html')
+ def index():
+ return dict(test_data='foo')
+
+ c = app.test_client()
+ response = c.get('/', headers=[('Accept', 'application/json')])
+
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(json.loads(response.data),
+ {'test_data': 'foo'})
+ self.assertEqual(response.headers['Content-Type'], 'application/json')
+
+ def test_render_json_arbitrary_error_code(self):
+ app = flask.Flask(__name__)
+
+ @app.route('/')
+ @render(template='test.html')
+ def index():
+ return dict(test_data='foo', status_code=418)
+
+ c = app.test_client()
+ response = c.get('/', headers=[('Accept', 'application/json')])
+
+ self.assertEqual(response.status_code, 418)
+ self.assertEqual(json.loads(response.data),
+ {'test_data': 'foo'})
+ self.assertEqual(response.headers['Content-Type'], 'application/json')
View
@@ -44,7 +44,7 @@ def _fk_pragma_on_connect(dbapi_con, con_record):
dbapi_con.execute('pragma foreign_keys=ON')
-class PopcornTestCase(unittest.TestCase):
+class TestViews(unittest.TestCase):
def setUp(self):
engine = create_engine('sqlite:///:memory:')
@@ -102,54 +102,46 @@ def test_submission_plaintext_without_header(self):
def test_index_json(self):
self.submit(compress=False, header=False)
- with app.test_request_context(
- path='/', method='GET',
- headers=[('Accept', 'application/json')]):
- response = app.dispatch_request()
- self.assertEqual(json.loads(response.data), {
+
+ response = self.app.get('/', headers=[('Accept', 'application/json')])
+ self.assertEqual(json.loads(response.data), {
"submissions_distrover": u'[["openSUSE 12.1", 1]]',
"distro_packages": "[[\"openSUSE\", 1285]]",
})
- self.assertEqual(response.headers['Content-Type'],
- 'application/json')
+ self.assertEqual(response.headers['Content-Type'],
+ 'application/json')
def test_vendor_json(self):
self.submit(compress=False, header=False)
- with app.test_request_context(
- path='/vendor/openSUSE', method='GET',
- headers=[('Accept', 'application/json')]):
- response = app.dispatch_request()
- self.assertEqual(json.loads(response.data), {
+
+ response = self.app.get('/vendor/openSUSE',
+ headers=[('Accept', 'application/json')])
+ self.assertEqual(json.loads(response.data), {
"vendor": {
"vendor_name": "openSUSE",
"vendor_url": "openSUSE"
}})
- self.assertEqual(response.headers['Content-Type'],
- 'application/json')
+ self.assertEqual(response.headers['Content-Type'],
+ 'application/json')
def test_system_json(self):
self.submit(compress=False, header=False)
- with app.test_request_context(
- path='/system/%s' % SYS_HWUUID,
- method='GET', headers=[('Accept', 'application/json')]):
- response = app.dispatch_request()
- self.assertEqual(json.loads(response.data), {
+ response = self.app.get('/system/' + SYS_HWUUID,
+ headers=[('Accept', 'application/json')])
+ self.assertEqual(json.loads(response.data), {
"system": {
"last_sub_date": today.strftime("%Y-%m-%d"),
"sys_hwuuid": SYS_HWUUID
}
})
- self.assertEqual(response.headers['Content-Type'],
- 'application/json')
+ self.assertEqual(response.headers['Content-Type'],
+ 'application/json')
def test_package_json(self):
self.submit(compress=False, header=False)
- with app.test_request_context(path='/package/sed/4.2.1/5.1.2/x86_64',
- method='GET',
- headers=[('Accept', 'application/json')]
- ):
- response = app.dispatch_request()
- self.assertEqual(json.loads(response.data), {
+ response = self.app.get('/package/sed/4.2.1/5.1.2/x86_64',
+ headers=[('Accept', 'application/json')])
+ self.assertEqual(json.loads(response.data), {
"packages": [{
"sub_id": 1,
"sub_date": today.strftime("%Y-%m-%d"),
@@ -174,17 +166,14 @@ def test_package_json(self):
"pkg_release": "5.1.2"
}
})
- self.assertEqual(response.headers['Content-Type'],
- 'application/json')
+ self.assertEqual(response.headers['Content-Type'],
+ 'application/json')
def test_distro_json(self):
self.submit(compress=False, header=False)
- with app.test_request_context(path='/distro/openSUSE/12.1',
- method='GET',
- headers=[('Accept', 'application/json')]
- ):
- response = app.dispatch_request()
- self.assertEqual(json.loads(response.data), {
+ response = self.app.get('/distro/openSUSE/12.1',
+ headers=[('Accept', 'application/json')])
+ self.assertEqual(json.loads(response.data), {
"distro": {
"submissions": [{
"sub_id": 1,
@@ -198,17 +187,15 @@ def test_distro_json(self):
"distro_version": "12.1"
}
})
- self.assertEqual(response.headers['Content-Type'],
- 'application/json')
+ self.assertEqual(response.headers['Content-Type'],
+ 'application/json')
def test_distro_doc_json(self):
self.submit(compress=False, header=False)
- with app.test_request_context(
- path='/distro', method='GET',
- headers=[('Accept', 'application/json')]):
- response = app.dispatch_request()
- self.assertEqual(json.loads(response.data), {
+ response = self.app.get('/distro',
+ headers=[('Accept', 'application/json')])
+ self.assertEqual(json.loads(response.data), {
"submissions_distrover": u'[["openSUSE 12.1", 1]]',
})
- self.assertEqual(response.headers['Content-Type'],
- 'application/json')
+ self.assertEqual(response.headers['Content-Type'],
+ 'application/json')

0 comments on commit 594ee9f

Please sign in to comment.