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