def test_team_memberships_post_success_for_multiple_teams(
        db_session, test_users, test_teams, default_team_roles, token_fixture):
    # Member Role Request
    test_user1, test_user2 = test_users
    test_team1, test_team2 = test_teams
    member_role, admin_role = default_team_roles
    membership = TeamMembershipCreate(
        team_id=test_team1.public_id,
        user_id=test_user1.public_id,
        role_id=member_role.public_id,
    )
    response = client.post("/memberships/", json=membership.dict())

    json_response = response.json()

    assert response.status_code == 201
    assert json_response["team"]["public_id"] == test_team1.public_id
    assert json_response["role"]["public_id"] == member_role.public_id
    assert json_response["user"]["public_id"] == test_user1.public_id
    assert "public_id" in json_response

    # Second Teams Request
    membership.team_id = test_team2.public_id
    response = client.post("/memberships/", json=membership.dict())

    json_response = response.json()

    assert response.status_code == 201
    assert json_response["team"]["public_id"] == test_team2.public_id
    assert json_response["role"]["public_id"] == member_role.public_id
    assert json_response["user"]["public_id"] == test_user1.public_id
    assert "public_id" in json_response
def test_team_memberships_post_fails_with_duplicate_entry(
        db_session, test_users, test_teams, default_team_roles, token_fixture):
    # First Teams Request
    test_user1, test_user2 = test_users
    test_team1, test_team2 = test_teams
    member_role, admin_role = default_team_roles
    membership = TeamMembershipCreate(
        team_id=test_team1.public_id,
        user_id=test_user1.public_id,
        role_id=member_role.public_id,
    )
    response = client.post("/memberships/", json=membership.dict())

    json_response = response.json()

    assert response.status_code == 201
    assert json_response["team"]["public_id"] == test_team1.public_id
    assert json_response["role"]["public_id"] == member_role.public_id
    assert json_response["user"]["public_id"] == test_user1.public_id
    assert "public_id" in json_response

    # Duplicate Request
    response = client.post("/memberships/", json=membership.dict())

    json_response = response.json()

    assert response.status_code == 409
    assert (json_response["detail"] ==
            "The user already has membership of that role within the team")
def test_team_memberships_post_fails_with_invalid_team_id(
        db_session, test_users, default_team_roles, token_fixture):
    invalid_id = "invalid_team_id"
    test_user1, test_user2 = test_users
    member_role, admin_role = default_team_roles
    membership = TeamMembershipCreate(team_id=invalid_id,
                                      user_id=test_user1.public_id,
                                      role_id=member_role.public_id)
    response = client.post("/memberships/", json=membership.dict())

    json_response = response.json()
    assert response.status_code == 404
    assert json_response[
        "detail"] == f"The team with id {invalid_id} does not exist"
def test_team_memberships_post_single_member_addition(db_session, test_users,
                                                      test_teams,
                                                      default_team_roles,
                                                      token_fixture):
    test_user1, test_user2 = test_users
    test_team1, test_team2 = test_teams
    member_role, admin_role = default_team_roles
    membership = TeamMembershipCreate(
        team_id=test_team1.public_id,
        user_id=test_user1.public_id,
        role_id=member_role.public_id,
    )
    response = client.post("/memberships/", json=membership.dict())

    json_response = response.json()

    assert response.status_code == 201
    assert json_response["team"]["public_id"] == test_team1.public_id
    assert json_response["role"]["public_id"] == member_role.public_id
    assert json_response["user"]["public_id"] == test_user1.public_id
    assert "public_id" in json_response