Ejemplo n.º 1
0
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"]
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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"])
Ejemplo n.º 4
0
def test_create_order__pending():
    user = create_user(can_sell=False)
    user_id = user["id"]
    security_id = create_security()["id"]
    create_user_request(user_id=user_id, is_buy=False)

    with pytest.raises(UnauthorizedException), patch(
        "src.services.EmailService.send_email"
    ) as email_mock:
        sell_order_service.create_order(
            user_id=user_id,
            number_of_shares=20,
            price=30,
            security_id=security_id,
            scheduler=None,
        )
        email_mock.assert_not_called()
Ejemplo n.º 5
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