forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #220 from CUCWD/feature.maple/ztraboo/merge-lti-ed…
…x-authentication-using-email Link existing platform users to the LtiUser via email `lis_person_contact_email_primary` payload data.
- Loading branch information
Showing
5 changed files
with
74 additions
and
27 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
lms/djangoapps/lti_provider/migrations/0004_lticonsumer_auto_link_users_using_email.py
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,18 @@ | ||
# Generated by Django 3.2.21 on 2023-09-21 12:18 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('lti_provider', '0003_auto_20161118_1040'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='lticonsumer', | ||
name='auto_link_users_using_email', | ||
field=models.BooleanField(blank=True, default=False), | ||
), | ||
] |
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 |
---|---|---|
|
@@ -112,7 +112,7 @@ def test_authentication_with_new_user(self, _create_user, switch_user): | |
lti_user.edx_user_id = self.edx_user_id | ||
with patch('lms.djangoapps.lti_provider.users.create_lti_user', return_value=lti_user) as create_user: | ||
users.authenticate_lti_user(self.request, self.lti_user_id, self.lti_consumer) | ||
create_user.assert_called_with(self.lti_user_id, self.lti_consumer) | ||
create_user.assert_called_with(self.lti_user_id, self.lti_consumer, "") | ||
switch_user.assert_called_with(self.request, lti_user, self.lti_consumer) | ||
|
||
def test_authentication_with_authenticated_user(self, create_user, switch_user): | ||
|
@@ -140,6 +140,18 @@ def test_authentication_with_wrong_user(self, create_user, switch_user): | |
assert not create_user.called | ||
switch_user.assert_called_with(self.request, lti_user, self.lti_consumer) | ||
|
||
def test_auto_linking_of_users_using_lis_person_contact_email_primary(self, create_user, switch_user): | ||
request = RequestFactory().post("/", {"lis_person_contact_email_primary": self.old_user.email}) | ||
request.user = self.old_user | ||
|
||
users.authenticate_lti_user(request, self.lti_user_id, self.lti_consumer) | ||
create_user.assert_called_with(self.lti_user_id, self.lti_consumer, "") | ||
|
||
self.lti_consumer.auto_link_users_using_email = True | ||
self.lti_consumer.save() | ||
users.authenticate_lti_user(request, self.lti_user_id, self.lti_consumer) | ||
create_user.assert_called_with(self.lti_user_id, self.lti_consumer, self.old_user.email) | ||
|
||
|
||
class CreateLtiUserTest(TestCase): | ||
""" | ||
|
@@ -154,16 +166,17 @@ def setUp(self): | |
consumer_secret='TestSecret' | ||
) | ||
self.lti_consumer.save() | ||
self.existing_user = UserFactory.create() | ||
|
||
def test_create_lti_user_creates_auth_user_model(self): | ||
users.create_lti_user('lti_user_id', self.lti_consumer) | ||
assert User.objects.count() == 1 | ||
assert User.objects.count() == 2 | ||
|
||
@patch('uuid.uuid4', return_value='random_uuid') | ||
@patch('lms.djangoapps.lti_provider.users.generate_random_edx_username', return_value='edx_id') | ||
def test_create_lti_user_creates_correct_user(self, uuid_mock, _username_mock): | ||
users.create_lti_user('lti_user_id', self.lti_consumer) | ||
assert User.objects.count() == 1 | ||
assert User.objects.count() == 2 | ||
user = User.objects.get(username='edx_id') | ||
assert user.email == '[email protected]' | ||
uuid_mock.assert_called_with() | ||
|
@@ -173,10 +186,15 @@ def test_unique_username_created(self, username_mock): | |
User(username='edx_id').save() | ||
users.create_lti_user('lti_user_id', self.lti_consumer) | ||
assert username_mock.call_count == 2 | ||
assert User.objects.count() == 2 | ||
assert User.objects.count() == 3 | ||
user = User.objects.get(username='new_edx_id') | ||
assert user.email == '[email protected]' | ||
|
||
def test_existing_user_is_linked(self): | ||
lti_user = users.create_lti_user('lti_user_id', self.lti_consumer, self.existing_user.email) | ||
assert lti_user.lti_consumer == self.lti_consumer | ||
assert lti_user.edx_user == self.existing_user | ||
|
||
|
||
class LtiBackendTest(TestCase): | ||
""" | ||
|
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