def test_conversation_reply_shows_templates(
    client_request,
    fake_uuid,
    mocker,
    mock_get_template_folders,
    active_user_with_permissions,
    service_one
):

    all_templates = {'data': [
        _template('sms', 'sms_template_one', parent=INV_PARENT_FOLDER_ID),
        _template('sms', 'sms_template_two'),
        _template('sms', 'sms_template_three', parent=VIS_PARENT_FOLDER_ID),
        _template('letter', 'letter_template_one')
    ]}
    mocker.patch('app.service_api_client.get_service_templates', return_value=all_templates)
    mock_get_template_folders.return_value = [
        {
            'name': "Parent 1 - invisible",
            'id': INV_PARENT_FOLDER_ID,
            'parent_id': None,
            'users_with_permission': []
        },
        {
            'name': "Parent 2 - visible",
            'id': VIS_PARENT_FOLDER_ID,
            'parent_id': None,
            'users_with_permission': [active_user_with_permissions['id']]
        },
    ]
    page = client_request.get(
        'main.conversation_reply',
        service_id=SERVICE_ONE_ID,
        notification_id=fake_uuid,
    )

    link = page.select('.template-list-item-without-ancestors')
    assert normalize_spaces(link[0].text) == "Parent 2 - visible 1 template"
    assert normalize_spaces(link[1].text) == 'sms_template_two Text message template'

    assert link[0].select_one('a')['href'].startswith(
        url_for(
            'main.conversation_reply',
            service_id=SERVICE_ONE_ID,
            notification_id=fake_uuid,
            from_folder=VIS_PARENT_FOLDER_ID
        )
    )

    assert link[1].select_one('a')['href'].startswith(
        url_for(
            'main.conversation_reply_with_template',
            service_id=SERVICE_ONE_ID,
            notification_id=fake_uuid,
            template_id='',
        )
    )
def test_should_show_templates_folder_page(
    client_request,
    mock_get_template_folders,
    mock_has_no_jobs,
    service_one,
    mocker,
    expected_title_tag,
    expected_page_title,
    expected_parent_link_args,
    extra_args,
    expected_nav_links,
    expected_items,
    expected_displayed_items,
    expected_searchable_text,
    expected_empty_message,
):
    mock_get_template_folders.return_value = [
        _folder("folder_two", FOLDER_TWO_ID),
        _folder("folder_one", PARENT_FOLDER_ID),
        _folder("folder_one_two", parent=PARENT_FOLDER_ID),
        _folder("folder_one_one", CHILD_FOLDER_ID, parent=PARENT_FOLDER_ID),
        _folder("folder_one_one_one", GRANDCHILD_FOLDER_ID, parent=CHILD_FOLDER_ID),
    ]
    mock_get_service_templates = mocker.patch(
        "app.service_api_client.get_service_templates",
        return_value={
            "data": [
                _template("sms", "sms_template_one"),
                _template("sms", "sms_template_two"),
                _template("email", "email_template_one"),
                _template("email", "email_template_two"),
                _template("letter", "letter_template_one"),
                _template("letter", "letter_template_two"),
                _template("letter", "letter_template_nested", parent=CHILD_FOLDER_ID),
                _template("sms", "sms_template_nested", parent=GRANDCHILD_FOLDER_ID),
            ]
        },
    )

    service_one["permissions"] += ["letter"]

    page = client_request.get(
        "main.choose_template",
        service_id=SERVICE_ONE_ID,
        _test_page_title=False,
        **extra_args,
    )

    assert normalize_spaces(page.select_one("title").text) == expected_title_tag
    assert normalize_spaces(page.select_one("h1").text) == expected_page_title

    assert len(page.select("h1 a")) == len(expected_parent_link_args)

    for index, parent_link in enumerate(page.select("h1 a")):
        assert parent_link["href"] == url_for(
            "main.choose_template",
            service_id=SERVICE_ONE_ID,
            **expected_parent_link_args[index],
        )

    links_in_page = page.select(".pill a")

    assert len(links_in_page) == len(expected_nav_links)

    for index, expected_link in enumerate(expected_nav_links):
        assert links_in_page[index].text.strip() == expected_link

    all_page_items = page.select(".template-list-item")
    all_page_items_styled_with_checkboxes = page.select(".template-list-item-with-checkbox")

    assert len(all_page_items) == len(all_page_items_styled_with_checkboxes)

    checkboxes = page.select("input[name=templates_and_folders]")
    unique_checkbox_values = set(item["value"] for item in checkboxes)
    assert len(all_page_items) == len(expected_items)
    assert len(checkboxes) == len(expected_items)
    assert len(unique_checkbox_values) == len(expected_items)

    for index, expected_item in enumerate(expected_items):
        assert normalize_spaces(all_page_items[index].text) == expected_item

    displayed_page_items = page.find_all(
        lambda tag: (
            tag.has_attr("class")
            and "template-list-item" in tag["class"]
            and "template-list-item-hidden-by-default" not in tag["class"]
        )
    )
    assert len(displayed_page_items) == len(expected_displayed_items)

    for index, expected_item in enumerate(expected_displayed_items):
        assert "/" not in expected_item  # Yo dawg I heard you like tests…
        assert normalize_spaces(displayed_page_items[index].text) == expected_item

    all_searchable_text = page.select("#template-list .template-list-item .live-search-relevant")
    assert len(all_searchable_text) == len(expected_searchable_text)

    for index, expected_item in enumerate(expected_searchable_text):
        assert normalize_spaces(all_searchable_text[index].text) == expected_item

    if expected_empty_message:
        assert normalize_spaces(page.select_one(".template-list-empty").text) == (expected_empty_message)
    else:
        assert not page.select(".template-list-empty")

    mock_get_service_templates.assert_called_once_with(SERVICE_ONE_ID)
