|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Seed synthetic data for pagination integration tests. |
| 3 | +
|
| 4 | +Creates bulk test data across multiple Nextcloud apps to verify |
| 5 | +pagination behavior with limit/offset parameters. |
| 6 | +
|
| 7 | +Usage: python scripts/seed_pagination_data.py <NC_URL> <USER> <PASSWORD> |
| 8 | +
|
| 9 | +Seeded data uses the "mcp-pagtest" prefix and lives outside the |
| 10 | +regular test cleanup path (mcp-test-suite), so it persists across |
| 11 | +individual test runs but is ephemeral in CI (container destroyed). |
| 12 | +""" |
| 13 | + |
| 14 | +import sys |
| 15 | +import xml.etree.ElementTree as ET |
| 16 | + |
| 17 | +import niquests |
| 18 | + |
| 19 | +COUNT = 55 |
| 20 | +PREFIX = "mcp-pagtest" |
| 21 | +PAGINATION_DIR = "mcp-pagination-data" |
| 22 | + |
| 23 | + |
| 24 | +def _ocs_data(resp: niquests.Response) -> object: |
| 25 | + """Extract data from an OCS JSON response.""" |
| 26 | + return resp.json()["ocs"]["data"] |
| 27 | + |
| 28 | + |
| 29 | +def seed_files(s: niquests.Session, url: str, user: str) -> None: |
| 30 | + """Create files in a dedicated pagination test directory.""" |
| 31 | + dav = f"{url}/remote.php/dav/files/{user}" |
| 32 | + s.request("MKCOL", f"{dav}/{PAGINATION_DIR}/") |
| 33 | + for i in range(1, COUNT + 1): |
| 34 | + s.put( |
| 35 | + f"{dav}/{PAGINATION_DIR}/pagtest-{i:03d}.txt", |
| 36 | + data=f"Pagination test file {i:03d}", |
| 37 | + headers={"Content-Type": "text/plain"}, |
| 38 | + ) |
| 39 | + print(f" {COUNT} files in {PAGINATION_DIR}/") |
| 40 | + |
| 41 | + |
| 42 | +def seed_conversations(s: niquests.Session, url: str) -> None: |
| 43 | + """Create Talk group conversations.""" |
| 44 | + api = f"{url}/ocs/v2.php/apps/spreed/api/v4/room" |
| 45 | + existing = {r["name"] for r in _ocs_data(s.get(api))} |
| 46 | + created = 0 |
| 47 | + for i in range(1, COUNT + 1): |
| 48 | + name = f"{PREFIX}-conv-{i:03d}" |
| 49 | + if name not in existing: |
| 50 | + s.post(api, json={"roomType": 2, "roomName": name}) |
| 51 | + created += 1 |
| 52 | + print(f" {created} conversations (skipped {COUNT - created})") |
| 53 | + |
| 54 | + |
| 55 | +def seed_calendar_events(s: niquests.Session, url: str, user: str) -> None: |
| 56 | + """Create calendar events via CalDAV PUT.""" |
| 57 | + cal = f"{url}/remote.php/dav/calendars/{user}/personal" |
| 58 | + for i in range(1, COUNT + 1): |
| 59 | + uid = f"{PREFIX}-event-{i:03d}" |
| 60 | + hour = i % 24 |
| 61 | + ical = ( |
| 62 | + "BEGIN:VCALENDAR\r\n" |
| 63 | + "VERSION:2.0\r\n" |
| 64 | + "PRODID:-//NC MCP//Pagination Test//EN\r\n" |
| 65 | + "BEGIN:VEVENT\r\n" |
| 66 | + f"UID:{uid}\r\n" |
| 67 | + f"SUMMARY:Pagination Test Event {i:03d}\r\n" |
| 68 | + f"DTSTART:20270601T{hour:02d}0000Z\r\n" |
| 69 | + f"DTEND:20270601T{hour:02d}3000Z\r\n" |
| 70 | + f"DESCRIPTION:Seeded event {i:03d} for pagination testing\r\n" |
| 71 | + "DTSTAMP:20270101T000000Z\r\n" |
| 72 | + "END:VEVENT\r\n" |
| 73 | + "END:VCALENDAR\r\n" |
| 74 | + ) |
| 75 | + s.put(f"{cal}/{uid}.ics", data=ical, headers={"Content-Type": "text/calendar; charset=utf-8"}) |
| 76 | + print(f" {COUNT} calendar events") |
| 77 | + |
| 78 | + |
| 79 | +def seed_trash(s: niquests.Session, url: str, user: str) -> None: |
| 80 | + """Create files then delete them to populate the trash bin.""" |
| 81 | + dav = f"{url}/remote.php/dav/files/{user}" |
| 82 | + trash_dir = f"{PREFIX}-trash" |
| 83 | + s.request("MKCOL", f"{dav}/{trash_dir}/") |
| 84 | + for i in range(1, COUNT + 1): |
| 85 | + path = f"{dav}/{trash_dir}/trash-{i:03d}.txt" |
| 86 | + s.put(path, data=f"Trash item {i:03d}", headers={"Content-Type": "text/plain"}) |
| 87 | + for i in range(1, COUNT + 1): |
| 88 | + s.delete(f"{dav}/{trash_dir}/trash-{i:03d}.txt") |
| 89 | + s.delete(f"{dav}/{trash_dir}/") |
| 90 | + print(f" {COUNT} items in trash") |
| 91 | + |
| 92 | + |
| 93 | +def seed_collective_pages(s: niquests.Session, url: str) -> None: |
| 94 | + """Create a collective with many pages for pagination testing.""" |
| 95 | + api = f"{url}/ocs/v2.php/apps/collectives/api/v1.0" |
| 96 | + coll_name = f"{PREFIX}-collective" |
| 97 | + |
| 98 | + collectives = _ocs_data(s.get(f"{api}/collectives")) |
| 99 | + coll = next((c for c in collectives["collectives"] if c["name"] == coll_name), None) |
| 100 | + if not coll: |
| 101 | + resp = s.post( |
| 102 | + f"{api}/collectives", |
| 103 | + json={"name": coll_name}, |
| 104 | + headers={"Content-Type": "application/json"}, |
| 105 | + ) |
| 106 | + coll = _ocs_data(resp)["collective"] |
| 107 | + coll_id = coll["id"] |
| 108 | + |
| 109 | + pages_data = _ocs_data(s.get(f"{api}/collectives/{coll_id}/pages")) |
| 110 | + pages = pages_data["pages"] |
| 111 | + landing_id = pages[0]["id"] |
| 112 | + existing_titles = {p["title"] for p in pages} |
| 113 | + |
| 114 | + created = 0 |
| 115 | + for i in range(1, COUNT + 1): |
| 116 | + title = f"pagtest-page-{i:03d}" |
| 117 | + if title not in existing_titles: |
| 118 | + s.post( |
| 119 | + f"{api}/collectives/{coll_id}/pages/{landing_id}", |
| 120 | + json={"title": title}, |
| 121 | + headers={"Content-Type": "application/json"}, |
| 122 | + ) |
| 123 | + created += 1 |
| 124 | + # total = created + existing (minus landing page) |
| 125 | + print(f" {created} pages in collective '{coll_name}' (skipped {COUNT - created})") |
| 126 | + |
| 127 | + |
| 128 | +def seed_comments(s: niquests.Session, url: str, user: str) -> None: |
| 129 | + """Create a dedicated file and add many comments to it.""" |
| 130 | + dav = f"{url}/remote.php/dav/files/{user}" |
| 131 | + comment_file = f"{PAGINATION_DIR}/comment-target.txt" |
| 132 | + s.put(f"{dav}/{comment_file}", data="File with many comments", headers={"Content-Type": "text/plain"}) |
| 133 | + |
| 134 | + resp = s.request( |
| 135 | + "PROPFIND", |
| 136 | + f"{dav}/{comment_file}", |
| 137 | + data=( |
| 138 | + '<?xml version="1.0"?>' |
| 139 | + '<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">' |
| 140 | + "<d:prop><oc:fileid/></d:prop>" |
| 141 | + "</d:propfind>" |
| 142 | + ), |
| 143 | + headers={"Content-Type": "text/xml", "Depth": "0"}, |
| 144 | + ) |
| 145 | + root = ET.fromstring(resp.text) |
| 146 | + fileid_el = root.find(".//{http://owncloud.org/ns}fileid") |
| 147 | + if fileid_el is None or not fileid_el.text: |
| 148 | + print(" WARNING: could not resolve file ID for comments, skipping") |
| 149 | + return |
| 150 | + file_id = fileid_el.text |
| 151 | + |
| 152 | + for i in range(1, COUNT + 1): |
| 153 | + s.post( |
| 154 | + f"{url}/remote.php/dav/comments/files/{file_id}", |
| 155 | + json={"actorType": "users", "verb": "comment", "message": f"Pagination test comment {i:03d}"}, |
| 156 | + headers={"Content-Type": "application/json"}, |
| 157 | + ) |
| 158 | + print(f" {COUNT} comments on file {file_id}") |
| 159 | + |
| 160 | + |
| 161 | +def main() -> None: |
| 162 | + if len(sys.argv) != 4: |
| 163 | + print(f"Usage: {sys.argv[0]} <NC_URL> <USER> <PASSWORD>") |
| 164 | + sys.exit(1) |
| 165 | + |
| 166 | + url = sys.argv[1].rstrip("/") |
| 167 | + user = sys.argv[2] |
| 168 | + password = sys.argv[3] |
| 169 | + |
| 170 | + s = niquests.Session() |
| 171 | + s.auth = (user, password) |
| 172 | + s.headers.update({"OCS-APIRequest": "true", "Accept": "application/json"}) |
| 173 | + |
| 174 | + print(f"=== Seeding pagination test data ({COUNT} items per app) ===") |
| 175 | + |
| 176 | + print("Files...") |
| 177 | + seed_files(s, url, user) |
| 178 | + |
| 179 | + print("Talk conversations...") |
| 180 | + seed_conversations(s, url) |
| 181 | + |
| 182 | + print("Calendar events...") |
| 183 | + seed_calendar_events(s, url, user) |
| 184 | + |
| 185 | + print("Trash items...") |
| 186 | + seed_trash(s, url, user) |
| 187 | + |
| 188 | + print("Collective pages...") |
| 189 | + seed_collective_pages(s, url) |
| 190 | + |
| 191 | + print("Comments...") |
| 192 | + seed_comments(s, url, user) |
| 193 | + |
| 194 | + print("=== Seed complete ===") |
| 195 | + s.close() |
| 196 | + |
| 197 | + |
| 198 | +if __name__ == "__main__": |
| 199 | + main() |
0 commit comments