Skip to content

Commit c280b57

Browse files
ibizamanibizaman
ibizaman
authored andcommitted
add playwright test for nextcloud
1 parent d13ed1c commit c280b57

File tree

4 files changed

+79
-53
lines changed

4 files changed

+79
-53
lines changed

test/common.nix

+14-13
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ let
9191
if response['auth_query'] != "rd=${proto_fqdn}/":
9292
raise Exception(f"auth query should be rd=${proto_fqdn}/ but is {response['auth_query']}")
9393
'')
94+
+ (let
95+
script = extraScript args;
96+
in
97+
lib.optionalString (script != "") script)
9498
+ (optionalString (hasAttr "test" nodes.client && hasAttr "login" nodes.client.test) ''
9599
with subtest("Login"):
96100
code, logs = client.execute("login_playwright firefox")
@@ -99,10 +103,6 @@ let
99103
if code != 0:
100104
raise Exception("login_playwright did not succeed")
101105
'')
102-
+ (let
103-
script = extraScript args;
104-
in
105-
lib.optionalString (script != "") script)
106106
);
107107

108108
backupScript = args: (accessScript args).override {
@@ -153,15 +153,15 @@ in
153153
cfg = config.test.login;
154154
in {
155155
options.test.login = {
156-
usernameFieldLabel = mkOption {
156+
usernameFieldLabelRegex = mkOption {
157157
type = str;
158158
default = "username";
159159
};
160-
passwordFieldLabel = mkOption {
160+
passwordFieldLabelRegex = mkOption {
161161
type = str;
162162
default = "password";
163163
};
164-
loginButtonName = mkOption {
164+
loginButtonNameRegex = mkOption {
165165
type = str;
166166
default = "login";
167167
};
@@ -234,21 +234,22 @@ in
234234
print(f"Testing for user {u['username']} and password {u['password']}")
235235
236236
context = browser.new_context(ignore_https_errors=True)
237+
context.set_default_navigation_timeout(2 * 60 * 1000)
237238
context.tracing.start(screenshots=True, snapshots=True, sources=True)
238239
try:
239240
page = context.new_page()
240241
print(f"Going to {testCfg['startUrl']}")
241242
page.goto(testCfg['startUrl'])
242243
243244
if u['username'] is not None:
244-
print(f"Filling field {testCfg['usernameFieldLabel']} with {u['username']}")
245-
page.get_by_label(testCfg['usernameFieldLabel']).fill(u['username'])
245+
print(f"Filling field {testCfg['usernameFieldLabelRegex']} with {u['username']}")
246+
page.get_by_label(re.compile(testCfg['usernameFieldLabelRegex'])).fill(u['username'])
246247
if u['password'] is not None:
247-
print(f"Filling field {testCfg['passwordFieldLabel']} with {u['password']}")
248-
page.get_by_label(testCfg['passwordFieldLabel']).fill(u['password'])
248+
print(f"Filling field {testCfg['passwordFieldLabelRegex']} with {u['password']}")
249+
page.get_by_label(re.compile(testCfg['passwordFieldLabelRegex'])).fill(u['password'])
249250
250-
print(f"Clicking button {testCfg['loginButtonName']}")
251-
page.get_by_role("button", name=testCfg['loginButtonName']).click()
251+
print(f"Clicking button {testCfg['loginButtonNameRegex']}")
252+
page.get_by_role("button", name=re.compile(testCfg['loginButtonNameRegex'])).click()
252253
253254
for line in u['nextPageExpect']:
254255
print(f"Running: {line}")

test/services/deluge.nix

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ let
108108
};
109109

110110
test.login = {
111-
passwordFieldLabel = "Password";
112-
loginButtonName = "Login";
111+
passwordFieldLabelRegex = "Password";
112+
loginButtonNameRegex = "Login";
113113
testLoginWith = [
114114
{ password = "deluge"; nextPageExpect = [
115115
"expect(page.get_by_role('button', name='Login')).not_to_be_visible()"

test/services/forgejo.nix

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ let
8080

8181
test.login = {
8282
startUrl = "http://${config.test.fqdn}/user/login";
83-
usernameFieldLabel = "Username or email address";
84-
passwordFieldLabel = "Password";
85-
loginButtonName = "Sign In";
83+
usernameFieldLabelRegex = "Username or email address";
84+
passwordFieldLabelRegex = "Password";
85+
loginButtonNameRegex = "Sign In";
8686
testLoginWith = [
8787
{ username = "theadmin"; password = adminPassword + "oops"; nextPageExpect = [
8888
"expect(page.get_by_text('Username or password is incorrect.')).to_be_visible()"

test/services/nextcloud.nix

+60-35
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,9 @@ let
2121
def find_in_logs(unit, text):
2222
return server.systemctl("status {}".format(unit))[1].find(text) != -1
2323
24-
with subtest("cron job succeeds"):
25-
# This calls blocks until the service is done.
26-
server.systemctl("start nextcloud-cron.service")
27-
28-
# If the service failed, then we're not happy.
29-
status = "active"
30-
while status == "active":
31-
status = server.get_unit_info("nextcloud-cron")["ActiveState"]
32-
time.sleep(5)
33-
if status != "inactive":
34-
raise Exception("Cron job did not finish correctly")
35-
36-
if not find_in_logs("nextcloud-cron", "nextcloud-cron.service: Deactivated successfully."):
37-
raise Exception("Nextcloud cron job did not finish successfully.")
24+
with subtest("cron job starts"):
25+
# This call does not block until the service is done.
26+
server.succeed("systemctl start nextcloud-cron.service&")
3827
3928
with subtest("fails with incorrect authentication"):
4029
client.fail(
@@ -106,10 +95,27 @@ let
10695
)
10796
if content != "hello\n":
10897
raise Exception("Got incorrect content for file, expected 'hello\n' but got:\n{}".format(content))
98+
99+
with subtest("cron job succeeds"):
100+
# If the service failed, then we're not happy.
101+
status = "active"
102+
while status == "active":
103+
status = server.get_unit_info("nextcloud-cron")["ActiveState"]
104+
time.sleep(5)
105+
if status != "inactive":
106+
raise Exception("Cron job did not finish correctly")
107+
108+
if not find_in_logs("nextcloud-cron", "nextcloud-cron.service: Deactivated successfully."):
109+
raise Exception("Nextcloud cron job did not finish successfully.")
109110
'';
110111
};
111112

112113
basic = { config, ... }: {
114+
imports = [
115+
testLib.baseModule
116+
../../modules/services/nextcloud-server.nix
117+
];
118+
113119
test = {
114120
subdomain = "n";
115121
};
@@ -136,6 +142,36 @@ let
136142
};
137143
};
138144

145+
clientLogin = { config, ... }: {
146+
imports = [
147+
testLib.baseModule
148+
testLib.clientLoginModule
149+
];
150+
virtualisation.memorySize = 4096;
151+
152+
test = {
153+
subdomain = "n";
154+
};
155+
156+
test.login = {
157+
startUrl = "http://${config.test.fqdn}";
158+
usernameFieldLabelRegex = "[Uu]sername";
159+
passwordFieldLabelRegex = "^ *[Pp]assword";
160+
loginButtonNameRegex = "[Ll]og [Ii]n";
161+
testLoginWith = [
162+
{ username = adminUser; password = adminPass; nextPageExpect = [
163+
"expect(page.get_by_text('Wrong username or password')).not_to_be_visible()"
164+
"expect(page.get_by_role('button', name=re.compile('[Ll]og [Ii]n'))).not_to_be_visible()"
165+
"expect(page).to_have_title(re.compile('Dashboard'))"
166+
]; }
167+
# Failure is after so we're not throttled too much.
168+
{ username = adminUser; password = adminPass + "oops"; nextPageExpect = [
169+
"expect(page.get_by_text('Wrong username or password')).to_be_visible()"
170+
]; }
171+
];
172+
};
173+
};
174+
139175
https = { config, ...}: {
140176
shb.nextcloud = {
141177
ssl = config.shb.certs.certs.selfsigned.n;
@@ -234,10 +270,13 @@ in
234270
basic = pkgs.testers.runNixOSTest {
235271
name = "nextcloud_basic";
236272

273+
nodes.client = {
274+
imports = [
275+
clientLogin
276+
];
277+
};
237278
nodes.server = {
238279
imports = [
239-
testLib.baseModule
240-
../../modules/services/nextcloud-server.nix
241280
basic
242281
];
243282
};
@@ -252,8 +291,6 @@ in
252291

253292
nodes.server = { config, ... }: {
254293
imports = [
255-
testLib.baseModule
256-
../../modules/services/nextcloud-server.nix
257294
basic
258295
(testLib.backup config.shb.nextcloud.backup)
259296
];
@@ -269,10 +306,8 @@ in
269306

270307
nodes.server = {
271308
imports = [
272-
testLib.baseModule
273-
../../modules/services/nextcloud-server.nix
274-
testLib.certs
275309
basic
310+
testLib.certs
276311
https
277312
];
278313
};
@@ -288,10 +323,8 @@ in
288323

289324
nodes.server = {
290325
imports = [
291-
testLib.baseModule
292-
../../modules/services/nextcloud-server.nix
293-
testLib.certs
294326
basic
327+
testLib.certs
295328
https
296329
previewgenerator
297330
];
@@ -307,10 +340,8 @@ in
307340

308341
nodes.server = {
309342
imports = [
310-
testLib.baseModule
311-
../../modules/services/nextcloud-server.nix
312-
testLib.certs
313343
basic
344+
testLib.certs
314345
https
315346
externalstorage
316347
];
@@ -326,10 +357,8 @@ in
326357

327358
nodes.server = { config, ... }: {
328359
imports = [
329-
testLib.baseModule
330-
../../modules/services/nextcloud-server.nix
331-
testLib.certs
332360
basic
361+
testLib.certs
333362
https
334363
testLib.ldap
335364
ldap
@@ -346,10 +375,8 @@ in
346375

347376
nodes.server = { config, ... }: {
348377
imports = [
349-
testLib.baseModule
350-
../../modules/services/nextcloud-server.nix
351-
testLib.certs
352378
basic
379+
testLib.certs
353380
https
354381
testLib.ldap
355382
ldap
@@ -368,8 +395,6 @@ in
368395

369396
nodes.server = { config, ... }: {
370397
imports = [
371-
testLib.baseModule
372-
../../modules/services/nextcloud-server.nix
373398
basic
374399
prometheus
375400
];

0 commit comments

Comments
 (0)