Exemplo n.º 1
0
def test_template_send_job_finish_event():
    message = parse_user_msg("""
    <xml>
        <ToUserName><![CDATA[gh_7f083739789a]]></ToUserName>
        <FromUserName><![CDATA[oia2TjuEGTNoeX76QEjQNrcURxG8]]></FromUserName>
        <CreateTime>1395658920</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[TEMPLATESENDJOBFINISH]]></Event>
        <MsgID>200163836</MsgID>
        <Status><![CDATA[success]]></Status>
    </xml>
    """)
    assert message.message_id == 200163836
    assert message.status == 'success'

    assert parse_user_msg("""
    <xml>
        <ToUserName><![CDATA[gh_7f083739789a]]></ToUserName>
        <FromUserName><![CDATA[oia2TjuEGTNoeX76QEjQNrcURxG8]]></FromUserName>
        <CreateTime>1395658984</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[TEMPLATESENDJOBFINISH]]></Event>
        <MsgID>200163840</MsgID>
        <Status><![CDATA[failed: system failed]]></Status>
    </xml>
    """).status == 'failed: system failed'
Exemplo n.º 2
0
def test_subscribe_event():
    message = parse_user_msg("""
    <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[FromUser]]></FromUserName>
        <CreateTime>123456789</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[subscribe]]></Event>
    </xml>
    """)
    assert message.target == "toUser"
    assert message.source == "FromUser"
    assert message.time == 123456789
    assert message.type == "subscribe_event"

    message = parse_user_msg("""
    <xml><ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[FromUser]]></FromUserName>
        <CreateTime>123456789</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[subscribe]]></Event>
        <EventKey><![CDATA[qrscene_123123]]></EventKey>
        <Ticket><![CDATA[TICKET]]></Ticket>
    </xml>
    """)
    assert message.target == "toUser"
    assert message.source == "FromUser"
    assert message.time == 123456789
    assert message.type == "subscribe_event"
    assert message.key == "qrscene_123123"
    assert message.ticket == "TICKET"
Exemplo n.º 3
0
def test_pic_sysphoto_event():
    # count is 1
    message = parse_user_msg("""
    <xml>
        <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName>
        <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName>
        <CreateTime>1408090651</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[pic_sysphoto]]></Event>
        <EventKey><![CDATA[6]]></EventKey>
        <SendPicsInfo>
            <Count>1</Count>
            <PicList>
                <item>
                    <PicMd5Sum><![CDATA[1b5f7c23b5bf75682a53e7b6d163e185]]></PicMd5Sum>
                </item>
            </PicList>
        </SendPicsInfo>
    </xml>
    """)
    assert message.target == "gh_e136c6e50636"
    assert message.source == "oMgHVjngRipVsoxg6TuX3vz6glDg"
    assert message.time == 1408090651
    assert message.type == "pic_sysphoto_event"
    assert message.key == "6"
    assert message.count == 1
    assert message.pic_list == [{'pic_md5_sum': '1b5f7c23b5bf75682a53e7b6d163e185'}]

    # count is more than 1
    message = parse_user_msg("""
        <xml>
            <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName>
            <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName>
            <CreateTime>1408090651</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[pic_sysphoto]]></Event>
            <EventKey><![CDATA[6]]></EventKey>
            <SendPicsInfo>
                <Count>2</Count>
                <PicList>
                    <item>
                        <PicMd5Sum><![CDATA[1b5f7c23b5bf75682a53e7b6d163e185]]></PicMd5Sum>
                    </item>
                    <item>
                        <PicMd5Sum><![CDATA[233]]></PicMd5Sum>
                    </item>
                </PicList>
            </SendPicsInfo>
        </xml>
        """)
    assert message.target == "gh_e136c6e50636"
    assert message.source == "oMgHVjngRipVsoxg6TuX3vz6glDg"
    assert message.time == 1408090651
    assert message.type == "pic_sysphoto_event"
    assert message.key == "6"
    assert message.count == 2
    assert message.pic_list == [
        {'pic_md5_sum': '1b5f7c23b5bf75682a53e7b6d163e185'}, {'pic_md5_sum': '233'}
    ]
