Skip to content

Commit

Permalink
Merge pull request #631 from edx/aj/ENT-2082_enroll_task
Browse files Browse the repository at this point in the history
[ENT-2082] Added Source to the enterprise enrollment background task
  • Loading branch information
Albert (AJ) St. Aubin authored Nov 25, 2019
2 parents 755eb73 + 0004016 commit d350356
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Change Log
Unreleased
----------
[2.0.25] - 2019-11-22
---------------------

* Added logic to set the EnterpriseCourseEnrollmentSource for the Enterprise Enrollments background task.

[2.0.24] - 2019-11-21
---------------------

Expand Down
2 changes: 1 addition & 1 deletion enterprise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

from __future__ import absolute_import, unicode_literals

__version__ = "2.0.24"
__version__ = "2.0.25"

default_app_config = "enterprise.apps.EnterpriseConfig" # pylint: disable=invalid-name
49 changes: 49 additions & 0 deletions enterprise/migrations/0081_UpdateEnterpriseEnrollmentSource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.25 on 2019-10-24 11:00
from __future__ import unicode_literals

import django.utils.timezone
from django.db import migrations, models

import model_utils.fields

ORIGINAL_SOURCE_NAME = 'Enterprise User B2C Site Enrollment'
ORIGINAL_SOURCE_SLUG = 'b2c_site'
UPDATED_SOURCE_NAME = 'Enterprise User Enrollment Background Task'
UPDATED_SOURCE_SLUG = 'enrollment_task'


def update_source(apps, schema_editor):
"""
The name of this Source is being updated to better reflect it usage.
"""
enrollment_sources = apps.get_model('enterprise', 'EnterpriseEnrollmentSource')

source = enrollment_sources.objects.get(slug=ORIGINAL_SOURCE_SLUG)
source.name = UPDATED_SOURCE_NAME
source.slug = UPDATED_SOURCE_SLUG
source.save()


def revert_source(apps, schema_editor):
"""
The name of this Source is being updated to better reflect it usage.
"""
enrollment_sources = apps.get_model('enterprise', 'EnterpriseEnrollmentSource')

source = enrollment_sources.objects.get(slug=UPDATED_SOURCE_SLUG)
source.name = ORIGINAL_SOURCE_NAME
source.slug = ORIGINAL_SOURCE_SLUG
source.save()


class Migration(migrations.Migration):

dependencies = [
('enterprise', '0079_AddEnterpriseEnrollmentSource'),
('enterprise', '0080_auto_20191113_1708'),
]

operations = [
migrations.RunPython(update_source, revert_source)
]
2 changes: 1 addition & 1 deletion enterprise/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ class EnterpriseEnrollmentSource(TimeStampedModel):
API = 'enterprise_api'
ENROLLMENT_URL = 'enrollment_url'
OFFER_REDEMPTION = 'offer_redemption'
B2C_SITE = 'b2c_site'
ENROLLMENT_TASK = 'enrollment_task'

name = models.CharField(max_length=64)
slug = models.SlugField(max_length=30, unique=True)
Expand Down
12 changes: 11 additions & 1 deletion enterprise/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from celery import shared_task

from enterprise.models import EnterpriseCourseEnrollment, EnterpriseCustomerUser
from enterprise.models import EnterpriseCourseEnrollment, EnterpriseCustomerUser, EnterpriseEnrollmentSource

LOGGER = getLogger(__name__)

Expand Down Expand Up @@ -39,7 +39,17 @@ def create_enterprise_enrollment(course_id, enterprise_customer_user_id):
"Creating EnterpriseCourseEnrollment for user %s "
"on course %s for enterprise_customer %s"
), enterprise_customer_user.user_id, course_id, enterprise_customer)

# On Create we set the Source to be ENROLLMENT_TASK here. This Source
# is generalized from being just a B2C Source type because it is possible
# to reach this task before the EnterpriseCustomerEnrollment is created
# depending on timing.
#
# We have made changes elsewhere to avoid this issue, but in the mean time
# we believe a Source of ENROLLMENT_TASK is more clear.

EnterpriseCourseEnrollment.objects.create(
course_id=course_id,
enterprise_customer_user=enterprise_customer_user,
source=EnterpriseEnrollmentSource.get_source(EnterpriseEnrollmentSource.ENROLLMENT_TASK)
)
26 changes: 22 additions & 4 deletions tests/test_enterprise/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import mock
from pytest import mark

from enterprise.models import EnterpriseCourseEnrollment
from enterprise.models import EnterpriseCourseEnrollment, EnterpriseEnrollmentSource
from enterprise.tasks import create_enterprise_enrollment
from test_utils.factories import EnterpriseCustomerFactory, EnterpriseCustomerUserFactory, UserFactory

Expand All @@ -19,6 +19,8 @@ class TestEnterpriseTasks(unittest.TestCase):
"""
Tests tasks associated with Enterprise.
"""
FAKE_COURSE_ID = 'course-v1:edx+Test+2T2019'

def setUp(self):
"""
Setup for `TestEnterpriseTasks` test.
Expand All @@ -40,13 +42,29 @@ def test_create_enrollment_task_course_in_catalog(self, mock_contains_course):
the function is part of the EnterpriseCustomer's catalogs
"""
mock_contains_course.return_value = True
assert EnterpriseCourseEnrollment.objects.count() == 0
create_enterprise_enrollment(
self.FAKE_COURSE_ID,
self.enterprise_customer_user.id
)
assert EnterpriseCourseEnrollment.objects.count() == 1

@mock.patch('enterprise.models.EnterpriseCustomer.catalog_contains_course')
def test_create_enrollment_task_source_set(self, mock_contains_course):
"""
Task should create an enterprise enrollment if the course_id handed to
the function is part of the EnterpriseCustomer's catalogs
"""
mock_contains_course.return_value = True
assert EnterpriseCourseEnrollment.objects.count() == 0
create_enterprise_enrollment(
'fake:course',
self.FAKE_COURSE_ID,
self.enterprise_customer_user.id
)
assert EnterpriseCourseEnrollment.objects.count() == 1
assert EnterpriseCourseEnrollment.objects.get(
course_id=self.FAKE_COURSE_ID,
).source.slug == EnterpriseEnrollmentSource.ENROLLMENT_TASK

@mock.patch('enterprise.models.EnterpriseCustomer.catalog_contains_course')
def test_create_enrollment_task_course_not_in_catalog(self, mock_contains_course):
Expand All @@ -58,7 +76,7 @@ def test_create_enrollment_task_course_not_in_catalog(self, mock_contains_course

assert EnterpriseCourseEnrollment.objects.count() == 0
create_enterprise_enrollment(
'fake:course',
self.FAKE_COURSE_ID,
self.enterprise_customer_user.id
)
assert EnterpriseCourseEnrollment.objects.count() == 0
Expand All @@ -76,7 +94,7 @@ def test_create_enrollment_task_no_create_duplicates(self):

assert EnterpriseCourseEnrollment.objects.count() == 1
create_enterprise_enrollment(
'fake:course',
self.FAKE_COURSE_ID,
self.enterprise_customer_user.id
)
assert EnterpriseCourseEnrollment.objects.count() == 1

0 comments on commit d350356

Please sign in to comment.