simple-nixos-fediverse/tests/pixelfed-garage.nix

143 lines
4.9 KiB
Nix

{ pkgs, self }:
let
lib = pkgs.lib;
rebuildableTest = import ./rebuildableTest.nix pkgs;
seleniumScript = pkgs.writers.writePython3Bin "selenium-script"
{
libraries = with pkgs.python3Packages; [ selenium ];
} ''
import sys
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]
green_path = "${./green.png}"
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.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(10)
driver.set_window_size(1280, 960)
# FIXME: Replace the By.XPATH by By.CSS_SELECTOR.
# Go to Pixelfed and login.
print("Open login page...")
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)
# FIXME: This is disgusting. Find instead the input type submit in the form
# with action ending in "/login".
print("Click Login button...")
driver.find_element(By.XPATH, "//button[normalize-space()='Login']").click()
# Find the new post form, fill it in with our pictureand a caption.
print("Click on Create New Post...")
driver.find_element(By.LINK_TEXT, "Create New Post").click()
print("Add file to input element...")
driver.find_element(By.XPATH, "//input[@type='file']").send_keys(green_path)
print("Click on Post button...")
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...")
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"
),
)
)
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!")
'';
in
pkgs.nixosTest {
name = "test-pixelfed-garage";
nodes = {
server = { config, ... }: {
virtualisation = {
memorySize = lib.mkVMOverride 8192;
cores = 8;
};
imports = with self.nixosModules; [
garage
pixelfed
pixelfed-vm
];
# TODO: pair down
environment.systemPackages = with pkgs; [
python3
chromium
chromedriver
xh
seleniumScript
helix
imagemagick
];
environment.variables = {
POST_MEDIA = ./green.png;
# 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
users.users.selenium = {
isNormalUser = true;
};
};
};
testScript = ''
import re
import subprocess
server.start()
with subtest("Pixelfed starts"):
server.wait_for_unit("phpfpm-pixelfed.service")
with subtest("Account creation"):
password = "testtest"
server.succeed(f"pixelfed-manage user:create --name=test --username=test --email=test@test.com --password={password} --confirm_email=1")
# 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).
# 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.
with subtest("Image displays"):
server.succeed(f"su - selenium -c 'selenium-script test@test.com {password}' >&2")
server.copy_from_vm("/home/selenium/screenshot.png", "")
displayed_colors = subprocess.run(
["magick", "screenshot.png", "-define", "histogram:unique-colors=true", "-format", "%c", "histogram:info:"],
capture_output=True
)
# check that the green image displayed somewhere
green_check = re.match(".*#00FF00.*", displayed_colors.stdout.decode(), re.S)
if green_check is None:
raise Exception("cannot detect the uploaded image on pixelfed page.")
'';
}