예제 #1
0
def test_add_members_to_bill_if_bill_already_created(test_client,
                                                     api_headers_bearer,
                                                     insert_tokens):
    password = "******"
    now = datetime.datetime.utcnow()

    user1 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 confirmed=True,
                 password=password)
    insert_user(user1)
    user1_tokens = insert_tokens(user1.email)

    user2 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 confirmed=True,
                 password=password)
    insert_user(user2)

    user3 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 confirmed=True,
                 password=password)
    insert_user(user3)

    bill_member1 = BillMember(user_id=user1.id, amount="5.00")
    bill_member2 = BillMember(user_id=user2.id, amount="-5.00")

    bill1 = Bill(description="Bill",
                 date=now,
                 date_created=now,
                 members=[bill_member1, bill_member2])
    bill_id = insert_bill(bill1).id

    data = {
        "members": [{
            "user_id": user1.id,
            "amount": -3
        }, {
            "user_id": user2.id,
            "amount": -3
        }, {
            "user_id": user3.id,
            "amount": 6
        }]
    }

    response = test_client.put("/bills/{}".format(bill_id),
                               headers=api_headers_bearer(
                                   user1_tokens["access_token"]["token"]),
                               data=json.dumps(data))
    json_response = json.loads(response.get_data(as_text=True))

    bill = get_bill_by_id(bill_id)

    assert response.status_code == 200
    assert json_response["message"] == "Updated bill"

    assert len(bill.members) == 3

    assert bill.members[0].user_id == user1.id
    assert bill.members[0].amount == data["members"][0]["amount"]

    assert bill.members[1].user_id == user2.id
    assert bill.members[1].amount == data["members"][1]["amount"]

    assert bill.members[2].user_id == user3.id
    assert bill.members[2].amount == data["members"][2]["amount"]