Articles as the IndieWeb defines them: a name separate from the content.
- Migration 023 adds notes.title and notes.summary. A one-time Python data
step (migrations.DATA_MIGRATIONS) moves each note's '# Heading' first line
into title, rewrites the file and hash, empties FTS for a rebuild, and puts
files back if the migration fails. Trashed notes included.
- Micropub: name -> title, summary stored when sent with content, summary
updatable, q=source returns both. MCP tools take a summary.
- Feeds: titles only for articles (RSS/JSON none, Atom empty); Atom and
JSON Feed summaries; the title is no longer repeated in the content.
- Pages: p-name/p-summary on the article page. Homepage and tag pages show
articles as title, summary and "Read more" without images (hidden u-photo
keeps the post type for parsers).
- Crossposts: articles go out as title, summary, link and hashtags, no
images; the Bluesky card is the article. Notes unchanged.
- Editor: Title and Summary fields; an article may have no body.
- Fix: a line starting with a hashtag ('#indieweb ...') was rendered as a
heading and crossposted without its '#' (HashtagLineExtension).
- Fix: excerpts showed raw Markdown; they are plain text now.
Back up the database and the data folder before deploying: the migration
rewrites note files.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
883 lines
32 KiB
Python
883 lines
32 KiB
Python
"""
|
|
Tests for data models
|
|
|
|
Organized by model:
|
|
- Note model tests
|
|
- Session model tests
|
|
- Token model tests
|
|
- AuthState model tests
|
|
"""
|
|
|
|
import pytest
|
|
from datetime import datetime, timedelta, timezone
|
|
from pathlib import Path
|
|
from dataclasses import replace
|
|
from starpunk.models import Note, Session, Token, AuthState, split_title
|
|
from starpunk.utils import calculate_content_hash
|
|
|
|
|
|
class TestNoteModel:
|
|
"""Test Note model"""
|
|
|
|
def test_from_row_with_dict(self, tmp_path):
|
|
"""Test creating Note from dictionary row"""
|
|
row = {
|
|
"id": 1,
|
|
"slug": "test-note",
|
|
"file_path": "notes/2024/11/test-note.md",
|
|
"published": 1,
|
|
"created_at": datetime(2024, 11, 18, 14, 30),
|
|
"updated_at": datetime(2024, 11, 18, 14, 30),
|
|
"content_hash": "abc123",
|
|
}
|
|
note = Note.from_row(row, tmp_path)
|
|
assert note.id == 1
|
|
assert note.slug == "test-note"
|
|
assert note.file_path == "notes/2024/11/test-note.md"
|
|
assert note.published is True
|
|
assert note.content_hash == "abc123"
|
|
|
|
def test_from_row_with_string_timestamps(self, tmp_path):
|
|
"""Test creating Note with ISO timestamp strings"""
|
|
row = {
|
|
"id": 1,
|
|
"slug": "test-note",
|
|
"file_path": "notes/2024/11/test-note.md",
|
|
"published": True,
|
|
"created_at": "2024-11-18T14:30:00Z",
|
|
"updated_at": "2024-11-18T14:30:00Z",
|
|
"content_hash": None,
|
|
}
|
|
note = Note.from_row(row, tmp_path)
|
|
# Check year, month, day, hour, minute (ignore timezone)
|
|
assert note.created_at.year == 2024
|
|
assert note.created_at.month == 11
|
|
assert note.created_at.day == 18
|
|
assert note.created_at.hour == 14
|
|
assert note.created_at.minute == 30
|
|
|
|
def test_content_lazy_loading(self, tmp_path):
|
|
"""Test content is lazy-loaded from file"""
|
|
# Create test note file
|
|
note_file = tmp_path / "notes" / "2024" / "11" / "test.md"
|
|
note_file.parent.mkdir(parents=True)
|
|
note_file.write_text("# Test Note\n\nContent here.")
|
|
|
|
# Create note instance
|
|
note = Note(
|
|
id=1,
|
|
slug="test",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
_data_dir=tmp_path,
|
|
)
|
|
|
|
# Content should be loaded on first access
|
|
content = note.content
|
|
assert "# Test Note" in content
|
|
assert "Content here." in content
|
|
|
|
# Second access should return cached value
|
|
content2 = note.content
|
|
assert content2 == content
|
|
|
|
def test_content_file_not_found(self, tmp_path):
|
|
"""Test content raises FileNotFoundError if file missing"""
|
|
note = Note(
|
|
id=1,
|
|
slug="missing",
|
|
file_path="notes/2024/11/missing.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
_data_dir=tmp_path,
|
|
)
|
|
|
|
with pytest.raises(FileNotFoundError):
|
|
_ = note.content
|
|
|
|
def test_html_rendering(self, tmp_path):
|
|
"""Test HTML rendering with caching"""
|
|
# Create test note
|
|
note_file = tmp_path / "notes" / "2024" / "11" / "test.md"
|
|
note_file.parent.mkdir(parents=True)
|
|
note_file.write_text("# Heading\n\nParagraph here.")
|
|
|
|
note = Note(
|
|
id=1,
|
|
slug="test",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
_data_dir=tmp_path,
|
|
)
|
|
|
|
html = note.html
|
|
# Check for common markdown-rendered elements
|
|
assert "<h1" in html # Heading
|
|
assert "Paragraph here." in html
|
|
|
|
def test_html_caching(self, tmp_path):
|
|
"""Test HTML is cached after first render"""
|
|
note_file = tmp_path / "notes" / "2024" / "11" / "test.md"
|
|
note_file.parent.mkdir(parents=True)
|
|
note_file.write_text("Content")
|
|
|
|
note = Note(
|
|
id=1,
|
|
slug="test",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
_data_dir=tmp_path,
|
|
)
|
|
|
|
html1 = note.html
|
|
html2 = note.html
|
|
assert html1 == html2
|
|
|
|
def test_title_is_the_column_not_the_content(self, tmp_path):
|
|
"""A title comes from notes.title; a heading in the content is just a heading (ADR-076)"""
|
|
note_file = tmp_path / "notes" / "2024" / "11" / "test.md"
|
|
note_file.parent.mkdir(parents=True)
|
|
note_file.write_text("# A Heading\n\nContent.")
|
|
kwargs = dict(
|
|
id=1,
|
|
slug="test",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
_data_dir=tmp_path,
|
|
)
|
|
assert Note(**kwargs).title is None
|
|
assert "<h1>A Heading</h1>" in Note(**kwargs).html
|
|
assert Note(**kwargs, title="My Note Title").title == "My Note Title"
|
|
|
|
def test_label(self, tmp_path):
|
|
"""label: title, else excerpt, else slug"""
|
|
assert self._note_with(tmp_path, "Just a note\n\nMore.").label == "Just a note More."
|
|
note = Note(
|
|
id=1,
|
|
slug="my-test-note",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
_data_dir=Path("/nonexistent"),
|
|
title="Titled",
|
|
)
|
|
assert note.label == "Titled"
|
|
assert replace(note, title=None).label == "my-test-note"
|
|
|
|
def test_excerpt_generation(self, tmp_path):
|
|
"""Test excerpt generation"""
|
|
note_file = tmp_path / "notes" / "2024" / "11" / "test.md"
|
|
note_file.parent.mkdir(parents=True)
|
|
content = "# Title\n\n" + ("This is a test. " * 50) # Long content
|
|
note_file.write_text(content)
|
|
|
|
note = Note(
|
|
id=1,
|
|
slug="test",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
_data_dir=tmp_path,
|
|
)
|
|
|
|
excerpt = note.excerpt
|
|
assert len(excerpt) <= 201 # EXCERPT_LENGTH + "…"
|
|
assert excerpt.endswith("…")
|
|
|
|
def test_excerpt_short_content(self, tmp_path):
|
|
"""Test excerpt with short content (no ellipsis)"""
|
|
note_file = tmp_path / "notes" / "2024" / "11" / "test.md"
|
|
note_file.parent.mkdir(parents=True)
|
|
note_file.write_text("# Title\n\nShort content.")
|
|
|
|
note = Note(
|
|
id=1,
|
|
slug="test",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
_data_dir=tmp_path,
|
|
)
|
|
|
|
excerpt = note.excerpt
|
|
assert "Short content." in excerpt
|
|
assert not excerpt.endswith("...")
|
|
|
|
def test_excerpt_fallback_to_slug(self):
|
|
"""Test excerpt falls back to slug if file missing"""
|
|
note = Note(
|
|
id=1,
|
|
slug="my-note",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
_data_dir=Path("/nonexistent"),
|
|
)
|
|
|
|
assert note.excerpt == "my-note"
|
|
|
|
def _note_with(self, tmp_path, content):
|
|
note_file = tmp_path / "notes" / "2024" / "11" / "test.md"
|
|
note_file.parent.mkdir(parents=True, exist_ok=True)
|
|
note_file.write_text(content)
|
|
return Note(
|
|
id=1,
|
|
slug="test",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
_data_dir=tmp_path,
|
|
)
|
|
|
|
def test_excerpt_is_plain_text(self, tmp_path):
|
|
"""No Markdown in previews, alt text or crosspost teasers (v2.16.0)"""
|
|
note = self._note_with(
|
|
tmp_path, "Some **bold** and a [link](https://x.example).\n\n\n\n- one\n- two")
|
|
assert note.excerpt == "Some bold and a link. • one • two"
|
|
|
|
def test_hashtag_line_is_not_a_heading(self, tmp_path):
|
|
"""'#indieweb ...' renders as text; '# Heading' still a heading (v2.16.0)"""
|
|
note = self._note_with(tmp_path, "#indieweb is fun\n\n##two\n\n# Real heading")
|
|
assert "<p>#indieweb is fun</p>" in note.html
|
|
assert "<p>##two</p>" in note.html
|
|
assert "<h1>Real heading</h1>" in note.html
|
|
assert note.excerpt.startswith("#indieweb is fun ##two")
|
|
|
|
def test_hashtag_in_code_untouched(self, tmp_path):
|
|
note = self._note_with(tmp_path, "```\n#include <x>\n```")
|
|
assert "\\" not in note.html and "include" in note.html
|
|
|
|
def test_split_title(self):
|
|
"""A '# Heading' first line is split off as the title"""
|
|
assert split_title("# My Title\n\nBody") == ("My Title", "Body")
|
|
assert split_title("\n## Sub\nBody") == ("Sub", "Body")
|
|
assert split_title("No heading\n# later") == (None, "No heading\n# later")
|
|
assert split_title("#\nBody") == (None, "#\nBody")
|
|
|
|
def test_permalink(self):
|
|
"""Test permalink generation"""
|
|
note = Note(
|
|
id=1,
|
|
slug="my-note",
|
|
file_path="notes/2024/11/my-note.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
_data_dir=Path("data"),
|
|
)
|
|
|
|
assert note.permalink == "/note/my-note"
|
|
|
|
def test_is_published_property(self):
|
|
"""Test is_published property"""
|
|
note = Note(
|
|
id=1,
|
|
slug="test",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
_data_dir=Path("data"),
|
|
)
|
|
|
|
assert note.is_published is True
|
|
assert note.is_published == note.published
|
|
|
|
def test_to_dict_basic(self):
|
|
"""Test serialization to dictionary"""
|
|
note = Note(
|
|
id=1,
|
|
slug="test",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime(2024, 11, 18, 14, 30),
|
|
updated_at=datetime(2024, 11, 18, 14, 30),
|
|
_data_dir=Path("data"),
|
|
)
|
|
|
|
data = note.to_dict()
|
|
assert data["id"] == 1
|
|
assert data["slug"] == "test"
|
|
assert data["published"] is True
|
|
assert data["permalink"] == "/note/test"
|
|
assert "content" not in data # Not included by default
|
|
assert "html" not in data # Not included by default
|
|
|
|
def test_to_dict_with_content(self, tmp_path):
|
|
"""Test serialization includes content when requested"""
|
|
note_file = tmp_path / "notes" / "2024" / "11" / "test.md"
|
|
note_file.parent.mkdir(parents=True)
|
|
note_file.write_text("Test content")
|
|
|
|
note = Note(
|
|
id=1,
|
|
slug="test",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
_data_dir=tmp_path,
|
|
)
|
|
|
|
data = note.to_dict(include_content=True)
|
|
assert "content" in data
|
|
assert data["content"] == "Test content"
|
|
|
|
def test_to_dict_with_html(self, tmp_path):
|
|
"""Test serialization includes HTML when requested"""
|
|
note_file = tmp_path / "notes" / "2024" / "11" / "test.md"
|
|
note_file.parent.mkdir(parents=True)
|
|
note_file.write_text("# Test")
|
|
|
|
note = Note(
|
|
id=1,
|
|
slug="test",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
_data_dir=tmp_path,
|
|
)
|
|
|
|
data = note.to_dict(include_html=True)
|
|
assert "html" in data
|
|
assert "<h1" in data["html"]
|
|
|
|
def test_verify_integrity_success(self, tmp_path):
|
|
"""Test content integrity verification succeeds"""
|
|
note_file = tmp_path / "notes" / "2024" / "11" / "test.md"
|
|
note_file.parent.mkdir(parents=True)
|
|
content = "Test content"
|
|
note_file.write_text(content)
|
|
|
|
# Calculate correct hash
|
|
content_hash = calculate_content_hash(content)
|
|
|
|
note = Note(
|
|
id=1,
|
|
slug="test",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
content_hash=content_hash,
|
|
_data_dir=tmp_path,
|
|
)
|
|
|
|
# Should verify successfully
|
|
assert note.verify_integrity() is True
|
|
|
|
def test_verify_integrity_failure(self, tmp_path):
|
|
"""Test content integrity verification fails when modified"""
|
|
note_file = tmp_path / "notes" / "2024" / "11" / "test.md"
|
|
note_file.parent.mkdir(parents=True)
|
|
content = "Test content"
|
|
note_file.write_text(content)
|
|
|
|
# Calculate hash of original content
|
|
content_hash = calculate_content_hash(content)
|
|
|
|
note = Note(
|
|
id=1,
|
|
slug="test",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
content_hash=content_hash,
|
|
_data_dir=tmp_path,
|
|
)
|
|
|
|
# Modify file
|
|
note_file.write_text("Modified content")
|
|
|
|
# Should fail verification
|
|
assert note.verify_integrity() is False
|
|
|
|
def test_verify_integrity_no_hash(self, tmp_path):
|
|
"""Test integrity verification fails if no hash stored"""
|
|
note_file = tmp_path / "notes" / "2024" / "11" / "test.md"
|
|
note_file.parent.mkdir(parents=True)
|
|
note_file.write_text("Content")
|
|
|
|
note = Note(
|
|
id=1,
|
|
slug="test",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
content_hash=None,
|
|
_data_dir=tmp_path,
|
|
)
|
|
|
|
# Should return False (no hash to verify against)
|
|
assert note.verify_integrity() is False
|
|
|
|
def test_verify_integrity_file_not_found(self):
|
|
"""Test integrity verification fails if file missing"""
|
|
note = Note(
|
|
id=1,
|
|
slug="test",
|
|
file_path="notes/2024/11/test.md",
|
|
published=True,
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
updated_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
content_hash="abc123",
|
|
_data_dir=Path("/nonexistent"),
|
|
)
|
|
|
|
assert note.verify_integrity() is False
|
|
|
|
|
|
class TestSessionModel:
|
|
"""Test Session model"""
|
|
|
|
def test_from_row_with_dict(self):
|
|
"""Test creating Session from dictionary row"""
|
|
row = {
|
|
"id": 1,
|
|
"session_token": "abc123",
|
|
"me": "https://alice.example.com",
|
|
"created_at": datetime(2024, 11, 18, 14, 30),
|
|
"expires_at": datetime(2024, 12, 18, 14, 30),
|
|
"last_used_at": None,
|
|
}
|
|
session = Session.from_row(row)
|
|
assert session.id == 1
|
|
assert session.session_token == "abc123"
|
|
assert session.me == "https://alice.example.com"
|
|
|
|
def test_from_row_with_string_timestamps(self):
|
|
"""Test creating Session with ISO timestamp strings"""
|
|
row = {
|
|
"id": 1,
|
|
"session_token": "abc123",
|
|
"me": "https://alice.example.com",
|
|
"created_at": "2024-11-18T14:30:00Z",
|
|
"expires_at": "2024-12-18T14:30:00Z",
|
|
"last_used_at": "2024-11-18T15:00:00Z",
|
|
}
|
|
session = Session.from_row(row)
|
|
# Check timestamps were parsed correctly (ignore timezone)
|
|
assert session.created_at.year == 2024
|
|
assert session.created_at.month == 11
|
|
assert session.expires_at.month == 12
|
|
assert session.last_used_at.hour == 15
|
|
|
|
def test_is_expired_false(self):
|
|
"""Test is_expired returns False for active session"""
|
|
session = Session(
|
|
id=1,
|
|
session_token="abc123",
|
|
me="https://alice.example.com",
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(days=30),
|
|
)
|
|
assert session.is_expired is False
|
|
assert session.is_active is True
|
|
|
|
def test_is_expired_true(self):
|
|
"""Test is_expired returns True for expired session"""
|
|
session = Session(
|
|
id=1,
|
|
session_token="abc123",
|
|
me="https://alice.example.com",
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(days=31),
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(days=1),
|
|
)
|
|
assert session.is_expired is True
|
|
assert session.is_active is False
|
|
|
|
def test_age(self):
|
|
"""Test age calculation"""
|
|
created = datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(hours=2)
|
|
session = Session(
|
|
id=1,
|
|
session_token="abc123",
|
|
me="https://alice.example.com",
|
|
created_at=created,
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(days=30),
|
|
)
|
|
age = session.age
|
|
# Age should be at least 2 hours (account for test execution time)
|
|
assert age.total_seconds() >= 7200
|
|
|
|
def test_time_until_expiry(self):
|
|
"""Test time until expiry calculation"""
|
|
session = Session(
|
|
id=1,
|
|
session_token="abc123",
|
|
me="https://alice.example.com",
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(days=30),
|
|
)
|
|
time_left = session.time_until_expiry
|
|
# Should be approximately 30 days
|
|
assert time_left.days >= 29
|
|
|
|
def test_time_until_expiry_negative(self):
|
|
"""Test time until expiry is negative for expired session"""
|
|
session = Session(
|
|
id=1,
|
|
session_token="abc123",
|
|
me="https://alice.example.com",
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(days=31),
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(days=1),
|
|
)
|
|
time_left = session.time_until_expiry
|
|
assert time_left.total_seconds() < 0
|
|
|
|
def test_is_valid_active_session(self):
|
|
"""Test validation succeeds for active session"""
|
|
session = Session(
|
|
id=1,
|
|
session_token="abc123",
|
|
me="https://alice.example.com",
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(days=30),
|
|
)
|
|
assert session.is_valid() is True
|
|
|
|
def test_is_valid_expired_session(self):
|
|
"""Test validation fails for expired session"""
|
|
session = Session(
|
|
id=1,
|
|
session_token="abc123",
|
|
me="https://alice.example.com",
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(days=31),
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(days=1),
|
|
)
|
|
assert session.is_valid() is False
|
|
|
|
def test_is_valid_empty_token(self):
|
|
"""Test validation fails for empty token"""
|
|
session = Session(
|
|
id=1,
|
|
session_token="",
|
|
me="https://alice.example.com",
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(days=30),
|
|
)
|
|
assert session.is_valid() is False
|
|
|
|
def test_is_valid_empty_me(self):
|
|
"""Test validation fails for empty me URL"""
|
|
session = Session(
|
|
id=1,
|
|
session_token="abc123",
|
|
me="",
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(days=30),
|
|
)
|
|
assert session.is_valid() is False
|
|
|
|
def test_with_updated_last_used(self):
|
|
"""Test creating session with updated timestamp"""
|
|
original = Session(
|
|
id=1,
|
|
session_token="abc123",
|
|
me="https://alice.example.com",
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(days=30),
|
|
last_used_at=None,
|
|
)
|
|
|
|
updated = original.with_updated_last_used()
|
|
assert updated.last_used_at is not None
|
|
assert updated.session_token == original.session_token
|
|
assert updated.id == original.id
|
|
|
|
def test_to_dict(self):
|
|
"""Test serialization to dictionary"""
|
|
session = Session(
|
|
id=1,
|
|
session_token="abc123",
|
|
me="https://alice.example.com",
|
|
created_at=datetime(2024, 11, 18, 14, 30),
|
|
expires_at=datetime(2024, 12, 18, 14, 30),
|
|
)
|
|
data = session.to_dict()
|
|
assert data["id"] == 1
|
|
assert data["me"] == "https://alice.example.com"
|
|
assert "session_token" not in data # Excluded for security
|
|
assert "is_active" in data
|
|
|
|
|
|
class TestTokenModel:
|
|
"""Test Token model"""
|
|
|
|
def test_from_row_with_dict(self):
|
|
"""Test creating Token from dictionary row"""
|
|
row = {
|
|
"token": "xyz789",
|
|
"me": "https://alice.example.com",
|
|
"client_id": "https://quill.p3k.io",
|
|
"scope": "create update",
|
|
"created_at": datetime(2024, 11, 18, 14, 30),
|
|
"expires_at": None,
|
|
}
|
|
token = Token.from_row(row)
|
|
assert token.token == "xyz789"
|
|
assert token.me == "https://alice.example.com"
|
|
assert token.scope == "create update"
|
|
|
|
def test_from_row_with_string_timestamps(self):
|
|
"""Test creating Token with ISO timestamp strings"""
|
|
row = {
|
|
"token": "xyz789",
|
|
"me": "https://alice.example.com",
|
|
"client_id": None,
|
|
"scope": "create",
|
|
"created_at": "2024-11-18T14:30:00Z",
|
|
"expires_at": "2025-02-18T14:30:00Z",
|
|
}
|
|
token = Token.from_row(row)
|
|
# Check timestamps were parsed correctly (ignore timezone)
|
|
assert token.created_at.year == 2024
|
|
assert token.created_at.month == 11
|
|
assert token.expires_at.year == 2025
|
|
assert token.expires_at.month == 2
|
|
|
|
def test_scopes_property(self):
|
|
"""Test scope parsing"""
|
|
token = Token(
|
|
token="xyz789",
|
|
me="https://alice.example.com",
|
|
scope="create update delete",
|
|
)
|
|
assert token.scopes == ["create", "update", "delete"]
|
|
|
|
def test_scopes_empty(self):
|
|
"""Test empty scope"""
|
|
token = Token(token="xyz789", me="https://alice.example.com", scope=None)
|
|
assert token.scopes == []
|
|
|
|
def test_scopes_whitespace(self):
|
|
"""Test scope with multiple spaces"""
|
|
token = Token(
|
|
token="xyz789", me="https://alice.example.com", scope="create update"
|
|
)
|
|
scopes = token.scopes
|
|
assert "create" in scopes
|
|
assert "update" in scopes
|
|
|
|
def test_has_scope_true(self):
|
|
"""Test scope checking returns True"""
|
|
token = Token(
|
|
token="xyz789", me="https://alice.example.com", scope="create update"
|
|
)
|
|
assert token.has_scope("create") is True
|
|
assert token.has_scope("update") is True
|
|
|
|
def test_has_scope_false(self):
|
|
"""Test scope checking returns False"""
|
|
token = Token(
|
|
token="xyz789", me="https://alice.example.com", scope="create update"
|
|
)
|
|
assert token.has_scope("delete") is False
|
|
|
|
def test_has_scope_empty(self):
|
|
"""Test scope checking with no scopes"""
|
|
token = Token(token="xyz789", me="https://alice.example.com", scope=None)
|
|
assert token.has_scope("create") is False
|
|
|
|
def test_is_expired_never_expires(self):
|
|
"""Test token with no expiry"""
|
|
token = Token(token="xyz789", me="https://alice.example.com", expires_at=None)
|
|
assert token.is_expired is False
|
|
assert token.is_active is True
|
|
|
|
def test_is_expired_with_future_expiry(self):
|
|
"""Test token with future expiry"""
|
|
token = Token(
|
|
token="xyz789",
|
|
me="https://alice.example.com",
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(days=90),
|
|
)
|
|
assert token.is_expired is False
|
|
assert token.is_active is True
|
|
|
|
def test_is_expired_with_past_expiry(self):
|
|
"""Test token with past expiry"""
|
|
token = Token(
|
|
token="xyz789",
|
|
me="https://alice.example.com",
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(days=1),
|
|
)
|
|
assert token.is_expired is True
|
|
assert token.is_active is False
|
|
|
|
def test_is_valid_active_token(self):
|
|
"""Test validation succeeds for active token"""
|
|
token = Token(token="xyz789", me="https://alice.example.com", scope="create")
|
|
assert token.is_valid() is True
|
|
|
|
def test_is_valid_expired_token(self):
|
|
"""Test validation fails for expired token"""
|
|
token = Token(
|
|
token="xyz789",
|
|
me="https://alice.example.com",
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(days=1),
|
|
)
|
|
assert token.is_valid() is False
|
|
|
|
def test_is_valid_empty_token(self):
|
|
"""Test validation fails for empty token"""
|
|
token = Token(token="", me="https://alice.example.com")
|
|
assert token.is_valid() is False
|
|
|
|
def test_is_valid_with_required_scope_success(self):
|
|
"""Test validation with required scope succeeds"""
|
|
token = Token(
|
|
token="xyz789", me="https://alice.example.com", scope="create update"
|
|
)
|
|
assert token.is_valid(required_scope="create") is True
|
|
|
|
def test_is_valid_with_required_scope_failure(self):
|
|
"""Test validation with required scope fails"""
|
|
token = Token(
|
|
token="xyz789", me="https://alice.example.com", scope="create update"
|
|
)
|
|
assert token.is_valid(required_scope="delete") is False
|
|
|
|
def test_to_dict(self):
|
|
"""Test serialization to dictionary"""
|
|
token = Token(
|
|
token="xyz789",
|
|
me="https://alice.example.com",
|
|
client_id="https://quill.p3k.io",
|
|
scope="create update",
|
|
created_at=datetime(2024, 11, 18, 14, 30),
|
|
)
|
|
data = token.to_dict()
|
|
assert data["me"] == "https://alice.example.com"
|
|
assert data["client_id"] == "https://quill.p3k.io"
|
|
assert data["scope"] == "create update"
|
|
assert "token" not in data # Excluded for security
|
|
assert "is_active" in data
|
|
|
|
def test_to_dict_with_expires_at(self):
|
|
"""Test serialization includes expires_at if set"""
|
|
token = Token(
|
|
token="xyz789",
|
|
me="https://alice.example.com",
|
|
created_at=datetime(2024, 11, 18, 14, 30),
|
|
expires_at=datetime(2025, 2, 18, 14, 30),
|
|
)
|
|
data = token.to_dict()
|
|
assert "expires_at" in data
|
|
|
|
|
|
class TestAuthStateModel:
|
|
"""Test AuthState model"""
|
|
|
|
def test_from_row_with_dict(self):
|
|
"""Test creating AuthState from dictionary row"""
|
|
row = {
|
|
"state": "random123",
|
|
"created_at": datetime(2024, 11, 18, 14, 30),
|
|
"expires_at": datetime(2024, 11, 18, 14, 35),
|
|
}
|
|
auth_state = AuthState.from_row(row)
|
|
assert auth_state.state == "random123"
|
|
|
|
def test_from_row_with_string_timestamps(self):
|
|
"""Test creating AuthState with ISO timestamp strings"""
|
|
row = {
|
|
"state": "random123",
|
|
"created_at": "2024-11-18T14:30:00Z",
|
|
"expires_at": "2024-11-18T14:35:00Z",
|
|
}
|
|
auth_state = AuthState.from_row(row)
|
|
# Check timestamps were parsed correctly (ignore timezone)
|
|
assert auth_state.created_at.year == 2024
|
|
assert auth_state.created_at.minute == 30
|
|
assert auth_state.expires_at.minute == 35
|
|
|
|
def test_is_expired_false(self):
|
|
"""Test is_expired returns False for active state"""
|
|
auth_state = AuthState(
|
|
state="random123",
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(minutes=5),
|
|
)
|
|
assert auth_state.is_expired is False
|
|
assert auth_state.is_active is True
|
|
|
|
def test_is_expired_true(self):
|
|
"""Test is_expired returns True for expired state"""
|
|
auth_state = AuthState(
|
|
state="random123",
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(minutes=10),
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(minutes=5),
|
|
)
|
|
assert auth_state.is_expired is True
|
|
assert auth_state.is_active is False
|
|
|
|
def test_age(self):
|
|
"""Test age calculation"""
|
|
created = datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(minutes=2)
|
|
auth_state = AuthState(
|
|
state="random123",
|
|
created_at=created,
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(minutes=3),
|
|
)
|
|
age = auth_state.age
|
|
# Age should be at least 2 minutes
|
|
assert age.total_seconds() >= 120
|
|
|
|
def test_is_valid_active_state(self):
|
|
"""Test validation succeeds for active state"""
|
|
auth_state = AuthState(
|
|
state="random123",
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(minutes=5),
|
|
)
|
|
assert auth_state.is_valid() is True
|
|
|
|
def test_is_valid_expired_state(self):
|
|
"""Test validation fails for expired state"""
|
|
auth_state = AuthState(
|
|
state="random123",
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(minutes=10),
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(minutes=5),
|
|
)
|
|
assert auth_state.is_valid() is False
|
|
|
|
def test_is_valid_empty_state(self):
|
|
"""Test validation fails for empty state"""
|
|
auth_state = AuthState(
|
|
state="",
|
|
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
|
|
expires_at=datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(minutes=5),
|
|
)
|
|
assert auth_state.is_valid() is False
|
|
|
|
def test_to_dict(self):
|
|
"""Test serialization to dictionary"""
|
|
auth_state = AuthState(
|
|
state="random123",
|
|
created_at=datetime(2024, 11, 18, 14, 30),
|
|
expires_at=datetime(2024, 11, 18, 14, 35),
|
|
)
|
|
data = auth_state.to_dict()
|
|
assert "created_at" in data
|
|
assert "expires_at" in data
|
|
assert "is_active" in data
|
|
assert "state" not in data # State value not included in dict
|