diff --git a/.github/workflows/browser-e2e.yml b/.github/workflows/browser-e2e.yml new file mode 100644 index 0000000..2069ad1 --- /dev/null +++ b/.github/workflows/browser-e2e.yml @@ -0,0 +1,56 @@ +name: Browser E2E + +on: + pull_request: + branches: [main] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: browser-e2e-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + browser: + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: "3.12" + cache: pip + cache-dependency-path: requirements.txt + - uses: actions/setup-node@v4 + with: + node-version: "20" + - name: Install Python dependencies + run: python -m pip install --upgrade pip && python -m pip install -r requirements.txt + - name: Prepare test database and account + env: + DJANGO_DEBUG: "True" + DJANGO_SECRET_KEY: browser-test-only-secret + DJANGO_ALLOWED_HOSTS: localhost,127.0.0.1 + run: | + python manage.py migrate --noinput + python manage.py shell -c "from django.contrib.auth import get_user_model; get_user_model().objects.filter(username='e2e-user').delete(); get_user_model().objects.create_user('e2e-user', password='E2E-strong-pass-123!')" + - name: Install Playwright + run: npm install --no-save @playwright/test && npx playwright install --with-deps chromium + - name: Start Django server + env: + DJANGO_DEBUG: "True" + DJANGO_SECRET_KEY: browser-test-only-secret + DJANGO_ALLOWED_HOSTS: localhost,127.0.0.1 + run: | + python manage.py runserver 127.0.0.1:8000 > /tmp/ember-django.log 2>&1 & + echo $! > /tmp/ember-django.pid + for i in $(seq 1 30); do + if curl --fail --silent http://127.0.0.1:8000/accounts/login/ > /dev/null; then exit 0; fi + sleep 1 + done + cat /tmp/ember-django.log + exit 1 + - name: Run browser tests + run: npx playwright test diff --git a/README.md b/README.md index 58a8017..e98449d 100644 --- a/README.md +++ b/README.md @@ -199,3 +199,26 @@ requirements.txt ## License No license file was present when this README was written. Add a `LICENSE` file before inviting reuse or redistribution; without an explicit license, normal copyright restrictions apply. + + +## Phase 3: Markdown preview and data portability + +Phase 3 adds a server-rendered Markdown preview with HTML sanitization, versioned JSON export/import, and browser smoke tests. + +- Use **Preview** in the note editor to render headings, emphasis, lists, tables, blockquotes, links, and fenced code. The preview endpoint sanitizes generated HTML before it reaches the browser. +- Use **Export notes (JSON)** in the sidebar to download all of your own notes, including tags, pinned/archive state, and trash state. The export is private and marked `no-store`. +- Use **Import notes** to add notes from an Ember export. Imports are additive, limited to 2 MB / 5,000 notes, validated before writes, and processed atomically. Existing notes are not overwritten. +- The export format is versioned (`format: ember-notes`, `version: 1`). Keep a separate backup before importing into an account with important data. +- Django unit tests cover export privacy, import validation/atomicity, and Markdown sanitization. The separate Browser E2E workflow uses Playwright + Chromium to smoke-test login, editor preview, and export/import navigation. + +Run browser tests locally after installing Playwright and Chromium: + +```bash +npm install --no-save @playwright/test +npx playwright install chromium +python manage.py migrate +python manage.py runserver +npx playwright test +``` + +Browser tests use `playwright.config.cjs` and `tests/e2e/`. They expect a local test account named `e2e-user` with password `E2E-strong-pass-123!`; create it only in a local test database, never in production. diff --git a/notes/test_phase3.py b/notes/test_phase3.py new file mode 100644 index 0000000..03e8aee --- /dev/null +++ b/notes/test_phase3.py @@ -0,0 +1,82 @@ +import json +from django.contrib.auth import get_user_model +from django.core.files.uploadedfile import SimpleUploadedFile +from django.test import TestCase +from django.urls import reverse + +from .models import Note + +User = get_user_model() + + +class PhaseThreeTests(TestCase): + @classmethod + def setUpTestData(cls): + cls.alice = User.objects.create_user("phase3-alice", password="Strong-pass-123!") + cls.bob = User.objects.create_user("phase3-bob", password="Strong-pass-456!") + + def setUp(self): + self.client.force_login(self.alice) + + def test_export_contains_only_current_users_notes_and_is_private(self): + Note.objects.create(user=self.alice, title="My note", body="Private body") + Note.objects.create(user=self.bob, title="Other user's secret", body="Do not export") + response = self.client.get(reverse("notes:export")) + self.assertEqual(response.status_code, 200) + self.assertIn("attachment;", response["Content-Disposition"]) + self.assertEqual(response["Cache-Control"], "private, no-store") + payload = json.loads(response.content) + self.assertEqual(payload["format"], "ember-notes") + self.assertEqual(payload["version"], 1) + self.assertEqual([item["title"] for item in payload["notes"]], ["My note"]) + self.assertNotIn("Other user's secret", response.content.decode()) + + def test_import_creates_notes_and_tags_for_current_user(self): + payload = {"format": "ember-notes", "version": 1, "notes": [{ + "title": "Imported", "body": "Hello **world**", "tags": ["Work", "ideas"], + "is_pinned": True, "is_archived": False, "deleted_at": None, + }]} + upload = SimpleUploadedFile("backup.json", json.dumps(payload).encode(), content_type="application/json") + response = self.client.post(reverse("notes:import"), {"file": upload}) + self.assertRedirects(response, reverse("notes:list")) + note = Note.objects.get(user=self.alice, title="Imported") + self.assertEqual(note.body, "Hello **world**") + self.assertTrue(note.is_pinned) + self.assertEqual(sorted(note.tags.values_list("name", flat=True)), ["ideas", "work"]) + self.assertFalse(Note.objects.filter(user=self.bob, title="Imported").exists()) + + def test_invalid_import_is_rejected_without_partial_writes(self): + payload = {"format": "ember-notes", "version": 1, "notes": [ + {"title": "Valid-looking", "body": "ok", "tags": []}, + {"title": "x" * 201, "body": "too long title", "tags": []}, + ]} + upload = SimpleUploadedFile("bad.json", json.dumps(payload).encode(), content_type="application/json") + response = self.client.post(reverse("notes:import"), {"file": upload}) + self.assertRedirects(response, reverse("notes:import")) + self.assertFalse(Note.objects.filter(user=self.alice, title="Valid-looking").exists()) + + def test_markdown_preview_sanitizes_script_and_javascript_links(self): + response = self.client.post( + reverse("notes:preview"), + data=json.dumps({"body": "# Safe heading\n\n\n\n[bad](javascript:alert(2))"}), + content_type="application/json", + ) + self.assertEqual(response.status_code, 200) + html = response.json()["html"] + self.assertIn("