Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

# This file is part of verify. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <https://www.gnu.org/licenses/>. 

 

import json 

import unittest 

 

import requests 

try: 

# responses is not a formal LSST Stack dependency (yet), so we'll skip any 

# tests that require it in environments that don't have it. 

import responses 

except ImportError: 

responses = None 

 

from lsst.verify import squash 

 

 

class GetDefaultTimeoutTestCase(unittest.TestCase): 

 

def test_get_default_timeout(self): 

self.assertEqual( 

squash.get_default_timeout(), 

squash._TIMEOUT 

) 

 

 

class GetDefaultApiVersionTestCase(unittest.TestCase): 

 

def test_get_default_api_version(self): 

self.assertEqual( 

squash.get_default_api_version(), 

squash._API_VERSION 

) 

 

 

class MakeAcceptHeaderTestCase(unittest.TestCase): 

 

def test_make_accept_header(self): 

self.assertEqual( 

squash.make_accept_header(), 

'application/json; version=' + squash.get_default_api_version() 

) 

 

self.assertEqual( 

squash.make_accept_header(1.1), 

'application/json; version=1.1' 

) 

 

 

class GetEndpointUrlTestCase(unittest.TestCase): 

 

def setUp(self): 

self.api_url = 'https://example.com/api/' 

self.endpoints = { 

'jobs': 'https://example.com/api/jobs/' 

} 

squash.reset_endpoint_cache() 

 

def tearDown(self): 

squash.reset_endpoint_cache() 

 

@unittest.skipIf(responses is None, 

'Requires `responses` PyPI package') 

def test_jobs(self): 

with responses.RequestsMock() as reqmock: 

reqmock.add(responses.GET, 

self.api_url, 

json=self.endpoints, 

status=200, 

content_type='application/json') 

jobs_url = squash.get_endpoint_url(self.api_url, 'jobs') 

 

self.assertEqual(jobs_url, self.endpoints['jobs']) 

 

# call again, shouldn't register a new request 

jobs_url = squash.get_endpoint_url(self.api_url, 'jobs') 

 

self.assertEqual(jobs_url, self.endpoints['jobs']) 

self.assertEqual(len(reqmock.calls), 1) 

 

 

class GetTestCase(unittest.TestCase): 

 

def setUp(self): 

self.api_url = 'https://example.com/api/' 

# pre-set the _ENDPOINT_URLS to use the cache, not http get 

squash._ENDPOINT_URLS = { 

'jobs': self.api_url + 'jobs' 

} 

 

def tearDown(self): 

squash.reset_endpoint_cache() 

 

@unittest.skipIf(responses is None, 

'Requires `responses` PyPI package') 

def test_get(self): 

with responses.RequestsMock() as reqmock: 

reqmock.add(responses.GET, 

squash._ENDPOINT_URLS['jobs'], 

json={}, 

status=200, 

content_type='application/json') 

 

r = squash.get(self.api_url, api_endpoint='jobs') 

 

self.assertIsInstance(r, requests.Response) 

self.assertEqual(len(reqmock.calls), 1) 

self.assertEqual( 

reqmock.calls[0].request.url, 

squash._ENDPOINT_URLS['jobs'], 

) 

self.assertEqual( 

reqmock.calls[0].request.headers['Accept'], 

'application/json; version=' + squash._API_VERSION 

) 

 

@unittest.skipIf(responses is None, 

'Requires `responses` PyPI package') 

def test_versioned_get(self): 

with responses.RequestsMock() as reqmock: 

reqmock.add(responses.GET, 

squash._ENDPOINT_URLS['jobs'], 

json={}, 

status=200, 

content_type='application/json') 

 

squash.get(self.api_url, api_endpoint='jobs', version='1.2') 

 

self.assertEqual( 

reqmock.calls[0].request.headers['Accept'], 

'application/json; version=1.2' 

) 

 

@unittest.skipIf(responses is None, 

'Requires `responses` PyPI package') 

def test_raises(self): 

with responses.RequestsMock() as reqmock: 

reqmock.add(responses.GET, 

squash._ENDPOINT_URLS['jobs'], 

json={}, 

status=404, 

content_type='application/json') 

 

with self.assertRaises(requests.exceptions.RequestException): 

squash.get(self.api_url, api_endpoint='jobs', version='1.2') 

 

 

class PostTestCase(unittest.TestCase): 

 

def setUp(self): 

self.api_url = 'https://example.com/api/' 

# pre-set the _ENDPOINT_URLS to use the cache, not http get 

squash._ENDPOINT_URLS = { 

'jobs': self.api_url + 'jobs' 

} 

 

def tearDown(self): 

squash.reset_endpoint_cache() 

 

@unittest.skipIf(responses is None, 

'Requires `responses` PyPI package') 

def test_json_post(self): 

with responses.RequestsMock() as reqmock: 

reqmock.add(responses.POST, 

squash._ENDPOINT_URLS['jobs'], 

json={}, 

status=201, 

content_type='application/json') 

 

r = squash.post(self.api_url, api_endpoint='jobs', 

json_doc={'key': 'value'}, 

api_user='foo', api_password='bar') 

 

self.assertIsInstance(r, requests.Response) 

self.assertEqual(len(reqmock.calls), 1) 

self.assertEqual( 

reqmock.calls[0].request.url, 

squash._ENDPOINT_URLS['jobs'], 

) 

self.assertEqual( 

reqmock.calls[0].request.headers['Accept'], 

'application/json; version=' + squash._API_VERSION 

) 

self.assertEqual( 

json.loads(reqmock.calls[0].request.body), 

{'key': 'value'} 

) 

 

@unittest.skipIf(responses is None, 

'Requires `responses` PyPI package') 

def test_raises(self): 

with responses.RequestsMock() as reqmock: 

reqmock.add(responses.POST, 

squash._ENDPOINT_URLS['jobs'], 

json={}, 

status=503, 

content_type='application/json') 

 

with self.assertRaises(requests.exceptions.RequestException): 

squash.post(self.api_url, api_endpoint='jobs', 

json_doc={}, 

api_user='foo', api_password='bar') 

 

 

223 ↛ 224line 223 didn't jump to line 224, because the condition on line 223 was never trueif __name__ == "__main__": 

unittest.main()