def test_homepage_topics_display_in_rows_with_three_columns(
        number_of_topics, row_counts, test_app_client, logged_in_rdu_user):
    for i in range(number_of_topics):
        topic = TopicFactory(slug=f"topic-{i}",
                             title=f"Test topic page #{i}",
                             short_title=f"Testing #{i}")
        subtopic = SubtopicFactory(slug=f"subtopic-{i}",
                                   title=f"Test subtopic page #{i}",
                                   topic=topic)
        measure = MeasureFactory(slug=f"measure-{i}", subtopics=[subtopic])
        MeasureVersionFactory(status="APPROVED",
                              title=f"Test measure page #{i}",
                              version="1.0",
                              measure=measure)

    resp = test_app_client.get(url_for("static_site.index"))
    assert resp.status_code == 200

    page = BeautifulSoup(resp.data.decode("utf-8"), "html.parser")
    topic_rows = page.select(".topic-row")

    assert len(topic_rows) == len(row_counts)
    for i, topic_row in enumerate(topic_rows):
        assert len(topic_rows[i].select(".topic")) == row_counts[i]

    for i in range(number_of_topics):
        assert page.select(".topic a")[i].text.strip() == f"Testing #{i}"
def test_view_topic_page_contains_reordering_javascript_for_admin_user_only(
        test_app_client):
    rdu_user = UserFactory(user_type=TypeOfUser.RDU_USER)
    admin_user = UserFactory(user_type=TypeOfUser.ADMIN_USER)
    topic = TopicFactory(title="Test topic page")

    with test_app_client.session_transaction() as session:
        session["user_id"] = admin_user.id

    resp = test_app_client.get(
        url_for("static_site.topic", topic_slug=topic.slug))

    assert resp.status_code == 200
    page = BeautifulSoup(resp.data.decode("utf-8"), "html.parser")
    assert page.h1.text.strip() == "Test topic page"
    assert len(
        page.find_all("script",
                      text=re.compile("setupReorderableTables"))) == 1

    with test_app_client.session_transaction() as session:
        session["user_id"] = rdu_user.id

    resp = test_app_client.get(
        url_for("static_site.topic", topic_slug=topic.slug))

    assert resp.status_code == 200
    page = BeautifulSoup(resp.data.decode("utf-8"), "html.parser")
    assert page.h1.text.strip() == "Test topic page"
    assert len(
        page.find_all("script",
                      text=re.compile("setupReorderableTables"))) == 0
def test_view_topic_page(test_app_client, logged_in_rdu_user):
    topic = TopicFactory(title="Test topic page", short_title="Testing")
    resp = test_app_client.get(
        url_for("static_site.topic", topic_slug=topic.slug))

    assert resp.status_code == 200
    page = BeautifulSoup(resp.data.decode("utf-8"), "html.parser")
    assert page.h1.text.strip() == "Test topic page"
    def test_get_topics(self, topic_slugs, include_testing_space,
                        expected_topic_count):
        for topic_slug in topic_slugs:
            TopicFactory(slug=topic_slug)

        topics = page_service.get_topics(
            include_testing_space=include_testing_space)

        assert len(topics) == expected_topic_count
def test_view_sandbox_topic(test_app_client, logged_in_rdu_user):

    sandbox_topic = TopicFactory(slug=TESTING_SPACE_SLUG,
                                 title="Test sandbox topic")
    resp = test_app_client.get(
        url_for("static_site.topic", topic_slug=sandbox_topic.slug))

    assert resp.status_code == 200
    page = BeautifulSoup(resp.data.decode("utf-8"), "html.parser")
    assert page.h1.text.strip() == "Test sandbox topic"
def test_topic_meta_description(test_app_client, logged_in_rdu_user):
    TopicFactory(
        slug="test-topic",
        meta_description="I'm a description sentence for search engines.")

    resp = test_app_client.get(
        url_for("static_site.topic", topic_slug="test-topic"))
    page = BeautifulSoup(resp.data.decode("utf-8"), "html.parser")

    assert resp.status_code == 200

    meta_description = page.findAll("meta", property="description")
    assert len(
        meta_description) == 1, f"Missing meta description on topic page"
    assert meta_description[0].get(
        "content") == "I'm a description sentence for search engines."
