forked from openedx/credentials
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: [ACI-587, ACI-586] implement openedx origin badge progress completion processing #72
Merged
wowkalucky
merged 2 commits into
aci.main
from
hantkovskyi/aci-587/badge-progress-completion-signal
Mar 26, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
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,9 @@ | ||
from django.core.exceptions import ObjectDoesNotExist | ||
|
||
from ..models import BadgeTemplate | ||
|
||
def get_badge_template_by_id(badge_template_id): | ||
try: | ||
return BadgeTemplate.objects.get(id=badge_template_id) | ||
except ObjectDoesNotExist: | ||
return None |
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,19 @@ | ||
from django.contrib.contenttypes.models import ContentType | ||
|
||
from ..models import BadgeTemplate, UserCredential | ||
|
||
|
||
def create_user_credential(username, badge_template): | ||
if not isinstance(username, str): | ||
raise ValueError("`username` must be a string") | ||
|
||
if not isinstance(badge_template, BadgeTemplate): | ||
raise TypeError("`badge_template` must be an instance of BadgeTemplate") | ||
|
||
|
||
UserCredential.objects.create( | ||
credential_content_type=ContentType.objects.get_for_model( | ||
badge_template), | ||
credential_id=badge_template.id, | ||
username=username, | ||
) |
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 |
---|---|---|
|
@@ -5,8 +5,14 @@ | |
""" | ||
import logging | ||
|
||
from django.dispatch import receiver | ||
|
||
from openedx_events.tooling import OpenEdxPublicSignal, load_all_signals | ||
|
||
from .signals import BADGE_PROGRESS_COMPLETE | ||
|
||
from ..services.badge_templates import get_badge_template_by_id | ||
from ..services.user_credentials import create_user_credential | ||
from ..utils import get_badging_event_types | ||
from ..processing import process | ||
|
||
|
@@ -33,4 +39,18 @@ def event_handler(sender, signal, **kwargs): | |
logger.debug(f"Received signal {signal}") | ||
|
||
# NOTE (performance): all consumed messages from event bus trigger this. | ||
process(signal, sender=sender, **kwargs) | ||
process(signal, sender=sender, **kwargs) | ||
|
||
|
||
@receiver(BADGE_PROGRESS_COMPLETE) | ||
def listen_for_completed_badge(sender, username, badge_template_id, **kwargs): # pylint: disable=unused-argument | ||
badge_template = get_badge_template_by_id(badge_template_id) | ||
|
||
if badge_template is None: | ||
return | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note for future: here we'd want to make badge templates to be responsible for their type of user credentials:
|
||
if badge_template.origin == 'openedx': | ||
create_user_credential(username, badge_template) | ||
return | ||
|
||
# TODO: add processing for 'credly' origin |
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,59 @@ | ||
from django.test import TestCase | ||
from django.core.exceptions import ObjectDoesNotExist | ||
from django.contrib.contenttypes.models import ContentType | ||
|
||
from ..models import BadgeTemplate, UserCredential | ||
from ..services.badge_templates import get_badge_template_by_id | ||
from ..services.user_credentials import create_user_credential | ||
|
||
|
||
class UserCredentialServiceTestCase(TestCase): | ||
def setUp(self): | ||
# Create a test badge template | ||
self.badge_template = BadgeTemplate.objects.create( | ||
origin='openedx', site_id=1) | ||
|
||
def test_create_user_credential(self): | ||
# Call create_user_credential with valid arguments | ||
create_user_credential('test_user', self.badge_template) | ||
|
||
# Check if user credential is created | ||
self.assertTrue( | ||
UserCredential.objects.filter( | ||
username='test_user', | ||
credential_content_type=ContentType.objects.get_for_model( | ||
self.badge_template), | ||
credential_id=self.badge_template.id, | ||
).exists() | ||
) | ||
|
||
def test_create_user_credential_invalid_username(self): | ||
# Call create_user_credential with non-existent username | ||
with self.assertRaises(ValueError): | ||
# Passing int as username | ||
create_user_credential(123, self.badge_template) | ||
|
||
def test_create_user_credential_invalid_template(self): | ||
# Call create_user_credential with non-existent badge template | ||
with self.assertRaises(TypeError): | ||
# Passing None as badge template | ||
create_user_credential('test_user', None) | ||
|
||
|
||
class BadgeTemplateServiceTestCase(TestCase): | ||
def setUp(self): | ||
self.badge_template = BadgeTemplate.objects.create(origin='openedx', site_id=1) | ||
|
||
def test_get_badge_template_by_id(self): | ||
# Call get_badge_template_by_id with existing badge template ID | ||
badge_template = get_badge_template_by_id(self.badge_template.id) | ||
|
||
# Check if the returned badge template is correct | ||
self.assertEqual(badge_template, self.badge_template) | ||
|
||
def test_get_badge_template_by_id_nonexistent(self): | ||
# Call get_badge_template_by_id with non-existent ID | ||
badge_template = get_badge_template_by_id(999) # Non-existent ID | ||
|
||
# Check that None is returned | ||
self.assertIsNone(badge_template) |
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,48 @@ | ||
from django.test import TestCase | ||
from django.core.exceptions import ObjectDoesNotExist | ||
from django.contrib.contenttypes.models import ContentType | ||
|
||
from ..models import BadgeTemplate, UserCredential | ||
from ..signals.signals import BADGE_PROGRESS_COMPLETE | ||
|
||
|
||
class BadgeSignalReceiverTestCase(TestCase): | ||
def setUp(self): | ||
# Create a test badge template | ||
self.badge_template = BadgeTemplate.objects.create( | ||
name='test', site_id=1) | ||
|
||
def test_signal_emission_and_receiver_execution(self): | ||
# Emit the signal | ||
BADGE_PROGRESS_COMPLETE.send( | ||
sender=self, | ||
username='test_user', | ||
badge_template_id=self.badge_template.id, | ||
) | ||
|
||
# UserCredential object | ||
user_credential = UserCredential.objects.filter( | ||
username='test_user', | ||
credential_content_type=ContentType.objects.get_for_model( | ||
self.badge_template), | ||
credential_id=self.badge_template.id, | ||
) | ||
|
||
# Check if user credential is created | ||
self.assertTrue(user_credential.exists()) | ||
|
||
# Check if user credential status is 'awarded' | ||
self.assertTrue(user_credential[0].status == 'awarded') | ||
|
||
def test_behavior_for_nonexistent_badge_templates(self): | ||
# Emit the signal with a non-existent badge template ID | ||
BADGE_PROGRESS_COMPLETE.send( | ||
sender=self, | ||
username='test_user', | ||
badge_template_id=999, # Non-existent ID | ||
) | ||
|
||
# Check that no user credential is created | ||
self.assertFalse( | ||
UserCredential.objects.filter(username='test_user').exists() | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should possibly log an error.