Exemplo n.º 1
0
def test_get_groups_from_user(app, test_client, api_headers_bearer, insert_tokens):
    password = "******"

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

    user = insert_user(user)
    tokens = insert_tokens(user.email)

    group1_member = GroupMember(user=user)

    group1 = Group(name="group1", group_members=[group1_member])
    insert_group(group1)

    group2_member = GroupMember(user=user)

    group2 = Group(name="group2", group_members=[group2_member])
    insert_group(group2)

    response = test_client.get("/groups",
                               headers=api_headers_bearer(
                                   tokens["access_token"]["token"]))
    json_respone = json.loads(response.get_data(as_text=True))

    assert response.status_code == 200

    assert json_respone["message"] == "Returned groups"

    assert len(json_respone["groups"]) == 2
    assert json_respone["groups"][0]["name"] == group1.name
    assert json_respone["groups"][1]["name"] == group2.name
Exemplo 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"]
Exemplo n.º 3
0
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
Exemplo n.º 4
0
def test_get_just_groups_from_user(app, 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)
    user2_tokens = insert_tokens(user2.email)

    group1_member1 = GroupMember(user=user1)
    group1 = Group(name="group1",
                   group_members=[group1_member1])

    group2_member1 = GroupMember(user=user1)
    group2_member2 = GroupMember(user=user2)
    group2 = Group(name="group2",
                   group_members=[group2_member1, group2_member2])

    group3_member2 = GroupMember(user=user2)
    group3 = Group(name="group3",
                   group_members=[group3_member2])

    group1 = insert_group(group1)
    group2 = insert_group(group2)
    group3 = insert_group(group3)

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

    assert len(json_respone["groups"]) == 2
    assert json_respone["groups"][0]["name"] == group1.name
    assert json_respone["groups"][1]["name"] == group2.name

    response = test_client.get("/groups",
                               headers=api_headers_bearer(
                                   user2_tokens["access_token"]["token"]))
    json_respone = json.loads(response.get_data(as_text=True))

    assert len(json_respone["groups"]) == 2
    assert json_respone["groups"][0]["name"] == group2.name
    assert json_respone["groups"][1]["name"] == group3.name
Exemplo n.º 5
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
Exemplo n.º 6
0
def test_dont_delete_user_if_not_in_group(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)

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

    assert response.status_code == 401
    assert json_response["message"] == "Group does not exist"
    assert group_member1.valid == 1
    assert len(group.group_members) == 1
Exemplo n.º 7
0
def test_dont_add_user_if_user_not_exist(test_client, api_headers_bearer,
                                         insert_tokens):
    password = "******"

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

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

    group_add_user_data = {
        "user_id": 2,
    }

    response = test_client.post("/groups/{}/members".format(group.id),
                                headers=api_headers_bearer(
                                    tokens["access_token"]["token"]),
                                data=json.dumps(group_add_user_data))
    json_response = json.loads(response.get_data(as_text=True))

    assert response.status_code == 400
    assert json_response["message"] == "User does not exist"
    assert len(get_all_users()) == 1
Exemplo n.º 8
0
def test_add_not_registered_user_to_group_if_user_already_created(
        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)

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

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

    group_add_user_data = {"email": new_user_email}

    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))
    json_response = json.loads(response.get_data(as_text=True))

    assert response.status_code == 201
    assert json_response["message"] == "Added user to group"

    group = get_group_by_id(group.id)

    assert len(group.group_members) == 2
Exemplo n.º 9
0
def test_error_on_user_not_member_of_group(test_client, api_headers_bearer, insert_tokens):
    password = "******"
    user1 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 password=password)
    insert_user(user1)

    user2 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 password=password)
    insert_user(user2)
    user2_tokens = insert_tokens(user2.email)
    group = Group(name="Name", group_members=[GroupMember(user=user1)])
    insert_group(group)

    data = {
        "id": group.id,
        "name": "Changed",
        "members": [{"id": user1.id}]
    }

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

    assert response.status_code == 401
    assert json_response["message"] == "Group does not exist"
    assert group.name == "Name"
Exemplo n.º 10
0
def test_change_name_of_existing_group(test_client, api_headers_bearer, insert_tokens):
    password = "******"
    user = User(first_name="Max",
                last_name="Muster",
                email="*****@*****.**",
                password=password)
    insert_user(user)
    tokens = insert_tokens(user.email)
    group = Group(name="Name", group_members=[GroupMember(user=user)])
    insert_group(group)

    data = {
        "id": group.id,
        "name": "Changed",
        "members": [{"id": user.id}]
    }

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

    assert response.status_code == 200
    assert json_response["message"] == "Edited group"
    assert group.name == data["name"]
Exemplo n.º 11
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
Exemplo n.º 12
0
def _create_new_group(data):
    group = Group(name=data["name"])

    for member in data["members"]:
        user = _get_user_from_member_data(member)

        group_member = GroupMember(user=user, group=group)

        group.group_members.append(group_member)

    insert_group(group)

    return group
Exemplo n.º 13
0
def test_get_only_non_removed_groups(test_client, api_headers_bearer, insert_tokens):
    password = "******"

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

    group1 = Group(name="G1", group_members=[GroupMember(user=user1)])
    insert_group(group1)
    group2 = Group(
        name="G1", group_members=[GroupMember(user=user1)], valid=False)
    insert_group(group2)

    response = test_client.get("/groups",
                               headers=api_headers_bearer(
                                   tokens["access_token"]["token"]))
    json_respone = json.loads(response.get_data(as_text=True))

    assert len(json_respone["groups"]) == 1
    assert json_respone["groups"][0]["id"] == group1.id
Exemplo n.º 14
0
def test_dont_add_user_if_user_already_in_group(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_member2 = GroupMember(user=user2)
    group = Group(name="Group", group_members=[group_member1, group_member2])
    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))
    json_response = json.loads(response.get_data(as_text=True))

    assert response.status_code == 400
    assert json_response["message"] == "User is already in group"
    assert len(group.group_members) == 2
Exemplo n.º 15
0
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