Exemplo n.º 4
0
def test_user_scan_product_verify_action():
    @werobot.user_scan_product_verify_action
    def user_scan_product_verify_action():
        return '审核通过了喵'

    message = parse_user_msg(
        """
        <xml>
            <ToUserName><![CDATA[gh_404e58ec528e]]></ToUserName>
            <FromUserName><![CDATA[od_ikt8qi21-hVTtYgm8xSfTLH5w]]></FromUserName>
            <CreateTime>1450429257</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[user_scan_product_verify_action]]></Event>
            <KeyStandard><![CDATA[ean13]]></KeyStandard>
            <KeyStr><![CDATA[6901481811083]]></KeyStr>
            <Result><![CDATA[verify_ok]]></Result>
            <ReasonMsg><![CDATA[]]></ReasonMsg>
        </xml>
    """
    )

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'审核通过了喵'
Exemplo n.º 5
0
def test_user_scan_product_async():
    @werobot.user_scan_product_async
    def user_scan_product_async():
        return '异步的地理位置喵'

    message = parse_user_msg(
        """
        <xml>
            <ToUserName><![CDATA[gh_fbe8a958756e]]></ToUserName>
            <FromUserName><![CDATA[otAzGjrS4AYCmeJM1GhEOcHXXTAo]]></FromUserName>
            <CreateTime>1434541327</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[user_scan_product_async]]></Event>
            <KeyStandard><![CDATA[qrcode]]></KeyStandard>
            <KeyStr><![CDATA[lincolntest2]]></KeyStr>
            <ExtInfo><![CDATA[123]]></ExtInfo>
            <RegionCode><![CDATA[440105]]></RegionCode>
        </xml>
    """
    )

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'异步的地理位置喵'
Exemplo n.º 6
0
def test_scan_waitmsg_handler():
    @werobot.scancode_waitmsg
    def scancode_waitmsg(message):
        return '扫描弹消息'

    message = parse_user_msg(
        """
        <xml>
            <ToUserName><![CDATA[toUser]]></ToUserName>
            <FromUserName><![CDATA[FromUser]]></FromUserName>
            <CreateTime>123456789</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[scancode_waitmsg]]></Event>
            <EventKey><![CDATA[EVENTKEY]]></EventKey>
            <ScanCodeInfo>
                <ScanType><![CDATA[qrcode]]></ScanType>
                <ScanResult><![CDATA[http://www.qq.com]]></ScanResult>
            </ScanCodeInfo>
        </xml>
        """
    )

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'扫描弹消息'
Exemplo n.º 7
0
def test_user_scan_product():
    @werobot.user_scan_product
    def user_scan_product():
        return '打扰了'

    message = parse_user_msg(
        """
        <xml>
            <ToUserName><![CDATA[gh_4e47c9c9ecad]]></ToUserName>
            <FromUserName><![CDATA[okkeXs1nI-xU4ql8-5BXkv1f0gDo]]></FromUserName>
            <CreateTime>1438250110</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[user_scan_product]]></Event>
            <KeyStandard><![CDATA[ean13]]></KeyStandard>
            <KeyStr><![CDATA[6901481811083]]></KeyStr>
            <Country><![CDATA[中国]]></Country>
            <Province><![CDATA[广东]]></Province>
            <City><![CDATA[揭阳]]></City>
            <Sex>1</Sex>
            <Scene>2</Scene>
            <ExtInfo><![CDATA[123]]></ExtInfo>
        </xml>
    """
    )

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'打扰了'
Exemplo n.º 8
0
def test_location():
    @werobot.location
    def location(message):
        return '地理位置汪'

    message = parse_user_msg(
        """
        <xml>
            <ToUserName><![CDATA[toUser]]></ToUserName>
            <FromUserName><![CDATA[fromUser]]></FromUserName>
            <CreateTime>1351776360</CreateTime>
            <MsgType><![CDATA[location]]></MsgType>
            <Location_X>23.134521</Location_X>
            <Location_Y>113.358803</Location_Y>
            <Scale>20</Scale>
            <Label><![CDATA[Location]]></Label>
            <MsgId>1234567890123456</MsgId>
        </xml>
        """
    )

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'地理位置汪'
Exemplo n.º 9
0
def test_card_pay_order_handler():
    @werobot.card_pay_order
    def card_pay_order():
        return '冲动消费的凭证'

    message = parse_user_msg(
        """
            <xml>
                <ToUserName><![CDATA[gh_7223c83d4be5]]></ToUserName>
                <FromUserName><![CDATA[ob5E7s-HoN9tslQY3-0I4qmgluHk]]></FromUserName>
                <CreateTime>1453295737</CreateTime>
                <MsgType><![CDATA[event]]></MsgType>
                <Event><![CDATA[card_pay_order]]></Event>
                <OrderId><![CDATA[404091456]]></OrderId>
                <Status><![CDATA[ORDER_STATUS_FINANCE_SUCC]]></Status>
                <CreateOrderTime>1453295737</CreateOrderTime>
                <PayFinishTime>0</PayFinishTime>
                <Desc><![CDATA[]]></Desc>
                <FreeCoinCount><![CDATA[200]]></FreeCoinCount>
                <PayCoinCount><![CDATA[0]]></PayCoinCount>
                <RefundFreeCoinCount><![CDATA[0]]></RefundFreeCoinCount>
                <RefundPayCoinCount><![CDATA[0]]></RefundPayCoinCount>
                <OrderType><![CDATA[ORDER_TYPE_SYS_ADD]]></OrderType>
                <Memo><![CDATA[开通账户奖励]]></Memo>
                <ReceiptInfo><![CDATA[]]></ReceiptInfo>
            </xml>
        """
    )

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'冲动消费的凭证'
Exemplo n.º 10
0
def test_user_pay_from_pay_cell_handler():
    @werobot.user_pay_from_pay_cell
    def user_pay_from_pay_cell():
        return '冲动消费'

    message = parse_user_msg(
        """
            <xml>
                <ToUserName><![CDATA[gh_e2243xxxxxxx]]></ToUserName>
                <FromUserName><![CDATA[oo2VNuOUuZGMxxxxxxxx]]></FromUserName>
                <CreateTime>1442390947</CreateTime>
                <MsgType><![CDATA[event]]></MsgType>
                <Event><![CDATA[user_pay_from_pay_cell]]></Event>
                <CardId><![CDATA[po2VNuCuRo-8sxxxxxxxxxxx]]></CardId>
                <UserCardCode><![CDATA[38050000000]]></UserCardCode>
                <TransId><![CDATA[10022403432015000000000]]></TransId>
                <LocationId>291710000</LocationId>
                <Fee><![CDATA[10000]]></Fee>
                <OriginalFee><![CDATA[10000]]> </OriginalFee>
            </xml>
        """
    )

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'冲动消费'
Exemplo n.º 11
0
def test_user_consume_card_handler():
    @werobot.user_consume_card
    def user_consume_card():
        return '恭喜脱坑'

    message = parse_user_msg(
        """
            <xml>
                <ToUserName> <![CDATA[gh_fc0a06a20993]]> </ToUserName>
                <FromUserName> <![CDATA[oZI8Fj040-be6rlDohc6gkoPOQTQ]]> </FromUserName>
                <CreateTime>1472549042</CreateTime>
                <MsgType> <![CDATA[event]]> </MsgType>
                <Event> <![CDATA[user_consume_card]]> </Event>
                <CardId> <![CDATA[pZI8Fj8y-E8hpvho2d1ZvpGwQBvA]]> </CardId>
                <UserCardCode> <![CDATA[452998530302]]> </UserCardCode>
                <ConsumeSource> <![CDATA[FROM_API]]> </ConsumeSource>
                <LocationName> <![CDATA[]]> </LocationName>
                <StaffOpenId> <![CDATA[oZ********nJ3bPJu_Rtjkw4c]]> </StaffOpenId>
                <VerifyCode> <![CDATA[]]> </VerifyCode>
                <RemarkAmount> <![CDATA[]]> </RemarkAmount>
                <OuterStr> <![CDATA[xxxxx]]> </OuterStr>
            </xml>
        """
    )

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'恭喜脱坑'
Exemplo n.º 12
0
def test_user_gifting_card_handler():
    @werobot.user_gifting_card
    def user_gifting_card():
        return '锅从天上来'

    message = parse_user_msg(
        """
            <xml>
                <ToUserName><![CDATA[gh_3fcea188bf78]]></ToUserName>
                <FromUserName><![CDATA[obLatjjwDolFjRRd3doGIdwNqRXw]]></FromUserName>
                <CreateTime>1474181868</CreateTime>
                <MsgType><![CDATA[event]]></MsgType>
                <Event><![CDATA[user_gifting_card]]></Event>
                <CardId><![CDATA[pbLatjhU-3pik3d4PsbVzvBxZvJc]]></CardId>
                <UserCardCode><![CDATA[297466945104]]></UserCardCode>
                <IsReturnBack>0</IsReturnBack>
                <FriendUserName><![CDATA[obLatjlNerkb62HtSdQUx66C4NTU]]></FriendUserName>
                <IsChatRoom>0</IsChatRoom>
            </xml>
        """
    )

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'锅从天上来'
Exemplo n.º 13
0
def test_user_get_card_handler():
    @werobot.user_get_card
    def user_get_card():
        return '恭喜入坑'

    message = parse_user_msg(
        """
            <xml>
                <ToUserName> <![CDATA[gh_fc0a06a20993]]> </ToUserName>
                <FromUserName> <![CDATA[oZI8Fj040-be6rlDohc6gkoPOQTQ]]> </FromUserName>
                <CreateTime>1472551036</CreateTime>
                <MsgType> <![CDATA[event]]> </MsgType>
                <Event> <![CDATA[user_get_card]]> </Event>
                <CardId> <![CDATA[pZI8Fjwsy5fVPRBeD78J4RmqVvBc]]> </CardId>
                <IsGiveByFriend>0</IsGiveByFriend>
                <UserCardCode> <![CDATA[226009850808]]> </UserCardCode>
                <FriendUserName> <![CDATA[]]> </FriendUserName>
                <OuterId>0</OuterId>
                <OldUserCardCode> <![CDATA[]]> </OldUserCardCode>
                <OuterStr> <![CDATA[12b]]> </OuterStr>
                <IsRestoreMemberCard>0</IsRestoreMemberCard>
                <IsRecommendByFriend>0</IsRecommendByFriend>
            </xml>
        """
    )

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'恭喜入坑'
Exemplo n.º 14
0
def test_location_select_handler():
    @werobot.location_select
    def location_select():
        return '瞧一瞧地理位置'

    message = parse_user_msg(
        """
    <xml>
        <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName>
        <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName>
        <CreateTime>1408091189</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[location_select]]></Event>
        <EventKey><![CDATA[6]]></EventKey>
        <SendLocationInfo>
            <Location_X><![CDATA[23]]></Location_X>
            <Location_Y><![CDATA[113]]></Location_Y>
            <Scale><![CDATA[15]]></Scale>
            <Label><![CDATA[广州市海珠区客村艺苑路 106号]]></Label>
            <Poiname><![CDATA[]]></Poiname>
        </SendLocationInfo>
    </xml>
    """
    )

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'瞧一瞧地理位置'
Exemplo n.º 15
0
def test_pic_weixin_handler():
    @werobot.pic_weixin
    def pic_weixin():
        return '瞧一瞧微信相册'

    message = parse_user_msg(
        """
    <xml>
        <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName>
        <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName>
        <CreateTime>1408090816</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[pic_weixin]]></Event>
        <EventKey><![CDATA[6]]></EventKey>
        <SendPicsInfo>
            <Count>1</Count>
            <PicList>
                <item>
                    <PicMd5Sum><![CDATA[5a75aaca956d97be686719218f275c6b]]></PicMd5Sum>
                </item>
            </PicList>
        </SendPicsInfo>
    </xml>
    """
    )

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'瞧一瞧微信相册'
Exemplo n.º 16
0
def test_pic_sysphoto_handler():
    @werobot.pic_sysphoto
    def pic_sysphoto():
        return '瞧一瞧系统拍照'

    message = parse_user_msg(
        """
    <xml>
        <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName>
        <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName>
        <CreateTime>1408090651</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[pic_sysphoto]]></Event>
        <EventKey><![CDATA[6]]></EventKey>
        <SendPicsInfo>
            <Count>1</Count>
            <PicList>
                <item>
                    <PicMd5Sum><![CDATA[1b5f7c23b5bf75682a53e7b6d163e185]]></PicMd5Sum>
                </item>
            </PicList>
        </SendPicsInfo>
    </xml>
    """
    )

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'瞧一瞧系统拍照'
Exemplo n.º 17
0
def test_update_member_card_handler():
    @werobot.update_member_card
    def update_member_card():
        return '冲动消费导致余额减少'

    message = parse_user_msg(
        """
            <xml>
                <ToUserName><![CDATA[gh_9e1765b5568e]]></ToUserName>
                <FromUserName><![CDATA[ojZ8YtyVyr30HheH3CM73y7h4jJE]]></FromUserName>
                <CreateTime>1445507140</CreateTime>
                <MsgType><![CDATA[event]]></MsgType>
                <Event><![CDATA[update_member_card]]></Event>
                <CardId><![CDATA[pjZ8Ytx-nwvpCRyQneH3Ncmh6N94]]></CardId>
                <UserCardCode><![CDATA[485027611252]]></UserCardCode>
                <ModifyBonus>3</ModifyBonus>
                <ModifyBalance>0</ModifyBalance>
            </xml>
        """
    )

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'冲动消费导致余额减少'
Exemplo n.º 18
0
        def handler():
            if not self.check_signature(
                    request.args.get('timestamp', ''),
                    request.args.get('nonce', ''),
                    request.args.get('signature', '')
            ):
                return 'Invalid Request.'
            if request.method == 'GET':
                return request.args('echostr')

            body = request.data
            message = parse_user_msg(body)
            # get_reply() return the reply object for given message
            reply = self.get_reply(message)
            if not reply:
                return ''
            # flask将视图函数的返回值自动转为一个响应对像. 如果返回值是一个字符串
            # 它将被转换为以该字符串为主体的, 状态码为200, MIME类型是 text/html的响应对像.
            # flask 把返回值转为响应对像的逻辑如下:
            #   1. 如果返回值是一个合法的响应对像, 它会从视图直接返回
            #   2. 如果返回的是一个字符串, 响应对像会用字符串数据和默认参数创建
            #   3. 如果返回的是一个元组, 且元组中的元素可以提供额外的信息.
            #      这样的元组必须是 (response, status, headers) 形式, 且至少包含一个元素.
            #      status值会覆盖状态代码, headers可以是一个列表或者字典, 做为额外的消息标头值
            #   4. 如果上述条件均不满足, flask假设返回值是一个合法的WSGI应用程序,
            #      并转换为一个请求对像
            # 如果想在视图中操纵上述步骤结果的响应对像, 可以使用make_response()函数
            response = make_response(create_reply(reply, message=message))
            response.headers['content_type'] = 'application/xml'
            return response
