-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed_pagination_data.py
More file actions
295 lines (247 loc) · 10.6 KB
/
Copy pathseed_pagination_data.py
File metadata and controls
295 lines (247 loc) · 10.6 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
"""Seed synthetic data for pagination integration tests.
Creates bulk test data across multiple Nextcloud apps to verify
pagination behavior with limit/offset parameters.
Usage: python scripts/seed_pagination_data.py <NC_URL> <USER> <PASSWORD>
Seeded data uses the "mcp-pagtest" prefix and lives outside the
regular test cleanup path (mcp-test-suite), so it persists across
individual test runs but is ephemeral in CI (container destroyed).
"""
import sys
import time
import xml.etree.ElementTree as ET
import niquests
COUNT = 55
PREFIX = "mcp-pagtest"
PAGINATION_DIR = "mcp-pagination-data"
def _ocs_data(resp: niquests.Response) -> object:
"""Extract data from an OCS JSON response."""
return resp.json()["ocs"]["data"]
def seed_files(s: niquests.Session, url: str, user: str) -> None:
"""Create files in a dedicated pagination test directory."""
dav = f"{url}/remote.php/dav/files/{user}"
s.request("MKCOL", f"{dav}/{PAGINATION_DIR}/")
for i in range(1, COUNT + 1):
s.put(
f"{dav}/{PAGINATION_DIR}/pagtest-{i:03d}.txt",
data=f"Pagination test file {i:03d}",
headers={"Content-Type": "text/plain"},
)
print(f" {COUNT} files in {PAGINATION_DIR}/")
def seed_conversations(s: niquests.Session, url: str) -> None:
"""Create Talk group conversations."""
api = f"{url}/ocs/v2.php/apps/spreed/api/v4/room"
existing = {r["name"] for r in _ocs_data(s.get(api))}
created = 0
for i in range(1, COUNT + 1):
name = f"{PREFIX}-conv-{i:03d}"
if name not in existing:
s.post(api, json={"roomType": 2, "roomName": name})
created += 1
print(f" {created} conversations (skipped {COUNT - created})")
def seed_calendar_events(s: niquests.Session, url: str, user: str) -> None:
"""Create calendar events via CalDAV PUT."""
cal = f"{url}/remote.php/dav/calendars/{user}/personal"
for i in range(1, COUNT + 1):
uid = f"{PREFIX}-event-{i:03d}"
hour = i % 24
ical = (
"BEGIN:VCALENDAR\r\n"
"VERSION:2.0\r\n"
"PRODID:-//NC MCP//Pagination Test//EN\r\n"
"BEGIN:VEVENT\r\n"
f"UID:{uid}\r\n"
f"SUMMARY:Pagination Test Event {i:03d}\r\n"
f"DTSTART:20270601T{hour:02d}0000Z\r\n"
f"DTEND:20270601T{hour:02d}3000Z\r\n"
f"DESCRIPTION:Seeded event {i:03d} for pagination testing\r\n"
"DTSTAMP:20270101T000000Z\r\n"
"END:VEVENT\r\n"
"END:VCALENDAR\r\n"
)
s.put(f"{cal}/{uid}.ics", data=ical, headers={"Content-Type": "text/calendar; charset=utf-8"})
print(f" {COUNT} calendar events")
def seed_trash(s: niquests.Session, url: str, user: str) -> None:
"""Create files then delete them to populate the trash bin."""
dav = f"{url}/remote.php/dav/files/{user}"
trash_dir = f"{PREFIX}-trash"
s.request("MKCOL", f"{dav}/{trash_dir}/")
for i in range(1, COUNT + 1):
path = f"{dav}/{trash_dir}/trash-{i:03d}.txt"
s.put(path, data=f"Trash item {i:03d}", headers={"Content-Type": "text/plain"})
for i in range(1, COUNT + 1):
s.delete(f"{dav}/{trash_dir}/trash-{i:03d}.txt")
s.delete(f"{dav}/{trash_dir}/")
print(f" {COUNT} items in trash")
def seed_collective_pages(s: niquests.Session, url: str) -> None:
"""Create a collective with many pages for pagination testing."""
api = f"{url}/ocs/v2.php/apps/collectives/api/v1.0"
coll_name = f"{PREFIX}-collective"
collectives = _ocs_data(s.get(f"{api}/collectives"))
coll = next((c for c in collectives["collectives"] if c["name"] == coll_name), None)
if not coll:
resp = s.post(
f"{api}/collectives",
json={"name": coll_name},
headers={"Content-Type": "application/json"},
)
coll = _ocs_data(resp)["collective"]
coll_id = coll["id"]
pages_data = _ocs_data(s.get(f"{api}/collectives/{coll_id}/pages"))
pages = pages_data["pages"]
landing_id = pages[0]["id"]
existing_titles = {p["title"] for p in pages}
created = 0
for i in range(1, COUNT + 1):
title = f"pagtest-page-{i:03d}"
if title not in existing_titles:
s.post(
f"{api}/collectives/{coll_id}/pages/{landing_id}",
json={"title": title},
headers={"Content-Type": "application/json"},
)
created += 1
# total = created + existing (minus landing page)
print(f" {created} pages in collective '{coll_name}' (skipped {COUNT - created})")
_GIVEN_NAMES = ["Alice", "Bob", "Charlie", "Diana", "Eve", "Frank", "Grace", "Hank", "Iris", "Jack"]
_FAMILY_NAMES = ["Smith", "Jones", "Brown", "Davis", "Wilson", "Taylor", "Clark", "Lewis", "Hall", "Young"]
def seed_contacts(s: niquests.Session, url: str, user: str) -> None:
"""Create contacts with categories and varied name structures via CardDAV PUT."""
dav = f"{url}/remote.php/dav/addressbooks/users/{user}/contacts"
for i in range(1, COUNT + 1):
uid = f"{PREFIX}-contact-{i:03d}"
given = _GIVEN_NAMES[(i - 1) % len(_GIVEN_NAMES)]
family = _FAMILY_NAMES[(i - 1) % len(_FAMILY_NAMES)]
cats: list[str] = []
if i % 2 == 0:
cats.append("Work")
if i % 3 == 0:
cats.append("Family")
if i % 5 == 0:
cats.append("VIP")
if not cats:
cats.append("Friend")
lines = ["BEGIN:VCARD", "VERSION:3.0", f"UID:{uid}"]
remainder = i % 4
if remainder == 1:
lines.append(f"FN:{given} {family}")
lines.append(f"N:{family};{given};;;")
elif remainder == 2:
lines.append(f"FN:{given}")
lines.append(f"N:;{given};;;")
elif remainder == 3:
lines.append(f"FN:{family}")
lines.append(f"N:{family};;;;")
else:
lines.append(f"FN:{given} {family}")
lines.append(f"N:{family};{given};;;")
lines.extend(
[
f"EMAIL;TYPE=WORK:pagtest{i:03d}@example.com",
f"ORG:PagTest Corp {i:03d}",
f"CATEGORIES:{','.join(cats)}",
"END:VCARD",
]
)
vcard = "\r\n".join(lines) + "\r\n"
s.put(f"{dav}/{uid}.vcf", data=vcard, headers={"Content-Type": "text/vcard; charset=utf-8"})
print(f" {COUNT} contacts with categories and varied name structures")
def _fetch_all_comments(s: niquests.Session, url: str, file_id: str) -> list[tuple[str, str, str]]:
"""Return all comments on file_id as list of (id, message, creation_ts)."""
out: list[tuple[str, str, str]] = []
offset = 0
while True:
report = s.request(
"REPORT",
f"{url}/remote.php/dav/comments/files/{file_id}",
data=(
'<?xml version="1.0" encoding="utf-8"?>'
'<oc:filter-comments xmlns:oc="http://owncloud.org/ns">'
f"<oc:limit>100</oc:limit><oc:offset>{offset}</oc:offset>"
"</oc:filter-comments>"
),
headers={"Content-Type": "application/xml"},
)
batch: list[tuple[str, str, str]] = []
for resp_el in ET.fromstring(report.text).findall("{DAV:}response"):
href = resp_el.find("{DAV:}href")
msg_el = resp_el.find(".//{http://owncloud.org/ns}message")
ts_el = resp_el.find(".//{http://owncloud.org/ns}creationDateTime")
if href is not None and msg_el is not None and msg_el.text and ts_el is not None:
cid = href.text.rstrip("/").split("/")[-1]
batch.append((cid, msg_el.text, ts_el.text or ""))
if not batch:
break
out.extend(batch)
if len(batch) < 100:
break
offset += 100
return out
def seed_comments(s: niquests.Session, url: str, user: str) -> None:
"""Create a dedicated file and add many comments to it."""
dav = f"{url}/remote.php/dav/files/{user}"
comment_file = f"{PAGINATION_DIR}/comment-target.txt"
s.put(f"{dav}/{comment_file}", data="File with many comments", headers={"Content-Type": "text/plain"})
resp = s.request(
"PROPFIND",
f"{dav}/{comment_file}",
data=(
'<?xml version="1.0"?>'
'<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">'
"<d:prop><oc:fileid/></d:prop>"
"</d:propfind>"
),
headers={"Content-Type": "text/xml", "Depth": "0"},
)
root = ET.fromstring(resp.text)
fileid_el = root.find(".//{http://owncloud.org/ns}fileid")
if fileid_el is None or not fileid_el.text:
print(" WARNING: could not resolve file ID for comments, skipping")
return
file_id = fileid_el.text
expected = {f"Pagination test comment {i:03d}" for i in range(1, COUNT + 1)}
existing = _fetch_all_comments(s, url, file_id)
expected_ts = [ts for _, msg, ts in existing if msg in expected]
if expected.issubset({m for _, m, _ in existing}) and len(set(expected_ts)) >= COUNT:
print(f" {COUNT} comments on file {file_id} (already present with distinct timestamps)")
return
for cid, _, _ in existing:
s.delete(f"{url}/remote.php/dav/comments/files/{file_id}/{cid}")
for i in range(1, COUNT + 1):
s.post(
f"{url}/remote.php/dav/comments/files/{file_id}",
json={"actorType": "users", "verb": "comment", "message": f"Pagination test comment {i:03d}"},
headers={"Content-Type": "application/json"},
)
if i < COUNT:
time.sleep(1.05)
print(f" {COUNT} comments on file {file_id} (reset and re-created with 1.05s spacing for stable pagination)")
def main() -> None:
if len(sys.argv) != 4:
print(f"Usage: {sys.argv[0]} <NC_URL> <USER> <PASSWORD>")
sys.exit(1)
url = sys.argv[1].rstrip("/")
user = sys.argv[2]
password = sys.argv[3]
s = niquests.Session()
s.auth = (user, password)
s.headers.update({"OCS-APIRequest": "true", "Accept": "application/json"})
print(f"=== Seeding pagination test data ({COUNT} items per app) ===")
print("Files...")
seed_files(s, url, user)
print("Talk conversations...")
seed_conversations(s, url)
print("Calendar events...")
seed_calendar_events(s, url, user)
print("Trash items...")
seed_trash(s, url, user)
print("Collective pages...")
seed_collective_pages(s, url)
print("Contacts...")
seed_contacts(s, url, user)
print("Comments...")
seed_comments(s, url, user)
print("=== Seed complete ===")
s.close()
if __name__ == "__main__":
main()