def assertSimpleMessageCorrect(self, m, message, domain, args, kwargs, expected_result): self.assertIsInstance(m, SimpleMessage) self.assertEqual(m.message, message) self.assertMessageEquals(m, domain, args, kwargs, expected_result) json_string = m.to_json() self.assertValidJSON(json_string) m2 = Message.from_json(json_string) self.assertIsInstance(m2, SimpleMessage) self.assertMessageEquals(m2, domain, args, kwargs, expected_result)
def assertSimpleMessageCorrect(m, message, domain, args, kwargs, expected_result): assert isinstance(m, SimpleMessage) assert m.message == message assertMessageEquals(m, domain, args, kwargs, expected_result) json_string = m.to_json() assertValidJSON(json_string) m2 = Message.from_json(json_string) assert isinstance(m2, SimpleMessage) assertMessageEquals(m2, domain, args, kwargs, expected_result)
def assertNumericMessageCorrect(self, m, singular, plural, n, domain, args, kwargs, expected_result): self.assertIsInstance(m, NumericalMessage) self.assertEqual(m.singular, singular) self.assertEqual(m.plural, plural) self.assertEqual(m.n, n) self.assertMessageEquals(m, domain, args, kwargs, expected_result) json_string = m.to_json() self.assertValidJSON(json_string) m2 = Message.from_json(json_string) self.assertIsInstance(m2, NumericalMessage) self.assertMessageEquals(m2, domain, args, kwargs, expected_result)
def assertNumericMessageCorrect(m, singular, plural, n, domain, args, kwargs, expected_result): assert isinstance(m, NumericalMessage) assert m.singular == singular assert m.plural == plural assert m.n == n assertMessageEquals(m, domain, args, kwargs, expected_result) json_string = m.to_json() assertValidJSON(json_string) m2 = Message.from_json(json_string) assert isinstance(m2, NumericalMessage) assertMessageEquals(m2, domain, args, kwargs, expected_result)
def format_log_entry(entry, log_type): """Format a logentry in correct json :param LogEntry entry: :param log_type: The logtype to include, currently ``'user'`` or ``'room'``. """ return { 'created_at': datetime_filter(entry.created_at), 'raw_created_at': entry.created_at, 'user': { 'type': 'native', # parses it as a link 'title': entry.author.name, 'href': url_for("user.user_show", user_id=entry.author.id) }, 'message': Message.from_json(entry.message).localize(), 'type': log_type }
def format_log_entry(entry, log_type): """Format a logentry in correct json :param LogEntry entry: :param log_type: The logtype to include, currently ``'user'`` or ``'room'``. """ return { 'created_at': datetime_format(entry.created_at), 'raw_created_at': entry.created_at, 'user': { 'type': 'native', # parses it as a link 'title': entry.author.name, 'href': url_for("user.user_show", user_id=entry.author.id) }, 'message': Message.from_json(entry.message).localize(), 'type': log_type }
def format_log_entry(entry, log_type): """Format a logentry in correct json :param LogEntry entry: :param log_type: The logtype to include, currently ``'user'`` or ``'room'``. """ return { 'created_at': datetime_format(entry.created_at, formatter=datetime_filter), 'raw_created_at': entry.created_at, 'user': UserColumn.value_native(title=entry.author.name, href=url_for("user.user_show", user_id=entry.author.id)), 'message': Message.from_json(entry.message).localize(), 'type': log_type }
def generate_user_data(user): props = {prop.property_name for prop in user.current_properties} user_status = status(user) interval = timedelta(days=7) step = timedelta(days=1) traffic_history = func_traffic_history( user.id, func.current_timestamp() - interval + step, func.current_timestamp()) finance_history = [{ 'valid_on': split.transaction.valid_on, # Invert amount, to display it from the user's point of view 'amount': -split.amount, 'description': Message.from_json(split.transaction.description).localize() } for split in user.account.splits] last_import_ts = get_last_import_date(session.session) last_finance_update = last_import_ts and last_import_ts.date() or None try: wifi_password = user.wifi_password except ValueError: wifi_password = None med = membership_end_date(user) mbd = membership_begin_date(user) interface_info = [{ 'id': i.id, 'mac': str(i.mac), 'ips': [str(ip.address) for ip in i.ips] } for h in user.hosts for i in h.interfaces] return jsonify( id=user.id, user_id=encode_type2_user_id(user.id), name=user.name, login=user.login, status={ 'member': user_status.member, 'traffic_exceeded': user_status.traffic_exceeded, 'network_access': user_status.network_access, 'account_balanced': user_status.account_balanced, 'violation': user_status.violation }, room=user.room.short_name if user.room is not None else None, interfaces=interface_info, mail=user.email, mail_forwarded=user.email_forwarded, mail_confirmed=user.email_confirmed, cache='cache_access' in props, # TODO: make `has_property` use `current_property` properties=list(props), traffic_history=[e.__dict__ for e in traffic_history], # TODO: think about better way for credit finance_balance=-user.account.balance, finance_history=finance_history, last_finance_update=last_finance_update.isoformat() if last_finance_update else None, membership_end_date=med.isoformat() if med else None, membership_begin_date=mbd.isoformat() if mbd else None, wifi_password=wifi_password, birthdate=user.birthdate.isoformat() if user.birthdate else None, )
def test_wrong_json(self): json_string = json.dumps({"key": "value"}) self.assertIsInstance(Message.from_json(json_string), ErroneousMessage)
def test_invalid_json(self): self.assertIsInstance(Message.from_json("not JSON"), ErroneousMessage)
def test_erroneous_json(json: str): assert isinstance(Message.from_json("not JSON"), ErroneousMessage)