Exemplo n.º 1
0
 def to_dict(self):
     return {
         "id": self.id,
         "description": self.description,
         "date": datetime_to_string(self.date),
         "date_created": datetime_to_string(self.date_created)
     }
Exemplo n.º 2
0
def test_add_bill(app, 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)

    bill_data = {
        "description":
        "Important bill",
        "date":
        datetime_to_string(now),
        "date_created":
        datetime_to_string(now),
        "members": [{
            "user_id": user1.id,
            "amount": 200
        }, {
            "user_id": user2.id,
            "amount": -200
        }]
    }

    response = test_client.post("/bills",
                                headers=api_headers_bearer(
                                    user1_tokens["access_token"]["token"]),
                                data=json.dumps(bill_data,
                                                default=json_data_encoder))
    json_response = json.loads(response.get_data(as_text=True))

    assert response.status_code == 201

    bill = get_bills_by_user_id(user1.id)[0]

    assert json_response["message"] == "Created new bill"
    assert json_response["bill"]["id"] == bill.id
    assert json_response["bill"]["description"] == bill.description
    assert json_response["bill"]["date"] == bill_data["date"]
    assert json_response["bill"]["date_created"] == bill_data["date_created"]

    assert len(get_bills_by_user_id(user1.id)) == 1
    assert len(get_bills_by_user_id(user2.id)) == 1

    assert bill.description == bill_data["description"]
    assert bill.date == string_to_datetime(bill_data["date"])
    assert bill.date_created == string_to_datetime(bill_data["date_created"])
    assert bill.members[0].user == user1
    assert (bill.members[0].amount == bill_data["members"][0]["amount"])
    assert bill.members[1].user == user2
    assert (bill.members[1].amount == bill_data["members"][1]["amount"])
Exemplo n.º 3
0
def test_can_create_multiple_times_bill_with_same_members(
        test_client, api_headers_bearer, insert_tokens):
    now = datetime.datetime.utcnow()
    password = "******"

    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)

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

    bill_data = {
        "description":
        "Important bill",
        "date":
        datetime_to_string(now),
        "date_created":
        datetime_to_string(now),
        "members": [{
            "user_id": user1.id,
            "amount": 200
        }, {
            "user_id": user2.id,
            "amount": -100
        }, {
            "user_id": user3.id,
            "amount": -100
        }]
    }

    # Create bill first time
    response = test_client.post("/bills",
                                headers=api_headers_bearer(
                                    user1_tokens["access_token"]["token"]),
                                data=json.dumps(bill_data))

    assert response.status_code == 201

    # Create bill second time
    response = test_client.post("/bills",
                                headers=api_headers_bearer(
                                    user1_tokens["access_token"]["token"]),
                                data=json.dumps(bill_data))

    assert response.status_code == 201
Exemplo n.º 4
0
def test_create_bill_with_not_registered_bill_member_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="*****@*****.**",
                 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)

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

    bill_data = {
        "description":
        "Important bill",
        "date":
        datetime_to_string(now),
        "date_created":
        datetime_to_string(now),
        "members": [{
            "user_id": user1.id,
            "amount": 200
        }, {
            "user_id": user2.id,
            "amount": -100
        }, {
            "email": new_user_email,
            "amount": -100
        }]
    }

    response = test_client.post("/bills",
                                headers=api_headers_bearer(
                                    user1_tokens["access_token"]["token"]),
                                data=json.dumps(bill_data,
                                                default=json_data_encoder))
    json_response = json.loads(response.get_data(as_text=True))

    assert response.status_code == 201

    bill = get_bills_by_user_id(user1.id)[0]

    assert json_response["message"] == "Created new bill"

    assert len(get_bills_by_user_id(user1.id)) == 1
    assert len(get_bills_by_user_id(user2.id)) == 1

    assert len(bill.members) == 3
Exemplo n.º 5
0
def test_error_on_member_is_more_than_one_time_debtor(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)

    bill_data = {
        "description":
        "Important bill",
        "date":
        datetime_to_string(now),
        "date_created":
        datetime_to_string(now),
        "members": [{
            "user_id": user1.id,
            "amount": 200
        }, {
            "user_id": user2.id,
            "amount": -100
        }, {
            "user_id": user2.id,
            "amount": -100
        }]
    }

    response = test_client.post("/bills",
                                headers=api_headers_bearer(
                                    user1_tokens["access_token"]["token"]),
                                data=json.dumps(bill_data,
                                                default=json_data_encoder))
    json_response = json.loads(response.get_data(as_text=True))

    assert response.status_code == 400
    assert json_response[
        "message"] == "User {} can only be one time a debtor".format(user2.id)
    assert len(get_all_bills()) == 0
Exemplo n.º 6
0
def test_datetime_to_string():
    now = datetime.datetime.utcnow()

    result = datetime_to_string(now)

    assert result == now.isoformat()
Exemplo n.º 7
0
def test_parse_datetime():
    now = datetime.datetime.utcnow()

    date = string_to_datetime(datetime_to_string(now))

    assert date == now
Exemplo n.º 8
0
def json_data_encoder(data):

    if isinstance(data, datetime.datetime):
        return datetime_to_string(data)

    raise NotImplementedError("Implement an encoder for this type")
Exemplo n.º 9
0
def test_insert_friends_of_user_after_adding_bill(test_client,
                                                  api_headers_bearer,
                                                  insert_tokens):
    now = datetime.datetime.utcnow()
    password = "******"

    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)

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

    bill_data = {
        "description":
        "Important bill",
        "date":
        datetime_to_string(now),
        "date_created":
        datetime_to_string(now),
        "members": [{
            "user_id": user1.id,
            "amount": 200
        }, {
            "user_id": user2.id,
            "amount": -100
        }, {
            "user_id": user3.id,
            "amount": -100
        }]
    }

    response = test_client.post("/bills",
                                headers=api_headers_bearer(
                                    user1_tokens["access_token"]["token"]),
                                data=json.dumps(bill_data))

    assert response.status_code == 201

    # Get friends of user1
    friends_user1 = get_friends_by_user_id(user1.id)
    assert len(friends_user1) == 2
    assert friends_user1[0].friend_id == user2.id
    assert friends_user1[1].friend_id == user3.id

    # Get friends of user2
    friends_user2 = get_friends_by_user_id(user2.id)
    assert len(friends_user2) == 2
    assert friends_user2[0].friend_id == user1.id
    assert friends_user2[1].friend_id == user3.id

    # Get friends of user3
    friends_user3 = get_friends_by_user_id(user3.id)
    assert len(friends_user3) == 2
    assert friends_user3[0].friend_id == user1.id
    assert friends_user3[1].friend_id == user2.id