Skip to content

Commit

Permalink
feat: force all enrollments (#1761)
Browse files Browse the repository at this point in the history
* feat: force all enrollments

* fix test

* formating

* fix tests

* formating

* fix tests

* fix tests

* fix test

* fix test
  • Loading branch information
asadali145 authored Aug 10, 2023
1 parent 0c88c61 commit 65ce553
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 92 deletions.
9 changes: 2 additions & 7 deletions courses/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
)
from openedx.constants import (
EDX_DEFAULT_ENROLLMENT_MODE,
EDX_ENROLLMENT_VERIFIED_MODE,
EDX_ENROLLMENT_AUDIT_MODE,
EDX_ENROLLMENT_VERIFIED_MODE,
)
from openedx.exceptions import (
EdxApiEnrollErrorException,
Expand Down Expand Up @@ -219,7 +219,6 @@ def create_run_enrollments(
*,
keep_failed_enrollments=False,
mode=EDX_DEFAULT_ENROLLMENT_MODE,
force_enrollment=False,
):
"""
Creates local records of a user's enrollment in course runs, and attempts to enroll them
Expand All @@ -230,8 +229,7 @@ def create_run_enrollments(
runs (iterable of CourseRun): The course runs to enroll in
keep_failed_enrollments: (boolean): If True, keeps the local enrollment record
in the database even if the enrollment fails in edX.
force_enrollment (bool): If True, Enforces Enrollment after the enrollment end date
has been passed or upgrade_deadline is ended
mode (str): The course mode
Returns:
(list of CourseRunEnrollment, bool): A list of enrollment objects that were successfully
Expand Down Expand Up @@ -265,8 +263,6 @@ def _enroll_learner_into_associated_programs():
user,
runs,
mode=mode,
force_enrollment=force_enrollment,
regen_auth_tokens=False,
)
except (
UnknownEdxApiEnrollException,
Expand Down Expand Up @@ -488,7 +484,6 @@ def defer_enrollment(
runs=[from_enrollment.run],
keep_failed_enrollments=True,
mode=EDX_ENROLLMENT_AUDIT_MODE,
force_enrollment=True,
)
return downgraded_enrollments, None

Expand Down
16 changes: 1 addition & 15 deletions courses/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,12 @@ def test_create_run_enrollments(
mocker.patch("courses.tasks.subscribe_edx_course_emails.delay")

successful_enrollments, edx_request_success = create_run_enrollments(
user, runs, mode=enrollment_mode, force_enrollment=False
user, runs, mode=enrollment_mode
)
patched_edx_enroll.assert_called_once_with(
user,
runs,
mode=enrollment_mode,
force_enrollment=False,
regen_auth_tokens=False,
)

with django_capture_on_commit_callbacks(execute=True):
Expand Down Expand Up @@ -399,8 +397,6 @@ def test_create_run_enrollments_upgrade(
user,
[test_enrollment.run],
mode=EDX_ENROLLMENT_VERIFIED_MODE,
force_enrollment=False,
regen_auth_tokens=False,
)

patched_send_enrollment_email.assert_called_once()
Expand Down Expand Up @@ -486,8 +482,6 @@ def test_create_run_enrollments_api_fail(mocker, user, exception_cls):
user,
[run],
mode=EDX_DEFAULT_ENROLLMENT_MODE,
force_enrollment=False,
regen_auth_tokens=False,
)
patched_log_exception.assert_called_once()
patched_send_enrollment_email.assert_not_called()
Expand Down Expand Up @@ -535,8 +529,6 @@ def test_create_run_enrollments_enroll_api_fail(
user,
runs,
mode=EDX_DEFAULT_ENROLLMENT_MODE,
force_enrollment=False,
regen_auth_tokens=False,
)
patched_log_exception.assert_called_once()
patched_send_enrollment_email.assert_not_called()
Expand Down Expand Up @@ -568,8 +560,6 @@ def test_create_run_enrollments_creation_fail(mocker, user):
user,
runs,
mode=EDX_DEFAULT_ENROLLMENT_MODE,
force_enrollment=False,
regen_auth_tokens=False,
)
patched_log_exception.assert_called_once()
patched_mail_api.send_course_run_enrollment_email.assert_not_called()
Expand Down Expand Up @@ -1319,8 +1309,6 @@ def test_create_run_enrollments_upgrade_edx_request_failure(mocker, user):
user,
[test_course_run],
mode=EDX_ENROLLMENT_AUDIT_MODE,
force_enrollment=False,
regen_auth_tokens=False,
)

patched_send_enrollment_email.assert_called_once()
Expand All @@ -1343,8 +1331,6 @@ def test_create_run_enrollments_upgrade_edx_request_failure(mocker, user):
user,
[test_course_run],
mode=EDX_ENROLLMENT_VERIFIED_MODE,
force_enrollment=False,
regen_auth_tokens=False,
)

assert edx_request_success is False
Expand Down
1 change: 0 additions & 1 deletion courses/management/commands/create_verified_enrollment.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ def handle(self, *args, **options):
[run],
keep_failed_enrollments=options["keep_failed_enrollments"],
mode=EDX_ENROLLMENT_VERIFIED_MODE,
force_enrollment=force_enrollment,
)
if not successful_enrollments:
raise CommandError("Failed to create the enrollment record")
Expand Down
3 changes: 1 addition & 2 deletions ecommerce/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.db import transaction
from django.db.models import Q
from django.urls import reverse
from hubspot_sync.task_helpers import sync_hubspot_deal
from ipware import get_client_ip
from mitol.common.utils.datetime import now_in_utc
from mitol.payment_gateway.api import CartItem as GatewayCartItem
Expand Down Expand Up @@ -42,6 +41,7 @@
)
from ecommerce.tasks import perform_downgrade_from_order
from flexiblepricing.api import determine_courseware_flexible_price_discount
from hubspot_sync.task_helpers import sync_hubspot_deal
from main.constants import (
USER_MSG_TYPE_COURSE_NON_UPGRADABLE,
USER_MSG_TYPE_DISCOUNT_INVALID,
Expand Down Expand Up @@ -479,7 +479,6 @@ def downgrade_learner_from_order(order_id):
runs=order.purchased_runs,
keep_failed_enrollments=True,
mode=EDX_ENROLLMENT_AUDIT_MODE,
force_enrollment=True,
)


Expand Down
5 changes: 4 additions & 1 deletion ecommerce/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,12 @@ def test_unenrollment_unenrolls_learner(mocker, user):
unenroll_mock.assert_called()


def test_process_cybersource_payment_response(rf, mocker, user_client, user, products):
def test_process_cybersource_payment_response(
settings, rf, mocker, user_client, user, products
):
"""Test that ensures the response from Cybersource for an ACCEPTed payment updates the orders state"""

settings.OPENEDX_SERVICE_WORKER_API_TOKEN = "mock_api_token"
mocker.patch(
"mitol.payment_gateway.api.PaymentGateway.validate_processor_response",
return_value=True,
Expand Down
11 changes: 8 additions & 3 deletions ecommerce/mail_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ def products():


def test_mail_api_task_called(
mocker, user, products, user_client, django_capture_on_commit_callbacks
settings, mocker, user, products, user_client, django_capture_on_commit_callbacks
):
"""
Tests that the Order model is properly calling the send email receipt task
rather than calling the mail_api version directly. The create_order_receipt
function should create a basket and process the order through to the point
where the Order model itself will send the receipt email.
"""
settings.OPENEDX_SERVICE_WORKER_API_TOKEN = "mock_api_token"
mock_delayed_send_ecommerce_order_receipt = mocker.patch(
"ecommerce.tasks.send_ecommerce_order_receipt.delay"
)
Expand All @@ -38,13 +39,14 @@ def test_mail_api_task_called(


def test_mail_api_receipt_generation(
mocker, user, products, user_client, django_capture_on_commit_callbacks
settings, mocker, user, products, user_client, django_capture_on_commit_callbacks
):
"""
Tests email generation. Mocks actual message sending and then looks for some
key data in the rendered template body (name from legal address, order ID,
and line item unit price).
"""
settings.OPENEDX_SERVICE_WORKER_API_TOKEN = "mock_api_token"
mock_send_message = mocker.patch("mitol.mail.api.send_message")

with django_capture_on_commit_callbacks(execute=True):
Expand All @@ -67,11 +69,14 @@ def test_mail_api_receipt_generation(
assert str(lines[0].unit_price) in rendered_template.body


def test_mail_api_refund_email_generation(mocker, user, products, user_client):
def test_mail_api_refund_email_generation(
settings, mocker, user, products, user_client
):
"""
Tests email generation for the refund message. Generates a fulfilled order,
then attemps to refund it after mocking the mail sender.
"""
settings.OPENEDX_SERVICE_WORKER_API_TOKEN = "mock_api_token"
order = create_order_receipt(mocker, user, products, user_client)

mock_send_message = mocker.patch("mitol.mail.api.send_message")
Expand Down
4 changes: 2 additions & 2 deletions ecommerce/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ def test_basket_discount_conversion(user, unlimited_discount):
assert converted_discount == reconverted_discount


def test_order_refund():
def test_order_refund(settings):
"""
Tests state change from fulfilled to refund. There should be a new
Transaction record after the order has been refunded.
"""

settings.OPENEDX_SERVICE_WORKER_API_TOKEN = "mock_api_token"
with reversion.create_revision():
basket_item = BasketItemFactory.create()

Expand Down
16 changes: 12 additions & 4 deletions ecommerce/serializers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,10 @@ def get_receipt_serializer_test_data(mocker, user, products, user_client):
return (order, test_data)


def test_order_receipt_purchase_serializer(mocker, user, products, user_client):
def test_order_receipt_purchase_serializer(
settings, mocker, user, products, user_client
):
settings.OPENEDX_SERVICE_WORKER_API_TOKEN = "mock_api_token"
(order, test_data) = get_receipt_serializer_test_data(
mocker, user, products, user_client
)
Expand All @@ -358,7 +361,10 @@ def test_order_receipt_purchase_serializer(mocker, user, products, user_client):
assert serialized_data == test_data["receipt"]


def test_order_receipt_purchaser_serializer(mocker, user, products, user_client):
def test_order_receipt_purchaser_serializer(
settings, mocker, user, products, user_client
):
settings.OPENEDX_SERVICE_WORKER_API_TOKEN = "mock_api_token"
(order, test_data) = get_receipt_serializer_test_data(
mocker, user, products, user_client
)
Expand All @@ -368,7 +374,8 @@ def test_order_receipt_purchaser_serializer(mocker, user, products, user_client)
assert serialized_data == test_data["purchaser"]


def test_order_receipt_order_serializer(mocker, user, products, user_client):
def test_order_receipt_order_serializer(settings, mocker, user, products, user_client):
settings.OPENEDX_SERVICE_WORKER_API_TOKEN = "mock_api_token"
(order, test_data) = get_receipt_serializer_test_data(
mocker, user, products, user_client
)
Expand All @@ -379,7 +386,8 @@ def test_order_receipt_order_serializer(mocker, user, products, user_client):
assert serialized_data == test_data["order"]


def test_order_receipt_lines_serializer(mocker, user, products, user_client):
def test_order_receipt_lines_serializer(settings, mocker, user, products, user_client):
settings.OPENEDX_SERVICE_WORKER_API_TOKEN = "mock_api_token"
(order, test_data) = get_receipt_serializer_test_data(
mocker, user, products, user_client
)
Expand Down
10 changes: 6 additions & 4 deletions ecommerce/tasks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ def products():


def test_delayed_order_receipt_sends_email(
mocker, user, products, user_client, django_capture_on_commit_callbacks
settings, mocker, user, products, user_client, django_capture_on_commit_callbacks
):
"""
Tests that the Order model is properly calling the send email receipt task
rather than calling the mail_api version directly. The create_order_receipt
function should create a basket and process the order through to the point
where the Order model itself will send the receipt email.
"""

settings.OPENEDX_SERVICE_WORKER_API_TOKEN = "mock_api_token"
mock_send_ecommerce_order_receipt = mocker.patch(
"ecommerce.mail_api.send_ecommerce_order_receipt"
)
Expand All @@ -35,14 +35,16 @@ def test_delayed_order_receipt_sends_email(
mock_send_ecommerce_order_receipt.assert_called()


def test_delayed_order_refund_sends_email(mocker, user, products, user_client):
def test_delayed_order_refund_sends_email(
settings, mocker, user, products, user_client
):
"""
Tests that the Order model is properly calling the order refund email task
rather than calling the mail_api version directly. The create_order_receipt
function creates a fulfilled order, then we refund it and make sure the
right task got called.
"""

settings.OPENEDX_SERVICE_WORKER_API_TOKEN = "mock_api_token"
mock_send_refund_email = mocker.patch(
"ecommerce.mail_api.send_ecommerce_refund_message"
)
Expand Down
10 changes: 8 additions & 2 deletions ecommerce/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ def test_start_checkout_with_invalid_discounts(user, user_client, products, disc
],
)
def test_checkout_result(
settings,
user,
user_client,
api_client,
Expand All @@ -519,6 +520,7 @@ def test_checkout_result(
Generates an order (using the API endpoint) and then cancels it using the endpoint.
There shouldn't be any PendingOrders after that happens.
"""
settings.OPENEDX_SERVICE_WORKER_API_TOKEN = "mock_api_token"
mocker.patch(
"mitol.payment_gateway.api.PaymentGateway.validate_processor_response",
return_value=True,
Expand Down Expand Up @@ -830,13 +832,14 @@ def test_user_discounts_api(user_drf_client, admin_drf_client, discounts, user):


def test_paid_and_unpaid_courserun_checkout(
user, user_client, user_drf_client, products
settings, user, user_client, user_drf_client, products
):
"""
Tests checking out a paid or unpaid course run:
- If a course run is already paid, it should redirect to cart with 302 status code including a user message in the response cookies
- Otherwise, it should be successful with 200 status code
"""
settings.OPENEDX_SERVICE_WORKER_API_TOKEN = "mock_api_token"
product = products[0]
basket = create_basket_with_product(user, product)
order = PendingOrder.create_from_basket(basket)
Expand Down Expand Up @@ -875,6 +878,7 @@ def test_paid_and_unpaid_courserun_checkout(
],
)
def test_checkout_api_result(
settings,
user,
user_client,
api_client,
Expand All @@ -887,6 +891,7 @@ def test_checkout_api_result(
"""
Tests the proper handling of an order after receiving a valid Cybersource payment response.
"""
settings.OPENEDX_SERVICE_WORKER_API_TOKEN = "mock_api_token"
mocker.patch(
"mitol.payment_gateway.api.PaymentGateway.validate_processor_response",
return_value=True,
Expand Down Expand Up @@ -1008,10 +1013,11 @@ def test_non_upgradable_courserun_checkout(
)


def test_start_checkout_with_zero_value(user, user_client, products):
def test_start_checkout_with_zero_value(settings, user, user_client, products):
"""
Check that the checkout redirects the user to dashboard when basket price is zero
"""
settings.OPENEDX_SERVICE_WORKER_API_TOKEN = "mock_api_token"
discount = DiscountFactory.create(
discount_type=DISCOUNT_TYPE_PERCENT_OFF, amount=100
)
Expand Down
Loading

0 comments on commit 65ce553

Please sign in to comment.