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
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")
'';
}