Skip to content

Commit

Permalink
Merge pull request #623 from edx/iahmad/ENT-2346-update-to-manage_lea…
Browse files Browse the repository at this point in the history
…rners-admin-page-for-multiple-learner-enterprises

ENT-2346 Overridden get in EnterpriseCustomerUser manager
  • Loading branch information
irfanuddinahmad authored Nov 18, 2019
2 parents ef734db + a0fc42c commit 07d9947
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Change Log
Unreleased
----------
[2.0.22] - 2019-11-18
---------------------

* Custom get function in EnterpriseCustomerUserManager to enable multiple user enterprises.

[2.0.21] - 2019-11-14
---------------------
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.21"
__version__ = "2.0.22"

default_app_config = "enterprise.apps.EnterpriseConfig" # pylint: disable=invalid-name
12 changes: 12 additions & 0 deletions enterprise/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,18 @@ class EnterpriseCustomerUserManager(models.Manager):
This class should contain methods that create, modify or query :class:`.EnterpriseCustomerUser` entities.
"""

def get(self, **kwargs):
"""
Overridden get method to return the first element in case of learner with multiple enterprises.
Raises EnterpriseCustomerUser.DoesNotExist if no records are found.
"""
fetched_object = super(EnterpriseCustomerUserManager, self).get_queryset().filter(**kwargs).first()
if fetched_object:
return fetched_object
else:
raise EnterpriseCustomerUser.DoesNotExist

def get_link_by_email(self, user_email):
"""
Return link by email.
Expand Down
24 changes: 24 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,30 @@ class TestEnterpriseCustomerUserManager(unittest.TestCase):
Tests EnterpriseCustomerUserManager.
"""

def test_get_returns_only_one_when_multiple_objects_returned(self):
"""
Test that get on custom model manager returns only the active object when multiple objects found.
"""
enterprise_customer1 = factories.EnterpriseCustomerFactory()
enterprise_customer2 = factories.EnterpriseCustomerFactory()
user = factories.UserFactory(email='[email protected]')
first_customer_user = factories.EnterpriseCustomerUserFactory(
enterprise_customer=enterprise_customer1,
user_id=user.id
)
second_customer_user = factories.EnterpriseCustomerUserFactory(
enterprise_customer=enterprise_customer2,
user_id=user.id,
active=False
)
all_customer_users = [first_customer_user, second_customer_user]
fetched_object = EnterpriseCustomerUser.objects.get(user_id=user.id)
self.assertIn(fetched_object, all_customer_users)
self.assertEqual(fetched_object.active, True)
EnterpriseCustomerUser.objects.filter(user_id=user.id).delete()
with raises(EnterpriseCustomerUser.DoesNotExist):
EnterpriseCustomerUser.objects.get(user_id=user.id)

@ddt.data("[email protected]", "[email protected]", "[email protected]")
def test_link_user_existing_user(self, user_email):
enterprise_customer = factories.EnterpriseCustomerFactory()
Expand Down

0 comments on commit 07d9947

Please sign in to comment.