summary refs log tree commit diff
path: root/nixos/tests/castopod.nix
blob: 4435ec617d4e673ffdf34fd5bbbf37955ad28db4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import ./make-test-python.nix ({ pkgs, lib, ... }:
{
  name = "castopod";
  meta = with lib.maintainers; {
    maintainers = [ alexoundos misuzu ];
  };
  nodes.castopod = { nodes, ... }: {
    networking.firewall.allowedTCPPorts = [ 80 ];
    networking.extraHosts = ''
      127.0.0.1 castopod.example.com
    '';
    services.castopod = {
      enable = true;
      database.createLocally = true;
      localDomain = "castopod.example.com";
    };
    environment.systemPackages =
      let
        username = "admin";
        email = "admin@castood.example.com";
        password = "v82HmEp5";
        testRunner = pkgs.writers.writePython3Bin "test-runner"
          {
            libraries = [ pkgs.python3Packages.selenium ];
            flakeIgnore = [
              "E501"
            ];
          } ''
          from selenium.webdriver.common.by import By
          from selenium.webdriver import Firefox
          from selenium.webdriver.firefox.options import Options
          from selenium.webdriver.support.ui import WebDriverWait
          from selenium.webdriver.support import expected_conditions as EC

          options = Options()
          options.add_argument('--headless')
          driver = Firefox(options=options)
          try:
              driver.implicitly_wait(20)
              driver.get('http://castopod.example.com/cp-install')

              wait = WebDriverWait(driver, 10)

              wait.until(EC.title_contains("installer"))

              driver.find_element(By.CSS_SELECTOR, '#username').send_keys(
                  '${username}'
              )
              driver.find_element(By.CSS_SELECTOR, '#email').send_keys(
                  '${email}'
              )
              driver.find_element(By.CSS_SELECTOR, '#password').send_keys(
                  '${password}'
              )
              driver.find_element(By.XPATH, "//button[contains(., 'Finish install')]").click()

              wait.until(EC.title_contains("Auth"))

              driver.find_element(By.CSS_SELECTOR, '#email').send_keys(
                  '${email}'
              )
              driver.find_element(By.CSS_SELECTOR, '#password').send_keys(
                  '${password}'
              )
              driver.find_element(By.XPATH, "//button[contains(., 'Login')]").click()

              wait.until(EC.title_contains("Admin dashboard"))
          finally:
              driver.close()
              driver.quit()
        '';
      in
      [ pkgs.firefox-unwrapped pkgs.geckodriver testRunner ];
  };
  testScript = ''
    start_all()
    castopod.wait_for_unit("castopod-setup.service")
    castopod.wait_for_file("/run/phpfpm/castopod.sock")
    castopod.wait_for_unit("nginx.service")
    castopod.wait_for_open_port(80)
    castopod.wait_until_succeeds("curl -sS -f http://castopod.example.com")
    castopod.succeed("curl -s http://localhost/cp-install | grep 'Create your Super Admin account' > /dev/null")

    with subtest("Create superadmin and log in"):
        castopod.succeed("PYTHONUNBUFFERED=1 systemd-cat -t test-runner test-runner")
  '';
})