def test_should_show_caseworker_on_overview_page(
    client_request,
    mocker,
    mock_get_invites_for_service,
    fake_uuid,
    service_one,
):
    service_one['permissions'].append('caseworking')
    current_user = active_user_view_permissions(active_user_view_permissions)
    other_user = active_caseworking_user(fake_uuid)
    other_user.email_address = '*****@*****.**'

    mocker.patch('app.user_api_client.get_user', return_value=current_user)
    mocker.patch('app.user_api_client.get_users_for_service',
                 return_value=[
                     current_user,
                     other_user,
                 ])

    page = client_request.get('main.manage_users', service_id=SERVICE_ONE_ID)

    assert normalize_spaces(page.select_one('h1').text) == 'Team members'
    assert normalize_spaces(page.select('.user-list-item')[0].text) == (
        'Test User With Permissions (you) '
        'Can Admin view '
        'Can’t Send messages '
        'Can’t Add and edit templates '
        'Can’t Manage service '
        'Can’t Access API keys')
    # [1:5] are invited users
    assert normalize_spaces(page.select('.user-list-item')[6].text) == (
        'Test User [email protected] '
        'Can Basic view')
def test_get_manage_folder_viewing_permissions_for_users_not_visible_when_no_manage_settings_permission(
    client_request,
    active_user_with_permissions,
    service_one,
    mock_get_template_folders,
    mocker,
):
    active_user_with_permissions["permissions"][SERVICE_ONE_ID] = [
        "send_texts",
        "send_emails",
        "send_letters",
        "manage_templates",
        "manage_api_keys",
        "view_activity",
    ]
    folder_id = str(uuid.uuid4())
    team_member = active_user_view_permissions(str(uuid.uuid4()))
    team_member_2 = active_user_view_permissions(str(uuid.uuid4()))
    service_one["permissions"] += ["edit_folder_permissions"]
    mock_get_template_folders.return_value = [
        {
            "id": folder_id,
            "name": "folder_two",
            "parent_id": None,
            "users_with_permission": [
                active_user_with_permissions["id"],
                team_member_2["id"],
            ],
        },
    ]
    mocker.patch(
        "app.models.user.Users.client",
        return_value=[active_user_with_permissions, team_member, team_member_2],
    )

    page = client_request.get(
        "main.manage_template_folder",
        service_id=service_one["id"],
        template_folder_id=folder_id,
        _test_page_title=False,
    )
    assert normalize_spaces(page.select_one("title").text) == ("folder_two – Templates - service one – Notify")
    form_labels = page.select("legend[class=form-label]")
    assert len(form_labels) == 0
    checkboxes = page.select("input[name=users_with_permission]")
    assert len(checkboxes) == 0
def test_get_manage_folder_viewing_permissions_for_users(
    client_request,
    active_user_with_permissions,
    service_one,
    mock_get_template_folders,
    mock_get_users_by_service,
    mocker,
):
    folder_id = str(uuid.uuid4())
    team_member = active_user_view_permissions(str(uuid.uuid4()))
    team_member_2 = active_user_view_permissions(str(uuid.uuid4()))
    mock_get_template_folders.return_value = [
        _folder(
            "folder_two",
            folder_id,
            None,
            [active_user_with_permissions["id"], team_member_2["id"]],
        ),
    ]
    mocker.patch(
        "app.models.user.Users.client",
        return_value=[active_user_with_permissions, team_member, team_member_2],
    )

    page = client_request.get(
        "main.manage_template_folder",
        service_id=service_one["id"],
        template_folder_id=folder_id,
        _test_page_title=False,
    )
    assert normalize_spaces(page.select_one("title").text) == ("folder_two – Templates - service one – Notify")
    form_labels = page.select("legend[class=form-label]")
    assert normalize_spaces(form_labels[0].text) == "Team members who can see this folder"
    checkboxes = page.select("input[name=users_with_permission]")

    assert len(checkboxes) == 2
    assert checkboxes[0]["value"] == team_member["id"]
    assert "checked" not in checkboxes[0].attrs

    assert checkboxes[1]["value"] == team_member_2["id"]
    assert "checked" in checkboxes[1].attrs

    assert "Test User" in page.findAll("label", {"for": "users_with_permission-0"})[0].text
