-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENT-2310 | Adding a post_save receiver to listen to CourseEnrollments…
… table (#589) Adding in a test file I forgot. Updating a few test doctrings Actually adding in the tasks file too Updating task to prevent duplicate recreation of EnterpriseCourseEnrollment Appeasing pylint bumping version
- Loading branch information
1 parent
e0e5600
commit efe35de
Showing
6 changed files
with
234 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Django tasks. | ||
""" | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
from logging import getLogger | ||
|
||
from celery import shared_task | ||
|
||
from enterprise.models import EnterpriseCourseEnrollment | ||
|
||
LOGGER = getLogger(__name__) | ||
|
||
|
||
@shared_task | ||
def create_enterprise_enrollment(course_id, enterprise_customer_user): | ||
""" | ||
Create enterprise enrollment for user if course_id part of catalog for the ENT customer. | ||
""" | ||
# Prevent duplicate records from being created if possible | ||
# before we need to make a call to discovery | ||
if EnterpriseCourseEnrollment.objects.filter( | ||
enterprise_customer_user=enterprise_customer_user, | ||
course_id=course_id, | ||
).exists(): | ||
LOGGER.info(( | ||
"EnterpriseCourseEnrollment record exists for user %s " | ||
"on course %s. Exiting task." | ||
), enterprise_customer_user.user_id, course_id) | ||
return | ||
|
||
enterprise_customer = enterprise_customer_user.enterprise_customer | ||
if enterprise_customer.catalog_contains_course(course_id): | ||
LOGGER.info(( | ||
"Creating EnterpriseCourseEnrollment for user %s " | ||
"on course %s for enterprise_customer %s" | ||
), enterprise_customer_user.user_id, course_id, enterprise_customer) | ||
EnterpriseCourseEnrollment.objects.create( | ||
course_id=course_id, | ||
enterprise_customer_user=enterprise_customer_user, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Tests for the `edx-enterprise` tasks module. | ||
""" | ||
from __future__ import absolute_import, unicode_literals, with_statement | ||
|
||
import unittest | ||
|
||
import mock | ||
from pytest import mark | ||
|
||
from enterprise.models import EnterpriseCourseEnrollment | ||
from enterprise.tasks import create_enterprise_enrollment | ||
from test_utils.factories import EnterpriseCustomerFactory, EnterpriseCustomerUserFactory, UserFactory | ||
|
||
|
||
@mark.django_db | ||
class TestEnterpriseTasks(unittest.TestCase): | ||
""" | ||
Tests tasks associated with Enterprise. | ||
""" | ||
def setUp(self): | ||
""" | ||
Setup for `TestEnterpriseTasks` test. | ||
""" | ||
self.user = UserFactory(id=2, email='user@example.com') | ||
self.enterprise_customer = EnterpriseCustomerFactory( | ||
name='Team Titans', | ||
) | ||
self.enterprise_customer_user = EnterpriseCustomerUserFactory( | ||
user_id=self.user.id, | ||
enterprise_customer=self.enterprise_customer, | ||
) | ||
super(TestEnterpriseTasks, self).setUp() | ||
|
||
@mock.patch('enterprise.models.EnterpriseCustomer.catalog_contains_course') | ||
def test_create_enrollment_task_course_in_catalog(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.enterprise_customer_user | ||
) | ||
assert EnterpriseCourseEnrollment.objects.count() == 1 | ||
|
||
@mock.patch('enterprise.models.EnterpriseCustomer.catalog_contains_course') | ||
def test_create_enrollment_task_course_not_in_catalog(self, mock_contains_course): | ||
""" | ||
Task should NOT create an enterprise enrollment if the course_id handed | ||
to the function is NOT part of the EnterpriseCustomer's catalogs | ||
""" | ||
mock_contains_course.return_value = False | ||
|
||
assert EnterpriseCourseEnrollment.objects.count() == 0 | ||
create_enterprise_enrollment( | ||
'fake:course', | ||
self.enterprise_customer_user | ||
) | ||
assert EnterpriseCourseEnrollment.objects.count() == 0 | ||
|
||
def test_create_enrollment_task_no_create_duplicates(self): | ||
""" | ||
Task should return without creating a new EnterpriseCourseEnrollment | ||
if one with the course_id and enterprise_customer_user specified | ||
already exists. | ||
""" | ||
EnterpriseCourseEnrollment.objects.create( | ||
course_id='fake:course', | ||
enterprise_customer_user=self.enterprise_customer_user, | ||
) | ||
|
||
assert EnterpriseCourseEnrollment.objects.count() == 1 | ||
create_enterprise_enrollment( | ||
'fake:course', | ||
self.enterprise_customer_user | ||
) | ||
assert EnterpriseCourseEnrollment.objects.count() == 1 |