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"])
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
def test_add_bill_in_group(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) group = Group( name="Name", group_members=[GroupMember(user=user1), GroupMember(user=user2)]) insert_group(group) bill_data = { "description": "Important bill", "date": now, "date_created": now, "group_id": group.id, "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 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 bill = get_bills_by_user_id(user1.id)[0] assert bill.group == group