def test_manage_folder_users_doesnt_change_permissions_current_user_cannot_manage_users(
    client_request,
    active_user_with_permissions,
    service_one,
    mock_get_template_folders,
    mocker,
):
    active_user_with_permissions["permissions"][SERVICE_ONE_ID] = [
        "send_texts",
        "send_emails",
        "send_letters",
        "manage_templates",
        "manage_api_keys",
        "view_activity",
    ]
    team_member = active_user_view_permissions(str(uuid.uuid4()))
    mock_update = mocker.patch("app.template_folder_api_client.update_template_folder")
    folder_id = str(uuid.uuid4())
    mock_get_template_folders.return_value = [
        {
            "id": folder_id,
            "name": "folder_two",
            "parent_id": None,
            "users_with_permission": [
                active_user_with_permissions["id"],
                team_member["id"],
            ],
        }
    ]
    mocker.patch(
        "app.models.user.Users.client",
        return_value=[active_user_with_permissions, team_member],
    )

    client_request.post(
        "main.manage_template_folder",
        service_id=service_one["id"],
        template_folder_id=folder_id,
        _data={"name": "new beautiful name", "users_with_permission": []},
        _expected_redirect=url_for(
            "main.choose_template",
            service_id=service_one["id"],
            template_folder_id=folder_id,
            _external=True,
        ),
    )

    mock_update.assert_called_once_with(
        service_one["id"],
        folder_id,
        name="new beautiful name",
        users_with_permission=None,
    )
def test_manage_folder_users(
    client_request,
    active_user_with_permissions,
    service_one,
    mock_get_template_folders,
    mocker,
):
    team_member = active_user_view_permissions(str(uuid.uuid4()))
    mock_update = mocker.patch("app.template_folder_api_client.update_template_folder")
    folder_id = str(uuid.uuid4())
    mock_get_template_folders.return_value = [
        _folder(
            "folder_two",
            folder_id,
            None,
            [active_user_with_permissions["id"], team_member["id"]],
        )
    ]
    mocker.patch(
        "app.models.user.Users.client",
        return_value=[active_user_with_permissions, team_member],
    )

    client_request.post(
        "main.manage_template_folder",
        service_id=service_one["id"],
        template_folder_id=folder_id,
        _data={"name": "new beautiful name", "users_with_permission": []},
        _expected_redirect=url_for(
            "main.choose_template",
            service_id=service_one["id"],
            template_folder_id=folder_id,
            _external=True,
        ),
    )

    mock_update.assert_called_once_with(
        service_one["id"],
        folder_id,
        name="new beautiful name",
        users_with_permission=[active_user_with_permissions["id"]],
    )
def test_should_show_caseworker_on_overview_page(
    client_request,
    mocker,
    mock_get_invites_for_service,
    mock_get_template_folders,
    fake_uuid,
    service_one,
):
    service_one['permissions'].append('caseworking')
    current_user = active_user_view_permissions(fake_uuid)
    other_user = active_caseworking_user(uuid.uuid4())
    other_user['email_address'] = '*****@*****.**'

    mocker.patch('app.user_api_client.get_user', return_value=current_user)
    mocker.patch('app.models.user.Users.client', return_value=[
        current_user,
        other_user,
    ])

    page = client_request.get('main.manage_users', service_id=SERVICE_ONE_ID)

    assert normalize_spaces(page.select_one('h1').text) == 'Team members'
    assert normalize_spaces(page.select('.user-list-item')[0].text) == (
        'Test User With Permissions (you) '
        'Can See dashboard '
        'Can’t Send messages '
        'Can’t Add and edit templates '
        'Can’t Manage settings and teams '
        'Can’t Manage API integration'
    )
    # [1:5] are invited users
    assert normalize_spaces(page.select('.user-list-item')[6].text) == (
        'Test User [email protected] '
        'Can’t See dashboard '
        'Can Send messages '
        'Can’t Add and edit templates '
        'Can’t Manage settings and teams '
        'Can’t Manage API integration'
    )