def test_get_friends_by_user_id(app): user1 = User(first_name="Max", last_name="Muster", email="*****@*****.**", password="******") insert_user(user1) user2 = User(first_name="Max", last_name="Muster", email="*****@*****.**", password="******") insert_user(user2) user3 = User(first_name="Max", last_name="Muster", email="*****@*****.**", password="******") insert_user(user3) friend1 = Friend(user=user1, friend=user2) insert_friend(friend1) friend2 = Friend(user=user1, friend=user3) insert_friend(friend2) friends_user1 = get_friends_by_user_id(user1.id) assert len(friends_user1) == 2 assert friends_user1[0] == friend1 assert friends_user1[1] == friend2
def test_insert_friends_by_user_relationship(app): user1 = User(first_name="Max", last_name="Muster", email="*****@*****.**", password="******") insert_user(user1) user2 = User(first_name="Max", last_name="Muster", email="*****@*****.**", password="******") insert_user(user2) user3 = User(first_name="Max", last_name="Muster", email="*****@*****.**", password="******") insert_user(user3) user1.friends.append(Friend(friend=user2)) user1.friends.append(Friend(friend=user3)) 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
def get(self): current_user = get_authorized_user() friends = get_friends_by_user_id(current_user.id) result = { "message": "Returned friends", "friends": [{ "user_id": friend.friend_id } for friend in friends] } return result, 200
def test_add_new_member_to_group_add_member_as_friend_of_group_members( test_client, api_headers_bearer, insert_tokens): password = "******" user1 = User(first_name="Max", last_name="Muster", email="*****@*****.**", password=password) user1 = insert_user(user1) user1_tokens = insert_tokens(user1.email) user2 = User(first_name="Max", last_name="Muster", email="*****@*****.**", password=password) user2 = insert_user(user2) group_member1 = GroupMember(user=user1) group = Group(name="Muster", group_members=[group_member1]) insert_group(group) group_add_user_data = { "user_id": user2.id, } response = test_client.post("/groups/{}/members".format(group.id), headers=api_headers_bearer( user1_tokens["access_token"]["token"]), data=json.dumps(group_add_user_data)) assert response.status_code == 201 user1_friends = get_friends_by_user_id(user1.id) assert len(user1_friends) == 1 assert user1_friends[0].friend_id == user2.id assert len(user2.friends) == 1 assert user2.friends[0].friend_id == user1.id
def test_insert_friends_of_user_after_adding_group(test_client, api_headers_bearer, insert_tokens): 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) group_data = { "name": "Muster", "members": [ { "id": user1.id, }, { "id": user2.id, }, { "id": user3.id, } ] } response = test_client.post("/groups", headers=api_headers_bearer( user1_tokens["access_token"]["token"]), data=json.dumps(group_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
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
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