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 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. from decimal import Decimal as D
  2. from django.urls import reverse
  3. from oscar.apps.dashboard.views import IndexView
  4. from oscar.apps.order.models import Order
  5. from oscar.core import prices
  6. from oscar.core.loading import get_model
  7. from oscar.test.factories import (
  8. UserFactory, create_basket, create_order, create_product)
  9. from oscar.test.testcases import WebTestCase
  10. StockAlert = get_model('partner', 'StockAlert')
  11. GENERIC_STATS_KEYS = (
  12. 'total_orders_last_day',
  13. 'total_lines_last_day',
  14. 'average_order_costs',
  15. 'total_revenue_last_day',
  16. 'hourly_report_dict',
  17. 'total_customers_last_day',
  18. 'total_open_baskets_last_day',
  19. 'total_products',
  20. 'total_open_stock_alerts',
  21. 'total_closed_stock_alerts',
  22. 'total_customers',
  23. 'total_open_baskets',
  24. 'total_orders',
  25. 'total_lines',
  26. 'total_revenue',
  27. 'order_status_breakdown',
  28. )
  29. STAFF_STATS_KEYS = (
  30. 'offer_maps',
  31. 'total_vouchers',
  32. )
  33. class TestDashboardIndexForAnonUser(WebTestCase):
  34. is_anonymous = True
  35. def test_is_not_available(self):
  36. response = self.get(reverse('dashboard:index')).follow()
  37. self.assertContains(response, 'username', status_code=200)
  38. class TestDashboardIndexForStaffUser(WebTestCase):
  39. is_staff = True
  40. def test_is_available(self):
  41. urls = ('dashboard:index',
  42. 'dashboard:order-list',
  43. 'dashboard:users-index',)
  44. for name in urls:
  45. response = self.get(reverse(name))
  46. self.assertContains(response, 'Welcome')
  47. def test_includes_hourly_report_with_no_orders(self):
  48. report = IndexView().get_hourly_report(Order.objects.all())
  49. self.assertEqual(len(report), 3)
  50. keys = ['max_revenue', 'order_total_hourly', 'y_range']
  51. for i, j in zip(sorted(report.keys()), keys):
  52. self.assertEqual(i, j)
  53. self.assertEqual(len(report['order_total_hourly']), 12)
  54. self.assertEqual(len(report['y_range']), 0)
  55. self.assertEqual(report['max_revenue'], 0)
  56. def test_includes_hourly_report_with_orders(self):
  57. create_order(total=prices.Price('GBP', excl_tax=D('34.05'),
  58. tax=D('0.00')))
  59. create_order(total=prices.Price('GBP', excl_tax=D('21.90'),
  60. tax=D('0.00')))
  61. report = IndexView().get_hourly_report(Order.objects.all())
  62. self.assertEqual(len(report['order_total_hourly']), 12)
  63. self.assertEqual(len(report['y_range']), 11)
  64. self.assertEqual(report['max_revenue'], D('60'))
  65. def test_has_stats_vars_in_context(self):
  66. response = self.get(reverse('dashboard:index'))
  67. for key in GENERIC_STATS_KEYS + STAFF_STATS_KEYS:
  68. self.assertInContext(response, key)
  69. def test_login_redirects_to_dashboard_index(self):
  70. page = self.get(reverse('dashboard:login'))
  71. form = page.forms['dashboard_login_form']
  72. form['username'] = self.email
  73. form['password'] = self.password
  74. response = form.submit('login_submit')
  75. self.assertRedirectsTo(response, 'dashboard:index')
  76. class TestDashboardIndexForPartnerUser(WebTestCase):
  77. permissions = ['partner.dashboard_access']
  78. def test_is_available(self):
  79. urls = ('dashboard:index', 'dashboard:order-list')
  80. for name in urls:
  81. response = self.get(reverse(name))
  82. self.assertContains(response, 'Welcome')
  83. def test_is_not_available(self):
  84. urls = ('dashboard:users-index',
  85. 'dashboard:partner-list',
  86. 'dashboard:partner-create',
  87. 'dashboard:offer-list',
  88. 'dashboard:reports-index')
  89. for name in urls:
  90. response = self.get(reverse(name), expect_errors=True)
  91. self.assertContains(response, 'Permission denied!',
  92. status_code=403)
  93. def test_stats(self):
  94. response = self.get(reverse('dashboard:index'))
  95. for key in GENERIC_STATS_KEYS:
  96. self.assertInContext(response, key)
  97. for key in STAFF_STATS_KEYS:
  98. self.assertNotInContext(response, key)
  99. class TestDashboardIndexStatsForNonStaffUser(WebTestCase):
  100. permissions = ['partner.dashboard_access']
  101. def setUp(self):
  102. super().setUp()
  103. customer = UserFactory()
  104. product1 = create_product(partner_name='Partner 1', price=D(5))
  105. product2 = create_product(partner_name='Partner 2', price=D(10))
  106. create_product(partner_name='Partner 2', price=D(15))
  107. basket1 = create_basket(empty=True)
  108. basket1.add_product(product1)
  109. create_order(basket=basket1, user=customer)
  110. basket2 = create_basket(empty=True)
  111. basket2.add_product(product1)
  112. basket2 = create_basket(empty=True)
  113. basket2.add_product(product2)
  114. for i in range(9):
  115. create_order(basket=basket2, user=customer, number='1000%s' % i)
  116. stockrecord1 = product1.stockrecords.first()
  117. stockrecord2 = product2.stockrecords.first()
  118. self.partner1 = stockrecord1.partner
  119. self.partner2 = stockrecord2.partner
  120. StockAlert.objects.create(stockrecord=stockrecord1, threshold=10)
  121. StockAlert.objects.create(stockrecord=stockrecord2, threshold=5)
  122. def test_partner1(self):
  123. user = self.create_user(username='user', email='testuser@example.com')
  124. self.partner1.users.add(self.user)
  125. self.partner2.users.add(user)
  126. response = self.get(reverse('dashboard:index'))
  127. context = response.context
  128. self.assertEqual(context['total_orders_last_day'], 1)
  129. self.assertEqual(context['total_lines_last_day'], 1)
  130. self.assertEqual(context['total_revenue_last_day'], D(27))
  131. self.assertEqual(context['total_customers_last_day'], 1)
  132. self.assertEqual(context['total_open_baskets_last_day'], 1)
  133. self.assertEqual(context['total_products'], 1)
  134. self.assertEqual(context['total_open_stock_alerts'], 1)
  135. self.assertEqual(context['total_closed_stock_alerts'], 0)
  136. self.assertEqual(context['total_customers'], 1)
  137. self.assertEqual(context['total_open_baskets'], 1)
  138. self.assertEqual(context['total_orders'], 1)
  139. self.assertEqual(context['total_lines'], 1)
  140. self.assertEqual(context['total_revenue'], D(27))
  141. def test_partner2(self):
  142. user = self.create_user(username='user', email='testuser@example.com')
  143. self.partner1.users.add(user)
  144. self.partner2.users.add(self.user)
  145. response = self.get(reverse('dashboard:index'))
  146. context = response.context
  147. self.assertEqual(context['total_orders_last_day'], 9)
  148. self.assertEqual(context['total_lines_last_day'], 9)
  149. self.assertEqual(context['total_revenue_last_day'], D(288))
  150. self.assertEqual(context['total_customers_last_day'], 1)
  151. self.assertEqual(context['total_open_baskets_last_day'], 0)
  152. self.assertEqual(context['total_products'], 2)
  153. self.assertEqual(context['total_open_stock_alerts'], 1)
  154. self.assertEqual(context['total_closed_stock_alerts'], 0)
  155. self.assertEqual(context['total_customers'], 1)
  156. self.assertEqual(context['total_open_baskets'], 0)
  157. self.assertEqual(context['total_orders'], 9)
  158. self.assertEqual(context['total_lines'], 9)
  159. self.assertEqual(context['total_revenue'], D(288))