| 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
| 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU Affero General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU Affero General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU Affero General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | from __future__ import absolute_import, unicode_literals |
| 18 | |
| 19 | from werkzeug.wrappers import Request |
| 20 | |
| 21 | from ..tools.response import redirect, redirect_obj |
| 22 | |
| 23 | class TestRedirect(object): |
| 24 | def test_redirect_respects_location(self): |
| 25 | """Test that redirect returns a 302 to location specified.""" |
| 26 | request = Request({}) |
| 27 | response = redirect(request, location='/test') |
| 28 | assert response.status_code == 302 |
| 29 | assert response.location == '/test' |
| 30 | |
| 31 | def test_redirect_respects_querystring(self): |
| 32 | """Test that redirect includes querystring in returned location.""" |
| 33 | request = Request({}) |
| 34 | response = redirect(request, location='', querystring='#baz') |
| 35 | assert response.location == '#baz' |
| 36 | |
| 37 | def test_redirect_respects_urlgen_args(self): |
| 38 | """Test that redirect returns a 302 to location from urlgen args.""" |
| 39 | |
| 40 | # Using a mock urlgen here so we're only testing redirect itself. We |
| 41 | # could instantiate a url_map and map_adaptor with WSGI environ as per |
| 42 | # app.py, but that would really just be testing Werkzeug. |
| 43 | def urlgen(endpoint, **kwargs): |
| 44 | return '/test?foo=bar' |
| 45 | |
| 46 | request = Request({}) |
| 47 | request.urlgen = urlgen |
| 48 | response = redirect(request, 'test-endpoint', foo='bar') |
| 49 | assert response.status_code == 302 |
| 50 | assert response.location == '/test?foo=bar' |
| 51 | |
| 52 | def test_redirect_obj_calls_url_for_self(self): |
| 53 | """Test that redirect_obj returns a 302 to obj's url_for_self().""" |
| 54 | |
| 55 | # Using a mock obj here so that we're only testing redirect_obj itself, |
| 56 | # rather than also testing the url_for_self implementation. |
| 57 | class Foo(object): |
| 58 | def url_for_self(*args, **kwargs): |
| 59 | return '/foo' |
| 60 | |
| 61 | request = Request({}) |
| 62 | request.urlgen = None |
| 63 | response = redirect_obj(request, Foo()) |
| 64 | assert response.status_code == 302 |
| 65 | assert response.location == '/foo' |