예제 #1
0
def edit_article(sr,
                 vals,
                 toast_type="info",
                 expected_error=None,
                 expected_constraints=None):  #pylint: disable=too-many-branches
    """Edit a article's details."""

    # initialize
    if sr:
        select_sr_menu_option(sr, "edit")
    else:
        pass  # nb: we assume that the dialog is already on-screen
    dlg = wait_for_elem(2, "#article-form")

    # update the specified article's details
    _update_values(dlg, vals)
    set_toast_marker(toast_type)
    find_child("button.ok", dlg).click()

    # check what happened
    if expected_error:
        # we were expecting an error, confirm the error message
        check_error_msg(expected_error)
        return dlg  # nb: the dialog is left on-screen
    elif expected_constraints:
        # we were expecting constraint warnings, confirm them
        check_constraint_warnings("Do you want to update this article?",
                                  expected_constraints, "cancel")
        return dlg  # nb: the dialog is left on-screen
    else:
        # we were expecting the update to work, confirm this
        expected = "updated OK" if sr else "created OK"
        wait_for(2, lambda: check_toast(toast_type, expected, contains=True))
        wait_for_not_elem(2, "#article-form")
        return None
예제 #2
0
def create_article(vals,
                   toast_type="info",
                   expected_error=None,
                   expected_constraints=None,
                   dlg=None):
    """Create a new article."""

    # initialize
    set_toast_marker(toast_type)

    # create the new article
    if not dlg:
        select_main_menu_option("new-article")
        dlg = wait_for_elem(2, "#article-form")
    _update_values(dlg, vals)
    find_child("button.ok", dlg).click()

    # check what happened
    if expected_error:
        # we were expecting an error, confirm the error message
        check_error_msg(expected_error)
        return dlg  # nb: the dialog is left on-screen
    elif expected_constraints:
        # we were expecting constraint warnings, confirm them
        check_constraint_warnings("Do you want to create this article?",
                                  expected_constraints, "cancel")
        return dlg  # nb: the dialog is left on-screen
    else:
        # we were expecting the create to work, confirm this
        wait_for(2,
                 lambda: check_toast(toast_type, "created OK", contains=True))
        wait_for_not_elem(2, "#article-form")
        return None
예제 #3
0
def edit_publisher( sr, vals, toast_type="info", expected_error=None ):
    """Edit a publisher's details."""

    # initialize
    if sr:
        select_sr_menu_option( sr, "edit" )
    else:
        pass # nb: we assume that the dialog is already on-screen
    dlg = wait_for_elem( 2, "#publisher-form" )

    # update the specified publisher's details
    _update_values( dlg, vals )
    set_toast_marker( toast_type )
    find_child( "button.ok", dlg ).click()

    # check what happened
    if expected_error:
        # we were expecting an error, confirm the error message
        check_error_msg( expected_error )
    else:
        # we were expecting the update to work, confirm this
        expected = "updated OK" if sr else "created OK"
        wait_for( 2,
            lambda: check_toast( toast_type, expected, contains=True )
        )
        wait_for_not_elem( 2, "#publisher-form" )
예제 #4
0
def create_publisher( vals, toast_type="info", expected_error=None, dlg=None ):
    """Create a new publisher."""

    # initialize
    set_toast_marker( toast_type )

    # create the new publisher
    if not dlg:
        select_main_menu_option( "new-publisher" )
        dlg = wait_for_elem( 2, "#publisher-form" )
    _update_values( dlg, vals )
    find_child( "button.ok", dlg ).click()

    # check what happened
    if expected_error:
        # we were expecting an error, confirm the error message
        check_error_msg( expected_error )
        return dlg # nb: the dialog is left on-screen
    else:
        # we were expecting the create to work, confirm this
        wait_for( 2,
            lambda: check_toast( toast_type, "created OK", contains=True )
        )
        wait_for_not_elem( 2, "#publisher-form" )
        return None
예제 #5
0
def test_images(webdriver, flask_app, dbconn):  #pylint: disable=too-many-statements
    """Test article images."""

    # initialize
    init_tests(webdriver, flask_app, dbconn, max_image_upload_size=2 * 1024)
    article_sr = article_id = None

    def check_image(expected):

        # check the image in the article's search result
        def check_sr_image():
            img = find_child("img.image", article_sr)
            if expected:
                expected_url = flask_app.url_for("get_image",
                                                 image_type="article",
                                                 image_id=article_id)
                image_url = img.get_attribute("src").split("?")[0]
                return image_url == expected_url
            else:
                return not img

        wait_for(2, check_sr_image)

        # check the image in the article's config
        select_sr_menu_option(article_sr, "edit")
        dlg = wait_for_elem(2, "#article-form")
        if expected:
            # make sure there is an image
            img = find_child(".row.image img.image", dlg)
            image_url = img.get_attribute("src")
            assert "/images/article/{}".format(article_id) in image_url
            # make sure the "remove image" icon is visible
            btn = find_child(".row.image .remove-image", dlg)
            assert btn.is_displayed()
            # make sure the article's image is correct
            resp = urllib.request.urlopen(image_url).read()
            assert resp == open(expected, "rb").read()
        else:
            # make sure there is no image
            img = find_child(".row.image img.image", dlg)
            assert img.get_attribute("src").endswith("/images/placeholder.png")
            # make sure the "remove image" icon is hidden
            btn = find_child(".row.image .remove-image", dlg)
            assert not btn.is_displayed()
            # make sure the article's image is not available
            url = flask_app.url_for("get_image",
                                    image_type="article",
                                    image_id=article_id)
            try:
                resp = urllib.request.urlopen(url)
                assert False, "Should never get here!"
            except urllib.error.HTTPError as ex:
                assert ex.code == 404
        find_child(".cancel", dlg).click()

    # create an article with no image
    create_article({"title": "Test Article"})
    results = get_search_results()
    assert len(results) == 1
    article_sr = results[0]
    article_id = article_sr.get_attribute("testing--article_id")
    check_image(None)

    # add an image to the article
    fname = os.path.join(os.path.split(__file__)[0], "fixtures/images/1.gif")
    edit_article(article_sr, {"image": fname})
    check_image(fname)

    # change the article's image
    fname = os.path.join(os.path.split(__file__)[0], "fixtures/images/2.gif")
    edit_article(article_sr, {"image": fname})
    check_image(fname)

    # remove the article's image
    edit_article(article_sr, {"image": None})
    check_image(None)

    # try to upload an image that's too large
    select_sr_menu_option(article_sr, "edit")
    dlg = wait_for_elem(2, "#article-form")
    data = base64.b64encode(5000 * b" ")
    data = "{}|{}".format("too-big.png", data.decode("ascii"))
    send_upload_data(data,
                     lambda: find_child(".row.image img.image", dlg).click())
    check_error_msg("The file must be no more than 2 KB in size.")