예제 #1
0
def check_bill_exists(bill_id):
    bill = get_bill_by_id(bill_id)

    if bill is None:
        abort(400, "Bill does not exist")

    return bill
예제 #2
0
def test_add_unregistered_member_to_bill_that_was_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)

    new_user_email = "*****@*****.**"
    insert_user(User(email=new_user_email))

    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
        }, {
            "email": new_user_email,
            "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
예제 #3
0
def test_group_member_is_allowed_to_modify_bill(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)

    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)
    user3_tokens = insert_tokens(user3.email)

    group = Group(
        name="Group",
        group_members=[GroupMember(user=user1),
                       GroupMember(user=user3)])
    insert_group(group)

    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,
                 group=group,
                 members=[bill_member1, bill_member2])
    bill_id = insert_bill(bill1).id

    data = {"description": "New"}

    response = test_client.put("/bills/{}".format(bill_id),
                               headers=api_headers_bearer(
                                   user3_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 bill.description == data["description"]
예제 #4
0
def test_dont_change_date_created_of_existing_bill(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)

    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 = {"date_created": datetime.datetime.utcnow()}

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

    bill = get_bill_by_id(bill_id)

    assert response.status_code == 400
    assert json_response[
        "message"] == "Attribute date_created should not be set"
    assert bill.date_created == now
예제 #5
0
def test_error_on_bill_not_existing(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)

    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 = {"description": "New"}

    response = test_client.put("/bills/2",
                               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 == 400
    assert json_response["message"] == "Bill does not exist"
    assert bill.description == "Bill"
예제 #6
0
def test_delete_members_from_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=-3)
    bill_member2 = BillMember(user_id=user2.id, amount=-3)
    bill_member3 = BillMember(user_id=user3.id, amount=6)

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

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

    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 json_response["message"] == "Updated bill"

    assert len(bill.members) == 2

    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"]
예제 #7
0
def test_dont_change_bill_if_amounts_sum_is_not_zero(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)
    bill_member2 = BillMember(user_id=user2.id, amount=-5)

    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": -4
        }, {
            "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 == 400
    assert json_response["message"] == "Sum of amounts must be zero"
    assert len(bill.members) == 2
    assert bill.members[0].user_id == bill_member1.user_id
    assert bill.members[0].amount == bill_member1.amount
    assert bill.members[1].user_id == bill_member2.user_id
    assert bill.members[1].amount == bill_member2.amount