5f9164001c34f8e07d725ac430e4d3c4041bffa3
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
17 from __future__
import absolute_import
, unicode_literals
22 import unittest
.mock
as mock
24 from werkzeug
.wrappers
import Request
25 from werkzeug
.test
import EnvironBuilder
27 from mediagoblin
.tools
.request
import decode_request
28 from mediagoblin
.tools
.pagination
import Pagination
30 class TestDecodeRequest(object):
31 """Test the decode_request function."""
33 def test_form_type(self
):
34 """Try a normal form-urlencoded request."""
35 builder
= EnvironBuilder(method
='POST', data
={'foo': 'bar'})
36 request
= Request(builder
.get_environ())
37 data
= decode_request(request
)
38 assert data
['foo'] == 'bar'
40 def test_json_type(self
):
41 """Try a normal JSON request."""
42 builder
= EnvironBuilder(
43 method
='POST', content_type
='application/json',
44 data
='{"foo": "bar"}')
45 request
= Request(builder
.get_environ())
46 data
= decode_request(request
)
47 assert data
['foo'] == 'bar'
49 def test_content_type_with_options(self
):
50 """Content-Type can also have options."""
51 builder
= EnvironBuilder(
53 content_type
='application/x-www-form-urlencoded; charset=utf-8')
54 request
= Request(builder
.get_environ())
55 # Must populate form field manually with non-default content-type.
56 request
.form
= {'foo': 'bar'}
57 data
= decode_request(request
)
58 assert data
['foo'] == 'bar'
60 def test_form_type_is_default(self
):
61 """Assume form-urlencoded if blank in the request."""
62 builder
= EnvironBuilder(method
='POST', content_type
='')
63 request
= Request(builder
.get_environ())
64 # Must populate form field manually with non-default content-type.
65 request
.form
= {'foo': 'bar'}
66 data
= decode_request(request
)
67 assert data
['foo'] == 'bar'
70 class TestPagination(object):
71 def _create_paginator(self
, num_items
, page
, per_page
):
72 """Create a Paginator with a mock database cursor."""
73 mock_cursor
= mock
.MagicMock()
74 mock_cursor
.count
.return_value
= num_items
75 return Pagination(page
, mock_cursor
, per_page
)
77 def test_creates_valid_page_url_from_explicit_base_url(self
):
78 """Check that test_page_url_explicit runs.
80 This is a regression test for a Python 2/3 compatibility fix.
83 paginator
= self
._create
_paginator
(num_items
=1, page
=1, per_page
=30)
84 url
= paginator
.get_page_url_explicit('http://example.com', [], 1)
85 assert url
== 'http://example.com?page=1'
87 def test_iter_pages_handles_single_page(self
):
88 """Check that iter_pages produces the expected result for single page.
90 This is a regression test for a Python 2/3 compatibility fix.
93 paginator
= self
._create
_paginator
(num_items
=1, page
=1, per_page
=30)
94 assert list(paginator
.iter_pages()) == [1]
96 def test_zero_items(self
):
97 """Check that no items produces no pages."""
98 paginator
= self
._create
_paginator
(num_items
=0, page
=1, per_page
=30)
99 assert paginator
.total_count
== 0
100 assert paginator
.pages
== 0
102 def test_single_item(self
):
103 """Check that one item produces one page."""
104 paginator
= self
._create
_paginator
(num_items
=1, page
=1, per_page
=30)
105 assert paginator
.total_count
== 1
106 assert paginator
.pages
== 1
108 def test_full_page(self
):
109 """Check that a full page of items produces one page."""
110 paginator
= self
._create
_paginator
(num_items
=30, page
=1, per_page
=30)
111 assert paginator
.total_count
== 30
112 assert paginator
.pages
== 1
114 def test_multiple_pages(self
):
115 """Check that more than a full page produces two pages."""
116 paginator
= self
._create
_paginator
(num_items
=31, page
=1, per_page
=30)
117 assert paginator
.total_count
== 31
118 assert paginator
.pages
== 2