Exemplo n.º 19
0
def test_location_select_event():
    message = parse_user_msg(u"""
    <xml>
        <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName>
        <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName>
        <CreateTime>1408091189</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[location_select]]></Event>
        <EventKey><![CDATA[6]]></EventKey>
        <SendLocationInfo>
            <Location_X><![CDATA[23]]></Location_X>
            <Location_Y><![CDATA[113]]></Location_Y>
            <Scale><![CDATA[15]]></Scale>
            <Label><![CDATA[广州市海珠区客村艺苑路 106号]]></Label>
            <Poiname><![CDATA[]]></Poiname>
        </SendLocationInfo>
    </xml>
    """)
    assert message.target == "gh_e136c6e50636"
    assert message.source == "oMgHVjngRipVsoxg6TuX3vz6glDg"
    assert message.time == 1408091189
    assert message.type == "location_select_event"
    assert message.key == "6"
    assert message.location_x == "23"
    assert message.location_y == "113"
    assert message.scale == "15"
    assert message.label == u"广州市海珠区客村艺苑路 106号"
    assert message.poi_name is None
Exemplo n.º 20
0
def test_user_scan_product_event():
    message = parse_user_msg("""
    <xml>
        <ToUserName><![CDATA[gh_4e47c9c9ecad]]></ToUserName>
        <FromUserName><![CDATA[okkeXs1nI-xU4ql8-5BXkv1f0gDo]]></FromUserName>
        <CreateTime>1438250110</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[user_scan_product]]></Event>
        <KeyStandard><![CDATA[ean13]]></KeyStandard>
        <KeyStr><![CDATA[6901481811083]]></KeyStr>
        <Country><![CDATA[中国]]></Country>
        <Province><![CDATA[广东]]></Province>
        <City><![CDATA[揭阳]]></City>
        <Sex>1</Sex>
        <Scene>2</Scene>
        <ExtInfo><![CDATA[123]]></ExtInfo>
    </xml>
    """)
    assert message.target == "gh_4e47c9c9ecad"
    assert message.source == "okkeXs1nI-xU4ql8-5BXkv1f0gDo"
    assert message.time == 1438250110
    assert message.type == "user_scan_product_event"
    assert message.key_standard == "ean13"
    assert message.key_str == "6901481811083"
    assert message.country == u"中国"
    assert message.province == u"广东"
    assert message.city == u"揭阳"
    assert message.sex == 1
    assert message.scene == 2
    assert message.ext_info == "123"
