~ubuntu-branches/ubuntu/wily/heat/wily

« back to all changes in this revision

Viewing changes to heat/tests/test_api_openstack_v1_views_views_common.py

  • Committer: Package Import Robot
  • Author(s): Corey Bryant
  • Date: 2015-08-19 08:11:50 UTC
  • mfrom: (1.1.27)
  • Revision ID: package-import@ubuntu.com-20150819081150-m969fd35xn8bdmfu
Tags: 1:5.0.0~b2-0ubuntu1
* New upstream milestone for OpenStack Liberty.
* d/control: Align (build-)depends with upstream.
* d/p/fix-requirements.patch: Dropped. No longer needed.
* d/p/fixup-assert-regex.patch: Rebased.
* d/rules: Remove .eggs directory in override_dh_auto_clean.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
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
5
 
#
6
 
#         http://www.apache.org/licenses/LICENSE-2.0
7
 
#
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
12
 
#    under the License.
13
 
 
14
 
import mock
15
 
from six.moves.urllib import parse as urlparse
16
 
 
17
 
from heat.api.openstack.v1.views import views_common
18
 
from heat.tests import common
19
 
 
20
 
 
21
 
class TestViewsCommon(common.HeatTestCase):
22
 
    def setUp(self):
23
 
        super(TestViewsCommon, self).setUp()
24
 
        self.request = mock.Mock()
25
 
        self.stack1 = {
26
 
            'id': 'id1',
27
 
        }
28
 
        self.stack2 = {
29
 
            'id': 'id2',
30
 
        }
31
 
 
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"
36
 
 
37
 
    def test_get_collection_links_creates_next(self):
38
 
        self.setUpGetCollectionLinks()
39
 
        links = views_common.get_collection_links(self.request, self.items)
40
 
 
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'])
45
 
 
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)
50
 
 
51
 
        self.assertEqual([], links)
52
 
 
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)
57
 
 
58
 
        self.assertEqual([], links)
59
 
 
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)
64
 
 
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'])
68
 
 
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)
73
 
 
74
 
        next_link = filter(lambda link: link['rel'] == 'next', links).pop()
75
 
        url = next_link['href']
76
 
        query_string = urlparse.urlparse(url).query
77
 
        params = {}
78
 
        params.update(urlparse.parse_qsl(query_string))
79
 
        self.assertEqual('2', params['limit'])
80
 
        self.assertEqual('bar', params['foo'])
81
 
 
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)
87
 
 
88
 
        self.request.params = {'limit': None}
89
 
        links = views_common.get_collection_links(self.request, self.items)
90
 
        self.assertEqual([], links)