From 82053303410ba7c4327dd570a52c1ab7b3f70e46 Mon Sep 17 00:00:00 2001 From: Nicolas Jeannerod Date: Tue, 10 Sep 2024 12:17:32 +0000 Subject: [PATCH] Check that `src` points to Garage --- tests/pixelfed-garage.nix | 127 +++++++++++++++++++++++--------------- 1 file changed, 78 insertions(+), 49 deletions(-) diff --git a/tests/pixelfed-garage.nix b/tests/pixelfed-garage.nix index ded7778..853b01f 100644 --- a/tests/pixelfed-garage.nix +++ b/tests/pixelfed-garage.nix @@ -2,87 +2,110 @@ let lib = pkgs.lib; rebuildableTest = import ./rebuildableTest.nix pkgs; - seleniumScript = pkgs.writers.writePython3Bin "selenium-script" - { - libraries = with pkgs.python3Packages; [ selenium ]; - } '' + + # FIXME: Replace all the By.XPATH by By.CSS_SELECTOR. + + seleniumImports = '' import sys - import os - import time from selenium import webdriver from selenium.webdriver.common.by import By - from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.chrome.options import Options + ''; - print("Starting selenium script...") - - email = sys.argv[1] - password = sys.argv[2] - - media_path = os.environ['POST_MEDIA'] - screenshot_path = "/home/selenium/screenshot.png" - - # Create and configure driver. It is important to set the window size such that - # the “Create New Post” button is visible. - print("Create and configure driver...") + seleniumSetup = '' + print("Create and configure driver...", file=sys.stderr) options = Options() # options.add_argument("--headless=new") service = webdriver.ChromeService(executable_path="${lib.getExe pkgs.chromedriver}") # noqa: E501 driver = webdriver.Chrome(options=options, service=service) driver.implicitly_wait(30) driver.set_window_size(1280, 960) + ''; - # FIXME: Replace the By.XPATH by By.CSS_SELECTOR. - - # Go to Pixelfed and login. - print("Open login page...") + seleniumPixelfedLogin = email: password: '' + print("Open login page...", file=sys.stderr) driver.get("http://pixelfed.localhost/login") - print("Enter email...") - driver.find_element(By.ID, "email").send_keys(email) - print("Enter password...") - driver.find_element(By.ID, "password").send_keys(password) + print("Enter email...", file=sys.stderr) + driver.find_element(By.ID, "email").send_keys(${email}) + print("Enter password...", file=sys.stderr) + driver.find_element(By.ID, "password").send_keys(${password}) # FIXME: This is disgusting. Find instead the input type submit in the form # with action ending in "/login". - time.sleep(1) - print("Click “Login” button...") + print("Click “Login” button...", file=sys.stderr) driver.find_element(By.XPATH, "//button[normalize-space()='Login']").click() + ''; + seleniumTakeScreenshot = path: '' + print("Take screenshot...", file=sys.stderr) + if not driver.save_screenshot(${path}): + raise Exception("selenium could not save screenshot") + ''; + + seleniumQuit = '' + print("Quitting...", file=sys.stderr) + driver.quit() + ''; + + seleniumScriptPostPicture = pkgs.writers.writePython3Bin "selenium-script-post-picture" + { + libraries = with pkgs.python3Packages; [ selenium ]; + } '' + import os + import time + ${seleniumImports} + from selenium.webdriver.support.wait import WebDriverWait + + ${seleniumSetup} + ${seleniumPixelfedLogin "sys.argv[1]" "sys.argv[2]"} time.sleep(3) + media_path = os.environ['POST_MEDIA'] + # Find the new post form, fill it in with our pictureand a caption. - print("Click on “Create New Post”...") + print("Click on “Create New Post”...", file=sys.stderr) driver.find_element(By.LINK_TEXT, "Create New Post").click() - print("Add file to input element...") + print("Add file to input element...", file=sys.stderr) driver.find_element(By.XPATH, "//input[@type='file']").send_keys(media_path) - print("Add a caption") + print("Add a caption", file=sys.stderr) driver.find_element(By.CSS_SELECTOR, ".media-body textarea").send_keys( "Fediversity test of image upload to pixelfed with garage storage." ) time.sleep(3) - print("Click on “Post” button...") + print("Click on “Post” button...", file=sys.stderr) driver.find_element(By.LINK_TEXT, "Post").click() # Wait until the post loads, and in particular its picture, then take a # screenshot of the whole page. - print("Wait for post and image to be loaded...") + print("Wait for post and image to be loaded...", file=sys.stderr) + img = driver.find_element( + By.XPATH, + "//div[@class='timeline-status-component-content']//img" + ) WebDriverWait(driver, timeout=10).until( - lambda d: d.execute_script( - "return arguments[0].complete", - d.find_element( - By.XPATH, "//div[@class='timeline-status-component-content']//img" - ), - ) + lambda d: d.execute_script("return arguments[0].complete", img) ) time.sleep(3) - print("Take screenshot...") - if not driver.save_screenshot(screenshot_path): - raise Exception("selenium could not save screenshot") - # All done ^-^ - print("Quitting...") - driver.quit() - print("All done!") - ''; + ${seleniumTakeScreenshot "\"/home/selenium/screenshot.png\""} + ${seleniumQuit}''; + + seleniumScriptGetSrc = pkgs.writers.writePython3Bin "selenium-script-get-src" + { + libraries = with pkgs.python3Packages; [ selenium ]; + } '' + ${seleniumImports} + ${seleniumSetup} + ${seleniumPixelfedLogin "sys.argv[1]" "sys.argv[2]"} + + img = driver.find_element( + By.XPATH, + "//div[@class='timeline-status-component-content']//img" + ) + # REVIEW: Need to wait for it to be loaded? + print(img.get_attribute('src')) + + ${seleniumQuit}''; + in pkgs.nixosTest { name = "test-pixelfed-garage"; @@ -120,7 +143,8 @@ pkgs.nixosTest { chromium chromedriver xh - seleniumScript + seleniumScriptPostPicture + seleniumScriptGetSrc helix imagemagick ]; @@ -154,7 +178,7 @@ pkgs.nixosTest { # there, then post a green image and check that the green pixel IS there. with subtest("Image displays"): - server.succeed(f"su - selenium -c 'selenium-script test@test.com {password}' >&2") + server.succeed(f"su - selenium -c 'selenium-script-post-picture test@test.com {password}'") server.copy_from_vm("/home/selenium/screenshot.png", "") displayed_colors = server.succeed("magick /home/selenium/screenshot.png -define histogram:unique-colors=true -format %c histogram:info:") # check that the green image displayed somewhere @@ -176,5 +200,10 @@ pkgs.nixosTest { image_hash = server.succeed("identify -quiet -format '%#' $POST_MEDIA") if garage_image_hash != image_hash: raise Exception("image stored in garage did not match image uploaded") + + with subtest("Check that image comes from garage"): + src = server.succeed(f"su - selenium -c 'selenium-script-get-src test@test.com {password}'") + if not src.startswith("http://pixelfed.web.garage.localhost:3902/"): + raise Exception("image does not come from garage") ''; }