Exemplo n.º 21
0
def test_user_get_card_event():
    message = parse_user_msg("""
        <xml>
            <ToUserName> <![CDATA[gh_fc0a06a20993]]> </ToUserName>
            <FromUserName> <![CDATA[oZI8Fj040-be6rlDohc6gkoPOQTQ]]> </FromUserName>
            <CreateTime>1472551036</CreateTime>
            <MsgType> <![CDATA[event]]> </MsgType>
            <Event> <![CDATA[user_get_card]]> </Event>
            <CardId> <![CDATA[pZI8Fjwsy5fVPRBeD78J4RmqVvBc]]> </CardId>
            <IsGiveByFriend>0</IsGiveByFriend>
            <UserCardCode> <![CDATA[226009850808]]> </UserCardCode>
            <FriendUserName> <![CDATA[]]> </FriendUserName>
            <OuterId>0</OuterId>
            <OldUserCardCode> <![CDATA[]]> </OldUserCardCode>
            <OuterStr> <![CDATA[12b]]> </OuterStr>
            <IsRestoreMemberCard>0</IsRestoreMemberCard>
            <IsRecommendByFriend>0</IsRecommendByFriend>
        </xml>
    """)
    assert message.target == "gh_fc0a06a20993"
    assert message.source == "oZI8Fj040-be6rlDohc6gkoPOQTQ"
    assert message.time == 1472551036
    assert message.type == "user_get_card_event"
    assert message.card_id == "pZI8Fjwsy5fVPRBeD78J4RmqVvBc"
    assert message.is_give_by_friend == 0
    assert message.user_card_code == "226009850808"
    assert message.friend_user_name is None
    assert message.outer_id == 0
    assert message.old_user_card_code is None
    assert message.outer_str == '12b'
    assert message.is_restore_member_card == 0
    assert message.is_recommend_by_friend == 0
Exemplo n.º 22
0
def test_user_pay_from_pay_cell_event():
    message = parse_user_msg("""
        <xml>
            <ToUserName><![CDATA[gh_e2243xxxxxxx]]></ToUserName>
            <FromUserName><![CDATA[oo2VNuOUuZGMxxxxxxxx]]></FromUserName>
            <CreateTime>1442390947</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[user_pay_from_pay_cell]]></Event>
            <CardId><![CDATA[po2VNuCuRo-8sxxxxxxxxxxx]]></CardId>
            <UserCardCode><![CDATA[38050000000]]></UserCardCode>
            <TransId><![CDATA[10022403432015000000000]]></TransId>
            <LocationId>291710000</LocationId>
            <Fee><![CDATA[10000]]></Fee>
            <OriginalFee><![CDATA[10000]]> </OriginalFee>
        </xml>
    """)
    assert message.target == "gh_e2243xxxxxxx"
    assert message.source == "oo2VNuOUuZGMxxxxxxxx"
    assert message.time == 1442390947
    assert message.type == "user_pay_from_pay_cell_event"
    assert message.card_id == "po2VNuCuRo-8sxxxxxxxxxxx"
    assert message.user_card_code == "38050000000"
    assert message.trans_id == "10022403432015000000000"
    assert message.location_id == 291710000
    assert message.fee == "10000"
    assert message.original_fee == "10000"
Exemplo n.º 23
0
def test_user_consume_card_event():
    message = parse_user_msg("""
        <xml>
            <ToUserName> <![CDATA[gh_fc0a06a20993]]> </ToUserName>
            <FromUserName> <![CDATA[oZI8Fj040-be6rlDohc6gkoPOQTQ]]> </FromUserName>
            <CreateTime>1472549042</CreateTime>
            <MsgType> <![CDATA[event]]> </MsgType>
            <Event> <![CDATA[user_consume_card]]> </Event>
            <CardId> <![CDATA[pZI8Fj8y-E8hpvho2d1ZvpGwQBvA]]> </CardId>
            <UserCardCode> <![CDATA[452998530302]]> </UserCardCode>
            <ConsumeSource> <![CDATA[FROM_API]]> </ConsumeSource>
            <LocationName> <![CDATA[]]> </LocationName>
            <StaffOpenId> <![CDATA[oZ********nJ3bPJu_Rtjkw4c]]> </StaffOpenId>
            <VerifyCode> <![CDATA[]]> </VerifyCode>
            <RemarkAmount> <![CDATA[]]> </RemarkAmount>
            <OuterStr> <![CDATA[xxxxx]]> </OuterStr>
        </xml>
    """)
    assert message.target == "gh_fc0a06a20993"
    assert message.source == "oZI8Fj040-be6rlDohc6gkoPOQTQ"
    assert message.time == 1472549042
    assert message.type == "user_consume_card_event"
    assert message.card_id == "pZI8Fj8y-E8hpvho2d1ZvpGwQBvA"
    assert message.user_card_code == "452998530302"
    assert message.consume_source == "FROM_API"
    assert message.location_name is None
    assert message.staff_open_id == "oZ********nJ3bPJu_Rtjkw4c"
    assert message.verify_code is None
    assert message.remark_amount is None
    assert message.outer_str == "xxxxx"
Exemplo n.º 24
0
def test_pic_weixin_event():
    message = parse_user_msg("""
    <xml>
        <ToUserName><![CDATA[gh_e136c6e50636]]></ToUserName>
        <FromUserName><![CDATA[oMgHVjngRipVsoxg6TuX3vz6glDg]]></FromUserName>
        <CreateTime>1408090816</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[pic_weixin]]></Event>
        <EventKey><![CDATA[6]]></EventKey>
        <SendPicsInfo>
            <Count>1</Count>
            <PicList>
                <item>
                    <PicMd5Sum><![CDATA[5a75aaca956d97be686719218f275c6b]]></PicMd5Sum>
                </item>
            </PicList>
        </SendPicsInfo>
    </xml>
    """)
    assert message.target == "gh_e136c6e50636"
    assert message.source == "oMgHVjngRipVsoxg6TuX3vz6glDg"
    assert message.time == 1408090816
    assert message.type == "pic_weixin_event"
    assert message.key == "6"
    assert message.count == 1
    assert message.pic_list == [{'pic_md5_sum': '5a75aaca956d97be686719218f275c6b'}]
Exemplo n.º 25
0
def test_user_gifting_card_event():
    message = parse_user_msg(
        """
        <xml>
            <ToUserName><![CDATA[gh_3fcea188bf78]]></ToUserName>
            <FromUserName><![CDATA[obLatjjwDolFjRRd3doGIdwNqRXw]]></FromUserName>
            <CreateTime>1474181868</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[user_gifting_card]]></Event>
            <CardId><![CDATA[pbLatjhU-3pik3d4PsbVzvBxZvJc]]></CardId>
            <UserCardCode><![CDATA[297466945104]]></UserCardCode>
            <IsReturnBack>0</IsReturnBack>
            <FriendUserName><![CDATA[obLatjlNerkb62HtSdQUx66C4NTU]]></FriendUserName>
            <IsChatRoom>0</IsChatRoom>
        </xml>
    """
    )
    assert message.target == "gh_3fcea188bf78"
    assert message.source == "obLatjjwDolFjRRd3doGIdwNqRXw"
    assert message.time == 1474181868
    assert message.type == "user_gifting_card_event"
    assert message.card_id == "pbLatjhU-3pik3d4PsbVzvBxZvJc"
    assert message.user_card_code == "297466945104"
    assert message.is_return_back == 0
    assert message.friend_user_name == "obLatjlNerkb62HtSdQUx66C4NTU"
    assert message.is_chat_room == 0