def test_adding_an_existing_data_source(driver, app, live_server):
    rdu_user = UserFactory(user_type=TypeOfUser.RDU_USER, active=True)

    topic = TopicFactory.create(title="Police and crime")
    subtopic = SubtopicFactory.create(title="Policing", topic=topic)
    DataSourceFactory.create(title="Police statistics 2019")

    existing_measure = MeasureFactory.create(subtopics=[subtopic])
    MeasureVersionFactory.create(status="APPROVED", measure=existing_measure)

    driver_login(driver, live_server, rdu_user)
    home_page = HomePage(driver, live_server)

    home_page.click_topic_link(topic)

    topic_page = TopicPage(driver, live_server, topic)
    topic_page.expand_accordion_for_subtopic(subtopic)

    topic_page.click_add_measure(subtopic)

    create_measure_page = MeasureEditPage(driver)
    create_measure_page.set_title("Arrests")
    create_measure_page.click_save()

    create_measure_page.click_add_primary_data_source()

    fill_in(driver,
            label_text="Search for an existing data source",
            with_text="Police statistics")
    click_button_with_text(driver, "Search")

    label_for_existing_data_source = driver.find_element_by_xpath(
        "//label[text()='Police statistics 2019']")

    label_for_existing_data_source.click()

    click_button_with_text(driver, "Select")

    assert "Successfully added the data source ‘Police statistics 2019’" in driver.page_source

    click_link_with_text(driver, "Preview this version")

    assert "Police statistics 2019" in driver.page_source
def test_view_topic_page_in_static_mode_does_not_contain_reordering_javascript(
        test_app_client, logged_in_admin_user):
    topic = TopicFactory(title="Test topic page")
    resp = test_app_client.get(
        url_for("static_site.topic", topic_slug=topic.slug))

    assert resp.status_code == 200
    page = BeautifulSoup(resp.data.decode("utf-8"), "html.parser")
    assert page.h1.text.strip() == "Test topic page"
    assert len(
        page.find_all("script",
                      text=re.compile("setupReorderableTables"))) == 1

    resp = test_app_client.get(
        url_for("static_site.topic", topic_slug=topic.slug, static_mode=True))

    assert resp.status_code == 200
    page = BeautifulSoup(resp.data.decode("utf-8"), "html.parser")
    assert page.h1.text.strip() == "Test topic page"
    assert len(
        page.find_all("script",
                      text=re.compile("setupReorderableTables"))) == 0
def test_version_history(test_app_client, logged_in_rdu_user):

    topic = TopicFactory(slug="my-topic")
    subtopic = SubtopicFactory(slug="my-subtopic", topic=topic)

    measure = MeasureFactory(slug="my-measure", subtopics=[subtopic])

    MeasureVersionFactory(
        measure=measure,
        status="APPROVED",
        version="1.0",
        published_at=datetime.datetime(2018, 1, 10),
        external_edit_summary="First published",
    )

    MeasureVersionFactory(
        measure=measure,
        status="APPROVED",
        version="1.1",
        published_at=datetime.datetime(2018, 2, 20),
        external_edit_summary="Fixed a spelling mistake.",
    )

    MeasureVersionFactory(
        measure=measure,
        status="APPROVED",
        version="1.2",
        published_at=datetime.datetime(2018, 12, 13),
        external_edit_summary="Updated headings for clarity.",
    )

    resp = test_app_client.get("/my-topic/my-subtopic/my-measure/latest")

    assert resp.status_code == 200

    page = BeautifulSoup(resp.data.decode("utf-8"), "html.parser")

    details_tag = details_tag_with_summary(page, "full page history")

    assert details_tag

    details_text = details_tag.get_text(separator="\n", strip=True).split("\n")

    assert details_text == [
        "full page history",
        "13 December 2018",
        "Updated headings for clarity.",
        "20 February 2018",
        "Fixed a spelling mistake.",
        "10 January 2018",
        "First published",
    ]

    first_published_link = find_link_with_text(details_tag, "10 January 2018")

    assert first_published_link
    assert first_published_link.get(
        "href") == "/my-topic/my-subtopic/my-measure/1.0"

    spelling_mistake_link = find_link_with_text(details_tag,
                                                "20 February 2018")

    assert spelling_mistake_link
    assert spelling_mistake_link.get(
        "href") == "/my-topic/my-subtopic/my-measure/1.1"

    updated_headings_link = find_link_with_text(details_tag,
                                                "13 December 2018")

    assert updated_headings_link
    assert updated_headings_link.get(
        "href") == "/my-topic/my-subtopic/my-measure/1.2"
def test_references_between_subtopic_and_topic():
    topic = TopicFactory()
    subtopic = SubtopicFactory(topic=topic)

    assert topic.subtopics == [subtopic]
    assert subtopic.topic == topic
 def test_get_topic_raises_if_no_topic_found(self):
     TopicFactory(slug="topic-slug")
     with pytest.raises(PageNotFoundException):
         page_service.get_topic("not-the-right-slug")
 def test_get_topic_finds_topic_by_slug(self):
     topic = TopicFactory(slug="topic-slug")
     assert page_service.get_topic("topic-slug") is topic