Check that src points to Garage

This commit is contained in:
Nicolas Jeannerod 2024-09-10 12:17:32 +00:00
parent 3652a443b1
commit 9346bcca3d

View file

@ -2,87 +2,110 @@
let let
lib = pkgs.lib; lib = pkgs.lib;
rebuildableTest = import ./rebuildableTest.nix pkgs; rebuildableTest = import ./rebuildableTest.nix pkgs;
seleniumScript = pkgs.writers.writePython3Bin "selenium-script"
{ # FIXME: Replace all the By.XPATH by By.CSS_SELECTOR.
libraries = with pkgs.python3Packages; [ selenium ];
} '' seleniumImports = ''
import sys import sys
import os
import time
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.options import Options
'';
print("Starting selenium script...") seleniumSetup = ''
print("Create and configure driver...", file=sys.stderr)
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...")
options = Options() options = Options()
# options.add_argument("--headless=new") # options.add_argument("--headless=new")
service = webdriver.ChromeService(executable_path="${lib.getExe pkgs.chromedriver}") # noqa: E501 service = webdriver.ChromeService(executable_path="${lib.getExe pkgs.chromedriver}") # noqa: E501
driver = webdriver.Chrome(options=options, service=service) driver = webdriver.Chrome(options=options, service=service)
driver.implicitly_wait(30) driver.implicitly_wait(30)
driver.set_window_size(1280, 960) driver.set_window_size(1280, 960)
'';
# FIXME: Replace the By.XPATH by By.CSS_SELECTOR. seleniumPixelfedLogin = email: password: ''
print("Open login page...", file=sys.stderr)
# Go to Pixelfed and login.
print("Open login page...")
driver.get("http://pixelfed.localhost/login") driver.get("http://pixelfed.localhost/login")
print("Enter email...") print("Enter email...", file=sys.stderr)
driver.find_element(By.ID, "email").send_keys(email) driver.find_element(By.ID, "email").send_keys(${email})
print("Enter password...") print("Enter password...", file=sys.stderr)
driver.find_element(By.ID, "password").send_keys(password) driver.find_element(By.ID, "password").send_keys(${password})
# FIXME: This is disgusting. Find instead the input type submit in the form # FIXME: This is disgusting. Find instead the input type submit in the form
# with action ending in "/login". # with action ending in "/login".
time.sleep(1) print("Click Login button...", file=sys.stderr)
print("Click Login button...")
driver.find_element(By.XPATH, "//button[normalize-space()='Login']").click() 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) time.sleep(3)
media_path = os.environ['POST_MEDIA']
# Find the new post form, fill it in with our pictureand a caption. # 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() 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) 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( driver.find_element(By.CSS_SELECTOR, ".media-body textarea").send_keys(
"Fediversity test of image upload to pixelfed with garage storage." "Fediversity test of image upload to pixelfed with garage storage."
) )
time.sleep(3) time.sleep(3)
print("Click on Post button...") print("Click on Post button...", file=sys.stderr)
driver.find_element(By.LINK_TEXT, "Post").click() driver.find_element(By.LINK_TEXT, "Post").click()
# Wait until the post loads, and in particular its picture, then take a # Wait until the post loads, and in particular its picture, then take a
# screenshot of the whole page. # 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( WebDriverWait(driver, timeout=10).until(
lambda d: d.execute_script( lambda d: d.execute_script("return arguments[0].complete", img)
"return arguments[0].complete",
d.find_element(
By.XPATH, "//div[@class='timeline-status-component-content']//img"
),
)
) )
time.sleep(3) time.sleep(3)
print("Take screenshot...")
if not driver.save_screenshot(screenshot_path):
raise Exception("selenium could not save screenshot")
# All done ^-^ ${seleniumTakeScreenshot "\"/home/selenium/screenshot.png\""}
print("Quitting...") ${seleniumQuit}'';
driver.quit()
print("All done!") 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 in
pkgs.nixosTest { pkgs.nixosTest {
name = "test-pixelfed-garage"; name = "test-pixelfed-garage";
@ -120,7 +143,8 @@ pkgs.nixosTest {
chromium chromium
chromedriver chromedriver
xh xh
seleniumScript seleniumScriptPostPicture
seleniumScriptGetSrc
helix helix
imagemagick imagemagick
]; ];
@ -154,7 +178,7 @@ pkgs.nixosTest {
# there, then post a green image and check that the green pixel IS there. # there, then post a green image and check that the green pixel IS there.
with subtest("Image displays"): 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", "") 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:") 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 # check that the green image displayed somewhere
@ -176,5 +200,10 @@ pkgs.nixosTest {
image_hash = server.succeed("identify -quiet -format '%#' $POST_MEDIA") image_hash = server.succeed("identify -quiet -format '%#' $POST_MEDIA")
if garage_image_hash != image_hash: if garage_image_hash != image_hash:
raise Exception("image stored in garage did not match image uploaded") 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")
''; '';
} }