about summary refs log tree commit diff
path: root/nixos/tests/castopod.nix
blob: 3257cd3d363c773257947f1dda0a45c3933e077a (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import ./make-test-python.nix ({ pkgs, lib, ... }:
{
  name = "castopod";
  meta = with lib.maintainers; {
    maintainers = [ alexoundos ];
  };

  nodes.castopod = { nodes, ... }: {
    # otherwise 500 MiB file upload fails!
    virtualisation.diskSize = 512 + 3 * 512;

    networking.firewall.allowedTCPPorts = [ 80 ];
    networking.extraHosts =
      lib.strings.concatStringsSep "\n"
        (lib.attrsets.mapAttrsToList
          (name: _: "127.0.0.1 ${name}")
          nodes.castopod.services.nginx.virtualHosts);

    services.castopod = {
      enable = true;
      database.createLocally = true;
      localDomain = "castopod.example.com";
      maxUploadSize = "512M";
    };
  };

  nodes.client = { nodes, pkgs, lib, ... }:
    let
      domain = nodes.castopod.services.castopod.localDomain;

      getIP = node:
        (builtins.head node.networking.interfaces.eth1.ipv4.addresses).address;

      targetPodcastSize = 500 * 1024 * 1024;
      lameMp3Bitrate = 348300;
      lameMp3FileAdjust = -800;
      targetPodcastDuration = toString
        ((targetPodcastSize + lameMp3FileAdjust) / (lameMp3Bitrate / 8));
      bannerWidth = 3000;
      banner = pkgs.runCommand "gen-castopod-cover.jpg" { } ''
        ${pkgs.imagemagick}/bin/magick `
        `-background green -bordercolor white -gravity northwest xc:black `
        `-duplicate 99 `
        `-seed 1 -resize "%[fx:rand()*72+24]" `
        `-seed 0 -rotate "%[fx:rand()*360]" -border 6x6 -splice 16x36 `
        `-seed 0 -rotate "%[fx:floor(rand()*4)*90]" -resize "150x50!" `
        `+append -crop 10x1@ +repage -roll "+%[fx:(t%2)*72]+0" -append `
        `-resize ${toString bannerWidth} -quality 1 $out
      '';

      coverWidth = toString 3000;
      cover = pkgs.runCommand "gen-castopod-banner.jpg" { } ''
        ${pkgs.imagemagick}/bin/magick `
        `-background white -bordercolor white -gravity northwest xc:black `
        `-duplicate 99 `
        `-seed 1 -resize "%[fx:rand()*72+24]" `
        `-seed 0 -rotate "%[fx:rand()*360]" -border 6x6 -splice 36x36 `
        `-seed 0 -rotate "%[fx:floor(rand()*4)*90]" -resize "144x144!" `
        `+append -crop 10x1@ +repage -roll "+%[fx:(t%2)*72]+0" -append `
        `-resize ${coverWidth} -quality 1 $out
      '';
    in
    {
      networking.extraHosts =
        lib.strings.concatStringsSep "\n"
          (lib.attrsets.mapAttrsToList
            (name: _: "${getIP nodes.castopod} ${name}")
            nodes.castopod.services.nginx.virtualHosts);

      environment.systemPackages =
        let
          username = "admin";
          email = "admin@${domain}";
          password = "Abcd1234";
          podcastTitle = "Some Title";
          episodeTitle = "Episode Title";
          browser-test = pkgs.writers.writePython3Bin "browser-test"
            {
              libraries = [ pkgs.python3Packages.selenium ];
              flakeIgnore = [ "E124" "E501" ];
            } ''
            from selenium.webdriver.common.by import By
            from selenium.webdriver import Firefox
            from selenium.webdriver.firefox.options import Options
            from selenium.webdriver.firefox.service import Service
            from selenium.webdriver.support.ui import WebDriverWait
            from selenium.webdriver.support import expected_conditions as EC
            from subprocess import STDOUT
            import logging

            selenium_logger = logging.getLogger("selenium")
            selenium_logger.setLevel(logging.DEBUG)
            selenium_logger.addHandler(logging.StreamHandler())

            options = Options()
            options.add_argument('--headless')
            service = Service(log_output=STDOUT)
            driver = Firefox(options=options, service=service)
            driver = Firefox(options=options)
            driver.implicitly_wait(30)

            # install ##########################################################

            driver.get('http://${domain}/cp-install')

            wait = WebDriverWait(driver, 20)

            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"))

            # create podcast ###################################################

            driver.get('http://${domain}/admin/podcasts/new')

            wait.until(EC.title_contains("Create podcast"))

            driver.find_element(By.CSS_SELECTOR, '#cover').send_keys(
                '${cover}'
            )
            driver.find_element(By.CSS_SELECTOR, '#banner').send_keys(
                '${banner}'
            )
            driver.find_element(By.CSS_SELECTOR, '#title').send_keys(
                '${podcastTitle}'
            )
            driver.find_element(By.CSS_SELECTOR, '#handle').send_keys(
                'some_handle'
            )
            driver.find_element(By.CSS_SELECTOR, '#description').send_keys(
                'Some description'
            )
            driver.find_element(By.CSS_SELECTOR, '#owner_name').send_keys(
                'Owner Name'
            )
            driver.find_element(By.CSS_SELECTOR, '#owner_email').send_keys(
                'owner@email.xyz'
            )
            driver.find_element(By.XPATH,
                                "//button[contains(., 'Create podcast')]"
            ).click()

            wait.until(EC.title_contains("${podcastTitle}"))

            driver.find_element(By.XPATH,
                                "//span[contains(., 'Add an episode')]"
            ).click()

            wait.until(EC.title_contains("Add an episode"))

            # upload podcast ###################################################

            driver.find_element(By.CSS_SELECTOR, '#audio_file').send_keys(
                '/tmp/podcast.mp3'
            )
            driver.find_element(By.CSS_SELECTOR, '#cover').send_keys(
                '${cover}'
            )
            driver.find_element(By.CSS_SELECTOR, '#description').send_keys(
                'Episode description'
            )
            driver.find_element(By.CSS_SELECTOR, '#title').send_keys(
                '${episodeTitle}'
            )
            driver.find_element(By.XPATH,
                                "//button[contains(., 'Create episode')]"
            ).click()

            wait.until(EC.title_contains("${episodeTitle}"))

            driver.close()
            driver.quit()
          '';
        in
        [
          pkgs.firefox-unwrapped
          pkgs.geckodriver
          browser-test
          (pkgs.writeShellApplication {
            name = "build-mp3";
            runtimeInputs = with pkgs; [ sox lame ];
            text = ''
              out=/tmp/podcast.mp3
              sox -n -r 48000 -t wav - synth ${targetPodcastDuration} sine 440 `
              `| lame --noreplaygain -cbr -q 9 -b 320 - $out
              FILESIZE="$(stat -c%s $out)"
              [ "$FILESIZE" -gt 0 ]
              [ "$FILESIZE" -le "${toString targetPodcastSize}" ]
            '';
          })
        ];
    };

  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")

    client.succeed("build-mp3")

    with subtest("Create superadmin, log in, create and upload a podcast"):
        client.succeed(\
          "PYTHONUNBUFFERED=1 systemd-cat -t browser-test browser-test")
  '';
})