You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_dashboard.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from decimal import Decimal as D
  2. from django.urls import reverse
  3. from oscar.core import prices
  4. from oscar.apps.dashboard.views import IndexView
  5. from oscar.apps.order.models import Order
  6. from oscar.test.testcases import WebTestCase
  7. from oscar.test.factories import create_order
  8. GENERIC_STATS_KEYS = (
  9. 'total_orders_last_day',
  10. 'total_lines_last_day',
  11. 'average_order_costs',
  12. 'total_revenue_last_day',
  13. 'hourly_report_dict',
  14. 'total_customers_last_day',
  15. 'total_open_baskets_last_day',
  16. 'total_products',
  17. 'total_open_stock_alerts',
  18. 'total_closed_stock_alerts',
  19. 'total_customers',
  20. 'total_open_baskets',
  21. 'total_orders',
  22. 'total_lines',
  23. 'total_revenue',
  24. 'order_status_breakdown',
  25. )
  26. STAFF_STATS_KEYS = (
  27. 'total_site_offers',
  28. 'total_vouchers',
  29. 'total_promotions',
  30. )
  31. class TestDashboardIndexForAnonUser(WebTestCase):
  32. is_anonymous = True
  33. def test_is_not_available(self):
  34. response = self.get(reverse('dashboard:index')).follow()
  35. self.assertContains(response, 'username', status_code=200)
  36. class TestDashboardIndexForStaffUser(WebTestCase):
  37. is_staff = True
  38. def test_is_available(self):
  39. urls = ('dashboard:index',
  40. 'dashboard:order-list',
  41. 'dashboard:users-index',)
  42. for name in urls:
  43. response = self.get(reverse(name))
  44. self.assertContains(response, 'Welcome')
  45. def test_includes_hourly_report_with_no_orders(self):
  46. report = IndexView().get_hourly_report(Order.objects.all())
  47. self.assertEqual(len(report), 3)
  48. keys = ['max_revenue', 'order_total_hourly', 'y_range']
  49. for i, j in zip(sorted(report.keys()), keys):
  50. self.assertEqual(i, j)
  51. self.assertEqual(len(report['order_total_hourly']), 12)
  52. self.assertEqual(len(report['y_range']), 0)
  53. self.assertEqual(report['max_revenue'], 0)
  54. def test_includes_hourly_report_with_orders(self):
  55. create_order(total=prices.Price('GBP', excl_tax=D('34.05'),
  56. tax=D('0.00')))
  57. create_order(total=prices.Price('GBP', excl_tax=D('21.90'),
  58. tax=D('0.00')))
  59. report = IndexView().get_hourly_report(Order.objects.all())
  60. self.assertEqual(len(report['order_total_hourly']), 12)
  61. self.assertEqual(len(report['y_range']), 11)
  62. self.assertEqual(report['max_revenue'], D('60'))
  63. def test_has_stats_vars_in_context(self):
  64. response = self.get(reverse('dashboard:index'))
  65. for key in GENERIC_STATS_KEYS + STAFF_STATS_KEYS:
  66. self.assertInContext(response, key)
  67. class TestDashboardIndexForPartnerUser(WebTestCase):
  68. permissions = ['partner.dashboard_access']
  69. def test_is_available(self):
  70. urls = ('dashboard:index', 'dashboard:order-list')
  71. for name in urls:
  72. response = self.get(reverse(name))
  73. self.assertContains(response, 'Welcome')
  74. def test_is_not_available(self):
  75. urls = ('dashboard:users-index',
  76. 'dashboard:partner-list',
  77. 'dashboard:partner-create',
  78. 'dashboard:offer-list',
  79. 'dashboard:reports-index')
  80. for name in urls:
  81. response = self.get(reverse(name), expect_errors=True)
  82. self.assertContains(response, 'Permission denied!',
  83. status_code=403)
  84. def test_stats(self):
  85. response = self.get(reverse('dashboard:index'))
  86. for key in GENERIC_STATS_KEYS:
  87. self.assertInContext(response, key)
  88. for key in STAFF_STATS_KEYS:
  89. self.assertNotInContext(response, key)