def create_email_template(driver, name="test template", content=None):
    show_templates_page = ShowTemplatesPage(driver)
    show_templates_page.click_add_new_template()

    show_templates_page.select_email()

    template_page = EditEmailTemplatePage(driver)
    template_page.create_template(name=name, content=content)
    return template_page.get_template_id()
def get_service_templates_and_api_key_for_tests(driver, test_profile):

    dashboard_page = DashboardPage(driver)
    dashboard_page.click_email_templates()
    service_id = dashboard_page.get_service_id()

    email_template_page = SendEmailTemplatePage(driver)
    email_template_page.click_add_a_new_template()

    new_email_template_page = EditEmailTemplatePage(driver)
    new_email_template_page.create_template()

    email_template_page = SendEmailTemplatePage(driver)
    email_template_page.click_edit_template()

    edit_email_template_page = EditEmailTemplatePage(driver)
    email_template_id = edit_email_template_page.get_id()

    dashboard_page = DashboardPage(driver)
    dashboard_page.go_to_dashboard_for_service()
    dashboard_page.click_sms_templates()

    sms_template_page = SendSmsTemplatePage(driver)
    sms_template_page.click_add_new_template()

    new_sms_template = EditSmsTemplatePage(driver)
    new_sms_template.create_template()

    sms_template_page = SendSmsTemplatePage(driver)
    sms_template_page.click_edit_template()

    edit_sms_template = EditSmsTemplatePage(driver)
    sms_template_id = edit_sms_template.get_id()

    dashboard_page = DashboardPage(driver)
    dashboard_page.go_to_dashboard_for_service()
    dashboard_page.click_api_keys_link()

    api_key_page = ApiKeyPage(driver)
    api_key_page.click_keys_link()
    api_key_page.click_create_key()

    api_key_page.click_key_type_radio(key_type='team')
    api_key_page.enter_key_name(key_type='team')

    api_key_page.click_continue()
    api_key = api_key_page.get_api_key()

    test_profile.service_id = service_id
    test_profile.email_template_id = email_template_id
    test_profile.sms_template_id = sms_template_id
    test_profile.api_key = api_key

    return {'service_id': service_id, 'email_template_id': email_template_id, 'sms_template_id': sms_template_id, 'api_key': api_key}  # noqa
def delete_template(driver, template_name):
    show_templates_page = ShowTemplatesPage(driver)
    try:
        show_templates_page.click_template_by_link_text(template_name)
    except TimeoutException:
        dashboard_page = DashboardPage(driver)
        dashboard_page.go_to_dashboard_for_service(config['service']['id'])
        dashboard_page.click_templates()
        show_templates_page.click_template_by_link_text(template_name)
    template_page = EditEmailTemplatePage(driver)
    template_page.click_delete()
def do_edit_and_delete_email_template(driver):
    test_name = "edit/delete test"
    dashboard_page = DashboardPage(driver)
    dashboard_page.go_to_dashboard_for_service()
    dashboard_page.click_email_templates()

    existing_templates = [x.text for x in driver.find_elements_by_class_name("message-name")]

    all_templates_page = SendEmailTemplatePage(driver)
    all_templates_page.click_add_new_template()

    template_page = EditEmailTemplatePage(driver)
    template_page.create_template(name=test_name)

    assert test_name in [x.text for x in driver.find_elements_by_class_name("message-name")]

    all_templates_page.click_edit_template()
    template_page.click_delete()

    assert [x.text for x in driver.find_elements_by_class_name("message-name")] == existing_templates
