This repository has been archived on 2024-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
simple-nixos-fediverse/tests/pixelfed-garage.nix

209 lines
7.2 KiB
Nix
Raw Normal View History

2024-09-25 12:02:45 +02:00
{ pkgs }:
2024-08-30 17:23:55 +02:00
let
lib = pkgs.lib;
rebuildableTest = import ./rebuildableTest.nix pkgs;
2024-09-10 14:17:32 +02:00
email = "test@test.com";
password = "testtest";
2024-09-10 14:17:32 +02:00
# FIXME: Replace all the By.XPATH by By.CSS_SELECTOR.
seleniumImports = ''
2024-08-30 17:23:55 +02:00
import sys
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
2024-09-10 14:17:32 +02:00
'';
2024-08-30 17:23:55 +02:00
2024-09-10 14:17:32 +02:00
seleniumSetup = ''
print("Create and configure driver...", file=sys.stderr)
2024-08-30 17:23:55 +02:00
options = Options()
2024-09-09 14:08:12 +02:00
# options.add_argument("--headless=new")
2024-08-30 17:23:55 +02:00
service = webdriver.ChromeService(executable_path="${lib.getExe pkgs.chromedriver}") # noqa: E501
driver = webdriver.Chrome(options=options, service=service)
2024-09-09 14:08:12 +02:00
driver.implicitly_wait(30)
2024-09-09 14:09:54 +02:00
driver.set_window_size(1280, 960)
2024-09-10 14:17:32 +02:00
'';
2024-08-30 17:23:55 +02:00
seleniumPixelfedLogin = ''
2024-09-10 14:17:32 +02:00
print("Open login page...", file=sys.stderr)
2024-08-30 17:23:55 +02:00
driver.get("http://pixelfed.localhost/login")
2024-09-10 14:17:32 +02:00
print("Enter email...", file=sys.stderr)
2024-09-17 12:00:29 +02:00
driver.find_element(By.ID, "email").send_keys("${email}")
2024-09-10 14:17:32 +02:00
print("Enter password...", file=sys.stderr)
2024-09-17 12:00:29 +02:00
driver.find_element(By.ID, "password").send_keys("${password}")
2024-08-30 17:23:55 +02:00
# FIXME: This is disgusting. Find instead the input type submit in the form
# with action ending in "/login".
2024-09-10 14:17:32 +02:00
print("Click Login button...", file=sys.stderr)
2024-08-30 17:23:55 +02:00
driver.find_element(By.XPATH, "//button[normalize-space()='Login']").click()
2024-09-10 14:17:32 +02:00
'';
2024-09-17 12:00:29 +02:00
## NOTE: `path` must be a valid python string, either a variable or _quoted_.
2024-09-10 14:17:32 +02:00
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()
'';
2024-08-30 17:23:55 +02:00
2024-09-10 14:17:32 +02:00
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}
2024-09-09 16:13:13 +02:00
time.sleep(3)
2024-09-10 14:17:32 +02:00
media_path = os.environ['POST_MEDIA']
2024-08-30 17:23:55 +02:00
# Find the new post form, fill it in with our pictureand a caption.
2024-09-10 14:17:32 +02:00
print("Click on Create New Post...", file=sys.stderr)
2024-08-30 17:23:55 +02:00
driver.find_element(By.LINK_TEXT, "Create New Post").click()
2024-09-10 14:17:32 +02:00
print("Add file to input element...", file=sys.stderr)
2024-09-09 16:13:13 +02:00
driver.find_element(By.XPATH, "//input[@type='file']").send_keys(media_path)
2024-09-10 14:17:32 +02:00
print("Add a caption", file=sys.stderr)
2024-09-09 16:13:13 +02:00
driver.find_element(By.CSS_SELECTOR, ".media-body textarea").send_keys(
"Fediversity test of image upload to pixelfed with garage storage."
)
time.sleep(3)
2024-09-10 14:17:32 +02:00
print("Click on Post button...", file=sys.stderr)
2024-08-30 17:23:55 +02:00
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.
2024-09-10 14:17:32 +02:00
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"
)
2024-08-30 17:23:55 +02:00
WebDriverWait(driver, timeout=10).until(
2024-09-10 14:17:32 +02:00
lambda d: d.execute_script("return arguments[0].complete", img)
2024-08-30 17:23:55 +02:00
)
2024-09-09 16:13:13 +02:00
time.sleep(3)
2024-08-30 17:23:55 +02:00
2024-09-10 14:17:32 +02:00
${seleniumTakeScreenshot "\"/home/selenium/screenshot.png\""}
${seleniumQuit}'';
seleniumScriptGetSrc = pkgs.writers.writePython3Bin "selenium-script-get-src"
{
libraries = with pkgs.python3Packages; [ selenium ];
} ''
${seleniumImports}
${seleniumSetup}
${seleniumPixelfedLogin}
2024-09-10 14:17:32 +02:00
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}'';
2024-08-30 17:23:55 +02:00
in
pkgs.nixosTest {
name = "test-pixelfed-garage";
nodes = {
server = { config, ... }: {
2024-09-09 14:08:12 +02:00
services = {
xserver = {
enable = true;
displayManager.lightdm.enable = true;
desktopManager.lxqt.enable = true;
};
displayManager.autoLogin = {
enable = true;
user = "selenium";
};
};
virtualisation.resolution = { x = 1680; y = 1050; };
2024-08-30 17:23:55 +02:00
virtualisation = {
memorySize = lib.mkVMOverride 8192;
cores = 8;
};
2024-09-25 12:02:45 +02:00
imports = [ ../../vm/pixelfed-vm.nix ];
2024-08-30 17:23:55 +02:00
# TODO: pair down
environment.systemPackages = with pkgs; [
python3
chromium
chromedriver
2024-08-30 17:23:55 +02:00
xh
2024-09-10 14:17:32 +02:00
seleniumScriptPostPicture
seleniumScriptGetSrc
2024-08-30 17:23:55 +02:00
helix
imagemagick
];
environment.variables = {
2024-09-09 16:12:54 +02:00
POST_MEDIA = ./fediversity.png;
2024-09-09 16:13:23 +02:00
AWS_ACCESS_KEY_ID = config.services.garage.ensureKeys.pixelfed.id;
AWS_SECRET_ACCESS_KEY = config.services.garage.ensureKeys.pixelfed.secret;
};
# chrome does not like being run as root
2024-09-09 14:09:54 +02:00
users.users.selenium = {
isNormalUser = true;
2024-08-30 17:23:55 +02:00
};
};
};
2024-09-20 17:45:53 +02:00
testScript = { nodes, ... }: ''
2024-08-30 17:23:55 +02:00
import re
server.start()
with subtest("Pixelfed starts"):
server.wait_for_unit("phpfpm-pixelfed.service")
with subtest("Account creation"):
2024-09-17 12:00:29 +02:00
server.succeed("pixelfed-manage user:create --name=test --username=test --email=${email} --password=${password} --confirm_email=1")
2024-08-30 17:23:55 +02:00
# NOTE: This could in theory give a false positive if pixelfed changes it's
# colorscheme to include pure green. (see same problem in pixelfed-garage.nix).
2024-08-30 17:23:55 +02:00
# TODO: For instance: post a red image and check that the green pixel IS NOT
# there, then post a green image and check that the green pixel IS there.
2024-09-09 14:09:54 +02:00
with subtest("Image displays"):
2024-09-17 12:00:29 +02:00
server.succeed("su - selenium -c 'selenium-script-post-picture ${email} ${password}'")
2024-09-09 14:09:54 +02:00
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:")
2024-08-30 17:23:55 +02:00
# check that the green image displayed somewhere
2024-09-09 16:12:54 +02:00
image_check = re.match(".*#FF0500.*", displayed_colors, re.S)
if image_check is None:
2024-08-30 17:23:55 +02:00
raise Exception("cannot detect the uploaded image on pixelfed page.")
2024-09-09 16:13:23 +02:00
with subtest("access garage"):
2024-09-20 17:45:53 +02:00
server.succeed("mc alias set garage ${nodes.server.fediversity.internal.garage.api.url} --api s3v4 --path off $AWS_ACCESS_KEY_ID $AWS_SECRET_ACCESS_KEY")
2024-09-09 16:13:23 +02:00
server.succeed("mc ls garage/pixelfed")
with subtest("access image in garage"):
2024-09-17 12:00:29 +02:00
image = server.succeed("mc find garage --regex '\\.png' --ignore '*_thumb.png'")
2024-09-09 16:13:23 +02:00
image = image.rstrip()
if image == "":
2024-09-20 17:45:53 +02:00
raise Exception("image posted to Pixelfed did not get stored in garage")
2024-09-09 16:13:23 +02:00
server.succeed(f"mc cat {image} >/garage-image.png")
garage_image_hash = server.succeed("identify -quiet -format '%#' /garage-image.png")
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")
2024-09-10 14:17:32 +02:00
with subtest("Check that image comes from garage"):
2024-09-17 12:00:29 +02:00
src = server.succeed("su - selenium -c 'selenium-script-get-src ${email} ${password}'")
2024-09-20 17:45:53 +02:00
if not src.startswith("${nodes.server.fediversity.internal.garage.web.urlFor "pixelfed"}"):
2024-09-10 14:17:32 +02:00
raise Exception("image does not come from garage")
2024-08-30 17:23:55 +02:00
'';
}