Exemple #1
0
 def test_put_json(self):
     self.mock_sess.put.side_effect = [
         MockResponse(201, 'Created', _json={'list': ['three']})
     ]
     res = self.api._put_json('http://jive.example.com/foo', {'foo': 'bar'})
     assert res == {'list': ['three']}
     assert self.mock_sess.mock_calls == [
         call.put('http://jive.example.com/foo', json={'foo': 'bar'})
     ]
Exemple #2
0
 def test_500(self):
     api = JiveApi('http://jive.example.com/', 'jiveuser', 'jivepass')
     req_t = namedtuple('MockRequest', ['method', 'url'])
     req = req_t(method='PUT', url='http://jive.example.com/')
     with patch('%s._put_json' % pb) as mock_put:
         mock_put.side_effect = RequestFailedException(
             MockResponse(500, 'Conflict', request=req))
         with pytest.raises(RequestFailedException):
             api.update_content('cid', {'foo': 'bar'})
Exemple #3
0
 def test_get_http_url(self):
     self.mock_sess.get.side_effect = [
         MockResponse(200,
                      'OK',
                      _json={
                          'links': {
                              'next': 'http://jive.example.com/bar'
                          },
                          'list': ['one', 'two']
                      }),
         MockResponse(200, 'OK', _json={'list': ['three']})
     ]
     res = self.api._get('http://jive.example.com/foo', autopaginate=True)
     assert res == ['one', 'two', 'three']
     assert self.mock_sess.mock_calls == [
         call.get('http://jive.example.com/foo'),
         call.get('http://jive.example.com/bar')
     ]
Exemple #4
0
 def test_simple(self):
     req = req_t('GET', 'http://www.example.com')
     resp = MockResponse(404, 'Not Found', request=req, no_text=True)
     exc = RequestFailedException(resp)
     assert exc.response == resp
     assert exc.status_code == 404
     assert exc.reason == 'Not Found'
     assert str(
         exc) == 'GET http://www.example.com returned HTTP 404 Not Found'
Exemple #5
0
 def test_success(self):
     api = JiveApi('http://jive.example.com/', 'jiveuser', 'jivepass')
     mock_sess = MagicMock(spec_set=Session)
     api._requests = mock_sess
     mock_sess.get.return_value = MockResponse(200, 'OK', content=b'1234')
     res = api.get_image('imgid')
     assert res == b'1234'
     assert mock_sess.mock_calls == [
         call.get('http://jive.example.com/core/v3/images/imgid')
     ]
Exemple #6
0
 def test_simple(self):
     req = req_t('GET', 'http://www.example.com')
     resp = MockResponse(409, 'Conflict', request=req)
     exc = ContentConflictException(resp)
     assert exc.response == resp
     assert exc.status_code == 409
     assert exc.reason == 'Conflict'
     assert str(
         exc
     ) == 'GET http://www.example.com returned HTTP 409 Conflict: ' \
          'The new entity would conflict with system restrictions ' \
          '(such as two contents of the same type with the same name)'
Exemple #7
0
 def test_simple_error_json(self):
     req = req_t('GET', 'http://www.example.com')
     resp = MockResponse(409,
                         'Conflict',
                         request=req,
                         _json={'error': {
                             'message': 'eMsg'
                         }})
     exc = ContentConflictException(resp)
     assert exc.response == resp
     assert exc.status_code == 409
     assert exc.reason == 'Conflict'
     assert str(
         exc
     ) == 'GET http://www.example.com returned HTTP 409 Conflict: eMsg'
Exemple #8
0
 def test_simple_error_json(self):
     req = req_t('GET', 'http://www.example.com')
     resp = MockResponse(404,
                         'Not Found',
                         request=req,
                         _json={'error': {
                             'message': 'errMsg'
                         }})
     exc = RequestFailedException(resp)
     assert exc.response == resp
     assert exc.status_code == 404
     assert exc.reason == 'Not Found'
     assert str(
         exc
     ) == 'GET http://www.example.com returned HTTP 404 Not Found: errMsg'
Exemple #9
0
 def test_success(self):
     api = JiveApi('http://jive.example.com/', 'jiveuser', 'jivepass')
     mock_sess = MagicMock(spec_set=Session)
     api._requests = mock_sess
     mock_sess.post.return_value = MockResponse(
         201,
         'Created',
         _json={'foo': 'bar'},
         headers={'Location': 'http://some.location/'})
     res = api.upload_image(b'1234', 'img.jpg', 'image/jpeg')
     assert res == ('http://some.location/', {'foo': 'bar'})
     assert mock_sess.mock_calls == [
         call.post('http://jive.example.com/core/v3/images',
                   files={'file': ('img.jpg', b'1234', 'image/jpeg')},
                   allow_redirects=False)
     ]
Exemple #10
0
 def test_error(self):
     api = JiveApi('http://jive.example.com/', 'jiveuser', 'jivepass')
     mock_sess = MagicMock(spec_set=Session)
     api._requests = mock_sess
     req_t = namedtuple('MockRequest', ['method', 'url'])
     req = req_t(method='GET',
                 url='http://jive.example.com/core/v3/images/imgid')
     mock_sess.get.return_value = MockResponse(404,
                                               'Not Found',
                                               content=b'1234',
                                               request=req)
     with pytest.raises(RequestFailedException):
         api.get_image('imgid')
     assert mock_sess.mock_calls == [
         call.get('http://jive.example.com/core/v3/images/imgid')
     ]
Exemple #11
0
 def test_error(self):
     api = JiveApi('http://jive.example.com/', 'jiveuser', 'jivepass')
     mock_sess = MagicMock(spec_set=Session)
     api._requests = mock_sess
     req_t = namedtuple('MockRequest', ['method', 'url'])
     req = req_t(method='POST',
                 url='http://jive.example.com/core/v3/images')
     mock_sess.post.return_value = MockResponse(
         400,
         'Image is too large.',
         _json={'foo': 'bar'},
         headers={'Location': 'http://some.location/'},
         request=req)
     with pytest.raises(RequestFailedException):
         api.upload_image(b'1234', 'img.jpg', 'image/jpeg')
     assert mock_sess.mock_calls == [
         call.post('http://jive.example.com/core/v3/images',
                   files={'file': ('img.jpg', b'1234', 'image/jpeg')},
                   allow_redirects=False)
     ]