Example #1
0
def test_reply_voice(xml_builder):
    data = xml_builder(*parameters["voice"][0])
    processor = MessageHandlerTester(data, debug_to_wechat=True)
    message = Message.from_string(processor.reply())
    assert message.msg_type == "voice"
    assert (
        ET.fromstring(message.content)
        .find("Voice").find("MediaId").text == "media_id")
Example #2
0
 def func(msg_type, content, msg_id=None):
     return Message(
         client.appid,
         openid,
         msg_type,
         content,
         msg_id,
     ).build_xml()
Example #3
0
def test_reply_video(xml_builder):
    data = xml_builder(*parameters["video"][0])
    processor = MessageHandlerTester(data, debug_to_wechat=True)
    message = Message.from_string(processor.reply())
    assert message.msg_type == "video"
    video_xml = ET.fromstring(message.content).find("Video")
    assert video_xml.find("MediaId").text == "media_id"
    assert video_xml.find("Title").text == "video title"
    assert video_xml.find("Description") is None
Example #4
0
def test_video_message(app_client, args, openid, client, xml_builder):
    data = xml_builder(
        "video", '''<MediaId><![CDATA[gad4123fasf321]]></MediaId>
<ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>
''', 1234567890123456)
    rv = app_client.post("/wechat", query_string=args, data=data)
    message = Message.from_string(rv.data)
    assert message.msg_type == "video"
    assert message.to_id == openid
    assert message.from_id == client.appid
    assert "gad4123fasf321" in message.content
Example #5
0
def test_reply_news(xml_builder):
    data = xml_builder(*parameters["location_msg"][0])
    processor = MessageHandlerTester(data, debug_to_wechat=True)
    message = Message.from_string(processor.reply())
    assert message.msg_type == "news"
    assert ET.fromstring(message.content).find("ArticleCount").text == "5"
    video_xml = ET.fromstring(message.content).find("Articles").find("item")
    assert video_xml.find("Title").text == "news title"
    assert video_xml.find("Description").text == "news description"
    assert video_xml.find("PicUrl").text == "http://qq.com"
    assert video_xml.find("Url").text == "http://qq.com"
Example #6
0
def test_reply_music(xml_builder):
    data = xml_builder(*parameters["shortvideo"][0])
    processor = MessageHandlerTester(data, debug_to_wechat=True)
    message = Message.from_string(processor.reply())
    assert message.msg_type == "music"
    video_xml = ET.fromstring(message.content).find("Music")
    assert video_xml.find("ThumbMediaId").text == "media_id"
    assert video_xml.find("Title").text == "music title"
    assert video_xml.find("Description").text == "music description"
    assert video_xml.find("MusicUrl").text == "http://qq.com"
    assert video_xml.find("HQMusicUrl").text == "http://qq.com"
Example #7
0
def test_process_message(xml_builder):
    data = xml_builder(*parameters["text"][0])
    processor = MessageHandlerTester(data, debug_to_wechat=True)
    from_message = processor.message
    assert from_message.msg_id == 1234567890123456
    assert from_message.msg_type == "text"
    assert from_message.create_time > 1348831860
    assert from_message.create_time < time.time()
    assert "<Content>this is a test</Content>" in from_message.content

    reply_message = Message.from_string(processor.reply())
    assert from_message.from_id == reply_message.to_id
    assert from_message.to_id == reply_message.from_id
Example #8
0
def test_message(app_client, args, openid, client, xml_builder):
    data = xml_builder(
        "event_LOCATION", '''
<Latitude>39.120232</Latitude>
<Longitude>117.529915</Longitude>
<Precision>40.0</Precision>''')
    rv = app_client.post("/wechat", query_string=args, data=data)
    location = (db.session.query(Location).filter_by(openid=openid).order_by(
        Location.create_time.desc()).first())
    assert location.latitude == 39.120232
    assert location.longitude == 117.529915
    assert location.precision == 40.0
    msg_count = db.session.query(Message).count()
    assert msg_count >= 2
    user = db.session.query(User).filter_by(openid=openid).first()
    assert user is not None
    message = Message.from_string(rv.data)
    assert message.msg_type == "text"
    assert message.to_id == openid
    assert message.from_id == client.appid
    assert "39.120232" in message.content
    assert "117.529915" in message.content
Example #9
0
def test_reply_text(xml_builder, client, openid):
    data = xml_builder(*parameters["text"][0])
    processor = MessageHandlerTester(data, debug_to_wechat=True)
    xml_str = processor.reply()
    message = Message.from_string(xml_str)

    # 测试发送给微信服务器的消息的头部是正确的
    xml = ET.fromstring(xml_str)
    assert xml.find("ToUserName").text == message.to_id
    assert xml.find("FromUserName").text == message.from_id
    assert int(xml.find("CreateTime").text) == message.create_time
    assert xml.find("MsgType").text == message.msg_type
    assert xml.find("Content").text == (
        ET.fromstring(message.content).find("Content").text)

    # 测试Message类的属性是正确的
    assert message.from_id == client.appid
    assert message.to_id == openid
    assert message.msg_type == 'text'
    assert isinstance(message.create_time, int)
    assert (
        ET.fromstring(message.content)
        .find("Content").text == "this is a test")