def test_get_custom_homepage_page_with_default_data(app):
    response = do_get(app, CUSTOM_HOMEPAGE_URL, is_sysadmin=True)

    check_custom_homepage_html(response,
                               expected_form_data=DEFAULT_DATA.copy())

    homepage_response = do_get(app, '/', is_sysadmin=False)

    check_homepage_html(homepage_response, expected_data=DEFAULT_HEADERS)
def test_add_unsupported_link_to_custom_header(app):
    title = 'example'
    link = 'http://example.com'
    data = {
        'new_title': title,
        'new_link': link,
    }
    expected_links = [
        {'position': 4, 'title': title, 'link': link},
    ]
    expected_links.extend(DEFAULT_LINKS)

    expected_headers = list(DEFAULT_HEADERS)

    custom_header_response = do_get(app, CUSTOM_HEADER_URL, is_sysadmin=True)
    check_custom_header_page_html(custom_header_response, links=[], headers=expected_headers, default_layout=True)

    response = do_post(app, ADD_LINK_TO_HEADER_URL, data, is_sysadmin=True)
    expected_error_messages = [
        'Ckanext.opendata theme.custom header.data: Only HTTPS urls supported "{}"'.format(link)
    ]
    check_custom_header_page_html(response,
                                  links=expected_links,
                                  headers=expected_headers,
                                  errors=expected_error_messages)
def test_update_multiple_custom_header_links(app):
    data = {
        'layout_type': 'default',
        'link': ['/dataset/', '/organization/', '/group/', '/about'],
        'position': ['3', '2', '1', '0'],
        'title': ['datasets updated', 'organizations updated', 'groups updated', 'about']
    }
    expected_links = (
        {'position': 3, 'title': 'datasets updated', 'link': '/dataset/'},
        {'position': 2, 'title': 'organizations updated', 'link': '/organization/'},
        {'position': 1, 'title': 'groups updated', 'link': '/group/'},
        {'position': 0, 'title': 'about', 'link': '/about'},
    )
    expected_headers = (
        {'title': 'datasets updated', 'link': '/dataset/'},
        {'title': 'organizations updated', 'link': '/organization/'},
        {'title': 'groups updated', 'link': '/group/'},
        {'title': 'about', 'link': '/about'},
    )

    custom_header_response = do_get(app, CUSTOM_HEADER_URL, is_sysadmin=True)
    check_custom_header_page_html(custom_header_response, links=[], headers=list(DEFAULT_HEADERS), default_layout=True)

    response = do_post(app, CUSTOM_HEADER_URL, data, is_sysadmin=True)
    check_custom_header_page_html(response, links=expected_links, headers=expected_headers)
def test_get_reset_custom_footer_page(app):
    response = do_get(app, RESET_CUSTOM_FOOTER_URL, is_sysadmin=True)
    expected_data = {
        'layout_type': 1,
        'content_0': '',
        'content_1': '',
        'content_2': '',
        'content_3': '',
    }
    check_custom_footer_page_html(response, **expected_data)
def test_remove_link_to_custom_header(app):
    data = {
        'to_remove': 'about',
    }
    expected_links = list(DEFAULT_LINKS)

    expected_links.pop(3)

    expected_headers = list(DEFAULT_HEADERS)
    expected_headers.pop(3)

    custom_header_response = do_get(app, CUSTOM_HEADER_URL, is_sysadmin=True)
    check_custom_header_page_html(custom_header_response, links=[], headers=expected_headers, default_layout=True)

    response = do_post(app, REMOVE_LINK_FROM_HEADER_URL, data, is_sysadmin=True)
    check_custom_header_page_html(response, links=expected_links, headers=expected_headers)
    assert 'about' not in response