def test_should_filter_templates_folder_page_based_on_user_permissions(
    client_request,
    mock_get_template_folders,
    mock_has_no_jobs,
    service_one,
    mocker,
    active_user_with_permissions,
    extra_args,
    expected_displayed_items,
    expected_items,
    expected_empty_message,
):
    service_one["permissions"] += ["letter"]
    mock_get_template_folders.return_value = [
        _folder("folder_A", FOLDER_TWO_ID, None, [active_user_with_permissions["id"]]),
        _folder("folder_B", FOLDER_B_ID, FOLDER_TWO_ID, []),
        _folder("folder_C", FOLDER_C_ID, FOLDER_TWO_ID, [active_user_with_permissions["id"]]),
        _folder("folder_D", None, FOLDER_TWO_ID, [active_user_with_permissions["id"]]),
        _folder("folder_E", PARENT_FOLDER_ID, users_with_permission=[]),
        _folder("folder_F", CHILD_FOLDER_ID, PARENT_FOLDER_ID, []),
        _folder(
            "folder_G",
            GRANDCHILD_FOLDER_ID,
            CHILD_FOLDER_ID,
            [active_user_with_permissions["id"]],
        ),
    ]
    mocker.patch(
        "app.service_api_client.get_service_templates",
        return_value={
            "data": [
                _template("email", "email_template_root"),
                _template("sms", "sms_template_A", parent=FOLDER_TWO_ID),
                _template("sms", "sms_template_C", parent=FOLDER_C_ID),
                _template("email", "email_template_B", parent=FOLDER_B_ID),
                _template("email", "email_template_G", parent=GRANDCHILD_FOLDER_ID),
                _template("letter", "letter_template_F", parent=CHILD_FOLDER_ID),
            ]
        },
    )

    page = client_request.get(
        "main.choose_template",
        service_id=SERVICE_ONE_ID,
        _test_page_title=False,
        **extra_args,
    )

    displayed_page_items = page.find_all(
        lambda tag: (
            tag.has_attr("class")
            and "template-list-item" in tag["class"]
            and "template-list-item-hidden-by-default" not in tag["class"]
        )
    )
    assert [[i.strip() for i in e.text.split("\n") if i.strip()] for e in displayed_page_items] == expected_displayed_items

    all_page_items = page.select(".template-list-item")
    assert [[i.strip() for i in e.text.split("\n") if i.strip()] for e in all_page_items] == expected_items

    if expected_empty_message:
        assert normalize_spaces(page.select_one(".template-list-empty").text) == (expected_empty_message)
    else:
        assert not page.select(".template-list-empty")