Skip to content

Commit

Permalink
add playwright test for nextcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
ibizaman committed Jan 30, 2025
1 parent d13ed1c commit 358ce39
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 39 deletions.
27 changes: 14 additions & 13 deletions test/common.nix
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ let
if response['auth_query'] != "rd=${proto_fqdn}/":
raise Exception(f"auth query should be rd=${proto_fqdn}/ but is {response['auth_query']}")
'')
+ (let
script = extraScript args;
in
lib.optionalString (script != "") script)
+ (optionalString (hasAttr "test" nodes.client && hasAttr "login" nodes.client.test) ''
with subtest("Login"):
code, logs = client.execute("login_playwright firefox")
Expand All @@ -99,10 +103,6 @@ let
if code != 0:
raise Exception("login_playwright did not succeed")
'')
+ (let
script = extraScript args;
in
lib.optionalString (script != "") script)
);

backupScript = args: (accessScript args).override {
Expand Down Expand Up @@ -153,15 +153,15 @@ in
cfg = config.test.login;
in {
options.test.login = {
usernameFieldLabel = mkOption {
usernameFieldLabelRegex = mkOption {
type = str;
default = "username";
};
passwordFieldLabel = mkOption {
passwordFieldLabelRegex = mkOption {
type = str;
default = "password";
};
loginButtonName = mkOption {
loginButtonNameRegex = mkOption {
type = str;
default = "login";
};
Expand Down Expand Up @@ -234,21 +234,22 @@ in
print(f"Testing for user {u['username']} and password {u['password']}")
context = browser.new_context(ignore_https_errors=True)
context.set_default_navigation_timeout(2 * 60 * 1000)
context.tracing.start(screenshots=True, snapshots=True, sources=True)
try:
page = context.new_page()
print(f"Going to {testCfg['startUrl']}")
page.goto(testCfg['startUrl'])
if u['username'] is not None:
print(f"Filling field {testCfg['usernameFieldLabel']} with {u['username']}")
page.get_by_label(testCfg['usernameFieldLabel']).fill(u['username'])
print(f"Filling field {testCfg['usernameFieldLabelRegex']} with {u['username']}")
page.get_by_label(re.compile(testCfg['usernameFieldLabelRegex'])).fill(u['username'])
if u['password'] is not None:
print(f"Filling field {testCfg['passwordFieldLabel']} with {u['password']}")
page.get_by_label(testCfg['passwordFieldLabel']).fill(u['password'])
print(f"Filling field {testCfg['passwordFieldLabelRegex']} with {u['password']}")
page.get_by_label(re.compile(testCfg['passwordFieldLabelRegex'])).fill(u['password'])
print(f"Clicking button {testCfg['loginButtonName']}")
page.get_by_role("button", name=testCfg['loginButtonName']).click()
print(f"Clicking button {testCfg['loginButtonNameRegex']}")
page.get_by_role("button", name=re.compile(testCfg['loginButtonNameRegex'])).click()
for line in u['nextPageExpect']:
print(f"Running: {line}")
Expand Down
4 changes: 2 additions & 2 deletions test/services/deluge.nix
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ let
};

test.login = {
passwordFieldLabel = "Password";
loginButtonName = "Login";
passwordFieldLabelRegex = "Password";
loginButtonNameRegex = "Login";
testLoginWith = [
{ password = "deluge"; nextPageExpect = [
"expect(page.get_by_role('button', name='Login')).not_to_be_visible()"
Expand Down
6 changes: 3 additions & 3 deletions test/services/forgejo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ let

test.login = {
startUrl = "http://${config.test.fqdn}/user/login";
usernameFieldLabel = "Username or email address";
passwordFieldLabel = "Password";
loginButtonName = "Sign In";
usernameFieldLabelRegex = "Username or email address";
passwordFieldLabelRegex = "Password";
loginButtonNameRegex = "Sign In";
testLoginWith = [
{ username = "theadmin"; password = adminPassword + "oops"; nextPageExpect = [
"expect(page.get_by_text('Username or password is incorrect.')).to_be_visible()"
Expand Down
65 changes: 44 additions & 21 deletions test/services/nextcloud.nix
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ let
};

basic = { config, ... }: {
imports = [
testLib.baseModule
../../modules/services/nextcloud-server.nix
];

test = {
subdomain = "n";
};
Expand All @@ -136,6 +141,35 @@ let
};
};

clientLogin = { config, ... }: {
imports = [
testLib.baseModule
testLib.clientLoginModule
];
virtualisation.memorySize = 4096;

test = {
subdomain = "n";
};

test.login = {
startUrl = "http://${config.test.fqdn}";
usernameFieldLabelRegex = "[Uu]sername";
passwordFieldLabelRegex = "^ *[Pp]assword";
loginButtonNameRegex = "[Ll]og [Ii]n";
testLoginWith = [
{ username = adminUser; password = adminPass + "oops"; nextPageExpect = [
"expect(page.get_by_text('Wrong username or password')).to_be_visible()"
]; }
{ username = adminUser; password = adminPass; nextPageExpect = [
"expect(page.get_by_text('Wrong username or password')).not_to_be_visible()"
"expect(page.get_by_role('button', name=re.compile('[Ll]og [Ii]n'))).not_to_be_visible()"
"expect(page).to_have_title(re.compile('Dashboard'))"
]; }
];
};
};

https = { config, ...}: {
shb.nextcloud = {
ssl = config.shb.certs.certs.selfsigned.n;
Expand Down Expand Up @@ -234,10 +268,13 @@ in
basic = pkgs.testers.runNixOSTest {
name = "nextcloud_basic";

nodes.client = {
imports = [
clientLogin
];
};
nodes.server = {
imports = [
testLib.baseModule
../../modules/services/nextcloud-server.nix
basic
];
};
Expand All @@ -252,8 +289,6 @@ in

nodes.server = { config, ... }: {
imports = [
testLib.baseModule
../../modules/services/nextcloud-server.nix
basic
(testLib.backup config.shb.nextcloud.backup)
];
Expand All @@ -269,10 +304,8 @@ in

nodes.server = {
imports = [
testLib.baseModule
../../modules/services/nextcloud-server.nix
testLib.certs
basic
testLib.certs
https
];
};
Expand All @@ -288,10 +321,8 @@ in

nodes.server = {
imports = [
testLib.baseModule
../../modules/services/nextcloud-server.nix
testLib.certs
basic
testLib.certs
https
previewgenerator
];
Expand All @@ -307,10 +338,8 @@ in

nodes.server = {
imports = [
testLib.baseModule
../../modules/services/nextcloud-server.nix
testLib.certs
basic
testLib.certs
https
externalstorage
];
Expand All @@ -326,10 +355,8 @@ in

nodes.server = { config, ... }: {
imports = [
testLib.baseModule
../../modules/services/nextcloud-server.nix
testLib.certs
basic
testLib.certs
https
testLib.ldap
ldap
Expand All @@ -346,10 +373,8 @@ in

nodes.server = { config, ... }: {
imports = [
testLib.baseModule
../../modules/services/nextcloud-server.nix
testLib.certs
basic
testLib.certs
https
testLib.ldap
ldap
Expand All @@ -368,8 +393,6 @@ in

nodes.server = { config, ... }: {
imports = [
testLib.baseModule
../../modules/services/nextcloud-server.nix
basic
prometheus
];
Expand Down

0 comments on commit 358ce39

Please sign in to comment.