2
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3
# not use this file except in compliance with the License. You may obtain
4
# a copy of the License at
6
# http://www.apache.org/licenses/LICENSE-2.0
8
# Unless required by applicable law or agreed to in writing, software
9
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11
# License for the specific language governing permissions and limitations
15
from six.moves.urllib import parse as urlparse
17
from heat.api.openstack.v1.views import views_common
18
from heat.tests import common
21
class TestViewsCommon(common.HeatTestCase):
23
super(TestViewsCommon, self).setUp()
24
self.request = mock.Mock()
32
def setUpGetCollectionLinks(self):
33
self.items = [self.stack1, self.stack2]
34
self.request.params = {'limit': '2'}
35
self.request.path_url = "http://example.com/fake/path"
37
def test_get_collection_links_creates_next(self):
38
self.setUpGetCollectionLinks()
39
links = views_common.get_collection_links(self.request, self.items)
41
expected = 'http://example.com/fake/path?marker=id2&limit=2'
42
next_link = filter(lambda link: link['rel'] == 'next', links).pop()
43
self.assertEqual('next', next_link['rel'])
44
self.assertEqual(expected, next_link['href'])
46
def test_get_collection_links_doesnt_create_next_if_no_limit(self):
47
self.setUpGetCollectionLinks()
48
del self.request.params['limit']
49
links = views_common.get_collection_links(self.request, self.items)
51
self.assertEqual([], links)
53
def test_get_collection_links_doesnt_create_next_if_page_not_full(self):
54
self.setUpGetCollectionLinks()
55
self.request.params['limit'] = '10'
56
links = views_common.get_collection_links(self.request, self.items)
58
self.assertEqual([], links)
60
def test_get_collection_links_overwrites_url_marker(self):
61
self.setUpGetCollectionLinks()
62
self.request.params = {'limit': '2', 'marker': 'some_marker'}
63
links = views_common.get_collection_links(self.request, self.items)
65
expected = 'http://example.com/fake/path?marker=id2&limit=2'
66
next_link = filter(lambda link: link['rel'] == 'next', links).pop()
67
self.assertEqual(expected, next_link['href'])
69
def test_get_collection_links_does_not_overwrite_other_params(self):
70
self.setUpGetCollectionLinks()
71
self.request.params = {'limit': '2', 'foo': 'bar'}
72
links = views_common.get_collection_links(self.request, self.items)
74
next_link = filter(lambda link: link['rel'] == 'next', links).pop()
75
url = next_link['href']
76
query_string = urlparse.urlparse(url).query
78
params.update(urlparse.parse_qsl(query_string))
79
self.assertEqual('2', params['limit'])
80
self.assertEqual('bar', params['foo'])
82
def test_get_collection_links_handles_invalid_limits(self):
83
self.setUpGetCollectionLinks()
84
self.request.params = {'limit': 'foo'}
85
links = views_common.get_collection_links(self.request, self.items)
86
self.assertEqual([], links)
88
self.request.params = {'limit': None}
89
links = views_common.get_collection_links(self.request, self.items)
90
self.assertEqual([], links)