Ejemplo n.º 1
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
Ejemplo n.º 2
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"]
Ejemplo n.º 3
0
def test_get_only_bills_from_group(test_client, api_headers_bearer,
                                   insert_tokens):
    password = "******"
    now = datetime.datetime.utcnow()

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

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

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

    bill1 = Bill(description="Bill",
                 date=now,
                 date_created=now,
                 group=group,
                 members=[
                     BillMember(user=user1, amount=3),
                     BillMember(user=user2, amount=-3)
                 ])
    insert_bill(bill1)
    bill2 = Bill(description="Bill",
                 date=now,
                 date_created=now,
                 members=[
                     BillMember(user=user1, amount=3),
                     BillMember(user=user2, amount=-3)
                 ])
    insert_bill(bill2)

    response = test_client.get("/groups/{}/bills".format(group.id),
                               headers=api_headers_bearer(
                                   user1_tokens["access_token"]["token"]))
    json_response = json.loads(response.get_data(as_text=True))

    assert len(json_response["bills"]) == 1
    assert json_response["bills"][0]["id"] == bill1.id
Ejemplo n.º 4
0
def test_delete_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)

    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

    assert bill1.valid is True

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

    assert response.status_code == 200
    assert json_response["message"] == "Deleted bill"
    assert bill1.valid is False
Ejemplo n.º 5
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
Ejemplo n.º 6
0
def test_get_only_not_removed_bills(test_client, api_headers_bearer,
                                    insert_tokens):
    password = "******"
    now = datetime.datetime.utcnow()

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

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

    bill1 = Bill(description="Bill",
                 members=[
                     BillMember(user=user1, amount="-2.00"),
                     BillMember(user=user2, amount="2.00")
                 ],
                 date=now,
                 date_created=now)
    insert_bill(bill1)

    bill2 = Bill(description="Bill",
                 members=[
                     BillMember(user=user1, amount="-2.00"),
                     BillMember(user=user2, amount="2.00")
                 ],
                 date=now,
                 date_created=now,
                 valid=False)
    insert_bill(bill2)

    response = test_client.get("/bills",
                               headers=api_headers_bearer(
                                   user1_tokens["access_token"]["token"]))
    json_response = json.loads(response.get_data(as_text=True))

    assert len(json_response["bills"]) == 1
    assert json_response["bills"][0]["id"] == bill1.id
Ejemplo n.º 7
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"
Ejemplo n.º 8
0
def test_get_bills_from_user(app, test_client, api_headers_bearer,
                             insert_tokens):
    password = "******"
    now = datetime.datetime.utcnow()

    user = User(first_name="Max",
                last_name="Muster",
                email="*****@*****.**",
                password=password)
    insert_user(user)
    user_tokens = insert_tokens(user.email)

    bill_member1 = BillMember(user_id=user.id, amount=1)

    bill1 = Bill(description="Bill", date=now, date_created=now)
    insert_bill(bill1)
    bill_member1.bill = bill1
    bill1.members.append(bill_member1)

    bill_member2 = BillMember(user_id=user.id, amount=1)

    bill2 = Bill(description="Bill2", date=now, date_created=now)
    insert_bill(bill2)
    bill_member2.bill = bill2

    response = test_client.get("/bills",
                               headers=api_headers_bearer(
                                   user_tokens["access_token"]["token"]))
    json_response = json.loads(response.get_data(as_text=True))

    assert response.status_code == 200
    assert json_response["message"] == "Returned bills"
    assert len(json_response["bills"]) == 2
    assert json_response["bills"][0]["description"] == bill1.description
    assert json_response["bills"][1]["description"] == bill2.description
Ejemplo n.º 9
0
def test_delete_existing_group(test_client, api_headers_bearer, insert_tokens):
    password = "******"
    now = datetime.datetime.utcnow()

    user1 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 password=password)
    insert_user(user1)
    user1_tokens = insert_tokens(user1.email)
    user2 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 password=password)
    insert_user(user2)
    group = Group(name="Name", group_members=[GroupMember(user=user1),
                                              GroupMember(user=user2)])
    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",
                 group=group,
                 date=now,
                 date_created=now,
                 members=[bill_member1, bill_member2])
    insert_bill(bill1)

    assert group.valid is True

    response = test_client.delete("/groups/{}".format(group.id),
                                  headers=api_headers_bearer(
                                      user1_tokens["access_token"]["token"]))
    json_response = json.loads(response.get_data(as_text=True))

    assert response.status_code == 200
    assert json_response["message"] == "Deleted group"
    assert group.valid is False
    assert bill1.valid is False
Ejemplo n.º 10
0
def _create_new_bill(data):
    bill = Bill(description=data["description"],
                date=data.get("date", None),
                date_created=data.get("date_created", None))

    for member in data["members"]:
        user = get_user_by_id(member["user_id"])
        bill_member = BillMember(user=user,
                                 bill=bill,
                                 amount=member["amount"])
        bill.members.append(bill_member)

    if "group_id" in data:
        bill.group = get_group_by_id(data["group_id"])

    insert_bill(bill)

    return bill
Ejemplo n.º 11
0
def _update_bill_members(bill, data):
    if "members" in data:

        # Remove members that are in bill but not in new bill.
        i = 0
        while i < len(bill.members):

            found = False
            for m in data["members"]:
                if m["user_id"] == bill.members[i].user_id:
                    found = True
                    break
            if found:
                i += 1
                continue
            del bill.members[i]

        # Update members that are already in group.
        i = 0
        while i < len(data["members"]):
            member = data["members"][0]
            bill_member = _get_bill_member_by_id(bill, member["user_id"])

            if bill_member is not None:
                bill_member.amount = member["amount"]
                del data["members"][i]
                i -= 1

            i += 1

        # Add the rest.
        for member in data["members"]:
            bill_member = BillMember(user_id=member["user_id"],
                                     bill_id=bill.id,
                                     amount=member["amount"])
            bill.members.append(bill_member)
Ejemplo n.º 12
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"]
Ejemplo n.º 13
0
def test_add_new_member_to_bill_add_member_as_friend_of_bill_members(
        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))

    assert response.status_code == 200

    user1_friends = get_friends_by_user_id(user1.id)
    assert len(user1_friends) == 2
    assert user1_friends[0].friend_id == user2.id
    assert user1_friends[1].friend_id == user3.id

    user2_friends = get_friends_by_user_id(user2.id)
    assert len(user2_friends) == 2
    assert user2_friends[0].friend_id == user1.id
    assert user2_friends[1].friend_id == user3.id

    user3_friends = get_friends_by_user_id(user3.id)
    assert len(user3_friends) == 2
    assert user3_friends[0].friend_id == user1.id
    assert user3_friends[1].friend_id == user2.id
Ejemplo n.º 14
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