def test_update_single_custom_header_links(app):
    data = {
        'layout_type': 'compressed',
        'link': '/dataset/',
        'position': '0',
        'title': 'datasets updated',
    }
    expected_links = (
        {'position': 0, 'title': 'datasets updated', 'link': '/dataset/'},
    )
    expected_headers = (
        {'title': 'datasets updated', 'link': '/dataset/'},
    )

    custom_header_response = do_get(app, CUSTOM_HEADER_URL, is_sysadmin=True)
    check_custom_header_page_html(custom_header_response, links=[], headers=list(DEFAULT_HEADERS), default_layout=True)

    response = do_post(app, CUSTOM_HEADER_URL, data, is_sysadmin=True)
    check_custom_header_page_html(response, links=expected_links, headers=expected_headers, default_layout=False)
def test_reset_custom_homepage_changes(app):
    data = {
        'custom_homepage_layout': '2',
        'datasets-popular-custom-name': 'Test 1',
        'datasets-recent-custom-name': 'Test 2',
        'groups-custom-name': 'Test 3',
        'showcase-custom-name': 'Test 4'
    }
    response = do_post(app, CUSTOM_HOMEPAGE_URL, is_sysadmin=True, data=data)
    assert response.status_code == 200

    reset_response = do_post(app,
                             RESET_CUSTOM_HOMEPAGE_URL,
                             is_sysadmin=True,
                             data={})
    check_custom_homepage_html(reset_response,
                               expected_form_data=DEFAULT_DATA.copy())

    homepage_response = do_get(app, '/', is_sysadmin=False)
    check_homepage_html(homepage_response, expected_data=DEFAULT_HEADERS)
def test_post_custom_homepage_with_changes(app):
    data = {
        'custom_homepage_layout': '2',
        'datasets-popular-custom-name': 'Test 1',
        'datasets-recent-custom-name': 'Test 2',
        'groups-custom-name': 'Test 3',
        'showcase-custom-name': 'Test 4'
    }
    response = do_post(app, CUSTOM_HOMEPAGE_URL, is_sysadmin=True, data=data)

    check_custom_homepage_html(response, expected_form_data=data.copy())

    homepage_response = do_get(app, '/', is_sysadmin=False)

    expected_homepage_headers = (
        'Test 1',
        'Test 2',
    )
    check_homepage_html(homepage_response,
                        expected_data=expected_homepage_headers)
def test_add_link_to_custom_header(app):
    title = 'example'
    link = 'https://example.com'
    data = {
        'new_title': title,
        'new_link': link,
    }
    expected_links = [
        {'position': 4, 'title': title, 'link': link},
    ]
    expected_links.extend(DEFAULT_LINKS)

    expected_headers = [
        {'title': title, 'link': link},
    ]
    expected_headers.extend(DEFAULT_HEADERS)

    custom_header_response = do_get(app, CUSTOM_HEADER_URL, is_sysadmin=True)
    check_custom_header_page_html(custom_header_response, links=[], headers=DEFAULT_HEADERS, default_layout=True)

    response = do_post(app, ADD_LINK_TO_HEADER_URL, data, is_sysadmin=True)
    check_custom_header_page_html(response, links=expected_links, headers=expected_headers)
def test_get_custom_css_page_with_default_data(app):
    response = do_get(app, CUSTOM_CSS_URL, is_sysadmin=True)

    check_custom_css_page_html(response,
                               expected_form_data=DEFAULT_DATA.copy(),
                               expected_css_data=DEFAULT_CUSTOM_CSS)
def test_get_custom_css_page_with_not_sysadmin_user(app):
    with pytest.raises(NotAuthorized):
        do_get(app, CUSTOM_CSS_URL, is_sysadmin=False)
def test_get_custom_homepage_page_with_not_sysadmin_user(app):
    with pytest.raises(NotAuthorized):
        do_get(app, CUSTOM_HOMEPAGE_URL, is_sysadmin=False)
def test_get_custom_header_page(app):
    response = do_get(app, CUSTOM_HEADER_URL, is_sysadmin=True)
    check_custom_header_page_html(response, links=[], headers=DEFAULT_HEADERS, default_layout=True)
def test_get_custom_footer_page(app):
    response = do_get(app, CUSTOM_FOOTER_URL, is_sysadmin=True)

    assert 1 == response.body.count('data-module="ckedit"')