1 { system ? builtins.currentSystem
3 , pkgs ? import ../.. { inherit system config; }
7 # * Set up a vaultwarden server
8 # * Have Firefox use the web vault to create an account, log in, and save a password to the valut
9 # * Have the bw cli log in and read that password from the vault
11 # Note that Firefox must be on the same machine as the server for WebCrypto APIs to be available (or HTTPS must be configured)
13 # The same tests should work without modification on the official bitwarden server, if we ever package that.
15 with import ../lib/testing-python.nix { inherit system pkgs; };
18 backends = [ "sqlite" "mysql" "postgresql" ];
20 dbPassword = "please_dont_hack";
22 userEmail = "meow@example.com";
23 userPassword = "also_super_secret_ZJWpBKZi668QGt"; # Must be complex to avoid interstitial warning on the signup page
25 storedPassword = "seeeecret";
27 makeVaultwardenTest = backend: makeTest {
28 name = "vaultwarden-${backend}";
30 maintainers = with pkgs.lib.maintainers; [ jjjollyjim ];
34 server = { pkgs, ... }:
39 initialScript = pkgs.writeText "mysql-init.sql" ''
40 CREATE DATABASE bitwarden;
41 CREATE USER 'bitwardenuser'@'localhost' IDENTIFIED BY '${dbPassword}';
42 GRANT ALL ON `bitwarden`.* TO 'bitwardenuser'@'localhost';
45 package = pkgs.mariadb;
48 services.vaultwarden.config.databaseUrl = "mysql://bitwardenuser:${dbPassword}@localhost/bitwarden";
50 systemd.services.vaultwarden.after = [ "mysql.service" ];
54 services.postgresql = {
56 initialScript = pkgs.writeText "postgresql-init.sql" ''
57 CREATE DATABASE bitwarden;
58 CREATE USER bitwardenuser WITH PASSWORD '${dbPassword}';
59 GRANT ALL PRIVILEGES ON DATABASE bitwarden TO bitwardenuser;
63 services.vaultwarden.config.databaseUrl = "postgresql://bitwardenuser:${dbPassword}@localhost/bitwarden";
65 systemd.services.vaultwarden.after = [ "postgresql.service" ];
72 backendConfig.${backend}
74 services.vaultwarden = {
78 rocketAddress = "0.0.0.0";
83 networking.firewall.allowedTCPPorts = [ 80 ];
85 environment.systemPackages =
87 testRunner = pkgs.writers.writePython3Bin "test-runner"
89 libraries = [ pkgs.python3Packages.selenium ];
92 from selenium.webdriver.common.by import By
93 from selenium.webdriver import Firefox
94 from selenium.webdriver.firefox.options import Options
95 from selenium.webdriver.support.ui import WebDriverWait
96 from selenium.webdriver.support import expected_conditions as EC
99 options.add_argument('--headless')
100 driver = Firefox(options=options)
102 driver.implicitly_wait(20)
103 driver.get('http://localhost/#/register')
105 wait = WebDriverWait(driver, 10)
107 wait.until(EC.title_contains("Create Account"))
109 driver.find_element(By.CSS_SELECTOR, 'input#email').send_keys(
112 driver.find_element(By.CSS_SELECTOR, 'input#name').send_keys(
115 driver.find_element(By.CSS_SELECTOR, 'input#masterPassword').send_keys(
118 driver.find_element(By.CSS_SELECTOR, 'input#masterPasswordRetype').send_keys(
122 driver.find_element(By.XPATH, "//button[contains(., 'Submit')]").click()
124 wait.until_not(EC.title_contains("Create Account"))
126 driver.find_element(By.CSS_SELECTOR, 'input#masterPassword').send_keys(
129 driver.find_element(By.XPATH, "//button[contains(., 'Log In')]").click()
131 wait.until(EC.title_contains("Bitwarden Web Vault"))
133 driver.find_element(By.XPATH, "//button[contains(., 'Add Item')]").click()
135 driver.find_element(By.CSS_SELECTOR, 'input#name').send_keys(
138 driver.find_element(By.CSS_SELECTOR, 'input#loginPassword').send_keys(
142 driver.find_element(By.XPATH, "//button[contains(., 'Save')]").click()
145 [ pkgs.firefox-unwrapped pkgs.geckodriver testRunner ];
150 client = { pkgs, ... }:
152 environment.systemPackages = [ pkgs.bitwarden-cli ];
158 server.wait_for_unit("vaultwarden.service")
159 server.wait_for_open_port(80)
161 with subtest("configure the cli"):
162 client.succeed("bw --nointeraction config server http://server")
164 with subtest("can't login to nonexistant account"):
166 "bw --nointeraction --raw login ${userEmail} ${userPassword}"
169 with subtest("use the web interface to sign up, log in, and save a password"):
170 server.succeed("PYTHONUNBUFFERED=1 test-runner | systemd-cat -t test-runner")
172 with subtest("log in with the cli"):
173 key = client.succeed(
174 "bw --nointeraction --raw login ${userEmail} ${userPassword}"
177 with subtest("sync with the cli"):
178 client.succeed(f"bw --nointeraction --raw --session {key} sync -f")
180 with subtest("get the password with the cli"):
181 password = client.succeed(
182 f"bw --nointeraction --raw --session {key} list items | ${pkgs.jq}/bin/jq -r .[].login.password"
184 assert password.strip() == "${storedPassword}"
188 builtins.listToAttrs (
190 (backend: { name = backend; value = makeVaultwardenTest backend; })