def test_message_callback(monkeypatch): import requests mock_request(monkeypatch, requests) threads = bot.callback({u'entry': [{u'id': u'1878285869108664', u'messaging': [ {u'message': { u'mid': u'mid.$cAAb2vDmGvz1haB8LUlbNtnB5GBUd', u'seq': 998, u'text': u'a'}, u'recipient': { u'id': u'1878285869108664'}, u'sender': { u'id': u'1409602299103010'}, u'timestamp': 1491273892690}], u'time': 1491273892877}], u'object': u'page'}) for t in threads: t.join() args = requests.post.call_args_list assert len(args) == 3, "args must dashbot_in,dashbot_out but {}".format( args) assert requests.post.call_args_list[1][0][ 0] == 'https://graph.facebook.com/v2.6/me/messages?access_token=dummy', 'expect one out message but {}'.format( requests.post.call_args_list)
def test_match_last_matched(mocker, monkeypatch): fix_matcher_rand(mocker) from chatty.domain.user_state import ( User, ) from chatty.domain.model.match import Language, MatchingQueue import requests mock_request(monkeypatch, requests) session = Session() english = (session.query(Language).get('en_US') or Language(id='en_US', name='English')) user = User('TEST-01') user.languages = [english] partner = User('TEST-02') partner.languages = [english] session.add(user) session.add(partner) session.commit() MatchingQueue.push(partner) assert MatchingQueue.match_partner(user) == partner MatchingQueue.remove(partner) user.partner = None user.last_matched = partner.id MatchingQueue.push(partner) assert MatchingQueue.match_partner(user) == None user.last_matched = "other" assert MatchingQueue.match_partner(user) == partner
def test_message_callback(monkeypatch): app = BetterTestApp(server.app) import requests mock_request(monkeypatch, requests) res = app.post_json( "/bot/fb/callback", { u'entry': [{ u'id': u'1878285869108664', u'messaging': [{ u'message': { u'mid': u'mid.$cAAb2vDmGvz1haB8LUlbNtnB5GBUd', u'seq': 998, u'text': u'a' }, u'recipient': { u'id': u'1878285869108664' }, u'sender': { u'id': u'1409602299103010' }, u'timestamp': 1491273892690 }], u'time': 1491273892877 }], u'object': u'page' }) assert res.status_int == 200
def test_match_location(mocker, monkeypatch): fix_matcher_rand(mocker) from chatty.domain.user_state import ( User, ) from chatty.domain.model.match import Language import requests mock_request(monkeypatch, requests) session = Session() english = (session.query(Language).get('en_US') or Language(id='en_US', name='English')) user1 = User('TEST-01') user1.languages = [english] user2 = User('TEST-02') user2.languages = [english] session.add(user1) session.add(user2) session.commit() cache = dict(my_langs=set([english])) mat = matcher.get() # default, both don't use location assert mat.should_match(user1, user2, cache, True) user1.location_enabled = True # user1 wanna use location but don't have, so ignore. # user2 doesn't care location. match. assert mat.should_match(user1, user2, cache, True) # at TokyoTocho user1.current_longitude = 139.4130 user1.current_latitude = 35.4122 assert not mat.should_match(user1, user2, cache, True) assert mat.should_match(user1, user2, cache, False) # at OsakaShiyakusho user2.current_longitude = 135.3008 user2.current_latitude = 34.4138 # user2 is in user1 radius 400, and user2 doesn't care radius assert mat.should_match(user1, user2, cache, True) user2.location_enabled = True assert mat.should_match(user1, user2, cache, True) user2.search_radius = 100 assert not mat.should_match(user1, user2, cache, True) assert mat.should_match(user1, user2, cache, False) user1.current_longitude = 136.3008 user1.current_latitude = 34.4138 assert mat.should_match(user1, user2, cache, True)
def test_match_gender(mocker, monkeypatch): fix_matcher_rand(mocker) from chatty.domain.user_state import ( User, ) from chatty.domain.model.match import Language, MatchingQueue import requests mock_request(monkeypatch, requests) session = Session() english = (session.query(Language).get('en_US') or Language(id='en_US', name='English')) user = User('TEST-01') user.languages = [english] user.gender = "male" partner = User('TEST-02') partner.languages = [english] partner.gender = "female" partner.finding_genders = "male,female" user.finding_genders = "female" session.add(user) session.add(partner) session.commit() MatchingQueue.push(partner) assert MatchingQueue.match_partner(user) == partner MatchingQueue.remove(partner) user.partner = None MatchingQueue.push(partner) user.finding_genders = "male" assert MatchingQueue.match_partner(user) == None user.finding_genders = "male,female" assert MatchingQueue.match_partner(user) == partner user.partner = None MatchingQueue.remove(partner) MatchingQueue.push(partner) partner.finding_genders = "female" assert MatchingQueue.match_partner(user) == None user.gender = "undefined" assert MatchingQueue.match_partner(user) == None
def test_user_location(mocker, monkeypatch): fix_matcher_rand(mocker) from chatty.domain.user_state import ( User, ) import requests mock_request(monkeypatch, requests) user1 = User('TEST-01') assert user1.has_location() == False # at TokyoTocho user1.current_longitude = 139.4130 assert user1.has_location() == False user1.current_latitude = 35.4122 assert user1.has_location() == True user2 = User('TEST-02') # at OsakaShiyakusho user2.current_longitude = 135.3008 user2.current_latitude = 34.4138 # about 400km distance between tokyo and osaka assert abs(user1.distance(user2) - 400) < 10
def test_post_settings(monkeypatch): import requests mock_request(monkeypatch, requests) app = BetterTestApp(server.app) res = app.post( "/VVaEZ1RQh3EvlGJqj7m1NiD6oF3UrxAM8c1oeWh1UQ0=/settings", { "languages": ["en_US", "fr_FR"], "genders": ["male", "female"], "longitude": 139.11, "latitude": 35.11, "location_enabled": "true", "search_radius": 350 }) assert res.status == "200 OK" user = User.get("1183739191739808") assert user.languages[0] == Language.get("en_US") assert user.languages[1] == Language.get("fr_FR") assert len(user.languages) == 2 assert user.finding_genders == "male,female" assert user.current_longitude == 139.11 assert user.current_latitude == 35.11 assert user.location_enabled == True assert user.search_radius == 350 res = app.post("/VVaEZ1RQh3EvlGJqj7m1NiD6oF3UrxAM8c1oeWh1UQ0=/settings", { "languages": ["en_US"], "genders": ["male"], "location_enabled": "false", }) user = User.get("1183739191739808") assert res.status == "200 OK" assert user.languages[0] == Language.get("en_US") assert len(user.languages) == 1 assert user.finding_genders == "male" assert user.location_enabled == False
def test_state_transition(mocker, monkeypatch): """状態遷移のテスト""" fix_matcher_rand(mocker) from chatty.domain.user_state import ( User, IdleUserState, SearchingUserState, TalkingUserState, ) from chatty.domain.model.match import Language import requests mock_request(monkeypatch, requests) # session session = model.Session() # ユーザーの言語設定を行う english = (session.query(Language).get('en_US') or Language(id='en_US', name='English')) user1 = User('TEST-01') user1.languages = [english] user2 = User('TEST-02', 'TEST-02') user2.languages = [english] user3 = User('TEST-03', 'TEST-03') user3.languages = [english] session.add(user1) session.add(user2) session.commit() user1_c = UserContext(user1) user2_c = UserContext(user2) user3_c = UserContext(user3) # At first, must be NoSignUpIdle assert isinstance(user1_c.status, NoSignUpIdleUserState) user1.fb_id = "1234" assert isinstance(user1_c.status, IdleUserState) # ユーザをサーチ状態にする user1_c.do_search(None) session.commit() assert isinstance(user1_c.status, SearchingUserState) # ユーザーを会話状態にする user2_c.do_search(None) session.commit() assert isinstance(user1_c.status, TalkingUserState) assert isinstance(user2_c.status, TalkingUserState) # 会話を終了する user2_c.end_conversation_and_search(None) session.commit() assert isinstance(user1_c.status, IdleUserState) assert isinstance(user2_c.status, SearchingUserState) # not match same tuple user1_c.do_search(None) session.commit() assert isinstance(user1_c.status, SearchingUserState) assert isinstance(user2_c.status, SearchingUserState) # match with pother partners. user3_c.do_search(None) session.commit() assert isinstance(user1_c.status, SearchingUserState) assert isinstance(user2_c.status, TalkingUserState) assert isinstance(user3_c.status, TalkingUserState) # 会話を終了する user2_c.end_conversation(None) session.commit() assert isinstance(user1_c.status, SearchingUserState) assert isinstance(user2_c.status, IdleUserState) assert isinstance(user3_c.status, IdleUserState) user1_c.cancel_search(None) session.commit() assert isinstance(user1_c.status, IdleUserState)
def test_user_state_check_error(mocker, monkeypatch, caplog): fix_matcher_rand(mocker) from chatty.domain.user_state import ( User, IdleUserState, SearchingUserState, TalkingUserState, ) from chatty.domain.model.match import Language import requests from chatty.util.fb import requests as fbreq mock_request(monkeypatch, requests) mock_request(monkeypatch, fbreq) # session session = model.Session() # ユーザーの言語設定を行う english = (session.query(Language).get('en_US') or Language(id='en_US', name='English')) user = User('TEST-01', 'TEST-01') user.languages = [english] partner = User('TEST-02', 'TEST-02') partner.languages = [english] session.add(user) session.add(partner) session.commit() user_c = UserContext(user) partner_c = UserContext(partner) # logging 設定 caplog.set_level(logging.INFO) # エラーが返ってきた場合ワーニングログを出力 mock_request(monkeypatch, fbreq, {"error": { "code": 190, "error_subcode": 1234567, }}) user_c.status.send_message('message', action.IDLE__MESSAGE) # warning log が出力されているはず for rec in caplog.records: print('log: ', rec) if (rec.levelname == 'WARNING' and rec.message.startswith('Facebook send api error:')): break else: assert False, 'Not found error message' session.commit() # メッセージ送信先がメッセージを受信できない場合は検索,会話状態から削除する # 検索状態へ normalres = { 'message_id': 'mid.1484759787552:e29cca0c16', 'recipient_id': '1261793003907488' } mock_request(monkeypatch, fbreq, normalres) user_c.do_search(None) session.commit() assert isinstance(user_c.status, SearchingUserState) mock_request( monkeypatch, fbreq, {"error": { "code": 10, "error_subcode": 2018108, }}, ) user_c.status.send_message('message', action.SEARCHING__MESSAGE) session.commit() # アイドル状態になっているはず assert isinstance(user_c.status, IdleUserState) # 会話状態へ mock_request(monkeypatch, fbreq, normalres) user_c.do_search(None) partner_c.do_search(None) session.commit() assert isinstance(user_c.status, TalkingUserState) assert isinstance(partner_c.status, TalkingUserState) def m(url, json): recipient_ = json['recipient']['id'] if recipient_ == "TEST-01": return MagicMock(json=MagicMock(return_value=normalres)) elif recipient_ == "TEST-02": return MagicMock(json=MagicMock( return_value={ "error": { "code": 200, "error_subcode": 1545041, } })) raise ValueError( "dummy recipient must be TEST-01 or TEST-02 but {}".format( recipient_)) monkeypatch.setattr(fbreq, 'post', m) user_c.status.do_message( MagicMock(message=MagicMock(text="hello", _fields=["text"]))) session.commit() # アイドル状態になっているはず assert isinstance(user_c.status, IdleUserState) assert isinstance(partner_c.status, IdleUserState)