Coverage for manila/tests/api/views/test_versions.py: 100%

50 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2026-02-18 22:19 +0000

1# Copyright 2015 Clinton Knight 

2# All Rights Reserved. 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); you may 

5# not use this file except in compliance with the License. You may obtain 

6# a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

13# License for the specific language governing permissions and limitations 

14# under the License. 

15 

16import copy 

17from unittest import mock 

18 

19import ddt 

20 

21from manila.api.views import versions 

22from manila import test 

23 

24 

25class FakeRequest(object): 

26 def __init__(self, application_url): 

27 self.application_url = application_url 

28 

29 

30URL_BASE = 'http://localhost/' 

31FAKE_HREF = URL_BASE + 'v1/' 

32 

33FAKE_VERSIONS = { 

34 "v1.0": { 

35 "id": "v1.0", 

36 "status": "CURRENT", 

37 "version": "1.1", 

38 "min_version": "1.0", 

39 "updated": "2015-07-30T11:33:21Z", 

40 "links": [ 

41 { 

42 "rel": "describedby", 

43 "type": "text/html", 

44 "href": 'http://docs.openstack.org/', 

45 }, 

46 ], 

47 "media-types": [ 

48 { 

49 "base": "application/json", 

50 "type": "application/vnd.openstack.share+json;version=1", 

51 } 

52 ], 

53 }, 

54} 

55 

56FAKE_LINKS = [ 

57 { 

58 "rel": "describedby", 

59 "type": "text/html", 

60 "href": 'http://docs.openstack.org/', 

61 }, 

62 { 

63 'rel': 'self', 

64 'href': FAKE_HREF 

65 }, 

66] 

67 

68 

69@ddt.ddt 

70class ViewBuilderTestCase(test.TestCase): 

71 

72 def _get_builder(self): 

73 request = FakeRequest('fake') 

74 return versions.get_view_builder(request) 

75 

76 def test_build_versions(self): 

77 

78 self.mock_object(versions.ViewBuilder, 

79 '_build_links', 

80 mock.Mock(return_value=FAKE_LINKS)) 

81 

82 result = self._get_builder().build_versions(FAKE_VERSIONS) 

83 

84 expected = {'versions': list(FAKE_VERSIONS.values())} 

85 expected['versions'][0]['links'] = FAKE_LINKS 

86 

87 self.assertEqual(expected, result) 

88 

89 def test_build_version(self): 

90 

91 self.mock_object(versions.ViewBuilder, 

92 '_build_links', 

93 mock.Mock(return_value=FAKE_LINKS)) 

94 

95 result = self._get_builder()._build_version(FAKE_VERSIONS['v1.0']) 

96 

97 expected = copy.deepcopy(FAKE_VERSIONS['v1.0']) 

98 expected['links'] = FAKE_LINKS 

99 

100 self.assertEqual(expected, result) 

101 

102 def test_build_links(self): 

103 

104 self.mock_object(versions.ViewBuilder, 

105 '_generate_href', 

106 mock.Mock(return_value=FAKE_HREF)) 

107 

108 result = self._get_builder()._build_links(FAKE_VERSIONS['v1.0']) 

109 

110 self.assertEqual(FAKE_LINKS, result) 

111 

112 def test_generate_href_defaults(self): 

113 

114 self.mock_object(versions.ViewBuilder, 

115 '_get_base_url_without_version', 

116 mock.Mock(return_value=URL_BASE)) 

117 

118 result = self._get_builder()._generate_href() 

119 

120 self.assertEqual('http://localhost/v1/', result) 

121 

122 @ddt.data( 

123 ('v2', None, URL_BASE + 'v2/'), 

124 ('/v2/', None, URL_BASE + 'v2/'), 

125 ('/v2/', 'fake_path', URL_BASE + 'v2/fake_path'), 

126 ('/v2/', '/fake_path/', URL_BASE + 'v2/fake_path/'), 

127 ) 

128 @ddt.unpack 

129 def test_generate_href_no_path(self, version, path, expected): 

130 

131 self.mock_object(versions.ViewBuilder, 

132 '_get_base_url_without_version', 

133 mock.Mock(return_value=URL_BASE)) 

134 

135 result = self._get_builder()._generate_href(version=version, 

136 path=path) 

137 

138 self.assertEqual(expected, result) 

139 

140 @ddt.data( 

141 ('http://1.1.1.1/', 'http://1.1.1.1/'), 

142 ('http://localhost/', 'http://localhost/'), 

143 ('http://1.1.1.1/v1/', 'http://1.1.1.1/'), 

144 ('http://1.1.1.1/v1', 'http://1.1.1.1/'), 

145 ('http://1.1.1.1/v11', 'http://1.1.1.1/'), 

146 ) 

147 @ddt.unpack 

148 def test_get_base_url_without_version(self, base_url, base_url_no_version): 

149 

150 request = FakeRequest(base_url) 

151 builder = versions.get_view_builder(request) 

152 

153 result = builder._get_base_url_without_version() 

154 

155 self.assertEqual(base_url_no_version, result)