Exemplo n.º 26
0
            def post(self):
                body = self.request.body
                message = parse_user_msg(body)
                self.set_header("Content-Type",
                                "application/xml;charset=utf-8")

                reply = robot.get_reply(message)
                self.write(reply)
Exemplo n.º 27
0
def test_event_message():
    message = parse_user_msg("""
    <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[fromUser]]></FromUserName>
        <CreateTime>123456789</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[subscribe]]></Event>
    </xml>
    """)
    assert message.target == "toUser"
    assert message.source == "fromUser"
    assert message.time == 123456789
    assert message.type == "subscribe"

    message = parse_user_msg("""
    <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[fromUser]]></FromUserName>
        <CreateTime>123456789</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[unsubscribe]]></Event>
    </xml>
    """)
    assert message.target == "toUser"
    assert message.source == "fromUser"
    assert message.time == 123456789
    assert message.type == "unsubscribe"

    message = parse_user_msg("""
    <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[fromUser]]></FromUserName>
        <CreateTime>123456789</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[CLICK]]></Event>
        <EventKey><![CDATA[EVENTKEY]]></EventKey>
    </xml>
    """)
    assert message.target == "toUser"
    assert message.source == "fromUser"
    assert message.time == 123456789
    assert message.type == "click"
    assert message.key == "EVENTKEY"
Exemplo n.º 28
0
    def GET(self):
        #self.token = WEIXIN_TOKEN
        #self.werobot = WeRoBot(self.token, self.request, self.response)
        #self.werobot.text(Home.text)
        #self.werobot.test()

        #logging.info('This is info info')
        #logging.warning('This is info warning')
        #logging.error('This is info error')
        #logging.debug('This is info debug')
        from werobot.messages import TextMessage
        t = testxml
        self.write(Home.text(parse_user_msg(t)))
Exemplo n.º 29
0
        def handle(t):
            if not self.check_signature(request.query.timestamp, request.query.nonce, request.query.signature):
                return abort(403)

            body = request.body.read()
            message = parse_user_msg(body)
            logging.info("Receive message %s" % message)
            reply = self.get_reply(message)
            if not reply:
                self.logger.warning("No handler responded message %s" % message)
                return ""
            response.content_type = "application/xml"
            return create_reply(reply, message=message)
Exemplo n.º 30
0
def test_unknown_event():
    message = parse_user_msg("""
    <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[FromUser]]></FromUserName>
        <CreateTime>123456789</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[unknown]]></Event>
    </xml>
    """)
    assert message.target == "toUser"
    assert message.source == "FromUser"
    assert message.time == 123456789
    assert message.type == "unknown_event"