def test_template_folder_permissions(driver, login_seeded_user):
    family_id = uuid.uuid4()
    folder_names = [
        'test-parent-folder {}'.format(family_id),
        'test-child-folder {}'.format(family_id),
        'test-grandchild-folder {}'.format(family_id),
    ]
    dashboard_page = DashboardPage(driver)
    dashboard_page.go_to_dashboard_for_service(config['service']['id'])
    dashboard_page.click_templates()
    show_templates_page = ShowTemplatesPage(driver)
    # a loop to create a folder structure with parent folder, child folder and grandchild folder,
    # each folder with one template in it
    for folder_name in folder_names:
        # create a new folder
        show_templates_page.click_add_new_folder(folder_name)

        show_templates_page.click_template_by_link_text(folder_name)
        # create a new template
        show_templates_page.click_add_new_template()
        show_templates_page.select_email()

        edit_template_page = EditEmailTemplatePage(driver)
        edit_template_page.create_template(name=(folder_name + "_template"))
        # go back to view folder page
        edit_template_page.click_folder_path(folder_name)

    # go to Team members page
    dashboard_page.click_team_members_link()
    team_members_page = TeamMembersPage(driver)
    # edit colleague's permissions so child folder is invisible
    team_members_page.click_edit_team_member(
        config['service']['email_auth_account'])
    edit_team_member_page = InviteUserPage(driver)
    edit_team_member_page.uncheck_folder_permission_checkbox(folder_names[1])
    edit_team_member_page.click_save()

    # check if permissions saved correctly
    dashboard_page.click_team_members_link()
    team_members_page.click_edit_team_member(
        config['service']['email_auth_account'])
    assert not edit_team_member_page.is_checkbox_checked(folder_names[1])
    # log out
    dashboard_page.sign_out()
    # log in as that colleague
    sign_in_email_auth(driver)
    # go to Templates
    dashboard_page.go_to_dashboard_for_service(config['service']['id'])
    dashboard_page.click_templates()
    # click through, see that child folder invisible
    show_templates_page.click_template_by_link_text(folder_names[0])
    child_folder = show_templates_page.get_folder_by_name(folder_names[1])
    name_of_folder_with_invisible_parent = folder_names[
        1] + " " + folder_names[2]
    assert child_folder.text == name_of_folder_with_invisible_parent
    # grandchild folder has folder path as a name
    show_templates_page.click_template_by_link_text(
        name_of_folder_with_invisible_parent)
    # click grandchild folder template to see that it's there
    show_templates_page.click_template_by_link_text(folder_names[2] +
                                                    "_template")
    dashboard_page.sign_out()
    # delete everything
    sign_in(driver, seeded=True)
    dashboard_page.go_to_dashboard_for_service(config['service']['id'])
    dashboard_page.click_templates()
    show_templates_page = ShowTemplatesPage(driver)
    show_templates_page.click_template_by_link_text(folder_names[0])

    view_folder_page = ViewFolderPage(driver)
    view_folder_page.click_template_by_link_text(folder_names[1])
    view_folder_page.click_template_by_link_text(folder_names[2])

    for folder_name in reversed(folder_names):
        view_folder_page.click_template_by_link_text(folder_name + "_template")
        template_page = EditEmailTemplatePage(driver)
        template_page.click_delete()

        view_folder_page.click_manage_folder()
        manage_folder_page = ManageFolderPage(driver)
        manage_folder_page.delete_folder()
        manage_folder_page.confirm_delete_folder()
def test_creating_moving_and_deleting_template_folders(driver,
                                                       login_seeded_user):
    # create new template
    template_name = 'template-for-folder-test {}'.format(uuid.uuid4())
    folder_name = 'test-folder {}'.format(uuid.uuid4())

    dashboard_page = DashboardPage(driver)
    dashboard_page.go_to_dashboard_for_service(config['service']['id'])
    dashboard_page.click_templates()

    show_templates_page = ShowTemplatesPage(driver)
    show_templates_page.click_add_new_template()
    show_templates_page.select_email()

    edit_template_page = EditEmailTemplatePage(driver)
    edit_template_page.create_template(name=template_name)
    template_id = edit_template_page.get_template_id()
    edit_template_page.click_templates()

    # create folder using add to new folder
    show_templates_page.select_template_checkbox(template_id)
    show_templates_page.add_to_new_folder(folder_name)

    # navigate into folder
    show_templates_page.click_template_by_link_text(folder_name)

    # rename folder step
    view_folder_page = ViewFolderPage(driver)
    view_folder_page.click_manage_folder()

    manage_folder_page = ManageFolderPage(driver)
    new_folder_name = folder_name + '-new'
    manage_folder_page.set_name(new_folder_name)
    view_folder_page.assert_name_equals(new_folder_name)

    # try to delete folder
    view_folder_page.click_manage_folder()
    manage_folder_page.delete_folder()  # fails due to not being empty

    # check error message visible
    assert manage_folder_page.get_errors(
    ) == 'You must empty this folder before you can delete it'

    # move template out of folder
    view_folder_page.select_template_checkbox(template_id)
    view_folder_page.move_to_root_template_folder()

    # delete folder
    view_folder_page.click_manage_folder()
    manage_folder_page.delete_folder()
    manage_folder_page.confirm_delete_folder()
    current_folders = [
        x.text
        for x in driver.find_elements_by_class_name('template-list-item-label')
    ]
    if len(current_folders) == 0:
        current_folders = [
            x.text for x in driver.find_elements_by_class_name('message-name')
        ]
    # assert folder not visible
    assert new_folder_name not in current_folders

    # delete template
    show_templates_page.click_template_by_link_text(template_name)
    edit_template_page.click_delete()

    assert template_name not in [
        x.text for x in driver.find_elements_by_class_name('message-name')
    ]