#!/usr/bin/env python3 """Create and verify a note on HedgeDoc.""" import argparse import os import sys import time import urllib.request import urllib.error sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', '..')) from utils.tests.helpers import resolve_domain def main(): parser = argparse.ArgumentParser() parser.add_argument('--domain', default=os.environ.get('TEST_DOMAIN')) args = parser.parse_args() domain = args.domain or resolve_domain('hedgedoc') url = f"https://{domain}" timestamp = str(int(time.time())) test_content = f"# Test Note {timestamp}\n\nThis is an automated test note created at {timestamp}." print(f"Creating a test note on {url} ...") # POST a new note and capture the redirect URL data = test_content.encode('utf-8') req = urllib.request.Request(f"{url}/new", data=data, method="POST") req.add_header("Content-Type", "text/markdown") try: # HedgeDoc returns a redirect - we need to NOT follow it opener = urllib.request.build_opener(NoRedirectHandler()) resp = opener.open(req, timeout=15) redirect_url = resp.headers.get('Location', '') except urllib.error.HTTPError as e: if e.code in (301, 302, 303, 307, 308): redirect_url = e.headers.get('Location', '') else: print(f"FAIL: POST /new returned HTTP {e.code}") sys.exit(1) if not redirect_url: print("FAIL: No redirect URL returned from POST /new") sys.exit(1) # Extract the note ID from the redirect URL (last path segment) note_id = redirect_url.rstrip('/').split('/')[-1] print(f"Created note: {note_id}") # Download the note content and verify it matches print("Downloading note content ...") download_req = urllib.request.Request(f"{url}/{note_id}/download") try: with urllib.request.urlopen(download_req, timeout=15) as resp: downloaded = resp.read().decode('utf-8') except Exception as e: print(f"FAIL: Could not download note: {e}") sys.exit(1) if f"Test Note {timestamp}" in downloaded: print(f"PASS: Note created and content verified (id: {note_id})") else: print("FAIL: Downloaded content does not match posted content") print(f"Expected to find: Test Note {timestamp}") print(f"Got: {downloaded}") sys.exit(1) class NoRedirectHandler(urllib.request.HTTPRedirectHandler): def redirect_request(self, req, fp, code, msg, headers, newurl): raise urllib.error.HTTPError(newurl, code, msg, headers, fp) if __name__ == '__main__': main()