def request_backlog(self, a_backlog): returned_values = ValueObject( self.view.do_get( self.req(name=a_backlog.name, scope=a_backlog.scope))) returned_values.backlog_info = ValueObject( json.loads(returned_values.backlog_info)) return returned_values
def _load_json_data(self, http_body): try: data = json.loads(http_body) except Exception, e: msg = u'Received invalid JSON request %s' % \ exception_to_unicode(e) log.warning(self, msg) data = None
def mock_request(self, username='******', path_info='/', request_body='', **kwargs): response = ValueObject(headers=dict(), body='', code=None) as_json = lambda self: json.loads(self.body) response.body_as_json = new.instancemethod(as_json, response, response.__class__) perm = PermissionCache(self.env, username) attributes = dict( args=dict(), tz=localtz, perm=perm, method='GET', path_info=path_info, environ={}, session={}, form_token=None, ) attributes.update(kwargs) def read(): return request_body def write(string): response['body'] += string def redirect(url, permanent=False): raise RequestDone def get_header(header_name): header_name = header_name.lower() if header_name == 'content-length': return str(len(request_body)) return None req = Mock(authname=username, base_path=None, href=Href(path_info), chrome=dict(warnings=[], notices=[], scripts=[]), incookie=Cookie(), outcookie=Cookie(), response=response, end_headers=lambda: None, get_header=get_header, read=read, redirect=redirect, send_response=lambda code: response.update({'code': code}), send_header=lambda name, value: response['headers'].update( {name: value}), write=write, **attributes) # our jquery injects wines if it does not find trac's jquery req.chrome['scripts'].append(dict(href='/foo/jquery.js')) return req
def _decode_json(self, data): try: decoded_json = json.loads(data) except Exception, e: # We can wrap the exception here because the traceback is not # very interesting for us. Either it is a problem within simplejson # or something is wrong with the data. raise JSONDecodeError( unicode(e) + ' - undecodable json: ' + repr(data))
def parse_microformat(self): if self.value is None: return 0, dict() microformat = json.loads(self.value) if isinstance(microformat, int) or isinstance(microformat, float): return microformat, dict() elif isinstance(microformat, list): return microformat[0], microformat[1] else: raise ValueError('microformat <%s> not supported' % repr(self.value))
def assert_json_error(self, call_server, *args, **kwargs): """If you give code as an keyword argument, it is ensured that the server responded with that HTTP code.""" code = kwargs.pop('code', None) # REFACT: use assert_raises try: call_server(*args, **kwargs) self.fail() except GenericHTTPException, e: if code is not None: self.assertEqual(code, e.code) json_data = ValueObject(json.loads(e.detail)) return json_data
def runTest(self): # User "TeamMember" is not part of the team. This is why the test fails self.json_tester.login_as(Usernames.team_member) self.ensure_min_one_second_passed() exception = self.assert_raises(GenericHTTPException, \ lambda: self.json_tester.edit_ticket(self.task_id, simple_status='in_progress')) self.assert_contains("doesn't belong to the team", exception_to_unicode(exception)) response_json = json.loads(exception.detail) self.assertEqual(1, len(response_json['errors'])) ticket = ValueObject(response_json['current_data']) self.assertEqual(Status.NEW, ticket.status) self.assertEqual('', ticket.owner) task_owner = self.tester.get_owner_of_ticket(self.task_id) self.assertEqual('', task_owner)
def mock_request(self, username='******', path_info='/', request_body='', **kwargs): response = ValueObject(headers=dict(), body='', code=None) as_json = lambda self: json.loads(self.body) response.body_as_json = new.instancemethod(as_json, response, response.__class__) perm = PermissionCache(self.env, username) attributes = dict(args=dict(), tz=localtz, perm=perm, method='GET', path_info=path_info, environ={}, session={}, form_token=None, ) attributes.update(kwargs) def read(): return request_body def write(string): response['body'] += string def redirect(url, permanent=False): raise RequestDone def get_header(header_name): header_name = header_name.lower() if header_name == 'content-length': return str(len(request_body)) return None req = Mock(authname=username, base_path=None, href=Href(path_info), chrome=dict(warnings=[], notices=[], scripts=[]), incookie=Cookie(), outcookie=Cookie(), response=response, end_headers=lambda: None, get_header=get_header, read=read, redirect=redirect, send_response=lambda code: response.update({'code': code}), send_header=lambda name, value: response['headers'].update({name: value}), write=write, **attributes) # our jquery injects wines if it does not find trac's jquery req.chrome['scripts'].append(dict(href='/foo/jquery.js')) return req
def request_backlog(self, a_backlog): returned_values = ValueObject(self.view.do_get(self.req(name=a_backlog.name, scope=a_backlog.scope))) returned_values.backlog_info = ValueObject(json.loads(returned_values.backlog_info)) return returned_values
def _ticket_json_from_header(self, task_id): headers = self._response_headers_from_ticket_page(task_id) raw_json = headers.getheader('X-Agilo-Ticket-JSON') return json.loads(raw_json)
def test_contains_can_edit_on_error(self): self.assert_raises(RequestDone, self.update_contains_can_edit, {}) ticket = json.loads(self.req.response.body)['current_data'] self.assert_false(ticket['can_edit'])