|
| 1 | +"""Tests for send_learner_credit_bnr_daily_digest management command. |
| 2 | +
|
| 3 | +Covers command behavior for enqueuing tasks when there are open (REQUESTED) requests. |
| 4 | +""" |
| 5 | + |
| 6 | +from datetime import timedelta |
| 7 | +from unittest import mock |
| 8 | +from uuid import uuid4 |
| 9 | + |
| 10 | +import pytest |
| 11 | +from django.core.management import call_command |
| 12 | +from django.utils import timezone |
| 13 | + |
| 14 | +from enterprise_access.apps.subsidy_access_policy.tests.factories import ( |
| 15 | + PerLearnerSpendCapLearnerCreditAccessPolicyFactory |
| 16 | +) |
| 17 | +from enterprise_access.apps.subsidy_request.constants import SubsidyRequestStates |
| 18 | +from enterprise_access.apps.subsidy_request.tests.factories import ( |
| 19 | + LearnerCreditRequestConfigurationFactory, |
| 20 | + LearnerCreditRequestFactory |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.django_db |
| 25 | +class TestSendLearnerCreditBNRDailyDigestCommand: |
| 26 | + """Tests enqueuing behavior for the BNR daily digest management command.""" |
| 27 | + command_name = "send_learner_credit_bnr_daily_digest" |
| 28 | + |
| 29 | + def _make_policy_with_config( |
| 30 | + self, |
| 31 | + *, |
| 32 | + active=True, |
| 33 | + retired=False, |
| 34 | + config_active=True, |
| 35 | + enterprise_uuid=None |
| 36 | + ): |
| 37 | + """Helper to create a policy and its learner credit request config for test setups.""" |
| 38 | + enterprise_uuid = enterprise_uuid or uuid4() |
| 39 | + config = LearnerCreditRequestConfigurationFactory( |
| 40 | + active=config_active |
| 41 | + ) |
| 42 | + policy = PerLearnerSpendCapLearnerCreditAccessPolicyFactory( |
| 43 | + active=active, |
| 44 | + retired=retired, |
| 45 | + learner_credit_request_config=config, |
| 46 | + enterprise_customer_uuid=enterprise_uuid, |
| 47 | + ) |
| 48 | + return policy, config |
| 49 | + |
| 50 | + @mock.patch( |
| 51 | + "enterprise_access.apps.subsidy_request.management.commands." |
| 52 | + "send_learner_credit_bnr_daily_digest." |
| 53 | + "send_learner_credit_bnr_admins_email_with_new_requests_task.delay" |
| 54 | + ) |
| 55 | + def test_no_eligible_policies(self, mock_delay): |
| 56 | + # Inactive policy |
| 57 | + self._make_policy_with_config(active=False) |
| 58 | + # Retired policy |
| 59 | + self._make_policy_with_config(retired=True) |
| 60 | + # No config |
| 61 | + PerLearnerSpendCapLearnerCreditAccessPolicyFactory() |
| 62 | + # Inactive config |
| 63 | + self._make_policy_with_config(config_active=False) |
| 64 | + |
| 65 | + call_command(self.command_name) |
| 66 | + mock_delay.assert_not_called() |
| 67 | + |
| 68 | + @mock.patch( |
| 69 | + "enterprise_access.apps.subsidy_request.management.commands." |
| 70 | + "send_learner_credit_bnr_daily_digest." |
| 71 | + "send_learner_credit_bnr_admins_email_with_new_requests_task.delay" |
| 72 | + ) |
| 73 | + def test_eligible_policy_no_open_requests(self, mock_delay): |
| 74 | + policy, config = self._make_policy_with_config() |
| 75 | + LearnerCreditRequestFactory( |
| 76 | + enterprise_customer_uuid=policy.enterprise_customer_uuid, |
| 77 | + learner_credit_request_config=config, |
| 78 | + state=SubsidyRequestStates.APPROVED, |
| 79 | + ) |
| 80 | + call_command(self.command_name) |
| 81 | + mock_delay.assert_not_called() |
| 82 | + |
| 83 | + @mock.patch( |
| 84 | + "enterprise_access.apps.subsidy_request.management.commands." |
| 85 | + "send_learner_credit_bnr_daily_digest." |
| 86 | + "send_learner_credit_bnr_admins_email_with_new_requests_task.delay" |
| 87 | + ) |
| 88 | + def test_enqueues_for_open_request(self, mock_delay): |
| 89 | + policy, config = self._make_policy_with_config() |
| 90 | + LearnerCreditRequestFactory( |
| 91 | + enterprise_customer_uuid=policy.enterprise_customer_uuid, |
| 92 | + learner_credit_request_config=config, |
| 93 | + state=SubsidyRequestStates.REQUESTED, |
| 94 | + ) |
| 95 | + call_command(self.command_name) |
| 96 | + assert mock_delay.call_count == 1 |
| 97 | + args = mock_delay.call_args[0] |
| 98 | + assert str(policy.uuid) == args[0] |
| 99 | + assert str(config.uuid) == args[1] |
| 100 | + assert str(policy.enterprise_customer_uuid) == args[2] |
| 101 | + |
| 102 | + @mock.patch( |
| 103 | + "enterprise_access.apps.subsidy_request.management.commands." |
| 104 | + "send_learner_credit_bnr_daily_digest." |
| 105 | + "send_learner_credit_bnr_admins_email_with_new_requests_task.delay" |
| 106 | + ) |
| 107 | + def test_old_open_request_still_enqueues(self, mock_delay): |
| 108 | + policy, config = self._make_policy_with_config() |
| 109 | + req = LearnerCreditRequestFactory( |
| 110 | + enterprise_customer_uuid=policy.enterprise_customer_uuid, |
| 111 | + learner_credit_request_config=config, |
| 112 | + state=SubsidyRequestStates.REQUESTED, |
| 113 | + ) |
| 114 | + # Make it "old" (yesterday) |
| 115 | + req.created = timezone.now() - timedelta(days=7) |
| 116 | + req.save(update_fields=["created"]) |
| 117 | + |
| 118 | + call_command(self.command_name) |
| 119 | + assert mock_delay.call_count == 1 |
| 120 | + |
| 121 | + @mock.patch( |
| 122 | + "enterprise_access.apps.subsidy_request.management.commands." |
| 123 | + "send_learner_credit_bnr_daily_digest." |
| 124 | + "send_learner_credit_bnr_admins_email_with_new_requests_task.delay" |
| 125 | + ) |
| 126 | + def test_multiple_policies_mixed(self, mock_delay): |
| 127 | + policy_a, config_a = self._make_policy_with_config() |
| 128 | + LearnerCreditRequestFactory( |
| 129 | + enterprise_customer_uuid=policy_a.enterprise_customer_uuid, |
| 130 | + learner_credit_request_config=config_a, |
| 131 | + state=SubsidyRequestStates.REQUESTED, |
| 132 | + ) |
| 133 | + |
| 134 | + self._make_policy_with_config() |
| 135 | + |
| 136 | + policy_c, config_c = self._make_policy_with_config() |
| 137 | + LearnerCreditRequestFactory( |
| 138 | + enterprise_customer_uuid=policy_c.enterprise_customer_uuid, |
| 139 | + learner_credit_request_config=config_c, |
| 140 | + state=SubsidyRequestStates.REQUESTED, |
| 141 | + ) |
| 142 | + |
| 143 | + call_command(self.command_name) |
| 144 | + |
| 145 | + assert mock_delay.call_count == 2 |
| 146 | + calls = [ |
| 147 | + mock.call( |
| 148 | + str(policy_a.uuid), |
| 149 | + str(config_a.uuid), |
| 150 | + str(policy_a.enterprise_customer_uuid), |
| 151 | + ), |
| 152 | + mock.call( |
| 153 | + str(policy_c.uuid), |
| 154 | + str(config_c.uuid), |
| 155 | + str(policy_c.enterprise_customer_uuid), |
| 156 | + ), |
| 157 | + ] |
| 158 | + actual = [c.args for c in mock_delay.call_args_list] |
| 159 | + assert sorted(calls) == sorted([mock.call(*args) for args in actual]) |
0 commit comments