mirror of
https://github.com/wahyd4/links.git
synced 2026-08-09 05:06:16 +10:00
AlertsPartialView (htmx-polled every 30s from the dashboard) returned
500 because _alerts.html used {% trans %} without {% load i18n %}.
Added the load tag so the partial renders correctly.
The dashboard's inline {% include 'pricemon/_alerts.html' %} also
relied on an undefined 'alerts' context var (DashboardView sets
'active_alerts'), so the alerts panel rendered empty even when alerts
existed. Pass alerts=active_alerts explicitly via the include.
Add regression tests covering both the dashboard render with an alert
and the alerts partial endpoint.
86 lines
3.0 KiB
Python
86 lines
3.0 KiB
Python
from unittest.mock import patch
|
|
|
|
import pytest
|
|
from django.urls import reverse
|
|
|
|
from pricemon.models import PriceAlert, PriceWatcher
|
|
|
|
|
|
@pytest.fixture
|
|
def watcher():
|
|
with patch('pricemon.tasks.schedule_watcher'):
|
|
return PriceWatcher.objects.create(
|
|
name='Test product',
|
|
url='https://example.com/product',
|
|
css_selector='.price',
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def alert(watcher):
|
|
return PriceAlert.objects.create(
|
|
watcher=watcher,
|
|
old_price='100.00',
|
|
new_price='80.00',
|
|
drop_pct='20.00',
|
|
)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
class TestPriceMonitorToggle:
|
|
def test_dashboard_shows_disable_action_for_enabled_watcher(self, client, watcher):
|
|
response = client.get(reverse('pricemon-dashboard'))
|
|
|
|
assert response.status_code == 200
|
|
assert reverse('pricemon-toggle-enabled', args=[watcher.pk]) in response.content.decode()
|
|
assert 'Disable monitoring' in response.content.decode()
|
|
assert response.context['enabled_watchers_count'] == 1
|
|
|
|
def test_toggle_disables_watcher_and_unschedules_job(self, client, watcher):
|
|
with patch('pricemon.tasks.unschedule_watcher') as unschedule_watcher:
|
|
response = client.post(reverse('pricemon-toggle-enabled', args=[watcher.pk]))
|
|
|
|
watcher.refresh_from_db()
|
|
assert response.status_code == 302
|
|
assert response.url == reverse('pricemon-dashboard')
|
|
assert watcher.enabled is False
|
|
unschedule_watcher.assert_called_once()
|
|
|
|
def test_toggle_enables_paused_watcher_and_schedules_job(self, client, watcher):
|
|
with patch('pricemon.tasks.unschedule_watcher'):
|
|
watcher.enabled = False
|
|
watcher.save(update_fields=['enabled'])
|
|
|
|
with patch('pricemon.tasks.schedule_watcher') as schedule_watcher:
|
|
response = client.post(reverse('pricemon-toggle-enabled', args=[watcher.pk]))
|
|
|
|
watcher.refresh_from_db()
|
|
assert response.status_code == 302
|
|
assert watcher.enabled is True
|
|
schedule_watcher.assert_called_once()
|
|
|
|
def test_toggle_rejects_get_requests(self, client, watcher):
|
|
response = client.get(reverse('pricemon-toggle-enabled', args=[watcher.pk]))
|
|
|
|
assert response.status_code == 405
|
|
|
|
|
|
@pytest.mark.django_db
|
|
class TestPriceMonitorAlertsPartial:
|
|
def test_dashboard_renders_with_active_alert(self, client, watcher, alert):
|
|
response = client.get(reverse('pricemon-dashboard'))
|
|
|
|
assert response.status_code == 200
|
|
body = response.content.decode()
|
|
assert 'alert-{}'.format(alert.pk) in body
|
|
assert 'Price Drop Alerts' in body
|
|
|
|
def test_alerts_partial_renders_i18n_tags(self, client, watcher, alert):
|
|
response = client.get(reverse('pricemon-alerts-partial'))
|
|
|
|
assert response.status_code == 200
|
|
body = response.content.decode()
|
|
assert 'alert-{}'.format(alert.pk) in body
|
|
assert 'View' in body and 'Dismiss' in body
|
|
assert reverse('pricemon-dismiss-alert', args=[alert.pk]) in body
|