'Testing whether the API returns a formatted 500 error if ValueError is raised'

@deferred(timeout=10)
def test_add_torrent_to_channel_500(self):

    self.create_fake_channel('channel', '')
    torrent_path = TORRENT_UBUNTU_FILE
    def fake_error(channel_id, torrent_def, extra_info=None, forward=True):
        raise ValueError('Test    error')
    self.session.add_torrent_def_to_channel = fake_error
    def verify_error_message(body):
        error_response = json.loads(body)
        expected_response = {u'error': {u'handled': True, u'code': u'ValueError', u'message': u'Test    error'}}
        self.assertDictContainsSubset(expected_response[u'error'], error_response[u'error'])
    with open(torrent_path, mode='rb') as torrent_file:
        torrent_64 = base64.b64encode(torrent_file.read())
    post_data = {'torrent': torrent_64}
    self.should_check_equality = False
    return self.do_request(('channels/discovered/%s/torrents' % 'fakedispersyid'.encode('hex')), expected_code=500, expected_json=None, request_type='PUT', post_data=post_data).addCallback(verify_error_message)
