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') ]
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) ]
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') ]
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) ]