Exemplo n.º 31
0
def test_shortvideo():
    @werobot.shortvideo
    def shortvideo():
        return '请收下这一段榴莲的小视频'

    message = parse_user_msg("""
        <xml>
            <ToUserName><![CDATA[toUser]]></ToUserName>
            <FromUserName><![CDATA[fromUser]]></FromUserName>
            <CreateTime>1357290913</CreateTime>
            <MsgType><![CDATA[shortvideo]]></MsgType>
            <MediaId><![CDATA[media_id]]></MediaId>
            <ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>
            <MsgId>1234567890123456</MsgId>
        </xml>
    """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'请收下这一段榴莲的小视频'
Exemplo n.º 32
0
def test_submit_membercard_user_info_handler():
    @werobot.submit_membercard_user_info
    def submit_membercard_user_info():
        return '现在醒一醒还来得及'

    message = parse_user_msg("""
            <xml>
                <ToUserName> <![CDATA[gh_3fcea188bf78]]></ToUserName>
                <FromUserName><![CDATA[obLatjlaNQKb8FqOvt1M1x1lIBFE]]></FromUserName>
                <CreateTime>1432668700</CreateTime>
                <MsgType><![CDATA[event]]></MsgType>
                <Event><![CDATA[submit_membercard_user_info]]></Event>
                <CardId><![CDATA[pbLatjtZ7v1BG_ZnTjbW85GYc_E8]]></CardId>
                <UserCardCode><![CDATA[018255396048]]></UserCardCode>
            </xml>
        """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'现在醒一醒还来得及'
Exemplo n.º 33
0
def test_voice_message():
    message = parse_user_msg("""
    <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[fromUser]]></FromUserName>
        <CreateTime>1357290913</CreateTime>
        <MsgType><![CDATA[voice]]></MsgType>
        <MediaId><![CDATA[media_id]]></MediaId>
        <Format><![CDATA[Format]]></Format>
        <Recognition><![CDATA[Meow~]]></Recognition>
        <MsgId>1234567890123456</MsgId>
    </xml>
    """)
    assert message.target == "toUser"
    assert message.source == "fromUser"
    assert message.time == 1357290913
    assert message.type == "voice"
    assert message.media_id == "media_id"
    assert message.format == "Format"
    assert message.recognition == "Meow~"
    assert message.message_id == 1234567890123456
Exemplo n.º 34
0
def test_card_not_pass_check_handler():
    @werobot.card_not_pass_check
    def card_pass_not_check():
        return '瞧一瞧没过'

    message = parse_user_msg("""
        <xml>
            <ToUserName><![CDATA[toUser]]></ToUserName>
            <FromUserName><![CDATA[FromUser]]></FromUserName>
            <CreateTime>123456789</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[card_not_pass_check]]></Event>
            <CardId><![CDATA[cardid]]></CardId>
            <RefuseReason><![CDATA[非法代制]]></RefuseReason>
        </xml>
    """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'瞧一瞧没过'
Exemplo n.º 35
0
 def werobot_view(request):
     timestamp = request.GET.get("timestamp", "")
     nonce = request.GET.get("nonce", "")
     signature = request.GET.get("signature", "")
     if not robot.check_signature(
         timestamp=timestamp,
         nonce=nonce,
         signature=signature
     ):
         return HttpResponseForbidden()
     if request.method == "GET":
         return HttpResponse(request.GET.get("echostr", ""))
     elif request.method == "POST":
         body = request.body
         message = parse_user_msg(body)
         reply = robot.get_reply(message)
         return HttpResponse(
             create_reply(reply, message=message),
             content_type="application/xml;charset=utf-8"
         )
     return HttpResponseNotAllowed(['GET', 'POST'])
Exemplo n.º 36
0
def test_link_message():
    message = parse_user_msg("""
    <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[fromUser]]></FromUserName>
        <CreateTime>1351776360</CreateTime>
        <MsgType><![CDATA[link]]></MsgType>
        <Title><![CDATA[WeRoBot]]></Title>
        <Description><![CDATA[Link to WeRoBot]]></Description>
        <Url><![CDATA[https://github.com/whtsky/WeRoBot]]></Url>
        <MsgId>1234567890123456</MsgId>
    </xml>
    """)
    assert message.target == "toUser"
    assert message.source == "fromUser"
    assert message.time == 1351776360
    assert message.type == "link"
    assert message.title == "WeRoBot"
    assert message.description == "Link to WeRoBot"
    assert message.url == "https://github.com/whtsky/WeRoBot"
    assert message.message_id == 1234567890123456
Exemplo n.º 37
0
def test_user_enter_session_from_card_handler():
    @werobot.user_enter_session_from_card
    def user_enter_session_from_card():
        return '退货是不可能退货的'

    message = parse_user_msg("""
            <xml>
                <ToUserName><![CDATA[toUser]]></ToUserName>
                <FromUserName><![CDATA[FromUser]]></FromUserName>
                <CreateTime>123456789</CreateTime>
                <MsgType><![CDATA[event]]></MsgType>
                <Event><![CDATA[user_enter_session_from_card]]></Event>
                <CardId><![CDATA[cardid]]></CardId>
                <UserCardCode><![CDATA[12312312]]></UserCardCode>
            </xml>
        """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'退货是不可能退货的'
Exemplo n.º 38
0
def test_scan_handler():
    @werobot.scan
    def scan(message):
        return '扫描'

    message = parse_user_msg("""
        <xml>
            <ToUserName><![CDATA[toUser]]></ToUserName>
            <FromUserName><![CDATA[FromUser]]></FromUserName>
            <CreateTime>123456789</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[SCAN]]></Event>
            <EventKey><![CDATA[SCENE_VALUE]]></EventKey>
            <Ticket><![CDATA[TICKET]]></Ticket>
        </xml>
        """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'扫描'
Exemplo n.º 39
0
def test_location_message():
    message = parse_user_msg("""
    <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[fromUser]]></FromUserName>
        <CreateTime>1351776360</CreateTime>
        <MsgType><![CDATA[location]]></MsgType>
        <Location_X>23.134521</Location_X>
        <Location_Y>113.358803</Location_Y>
        <Scale>20</Scale>
        <Label><![CDATA[Location]]></Label>
        <MsgId>1234567890123456</MsgId>
    </xml>
    """)
    assert message.target == 'toUser'
    assert message.source == 'fromUser'
    assert message.time == 1351776360
    assert message.type == 'location'
    assert message.location == (23.134521, 113.358803)
    assert message.scale == 20
    assert message.label == 'Location'
Exemplo n.º 40
0
def test_card_sku_remind_handler():
    @werobot.card_sku_remind
    def card_sku_remind():
        return '骗钱大成功'

    message = parse_user_msg("""
            <xml>
                <ToUserName><![CDATA[gh_2d62d*****0]]></ToUserName>
                <FromUserName><![CDATA[oa3LFuBvWb7*********]]></FromUserName>
                <CreateTime>1443838506</CreateTime>
                <MsgType><![CDATA[event]]></MsgType>
                <Event><![CDATA[card_sku_remind]]></Event>
                <CardId><![CDATA[pa3LFuAh2P65**********]]></CardId>
                <Detail><![CDATA[the card's quantity is equal to 0]]></Detail>
            </xml>
        """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'骗钱大成功'
Exemplo n.º 41
0
def test_location_event_handler():
    @werobot.location_event
    def location_event(message):
        return '位置喵喵'

    message = parse_user_msg("""
        <xml>
            <ToUserName><![CDATA[toUser]]></ToUserName>
            <FromUserName><![CDATA[fromUser]]></FromUserName>
            <CreateTime>123456789</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[LOCATION]]></Event>
            <Latitude>23.137466</Latitude>
            <Longitude>113.352425</Longitude>
            <Precision>119.385040</Precision>
        </xml>
        """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'位置喵喵'
Exemplo n.º 42
0
def test_update_member_card_event():
    message = parse_user_msg("""
        <xml>
            <ToUserName><![CDATA[gh_9e1765b5568e]]></ToUserName>
            <FromUserName><![CDATA[ojZ8YtyVyr30HheH3CM73y7h4jJE]]></FromUserName>
            <CreateTime>1445507140</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[update_member_card]]></Event>
            <CardId><![CDATA[pjZ8Ytx-nwvpCRyQneH3Ncmh6N94]]></CardId>
            <UserCardCode><![CDATA[485027611252]]></UserCardCode>
            <ModifyBonus>3</ModifyBonus>
            <ModifyBalance>0</ModifyBalance>
        </xml>
    """)
    assert message.target == "gh_9e1765b5568e"
    assert message.source == "ojZ8YtyVyr30HheH3CM73y7h4jJE"
    assert message.time == 1445507140
    assert message.type == "update_member_card_event"
    assert message.card_id == "pjZ8Ytx-nwvpCRyQneH3Ncmh6N94"
    assert message.user_card_code == "485027611252"
    assert message.modify_bonus == 3
    assert message.modify_balance == 0
Exemplo n.º 43
0
def test_user_scan_product_async_event():
    message = parse_user_msg("""
    <xml>
        <ToUserName><![CDATA[gh_fbe8a958756e]]></ToUserName>
        <FromUserName><![CDATA[otAzGjrS4AYCmeJM1GhEOcHXXTAo]]></FromUserName>
        <CreateTime>1434541327</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[user_scan_product_async]]></Event>
        <KeyStandard><![CDATA[qrcode]]></KeyStandard>
        <KeyStr><![CDATA[lincolntest2]]></KeyStr>
        <ExtInfo><![CDATA[123]]></ExtInfo>
        <RegionCode><![CDATA[440105]]></RegionCode>
    </xml>
    """)
    assert message.target == "gh_fbe8a958756e"
    assert message.source == "otAzGjrS4AYCmeJM1GhEOcHXXTAo"
    assert message.time == 1434541327
    assert message.type == "user_scan_product_async_event"
    assert message.key_standard == "qrcode"
    assert message.key_str == "lincolntest2"
    assert message.ext_info == "123"
    assert message.region_code == "440105"
Exemplo n.º 44
0
def test_user_scan_product_verify_action_event():
    message = parse_user_msg("""
    <xml>
        <ToUserName><![CDATA[gh_404e58ec528e]]></ToUserName>
        <FromUserName><![CDATA[od_ikt8qi21-hVTtYgm8xSfTLH5w]]></FromUserName>
        <CreateTime>1450429257</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[user_scan_product_verify_action]]></Event>
        <KeyStandard><![CDATA[ean13]]></KeyStandard>
        <KeyStr><![CDATA[6901481811083]]></KeyStr>
        <Result><![CDATA[verify_ok]]></Result>
        <ReasonMsg><![CDATA[]]></ReasonMsg>
    </xml>
    """)
    assert message.target == "gh_404e58ec528e"
    assert message.source == "od_ikt8qi21-hVTtYgm8xSfTLH5w"
    assert message.time == 1450429257
    assert message.type == "user_scan_product_verify_action_event"
    assert message.key_standard == "ean13"
    assert message.key_str == "6901481811083"
    assert message.result == "verify_ok"
    assert message.reason_msg is None
Exemplo n.º 45
0
def test_voice():
    @werobot.voice
    def voice(message):
        return '声音喵'

    message = parse_user_msg("""
        <xml>
            <ToUserName><![CDATA[toUser]]></ToUserName>
            <FromUserName><![CDATA[fromUser]]></FromUserName>
            <CreateTime>1357290913</CreateTime>
            <MsgType><![CDATA[voice]]></MsgType>
            <MediaId><![CDATA[media_id]]></MediaId>
            <Format><![CDATA[Format]]></Format>
            <Recognition><![CDATA[Meow~]]></Recognition>
            <MsgId>1234567890123456</MsgId>
        </xml>
        """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'声音喵'
Exemplo n.º 46
0
def test_user_scan_product_enter_session():
    @werobot.user_scan_product_enter_session
    def user_scan_product_enter_session():
        return '再次打扰了'

    message = parse_user_msg("""
        <xml>
            <ToUserName><![CDATA[gh_fbe8a958756e]]></ToUserName>
            <FromUserName><![CDATA[otAzGjrS4AYCmeJM1GhEOcHXXTAo]]></FromUserName>
            <CreateTime>1433259128</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[user_scan_product_enter_session]]></Event>
            <KeyStandard><![CDATA[ena13]]></KeyStandard>
            <KeyStr><![CDATA[6954767461373]]></KeyStr>
            <ExtInfo><![CDATA[]]></ExtInfo>
        </xml>
    """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'再次打扰了'
Exemplo n.º 47
0
def test_unknown():
    @werobot.unknown
    def unknown(message):
        return '不知道喵'

    message = parse_user_msg("""
        <xml>
            <ToUserName><![CDATA[toUser]]></ToUserName>
            <FromUserName><![CDATA[fromUser]]></FromUserName>
            <CreateTime>1351776360</CreateTime>
            <MsgType><![CDATA[unknown]]></MsgType>
            <Title><![CDATA[WeRoBot]]></Title>
            <Description><![CDATA[Link to WeRoBot]]></Description>
            <Url><![CDATA[https://github.com/whtsky/WeRoBot]]></Url>
            <MsgId>1234567890123456</MsgId>
        </xml>
        """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'不知道喵'
Exemplo n.º 48
0
def test_scan_waitmsg_event():
    message = parse_user_msg("""
    <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[FromUser]]></FromUserName>
        <CreateTime>123456789</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[scancode_waitmsg]]></Event>
        <EventKey><![CDATA[EVENTKEY]]></EventKey>
        <ScanCodeInfo>
            <ScanType><![CDATA[qrcode]]></ScanType>
            <ScanResult><![CDATA[www.qq.com]]></ScanResult>
        </ScanCodeInfo>
    </xml>
    """)
    assert message.target == "toUser"
    assert message.source == "FromUser"
    assert message.time == 123456789
    assert message.type == "scancode_waitmsg_event"
    assert message.key == "EVENTKEY"
    assert message.scan_type == "qrcode"
    assert message.scan_result == "www.qq.com"
Exemplo n.º 49
0
def test_user_view_card_handler():
    @werobot.user_view_card
    def user_view_card():
        return '我就瞧一瞧,不买'

    message = parse_user_msg("""
            <xml>
                <ToUserName> <![CDATA[gh_fcxxxx6a20993]]> </ToUserName>
                <FromUserName> <![CDATA[oZI8Fj040-xxxxx6gkoPOQTQ]]> </FromUserName>
                <CreateTime>1467811138</CreateTime>
                <MsgType> <![CDATA[event]]> </MsgType>
                <Event> <![CDATA[user_view_card]]> </Event>
                <CardId> <![CDATA[pZI8Fj2ezBbxxxxxT2UbiiWLb7Bg]]> </CardId>
                <UserCardCode> <![CDATA[4xxxxxxxx8558]]> </UserCardCode>
                <OuterStr> <![CDATA[12b]]> </OuterStr>
            </xml>
        """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'我就瞧一瞧,不买'
Exemplo n.º 50
0
def test_card_pay_order_event():
    message = parse_user_msg("""
        <xml>
            <ToUserName><![CDATA[gh_7223c83d4be5]]></ToUserName>
            <FromUserName><![CDATA[ob5E7s-HoN9tslQY3-0I4qmgluHk]]></FromUserName>
            <CreateTime>1453295737</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[card_pay_order]]></Event>
            <OrderId><![CDATA[404091456]]></OrderId>
            <Status><![CDATA[ORDER_STATUS_FINANCE_SUCC]]></Status>
            <CreateOrderTime>1453295737</CreateOrderTime>
            <PayFinishTime>0</PayFinishTime>
            <Desc><![CDATA[]]></Desc>
            <FreeCoinCount><![CDATA[200]]></FreeCoinCount>
            <PayCoinCount><![CDATA[0]]></PayCoinCount>
            <RefundFreeCoinCount><![CDATA[0]]></RefundFreeCoinCount>
            <RefundPayCoinCount><![CDATA[0]]></RefundPayCoinCount>
            <OrderType><![CDATA[ORDER_TYPE_SYS_ADD]]></OrderType>
            <Memo><![CDATA[开通账户奖励]]></Memo>
            <ReceiptInfo><![CDATA[]]></ReceiptInfo>
        </xml>
    """)
    assert message.target == "gh_7223c83d4be5"
    assert message.source == "ob5E7s-HoN9tslQY3-0I4qmgluHk"
    assert message.time == 1453295737
    assert message.type == "card_pay_order_event"
    assert message.order_id == "404091456"
    assert message.status == "ORDER_STATUS_FINANCE_SUCC"
    assert message.create_order_time == 1453295737
    assert message.pay_finish_time == 0
    assert message.desc is None
    assert message.free_coin_count == "200"
    assert message.pay_coin_count == "0"
    assert message.refund_free_coin_count == "0"
    assert message.refund_pay_coin_count == "0"
    assert message.order_type == "ORDER_TYPE_SYS_ADD"
    assert message.memo == u"开通账户奖励"
    assert message.receipt_info is None
Exemplo n.º 51
0
def test_update_member_card_handler():
    @werobot.update_member_card
    def update_member_card():
        return '冲动消费导致余额减少'

    message = parse_user_msg("""
            <xml>
                <ToUserName><![CDATA[gh_9e1765b5568e]]></ToUserName>
                <FromUserName><![CDATA[ojZ8YtyVyr30HheH3CM73y7h4jJE]]></FromUserName>
                <CreateTime>1445507140</CreateTime>
                <MsgType><![CDATA[event]]></MsgType>
                <Event><![CDATA[update_member_card]]></Event>
                <CardId><![CDATA[pjZ8Ytx-nwvpCRyQneH3Ncmh6N94]]></CardId>
                <UserCardCode><![CDATA[485027611252]]></UserCardCode>
                <ModifyBonus>3</ModifyBonus>
                <ModifyBalance>0</ModifyBalance>
            </xml>
        """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'冲动消费导致余额减少'
Exemplo n.º 52
0
def test_location():
    @werobot.location
    def location(message):
        return '地理位置汪'

    message = parse_user_msg("""
        <xml>
            <ToUserName><![CDATA[toUser]]></ToUserName>
            <FromUserName><![CDATA[fromUser]]></FromUserName>
            <CreateTime>1351776360</CreateTime>
            <MsgType><![CDATA[location]]></MsgType>
            <Location_X>23.134521</Location_X>
            <Location_Y>113.358803</Location_Y>
            <Scale>20</Scale>
            <Label><![CDATA[Location]]></Label>
            <MsgId>1234567890123456</MsgId>
        </xml>
        """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'地理位置汪'
Exemplo n.º 53
0
def test_user_scan_product_async():
    @werobot.user_scan_product_async
    def user_scan_product_async():
        return '异步的地理位置喵'

    message = parse_user_msg("""
        <xml>
            <ToUserName><![CDATA[gh_fbe8a958756e]]></ToUserName>
            <FromUserName><![CDATA[otAzGjrS4AYCmeJM1GhEOcHXXTAo]]></FromUserName>
            <CreateTime>1434541327</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[user_scan_product_async]]></Event>
            <KeyStandard><![CDATA[qrcode]]></KeyStandard>
            <KeyStr><![CDATA[lincolntest2]]></KeyStr>
            <ExtInfo><![CDATA[123]]></ExtInfo>
            <RegionCode><![CDATA[440105]]></RegionCode>
        </xml>
    """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'异步的地理位置喵'
Exemplo n.º 54
0
def test_user_scan_product_verify_action():
    @werobot.user_scan_product_verify_action
    def user_scan_product_verify_action():
        return '审核通过了喵'

    message = parse_user_msg("""
        <xml>
            <ToUserName><![CDATA[gh_404e58ec528e]]></ToUserName>
            <FromUserName><![CDATA[od_ikt8qi21-hVTtYgm8xSfTLH5w]]></FromUserName>
            <CreateTime>1450429257</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[user_scan_product_verify_action]]></Event>
            <KeyStandard><![CDATA[ean13]]></KeyStandard>
            <KeyStr><![CDATA[6901481811083]]></KeyStr>
            <Result><![CDATA[verify_ok]]></Result>
            <ReasonMsg><![CDATA[]]></ReasonMsg>
        </xml>
    """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'审核通过了喵'
Exemplo n.º 55
0
def test_render_text_reply():
    message = parse_user_msg("""
        <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[fromUser]]></FromUserName>
        <CreateTime>1348831860</CreateTime>
        <MsgType><![CDATA[image]]></MsgType>
        <PicUrl><![CDATA[this is a url]]></PicUrl>
        <MediaId><![CDATA[media_id]]></MediaId>
        <MsgId>1234567890123456</MsgId>
        </xml>
    """)
    t = int(time.time())
    reply = TextReply(message=message, content="aa", time=t)
    reply.render().strip() == """
    <xml>
    <ToUserName><![CDATA[fromUser]]></ToUserName>
    <FromUserName><![CDATA[toUser]]></FromUserName>
    <CreateTime>{time}</CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[aa]]></Content>
    <FuncFlag>0</FuncFlag>
    </xml>""".format(time=t).strip()
Exemplo n.º 56
0
        def handle(t):
            wxcpt = WXBizMsgCrypt(self.config["TOKEN"],
                                  self.config["ENCODING_AES_KEY"],
                                  self.config["CORP_ID"])
            ret, sMsg = wxcpt.DecryptMsg(request.body.read(),
                                         request.query.msg_signature,
                                         request.query.timestamp,
                                         request.query.nonce)
            if (ret != 0):
                return abort(403)

            message = parse_user_msg(sMsg)
            logging.info("Receive message %s" % message)
            reply = self.get_reply(message)
            if not reply:
                self.logger.warning("No handler responded message %s" %
                                    message)
                return ''
            response.content_type = 'application/xml'
            ret, sEncryptMsg = wxcpt.EncryptMsg(
                create_reply(reply, message=message), request.query.nonce)
            if (ret != 0):
                return abort(403)
            return sEncryptMsg
Exemplo n.º 57
0
def test_user_gifting_card_handler():
    @werobot.user_gifting_card
    def user_gifting_card():
        return '锅从天上来'

    message = parse_user_msg("""
            <xml>
                <ToUserName><![CDATA[gh_3fcea188bf78]]></ToUserName>
                <FromUserName><![CDATA[obLatjjwDolFjRRd3doGIdwNqRXw]]></FromUserName>
                <CreateTime>1474181868</CreateTime>
                <MsgType><![CDATA[event]]></MsgType>
                <Event><![CDATA[user_gifting_card]]></Event>
                <CardId><![CDATA[pbLatjhU-3pik3d4PsbVzvBxZvJc]]></CardId>
                <UserCardCode><![CDATA[297466945104]]></UserCardCode>
                <IsReturnBack>0</IsReturnBack>
                <FriendUserName><![CDATA[obLatjlNerkb62HtSdQUx66C4NTU]]></FriendUserName>
                <IsChatRoom>0</IsChatRoom>
            </xml>
        """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'锅从天上来'
Exemplo n.º 58
0
def test_user_gifting_card_event():
    message = parse_user_msg("""
        <xml>
            <ToUserName><![CDATA[gh_3fcea188bf78]]></ToUserName>
            <FromUserName><![CDATA[obLatjjwDolFjRRd3doGIdwNqRXw]]></FromUserName>
            <CreateTime>1474181868</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[user_gifting_card]]></Event>
            <CardId><![CDATA[pbLatjhU-3pik3d4PsbVzvBxZvJc]]></CardId>
            <UserCardCode><![CDATA[297466945104]]></UserCardCode>
            <IsReturnBack>0</IsReturnBack>
            <FriendUserName><![CDATA[obLatjlNerkb62HtSdQUx66C4NTU]]></FriendUserName>
            <IsChatRoom>0</IsChatRoom>
        </xml>
    """)
    assert message.target == "gh_3fcea188bf78"
    assert message.source == "obLatjjwDolFjRRd3doGIdwNqRXw"
    assert message.time == 1474181868
    assert message.type == "user_gifting_card_event"
    assert message.card_id == "pbLatjhU-3pik3d4PsbVzvBxZvJc"
    assert message.user_card_code == "297466945104"
    assert message.is_return_back == 0
    assert message.friend_user_name == "obLatjlNerkb62HtSdQUx66C4NTU"
    assert message.is_chat_room == 0
Exemplo n.º 59
0
def test_scan_waitmsg_handler():
    @werobot.scancode_waitmsg
    def scancode_waitmsg(message):
        return '扫描弹消息'

    message = parse_user_msg("""
        <xml>
            <ToUserName><![CDATA[toUser]]></ToUserName>
            <FromUserName><![CDATA[FromUser]]></FromUserName>
            <CreateTime>123456789</CreateTime>
            <MsgType><![CDATA[event]]></MsgType>
            <Event><![CDATA[scancode_waitmsg]]></Event>
            <EventKey><![CDATA[EVENTKEY]]></EventKey>
            <ScanCodeInfo>
                <ScanType><![CDATA[qrcode]]></ScanType>
                <ScanResult><![CDATA[http://www.qq.com]]></ScanResult>
            </ScanCodeInfo>
        </xml>
        """)

    reply = werobot.get_reply(message)

    assert isinstance(reply, TextReply)
    assert reply._args['content'] == u'扫描弹消息'
Exemplo n.º 60
0
def test_none_message():
    assert not parse_user_msg("")