| 1 |
from unittest import skipUnless |
| 2 |
from django.test import TestCase |
| 3 |
from django.urls import reverse |
| 4 |
from playwright.sync_api import expect |
| 5 |
|
| 6 |
try: |
| 7 |
from reversion import revisions |
| 8 |
from reversion.models import Version |
| 9 |
|
| 10 |
REVERSION_INSTALLED = True |
| 11 |
except ImportError: |
| 12 |
REVERSION_INSTALLED = False |
| 13 |
revisions = None |
| 14 |
Version = None |
| 15 |
|
| 16 |
from ..models import Article, BlogPost, NewsArticle |
| 17 |
from polymorphic.tests.utils import _GenericUITest |
| 18 |
|
| 19 |
|
| 20 |
@skipUnless(REVERSION_INSTALLED, "django-reversion is not installed") |
| 21 |
class ReversionIntegrationTests(TestCase): |
| 22 |
""" |
| 23 |
Tests for django-reversion integration with polymorphic models. |
| 24 |
|
| 25 |
These tests verify that: |
| 26 |
1. Polymorphic models can be versioned |
| 27 |
2. The follow parameter correctly tracks parent model changes |
| 28 |
3. Revisions are created and can be retrieved |
| 29 |
""" |
| 30 |
|
| 31 |
def test_blogpost_versioning(self): |
| 32 |
"""Test that BlogPost instances are properly versioned.""" |
| 33 |
# Create a blog post with reversion |
| 34 |
with revisions.create_revision(): |
| 35 |
blog_post = BlogPost.objects.create( |
| 36 |
title="First Post", |
| 37 |
content="This is my first blog post.", |
| 38 |
author="John Doe", |
| 39 |
) |
| 40 |
revisions.set_comment("Initial version") |
| 41 |
|
| 42 |
# Verify a version was created |
| 43 |
versions = Version.objects.get_for_object(blog_post) |
| 44 |
self.assertEqual(versions.count(), 1) |
| 45 |
self.assertEqual(versions[0].revision.comment, "Initial version") |
| 46 |
|
| 47 |
# Update the blog post |
| 48 |
with revisions.create_revision(): |
| 49 |
blog_post.title = "Updated Post" |
| 50 |
blog_post.content = "This is my updated blog post." |
| 51 |
blog_post.save() |
| 52 |
revisions.set_comment("Updated title and content") |
| 53 |
|
| 54 |
# Verify we now have two versions |
| 55 |
versions = Version.objects.get_for_object(blog_post) |
| 56 |
self.assertEqual(versions.count(), 2) |
| 57 |
|
| 58 |
# Verify we can retrieve the old version |
| 59 |
old_version = versions[1] |
| 60 |
old_data = old_version.field_dict |
| 61 |
self.assertEqual(old_data["title"], "First Post") |
| 62 |
self.assertEqual(old_data["content"], "This is my first blog post.") |
| 63 |
|
| 64 |
def test_newsarticle_versioning(self): |
| 65 |
"""Test that NewsArticle instances are properly versioned.""" |
| 66 |
# Create a news article with reversion |
| 67 |
with revisions.create_revision(): |
| 68 |
news_article = NewsArticle.objects.create( |
| 69 |
title="Breaking News", |
| 70 |
content="Important news story.", |
| 71 |
source="Daily News", |
| 72 |
) |
| 73 |
revisions.set_comment("Published article") |
| 74 |
|
| 75 |
# Verify a version was created |
| 76 |
versions = Version.objects.get_for_object(news_article) |
| 77 |
self.assertEqual(versions.count(), 1) |
| 78 |
|
| 79 |
# Update the news article |
| 80 |
with revisions.create_revision(): |
| 81 |
news_article.content = "Updated news story with more details." |
| 82 |
news_article.save() |
| 83 |
revisions.set_comment("Added more details") |
| 84 |
|
| 85 |
# Verify we now have two versions |
| 86 |
versions = Version.objects.get_for_object(news_article) |
| 87 |
self.assertEqual(versions.count(), 2) |
| 88 |
|
| 89 |
def test_polymorphic_queryset_with_versioned_objects(self): |
| 90 |
"""Test that polymorphic queries work correctly with versioned objects.""" |
| 91 |
# Create instances of both child models |
| 92 |
with revisions.create_revision(): |
| 93 |
BlogPost.objects.create( |
| 94 |
title="Blog Post", |
| 95 |
content="Blog content", |
| 96 |
author="Jane Smith", |
| 97 |
) |
| 98 |
NewsArticle.objects.create( |
| 99 |
title="News Article", |
| 100 |
content="News content", |
| 101 |
source="News Corp", |
| 102 |
) |
| 103 |
|
| 104 |
# Query using the polymorphic base model |
| 105 |
articles = Article.objects.all() |
| 106 |
self.assertEqual(articles.count(), 2) |
| 107 |
|
| 108 |
# Verify polymorphic behavior |
| 109 |
self.assertIsInstance(articles[0], (BlogPost, NewsArticle)) |
| 110 |
self.assertIsInstance(articles[1], (BlogPost, NewsArticle)) |
| 111 |
|
| 112 |
# Verify both have versions |
| 113 |
for article in articles: |
| 114 |
versions = Version.objects.get_for_object(article) |
| 115 |
self.assertGreaterEqual(versions.count(), 1) |
| 116 |
|
| 117 |
def test_revert_to_previous_version(self): |
| 118 |
"""Test that we can revert an object to a previous version.""" |
| 119 |
# Create initial version |
| 120 |
with revisions.create_revision(): |
| 121 |
blog_post = BlogPost.objects.create( |
| 122 |
title="Original Title", |
| 123 |
content="Original content", |
| 124 |
author="Author One", |
| 125 |
) |
| 126 |
|
| 127 |
# Make several updates |
| 128 |
with revisions.create_revision(): |
| 129 |
blog_post.title = "Second Title" |
| 130 |
blog_post.save() |
| 131 |
|
| 132 |
with revisions.create_revision(): |
| 133 |
blog_post.title = "Third Title" |
| 134 |
blog_post.author = "Author Two" |
| 135 |
blog_post.save() |
| 136 |
|
| 137 |
# Verify current state |
| 138 |
blog_post.refresh_from_db() |
| 139 |
self.assertEqual(blog_post.title, "Third Title") |
| 140 |
self.assertEqual(blog_post.author, "Author Two") |
| 141 |
|
| 142 |
# Revert to first version |
| 143 |
versions = Version.objects.get_for_object(blog_post) |
| 144 |
first_version = versions[ |
| 145 |
2 |
| 146 |
] # Versions are in reverse chronological order |
| 147 |
first_version.revision.revert() |
| 148 |
|
| 149 |
# Verify reverted state |
| 150 |
blog_post.refresh_from_db() |
| 151 |
self.assertEqual(blog_post.title, "Original Title") |
| 152 |
self.assertEqual(blog_post.author, "Author One") |
| 153 |
|
| 154 |
def test_manual_reversion_workflow(self): |
| 155 |
"""Test complete manual reversion workflow without admin interface.""" |
| 156 |
# Create a blog post with initial version |
| 157 |
with revisions.create_revision(): |
| 158 |
blog_post = BlogPost.objects.create( |
| 159 |
title="Manual Test Post", |
| 160 |
content="Initial content for manual testing.", |
| 161 |
author="Test Author", |
| 162 |
) |
| 163 |
revisions.set_user(None) |
| 164 |
revisions.set_comment("Manual creation") |
| 165 |
|
| 166 |
original_id = blog_post.id |
| 167 |
|
| 168 |
# Make first update |
| 169 |
with revisions.create_revision(): |
| 170 |
blog_post.title = "Updated Manual Test Post" |
| 171 |
blog_post.content = "First update to content." |
| 172 |
blog_post.save() |
| 173 |
revisions.set_comment("First manual update") |
| 174 |
|
| 175 |
# Make second update |
| 176 |
with revisions.create_revision(): |
| 177 |
blog_post.author = "Updated Author" |
| 178 |
blog_post.content = "Second update to content." |
| 179 |
blog_post.save() |
| 180 |
revisions.set_comment("Second manual update") |
| 181 |
|
| 182 |
# Verify we have 3 versions |
| 183 |
versions = Version.objects.get_for_object(blog_post) |
| 184 |
self.assertEqual(versions.count(), 3) |
| 185 |
|
| 186 |
# Test getting version data |
| 187 |
latest_version = versions[0] |
| 188 |
self.assertEqual(latest_version.field_dict["author"], "Updated Author") |
| 189 |
self.assertEqual( |
| 190 |
latest_version.field_dict["content"], |
| 191 |
"Second update to content.", |
| 192 |
) |
| 193 |
|
| 194 |
middle_version = versions[1] |
| 195 |
self.assertEqual( |
| 196 |
middle_version.field_dict["title"], "Updated Manual Test Post" |
| 197 |
) |
| 198 |
self.assertEqual( |
| 199 |
middle_version.field_dict["content"], |
| 200 |
"First update to content.", |
| 201 |
) |
| 202 |
|
| 203 |
original_version = versions[2] |
| 204 |
self.assertEqual( |
| 205 |
original_version.field_dict["title"], "Manual Test Post" |
| 206 |
) |
| 207 |
self.assertEqual(original_version.field_dict["author"], "Test Author") |
| 208 |
|
| 209 |
# Test reverting to middle version manually |
| 210 |
middle_version.revision.revert() |
| 211 |
blog_post.refresh_from_db() |
| 212 |
self.assertEqual(blog_post.title, "Updated Manual Test Post") |
| 213 |
self.assertEqual(blog_post.content, "First update to content.") |
| 214 |
self.assertEqual( |
| 215 |
blog_post.author, "Test Author" |
| 216 |
) # Should be from original |
| 217 |
|
| 218 |
# Test accessing revision metadata |
| 219 |
revision = middle_version.revision |
| 220 |
self.assertEqual(revision.comment, "First manual update") |
| 221 |
|
| 222 |
# Test that polymorphic type is preserved across reversion |
| 223 |
self.assertIsInstance(blog_post, BlogPost) |
| 224 |
self.assertEqual(blog_post.id, original_id) |
| 225 |
|
| 226 |
def test_manual_newsarticle_reversion_with_deletion(self): |
| 227 |
"""Test manual reversion including object deletion and recovery.""" |
| 228 |
# Create a news article |
| 229 |
with revisions.create_revision(): |
| 230 |
news = NewsArticle.objects.create( |
| 231 |
title="Breaking News", |
| 232 |
content="Important breaking news.", |
| 233 |
source="Test News Network", |
| 234 |
) |
| 235 |
revisions.set_comment("Initial news article") |
| 236 |
|
| 237 |
news_id = news.id |
| 238 |
|
| 239 |
# Update the article |
| 240 |
with revisions.create_revision(): |
| 241 |
news.content = "Updated breaking news with more details." |
| 242 |
news.source = "Updated News Network" |
| 243 |
news.save() |
| 244 |
revisions.set_comment("Updated news details") |
| 245 |
|
| 246 |
# Get versions before deletion |
| 247 |
versions = Version.objects.get_for_object(news) |
| 248 |
self.assertEqual(versions.count(), 2) |
| 249 |
original_version = versions[1] |
| 250 |
|
| 251 |
# Delete the object |
| 252 |
with revisions.create_revision(): |
| 253 |
news.delete() |
| 254 |
revisions.set_comment("Deleted news article") |
| 255 |
|
| 256 |
# Verify object is deleted |
| 257 |
self.assertFalse(NewsArticle.objects.filter(id=news_id).exists()) |
| 258 |
|
| 259 |
# Manually recover from deletion by reverting to a previous version |
| 260 |
original_version.revision.revert() |
| 261 |
|
| 262 |
# Verify object is restored |
| 263 |
recovered_news = NewsArticle.objects.get(id=news_id) |
| 264 |
self.assertEqual(recovered_news.title, "Breaking News") |
| 265 |
self.assertEqual(recovered_news.content, "Important breaking news.") |
| 266 |
self.assertEqual(recovered_news.source, "Test News Network") |
| 267 |
self.assertIsInstance(recovered_news, NewsArticle) |
| 268 |
|
| 269 |
def test_manual_batch_reversion(self): |
| 270 |
"""Test reverting multiple polymorphic objects in a single revision.""" |
| 271 |
# Create multiple objects in one revision |
| 272 |
with revisions.create_revision(): |
| 273 |
blog1 = BlogPost.objects.create( |
| 274 |
title="Blog 1", content="Content 1", author="Author 1" |
| 275 |
) |
| 276 |
blog2 = BlogPost.objects.create( |
| 277 |
title="Blog 2", content="Content 2", author="Author 2" |
| 278 |
) |
| 279 |
news = NewsArticle.objects.create( |
| 280 |
title="News 1", content="News content", source="Source 1" |
| 281 |
) |
| 282 |
revisions.set_comment("Batch creation") |
| 283 |
|
| 284 |
# Update all objects in another revision |
| 285 |
with revisions.create_revision(): |
| 286 |
blog1.title = "Updated Blog 1" |
| 287 |
blog1.save() |
| 288 |
blog2.title = "Updated Blog 2" |
| 289 |
blog2.save() |
| 290 |
news.title = "Updated News 1" |
| 291 |
news.save() |
| 292 |
revisions.set_comment("Batch update") |
| 293 |
|
| 294 |
# Verify updated state |
| 295 |
blog1.refresh_from_db() |
| 296 |
blog2.refresh_from_db() |
| 297 |
news.refresh_from_db() |
| 298 |
self.assertEqual(blog1.title, "Updated Blog 1") |
| 299 |
self.assertEqual(blog2.title, "Updated Blog 2") |
| 300 |
self.assertEqual(news.title, "Updated News 1") |
| 301 |
|
| 302 |
# Get the original revision (should contain all three objects) |
| 303 |
from reversion.models import Revision |
| 304 |
|
| 305 |
original_revision = Revision.objects.order_by("date_created")[0] |
| 306 |
self.assertEqual(original_revision.comment, "Batch creation") |
| 307 |
|
| 308 |
# Revert the entire revision |
| 309 |
original_revision.revert() |
| 310 |
|
| 311 |
# Verify all objects reverted |
| 312 |
blog1.refresh_from_db() |
| 313 |
blog2.refresh_from_db() |
| 314 |
news.refresh_from_db() |
| 315 |
self.assertEqual(blog1.title, "Blog 1") |
| 316 |
self.assertEqual(blog2.title, "Blog 2") |
| 317 |
self.assertEqual(news.title, "News 1") |
| 318 |
|
| 319 |
|
| 320 |
@skipUnless(REVERSION_INSTALLED, "django-reversion is not installed") |
| 321 |
class ReversionAdminUITests(_GenericUITest): |
| 322 |
"""Test reversion functionality through the admin interface.""" |
| 323 |
|
| 324 |
def test_blogpost_admin_reversion(self): |
| 325 |
"""Test BlogPost admin integration: creating, updating, versioning, and reverting through UI.""" |
| 326 |
# Navigate to BlogPost add page |
| 327 |
add_url = self.add_url(BlogPost) |
| 328 |
self.page.goto(add_url) |
| 329 |
|
| 330 |
# Create initial BlogPost |
| 331 |
self.page.fill("input[name='title']", "Admin Test Post") |
| 332 |
self.page.fill("textarea[name='content']", "Initial admin content") |
| 333 |
self.page.fill("input[name='author']", "Admin Author") |
| 334 |
|
| 335 |
# Save the object |
| 336 |
with self.page.expect_navigation(timeout=30000): |
| 337 |
self.page.click("input[name='_save']") |
| 338 |
|
| 339 |
# Verify BlogPost was created |
| 340 |
blog_post = BlogPost.objects.get(title="Admin Test Post") |
| 341 |
self.assertEqual(blog_post.author, "Admin Author") |
| 342 |
self.assertEqual(blog_post.content, "Initial admin content") |
| 343 |
|
| 344 |
# Verify we have 1 version (created by admin) |
| 345 |
versions = Version.objects.get_for_object(blog_post) |
| 346 |
self.assertEqual(versions.count(), 1) |
| 347 |
first_version = versions[0] |
| 348 |
self.assertEqual(first_version.field_dict["title"], "Admin Test Post") |
| 349 |
self.assertEqual(first_version.field_dict["author"], "Admin Author") |
| 350 |
|
| 351 |
# Navigate to change page and update the BlogPost |
| 352 |
change_url = self.change_url(BlogPost, blog_post.pk) |
| 353 |
self.page.goto(change_url) |
| 354 |
|
| 355 |
self.page.fill("input[name='title']", "Updated Admin Test Post") |
| 356 |
self.page.fill("textarea[name='content']", "Updated admin content") |
| 357 |
self.page.fill("input[name='author']", "Updated Admin Author") |
| 358 |
|
| 359 |
with self.page.expect_navigation(timeout=30000): |
| 360 |
self.page.click("input[name='_save']") |
| 361 |
|
| 362 |
# Verify update |
| 363 |
blog_post.refresh_from_db() |
| 364 |
self.assertEqual(blog_post.title, "Updated Admin Test Post") |
| 365 |
self.assertEqual(blog_post.author, "Updated Admin Author") |
| 366 |
|
| 367 |
# Verify we now have 2 versions (admin creates version on each save) |
| 368 |
versions = Version.objects.get_for_object(blog_post) |
| 369 |
self.assertEqual(versions.count(), 2) |
| 370 |
latest_version = versions[0] |
| 371 |
self.assertEqual( |
| 372 |
latest_version.field_dict["title"], "Updated Admin Test Post" |
| 373 |
) |
| 374 |
self.assertEqual( |
| 375 |
latest_version.field_dict["author"], "Updated Admin Author" |
| 376 |
) |
| 377 |
|
| 378 |
# Navigate to history page and verify it's accessible |
| 379 |
history_url = f"{self.live_server_url}{reverse('admin:integrations_blogpost_history', args=[blog_post.pk])}" |
| 380 |
self.page.goto(history_url) |
| 381 |
|
| 382 |
# Verify we can see the history page |
| 383 |
expect(self.page.locator("#content h1")).to_contain_text( |
| 384 |
"Change history" |
| 385 |
) |
| 386 |
|
| 387 |
# Verify history table shows version information |
| 388 |
history_table = self.page.locator( |
| 389 |
"table#change-history, div#change-history" |
| 390 |
) |
| 391 |
expect(history_table).to_be_visible() |
| 392 |
|
| 393 |
# Use the UI to revert: Click on the oldest version's date/time link |
| 394 |
# The history table typically has links in each row - we want the last row (oldest) |
| 395 |
history_links = self.page.locator("table#change-history a").all() |
| 396 |
self.assertGreater(len(history_links), 1, "Should have history links") |
| 397 |
|
| 398 |
# Click on the last link (oldest version) to view that revision |
| 399 |
with self.page.expect_navigation(timeout=30000): |
| 400 |
history_links[0].click() |
| 401 |
|
| 402 |
# We're now on the specific version's history page (history/<version_id>/) |
| 403 |
current_url = self.page.url |
| 404 |
self.assertIn( |
| 405 |
"/history/", |
| 406 |
current_url, |
| 407 |
f"Should be on history detail page, got: {current_url}", |
| 408 |
) |
| 409 |
|
| 410 |
# Wait for page to fully load |
| 411 |
self.page.wait_for_load_state("domcontentloaded", timeout=10000) |
| 412 |
|
| 413 |
# The page should show the old version's data |
| 414 |
page_content = self.page.content() |
| 415 |
self.assertIn( |
| 416 |
"Admin Test Post", |
| 417 |
page_content, |
| 418 |
"Should see original title on the version page", |
| 419 |
) |
| 420 |
|
| 421 |
# Find and click the submit button to revert |
| 422 |
submit_button = self.page.locator("input[type='submit']").first |
| 423 |
|
| 424 |
with self.page.expect_navigation(timeout=30000): |
| 425 |
submit_button.click() |
| 426 |
|
| 427 |
# Verify the object was reverted |
| 428 |
blog_post.refresh_from_db() |
| 429 |
self.assertEqual(blog_post.title, "Admin Test Post") |
| 430 |
self.assertEqual(blog_post.author, "Admin Author") |
| 431 |
self.assertIsInstance(blog_post, BlogPost) |
| 432 |
|
| 433 |
def test_article_admin_reversion(self): |
| 434 |
"""Test Article (polymorphic parent) admin versioning and reversion through UI.""" |
| 435 |
# Create an article first via API, then test admin updates |
| 436 |
with revisions.create_revision(): |
| 437 |
article = BlogPost.objects.create( |
| 438 |
title="Parent Article Test", |
| 439 |
content="Parent article content", |
| 440 |
author="Parent Author", |
| 441 |
) |
| 442 |
|
| 443 |
# Verify version created |
| 444 |
versions = Version.objects.get_for_object(article) |
| 445 |
self.assertEqual(versions.count(), 1) |
| 446 |
|
| 447 |
# Update through parent Article admin interface |
| 448 |
change_url = self.change_url(Article, article.pk) |
| 449 |
self.page.goto(change_url) |
| 450 |
|
| 451 |
# Verify we're on the change page for the parent admin |
| 452 |
expect(self.page.locator("#content h1")).to_contain_text("Change") |
| 453 |
|
| 454 |
self.page.fill("input[name='title']", "Updated Parent Article") |
| 455 |
self.page.fill("textarea[name='content']", "Updated parent content") |
| 456 |
|
| 457 |
with self.page.expect_navigation(timeout=30000): |
| 458 |
self.page.click("input[name='_save']") |
| 459 |
|
| 460 |
# Verify update |
| 461 |
article.refresh_from_db() |
| 462 |
self.assertEqual(article.title, "Updated Parent Article") |
| 463 |
|
| 464 |
# Verify we now have 2 versions (1 from API, 1 from admin) |
| 465 |
versions = Version.objects.get_for_object(article) |
| 466 |
self.assertEqual(versions.count(), 2) |
| 467 |
self.assertEqual( |
| 468 |
versions[0].field_dict["title"], "Updated Parent Article" |
| 469 |
) |
| 470 |
self.assertEqual(versions[1].field_dict["title"], "Parent Article Test") |
| 471 |
|
| 472 |
# Navigate to history page through parent admin |
| 473 |
history_url = f"{self.live_server_url}{reverse('admin:integrations_article_history', args=[article.pk])}" |
| 474 |
self.page.goto(history_url) |
| 475 |
expect(self.page.locator("#content h1")).to_contain_text( |
| 476 |
"Change history" |
| 477 |
) |
| 478 |
|
| 479 |
# Use the UI to revert: Click on the oldest version |
| 480 |
history_links = self.page.locator("table#change-history a").all() |
| 481 |
self.assertGreater(len(history_links), 1) |
| 482 |
|
| 483 |
with self.page.expect_navigation(timeout=30000): |
| 484 |
history_links[0].click() |
| 485 |
|
| 486 |
# Wait for page to load |
| 487 |
self.page.wait_for_load_state("domcontentloaded", timeout=10000) |
| 488 |
|
| 489 |
# Verify we're on the revert page |
| 490 |
page_content = self.page.content() |
| 491 |
self.assertIn("Revert", page_content, "Should be on a revert page") |
| 492 |
|
| 493 |
# Click the submit button to revert |
| 494 |
submit_button = self.page.locator("input[type='submit']").first |
| 495 |
|
| 496 |
with self.page.expect_navigation(timeout=30000): |
| 497 |
submit_button.click() |
| 498 |
|
| 499 |
# Verify the object was reverted |
| 500 |
article.refresh_from_db() |
| 501 |
self.assertEqual(article.title, "Parent Article Test") |
| 502 |
self.assertEqual(article.content, "Parent article content") |
| 503 |
self.assertIsInstance(article, BlogPost) |
| 504 |
|
| 505 |
def test_newsarticle_admin_reversion(self): |
| 506 |
"""Test NewsArticle admin versioning and reversion through UI.""" |
| 507 |
# Navigate to NewsArticle add page |
| 508 |
add_url = self.add_url(NewsArticle) |
| 509 |
self.page.goto(add_url) |
| 510 |
|
| 511 |
# Create NewsArticle |
| 512 |
self.page.fill("input[name='title']", "Breaking Admin News") |
| 513 |
self.page.fill("textarea[name='content']", "Admin news content") |
| 514 |
self.page.fill("input[name='source']", "Admin News Network") |
| 515 |
|
| 516 |
with self.page.expect_navigation(timeout=30000): |
| 517 |
self.page.click("input[name='_save']") |
| 518 |
|
| 519 |
# Verify creation |
| 520 |
news = NewsArticle.objects.get(title="Breaking Admin News") |
| 521 |
self.assertEqual(news.source, "Admin News Network") |
| 522 |
|
| 523 |
# Verify version created |
| 524 |
versions = Version.objects.get_for_object(news) |
| 525 |
self.assertEqual(versions.count(), 1) |
| 526 |
|
| 527 |
# Update |
| 528 |
change_url = self.change_url(NewsArticle, news.pk) |
| 529 |
self.page.goto(change_url) |
| 530 |
|
| 531 |
self.page.fill("input[name='title']", "Updated Breaking News") |
| 532 |
self.page.fill("input[name='source']", "Updated Network") |
| 533 |
|
| 534 |
with self.page.expect_navigation(timeout=30000): |
| 535 |
self.page.click("input[name='_save']") |
| 536 |
|
| 537 |
news.refresh_from_db() |
| 538 |
self.assertEqual(news.title, "Updated Breaking News") |
| 539 |
|
| 540 |
# Verify we have 2 versions from admin operations |
| 541 |
versions = Version.objects.get_for_object(news) |
| 542 |
self.assertEqual(versions.count(), 2) |
| 543 |
self.assertEqual( |
| 544 |
versions[0].field_dict["title"], "Updated Breaking News" |
| 545 |
) |
| 546 |
self.assertEqual(versions[1].field_dict["title"], "Breaking Admin News") |
| 547 |
|
| 548 |
# Verify history page is accessible |
| 549 |
history_url = f"{self.live_server_url}{reverse('admin:integrations_newsarticle_history', args=[news.pk])}" |
| 550 |
self.page.goto(history_url) |
| 551 |
expect(self.page.locator("#content h1")).to_contain_text( |
| 552 |
"Change history" |
| 553 |
) |
| 554 |
|
| 555 |
# Use the UI to revert: Click on the oldest version |
| 556 |
history_links = self.page.locator("table#change-history a").all() |
| 557 |
self.assertGreater(len(history_links), 1) |
| 558 |
|
| 559 |
with self.page.expect_navigation(timeout=30000): |
| 560 |
history_links[0].click() |
| 561 |
|
| 562 |
# Wait for page to load |
| 563 |
self.page.wait_for_load_state("domcontentloaded", timeout=10000) |
| 564 |
|
| 565 |
# Verify we're on the revert page |
| 566 |
page_content = self.page.content() |
| 567 |
self.assertIn("Revert", page_content, "Should be on a revert page") |
| 568 |
|
| 569 |
# Try to revert through UI - click the save button |
| 570 |
submit_buttons = self.page.locator("input[type='submit']").all() |
| 571 |
|
| 572 |
with self.page.expect_navigation(timeout=30000): |
| 573 |
submit_buttons[0].click() |
| 574 |
|
| 575 |
# Check if reversion worked |
| 576 |
news.refresh_from_db() |
| 577 |
# Reversion worked! Verify values |
| 578 |
self.assertEqual(news.source, "Admin News Network") |
| 579 |
self.assertIsInstance(news, NewsArticle) |
| 580 |
|
| 581 |
# Verify a new version was created for the revert operation |
| 582 |
versions_after_revert = Version.objects.get_for_object(news) |
| 583 |
self.assertEqual(versions_after_revert.count(), 3) |