def test_run_matches__cannot_buy_or_sell(): round = create_round() buy_user = create_user("1", can_buy=True) buy_user2 = create_user("2", can_buy=False) sell_user = create_user("3", can_sell=True) sell_user2 = create_user("4", can_sell=False) create_buy_order("1", round_id=round["id"], user_id=buy_user["id"]) create_buy_order("2", round_id=round["id"], user_id=buy_user2["id"]) create_sell_order("3", round_id=round["id"], user_id=sell_user["id"]) create_sell_order("4", round_id=round["id"], user_id=sell_user2["id"]) with patch("src.services.match_buyers_and_sellers") as mock_match, patch( "src.services.RoundService.get_active", return_value=round ), patch("src.services.EmailService.send_email"): match_service.run_matches() assert set(u["user_id"] for u in mock_match.call_args[0][0]) == set( [buy_user["id"]] ) assert set(u["user_id"] for u in mock_match.call_args[0][1]) == set( [sell_user["id"]] ) assert mock_match.call_args[0][2] == []
def test_get_chats_by_user_id__last_read_id_not_none(): user = create_user("00") other_party = create_user("10") chat_room = create_chat_room("01") chat = create_chat("03", chat_room_id=chat_room["id"], author_id=other_party["id"]) create_user_chat_room_association( "02", user_id=user["id"], chat_room_id=chat_room["id"], is_archived=False, last_read_id=chat["id"], ) create_user_chat_room_association("12", user_id=other_party["id"], chat_room_id=chat_room["id"]) res = chat_service.get_chats_by_user_id( user_id=user["id"], as_buyer=True, as_seller=True)["unarchived"][chat_room["id"]] assert res["last_read_id"] == chat["id"] assert res["unread_count"] == 0
def test_get_chats_by_user_id__offer_responses(): user = create_user("00") other_party = create_user("10") chat_room = create_chat_room("01") create_user_chat_room_association("02", user_id=user["id"], chat_room_id=chat_room["id"], is_archived=False) create_user_chat_room_association("12", user_id=other_party["id"], chat_room_id=chat_room["id"]) offer = create_offer("03", chat_room_id=chat_room["id"], author_id=user["id"]) resp = create_offer_response("04", offer_id=offer["id"]) res = chat_service.get_chats_by_user_id(user_id=user["id"], as_buyer=True, as_seller=True) res_room = res["unarchived"][chat_room["id"]] chat_room.pop("disband_by_user_id") chat_room.pop("disband_time") assert_dict_in(chat_room, res_room) res_chats = res_room["chats"] assert {**offer, "type": "offer"} in res_chats assert { **offer, **resp, "author_id": other_party["id"], "is_deal_closed": False, "type": "offer_response", } in res_chats
def test_get_chats_by_user_id__chats(): user = create_user("00") other_party = create_user("10") chat_room = create_chat_room("01") create_user_chat_room_association("02", user_id=user["id"], chat_room_id=chat_room["id"], is_archived=False) create_user_chat_room_association("12", user_id=other_party["id"], chat_room_id=chat_room["id"]) chat = create_chat("03", chat_room_id=chat_room["id"], author_id=user["id"]) other_chat = create_chat("13", chat_room_id=chat_room["id"], author_id=other_party["id"]) res = chat_service.get_chats_by_user_id(user_id=user["id"], as_buyer=True, as_seller=True) res_room = res["unarchived"][chat_room["id"]] chat_room.pop("disband_by_user_id") chat_room.pop("disband_time") assert_dict_in(chat_room, res_room) res_chats = res_room["chats"] assert {**chat, "type": "chat"} in res_chats assert {**other_chat, "type": "chat"} in res_chats
def test_run_matches(): round = create_round() buy_user = create_user("1") buy_user2 = create_user("2") sell_user = create_user("3") sell_user2 = create_user("4") buy_order = create_buy_order("1", round_id=round["id"], user_id=buy_user["id"]) buy_order_id = buy_order["id"] create_buy_order("2", round_id=round["id"], user_id=buy_user2["id"]) sell_order = create_sell_order("3", round_id=round["id"], user_id=sell_user["id"]) sell_order_id = sell_order["id"] create_sell_order("4", round_id=round["id"], user_id=sell_user2["id"]) with patch( "src.services.match_buyers_and_sellers", return_value=[(buy_order_id, sell_order_id)], ) as mock_match, patch( "src.services.RoundService.get_active", return_value=round ), patch( "src.services.EmailService.send_email" ) as mock_email: match_service.run_matches() mock_email.assert_has_calls( [ call([buy_user["email"]], template="match_done_has_match_buyer"), call([sell_user["email"]], template="match_done_has_match_seller"), call( [buy_user2["email"], sell_user2["email"]], template="match_done_no_match", ), ] ) assert set(u["user_id"] for u in mock_match.call_args[0][0]) == set( [buy_user["id"], buy_user2["id"]] ) assert set(u["user_id"] for u in mock_match.call_args[0][1]) == set( [sell_user["id"], sell_user2["id"]] ) assert mock_match.call_args[0][2] == [] with session_scope() as session: match = session.query(Match).one() assert match.buy_order_id == buy_order_id assert match.sell_order_id == sell_order_id assert ( session.query(UserChatRoomAssociation) .filter_by(user_id=buy_order["user_id"]) .one() .chat_room_id == session.query(UserChatRoomAssociation) .filter_by(user_id=sell_order["user_id"]) .one() .chat_room_id ) assert session.query(Round).get(round["id"]).is_concluded
def test_get_chats_by_user_id__created_at_sort(): user = create_user("00") other_party = create_user("10") chat_room = create_chat_room("01") create_user_chat_room_association("02", user_id=user["id"], chat_room_id=chat_room["id"], is_archived=False) create_user_chat_room_association("12", user_id=other_party["id"], chat_room_id=chat_room["id"]) chat = create_chat( "03", chat_room_id=chat_room["id"], author_id=user["id"], created_at=datetime.now(), ) offer = create_offer( "04", chat_room_id=chat_room["id"], author_id=user["id"], created_at=datetime.now() - timedelta(hours=1), ) resp = create_offer_response("05", offer_id=offer["id"], created_at=datetime.now() + timedelta(hours=1)) res_chats = chat_service.get_chats_by_user_id( user_id=user["id"], as_buyer=True, as_seller=True)["unarchived"][chat_room["id"]]["chats"] assert [r["id"] for r in res_chats] == [offer["id"], chat["id"], resp["id"]]
def test_get_chats_by_user_id__identities_all_revealed(): user = create_user("00") other_party = create_user("10") chat_room = create_chat_room("01") create_user_chat_room_association( "02", user_id=user["id"], chat_room_id=chat_room["id"], is_archived=False, is_revealed=True, ) create_user_chat_room_association("12", user_id=other_party["id"], chat_room_id=chat_room["id"], is_revealed=True) res_identities = chat_service.get_chats_by_user_id( user_id=user["id"], as_buyer=True, as_seller=True)["unarchived"][chat_room["id"]]["identities"] assert res_identities[user["id"]]["email"] == user["email"] assert res_identities[user["id"]]["full_name"] == user["full_name"] assert res_identities[other_party["id"]]["email"] == other_party["email"] assert res_identities[ other_party["id"]]["full_name"] == other_party["full_name"]
def test_run_matches__banned_pairs(): round = create_round() buy_user = create_user("1") buy_user2 = create_user("2") sell_user = create_user("3") sell_user2 = create_user("4") buy_user_id = buy_user["id"] sell_user_id = sell_user["id"] create_buy_order("1", round_id=round["id"], user_id=buy_user["id"]) create_buy_order("2", round_id=round["id"], user_id=buy_user2["id"]) create_sell_order("3", round_id=round["id"], user_id=sell_user["id"]) create_sell_order("4", round_id=round["id"], user_id=sell_user2["id"]) create_banned_pair(buyer_id=buy_user_id, seller_id=sell_user_id) with patch("src.services.match_buyers_and_sellers") as mock_match, patch( "src.services.RoundService.get_active", return_value=round ), patch("src.services.EmailService.send_email"): match_service.run_matches() assert set(u["user_id"] for u in mock_match.call_args[0][0]) == set( [buy_user_id, buy_user2["id"]] ) assert set(u["user_id"] for u in mock_match.call_args[0][1]) == set( [sell_user_id, sell_user2["id"]] ) assert mock_match.call_args[0][2] == [(buy_user_id, sell_user_id)]
def test_run_matches__double_sell_orders(): round = create_round() sell_user = create_user("3") sell_user2 = create_user("4") sell_order1 = create_sell_order("3", round_id=round["id"], user_id=sell_user["id"]) sell_order21 = create_sell_order( "4", round_id=round["id"], user_id=sell_user2["id"] ) sell_order22 = create_sell_order( "5", round_id=round["id"], user_id=sell_user2["id"] ) with patch("src.services.match_buyers_and_sellers") as mock_match, patch( "src.services.RoundService.get_active", return_value=round ), patch("src.services.EmailService.send_email"): match_service.run_matches() assert ( len([o for o in mock_match.call_args[0][1] if o["id"] == sell_order1["id"]]) == 2 ) assert ( len( [o for o in mock_match.call_args[0][1] if o["id"] == sell_order21["id"]] ) == 1 ) assert ( len( [o for o in mock_match.call_args[0][1] if o["id"] == sell_order22["id"]] ) == 1 )
def test_get_chats_by_user_id__latest_offer(): user = create_user("00") other_party = create_user("10") chat_room = create_chat_room("01") create_user_chat_room_association("02", user_id=user["id"], chat_room_id=chat_room["id"], is_archived=False) create_user_chat_room_association("12", user_id=other_party["id"], chat_room_id=chat_room["id"]) pending_offer = create_offer("03", chat_room_id=chat_room["id"], author_id=user["id"], offer_status="PENDING") create_offer( "04", chat_room_id=chat_room["id"], author_id=user["id"], offer_status="REJECTED", ) assert (pending_offer == chat_service.get_chats_by_user_id( user_id=user["id"], as_buyer=True, as_seller=True)["unarchived"][chat_room["id"]]["latest_offer"])
def test_get_requests(): admin = create_user("1", is_committee=True) buyer = create_user("2") seller = create_user("3") buyer_request = create_user_request(user_id=buyer["id"], is_buy=True, closed_by_user_id=None) seller_request = create_user_request(user_id=seller["id"], is_buy=False, closed_by_user_id=None) reqs = user_request_service.get_requests(subject_id=admin["id"]) assert [{ **buyer_request, **{ k: v for k, v in buyer.items() if k not in [ "id", "created_at", "updated_at" ] }, "can_buy": "UNAPPROVED", }] == reqs["buyers"] assert [{ **seller_request, **{ k: v for k, v in seller.items() if k not in [ "id", "created_at", "updated_at" ] }, "can_sell": "UNAPPROVED", }] == reqs["sellers"]
def test_get_chats_by_user_id__is_revealed(): user = create_user("00") other_party = create_user("10") chat_room = create_chat_room("01") create_user_chat_room_association( "02", user_id=user["id"], chat_room_id=chat_room["id"], is_archived=False, is_revealed=True, ) create_user_chat_room_association( "12", user_id=other_party["id"], chat_room_id=chat_room["id"], is_archived=False, is_revealed=False, ) assert chat_service.get_chats_by_user_id( user_id=user["id"], as_buyer=True, as_seller=True)["unarchived"][chat_room["id"]]["is_revealed"] assert not chat_service.get_chats_by_user_id( user_id=other_party["id"], as_buyer=True, as_seller=True)["unarchived"][chat_room["id"]]["is_revealed"]
def test_get_chats_by_user_id__buy_sell_order(): user = create_user("00") other_party = create_user("10") buy_order = create_buy_order("02", user_id=user["id"]) sell_order = create_sell_order("03", user_id=user["id"]) match = create_match("04", buy_order_id=buy_order["id"], sell_order_id=sell_order["id"]) chat_room = create_chat_room("01", match_id=match["id"]) create_user_chat_room_association("02", user_id=user["id"], chat_room_id=chat_room["id"], is_archived=False) create_user_chat_room_association("12", user_id=other_party["id"], chat_room_id=chat_room["id"]) res = chat_service.get_chats_by_user_id(user_id=user["id"], as_buyer=True, as_seller=True) res_room = res["unarchived"][chat_room["id"]] assert buy_order == res_room["buy_order"] assert sell_order == res_room["sell_order"]
def test_get_chats_by_user_id__unauthorised(): user = create_user("00", can_buy=False) other_party = create_user("10", can_sell=False) with pytest.raises(UnauthorizedException): chat_service.get_chats_by_user_id(user_id=user["id"], as_buyer=True, as_seller=False) with pytest.raises(UnauthorizedException): chat_service.get_chats_by_user_id(user_id=other_party["id"], as_buyer=False, as_seller=True)
def test_create_order__pending(): user = create_user(can_buy=False) user_id = user["id"] security_id = create_security()["id"] create_user_request(user_id=user_id, is_buy=True) round = create_round() buy_order_params = { "user_id": user_id, "number_of_shares": 20, "price": 30, "security_id": security_id, } with patch("src.services.RoundService.get_active", return_value=round), patch( "src.services.RoundService.should_round_start", return_value=False ), patch("src.services.EmailService.send_email") as email_mock: buy_order_id = buy_order_service.create_order(**buy_order_params)["id"] email_mock.assert_called_with( emails=[user["email"]], template="create_buy_order" ) with session_scope() as session: buy_order = session.query(BuyOrder).get(buy_order_id).asdict() assert_dict_in({**buy_order_params, "round_id": round["id"]}, buy_order)
def test_create__is_sell(): user_params = { "email": "*****@*****.**", "full_name": "Ben", "provider_user_id": "testing", "display_image_url": "http://blah", "is_buy": False, "auth_token": None, } committee_email = create_user(is_committee=True)["email"] with patch("src.services.EmailService.send_email") as mock: user_service.create_if_not_exists(**user_params) mock.assert_any_call(emails=[user_params["email"]], template="register_seller") mock.assert_any_call(emails=[committee_email], template="new_user_review") with session_scope() as session: user = session.query(User).filter_by(email="*****@*****.**").one().asdict() req = session.query(UserRequest).one().asdict() user_expected = user_params user_expected.pop("is_buy") user_expected.update({"can_buy": "NO", "can_sell": "UNAPPROVED"}) assert_dict_in(user_expected, user) assert_dict_in({"user_id": user["id"], "is_buy": False}, req)
def test_create_order__authorized(): user = create_user() user_id = user["id"] security_id = create_security()["id"] sell_order_params = { "user_id": user_id, "number_of_shares": 20, "price": 30, "security_id": security_id, } with patch("src.services.RoundService.get_active", return_value=None), patch( "src.services.RoundService.should_round_start", return_value=False ), patch("src.services.EmailService.send_email") as email_mock: sell_order_id = sell_order_service.create_order( **sell_order_params, scheduler=None )["id"] email_mock.assert_called_with( emails=[user["email"]], template="create_sell_order" ) with session_scope() as session: sell_order = session.query(SellOrder).get(sell_order_id).asdict() assert_dict_in({**sell_order_params, "round_id": None}, sell_order)
def test_approve_request__unauthorized(): admin = create_user(is_committee=False) with pytest.raises(InvisibleUnauthorizedException): user_request_service.approve_request( request_id=create_user_request("1")["user_id"], subject_id=admin["id"])
def test_edit_market_price__unauthorized(): security = create_security() committee = create_user(is_committee=False) with pytest.raises(UnauthorizedException): security_service.edit_market_price( id=security["id"], subject_id=committee["id"], market_price=10 )
def test_delete_order(): user_id = create_user()["id"] sell_order_id = create_sell_order(user_id=user_id)["id"] sell_order_service.delete_order(id=sell_order_id, subject_id=user_id) with session_scope() as session: assert session.query(SellOrder).filter_by(id=sell_order_id).count() == 0
def test_create_order__add_new_round(): user = create_user() security_id = create_security()["id"] user_id = user["id"] sell_order_params = { "user_id": user_id, "number_of_shares": 20, "price": 30, "security_id": security_id, } create_buy_order(round_id=None, user_id=user_id) with patch("src.services.RoundService.get_active", return_value=None), patch( "src.services.RoundService.should_round_start", return_value=False), patch( "src.services.EmailService.send_email") as email_mock: sell_order_id = sell_order_service.create_order(**sell_order_params, scheduler=None)["id"] with patch("src.services.RoundService.get_active", return_value=None), patch( "src.services.RoundService.should_round_start", return_value=True), patch( "src.services.EmailService.send_email") as email_mock: scheduler_mock = MagicMock() class SchedulerMock(BaseScheduler): shutdown = MagicMock() wakeup = MagicMock() add_job = scheduler_mock sell_order_id2 = sell_order_service.create_order( **sell_order_params, scheduler=SchedulerMock())["id"] email_mock.assert_any_call([user["email"]], template="round_opened") email_mock.assert_any_call(emails=[user["email"]], template="create_sell_order") scheduler_args = scheduler_mock.call_args assert scheduler_args[0][1] == "date" with session_scope() as session: assert scheduler_args[1]["run_date"] == session.query( Round).one().end_time sell_order = session.query(SellOrder).get(sell_order_id).asdict() sell_order2 = session.query(SellOrder).get(sell_order_id2).asdict() buy_order = session.query(BuyOrder).one().asdict() assert_dict_in(sell_order_params, sell_order) assert sell_order["round_id"] is not None assert_dict_in(sell_order_params, sell_order2) assert sell_order2["round_id"] is not None assert buy_order["round_id"] is not None
def test_ban_user(): user_id = create_user("1")["id"] user2_id = create_user("2")["id"] banned_pair_service._ban_user(my_user_id=user_id, other_user_id=user2_id) with session_scope() as session: banned_pairs = [bp.asdict() for bp in session.query(BannedPair).all()] assert len(banned_pairs) == 2 first = (banned_pairs[0] if banned_pairs[0]["buyer_id"] == user_id else banned_pairs[1]) second = (banned_pairs[1] if banned_pairs[0]["buyer_id"] == user_id else banned_pairs[0]) assert first["seller_id"] == user2_id assert second["seller_id"] == user_id
def test_get_chats_by_user_id__not_in_room(): user = create_user("00") create_chat_room("01") res = chat_service.get_chats_by_user_id(user_id=user["id"], as_buyer=True, as_seller=True) assert len(res["unarchived"]) == 0 assert len(res["archived"]) == 0
def test_get_chats_by_user_id__as_buyer_seller(): user = create_user("00") other_party = create_user("10") chat_room = create_chat_room("01") create_user_chat_room_association( "02", user_id=user["id"], chat_room_id=chat_room["id"], is_archived=False, role="BUYER", ) create_user_chat_room_association("12", user_id=other_party["id"], chat_room_id=chat_room["id"], role="SELLER") create_chat(chat_room_id=chat_room["id"], author_id=user["id"]) chat_room2 = create_chat_room("11") create_user_chat_room_association( "22", user_id=user["id"], chat_room_id=chat_room2["id"], is_archived=False, role="SELLER", ) create_user_chat_room_association("32", user_id=other_party["id"], chat_room_id=chat_room2["id"], role="BUYER") create_chat(chat_room_id=chat_room2["id"], author_id=user["id"]) res_buyer = chat_service.get_chats_by_user_id(user_id=user["id"], as_buyer=True, as_seller=False) assert len(res_buyer["unarchived"]) == 1 assert chat_room["id"] in res_buyer["unarchived"] res_seller = chat_service.get_chats_by_user_id(user_id=user["id"], as_buyer=False, as_seller=True) assert len(res_seller["unarchived"]) == 1 assert chat_room2["id"] in res_seller["unarchived"]
def test_get_chats_by_user_id__hidden_chat_rooms(): user = create_user("00") other_party = create_user("10") chat_room = create_chat_room("01") create_user_chat_room_association( "02", user_id=user["id"], chat_room_id=chat_room["id"], is_archived=False, role="BUYER", ) create_user_chat_room_association("12", user_id=other_party["id"], chat_room_id=chat_room["id"]) res = chat_service.get_chats_by_user_id(user_id=user["id"], as_buyer=True, as_seller=False) assert len(res["unarchived"]) == 0
def test_reject_request(): admin = create_user("1", is_committee=True) buyer = create_user("2", can_buy=False, can_sell=False) buy_req = create_user_request(user_id=buyer["id"], is_buy=True) user_request_service.reject_request(request_id=buy_req["id"], subject_id=admin["id"]) with session_scope() as session: assert (session.query(UserRequest).get( buy_req["id"]).closed_by_user_id == admin["id"]) assert not session.query(User).get(buy_req["user_id"]).can_buy seller = create_user("3", can_buy=False, can_sell=False) sell_req = create_user_request(user_id=seller["id"], is_buy=False) user_request_service.reject_request(request_id=sell_req["id"], subject_id=admin["id"]) with session_scope() as session: assert (session.query(UserRequest).get( sell_req["id"]).closed_by_user_id == admin["id"]) assert not session.query(User).get(sell_req["user_id"]).can_sell
def test_create_order__unauthorized(): user_id = create_user(can_buy=False)["id"] security_id = create_security()["id"] with pytest.raises(UnauthorizedException), patch( "src.services.EmailService.send_email" ) as email_mock: buy_order_service.create_order( user_id=user_id, number_of_shares=20, price=30, security_id=security_id ) email_mock.assert_not_called()
def test_get_chats_by_user_id__archived(): user = create_user("00") other_party = create_user("10") chat_room = create_chat_room("01") create_user_chat_room_association("02", user_id=user["id"], chat_room_id=chat_room["id"], is_archived=True) create_user_chat_room_association("12", user_id=other_party["id"], chat_room_id=chat_room["id"]) res = chat_service.get_chats_by_user_id(user_id=user["id"], as_buyer=True, as_seller=True) res_room = res["archived"][chat_room["id"]] chat_room.pop("disband_by_user_id") chat_room.pop("disband_time") assert_dict_in(chat_room, res_room)
def test_get_orders_by_user(): user_id = create_user()["id"] sell_order = create_sell_order("1", user_id=user_id) sell_order2 = create_sell_order("2", user_id=user_id) orders = sell_order_service.get_orders_by_user(user_id=user_id) assert len(orders) == 2 assert (sell_order == orders[0] if orders[0]["number_of_shares"] == sell_order["number_of_shares"] else orders[1]) assert (sell_order2 == orders[1] if orders[0]["number_of_shares"] == sell_order["number_of_shares"] else orders[0])
def test_edit_market_price(): security = create_security() committee = create_user(is_committee=True) security_service.edit_market_price( id=security["id"], subject_id=committee["id"], market_price=10 ) with session_scope() as session: assert session.query(Security).one().market_price == 10 security_service.edit_market_price( id=security["id"], subject_id=committee["id"], market_price=None ) with session_scope() as session: assert session.query(Security).one().market_price is None