Skip to content

Commit

Permalink
Merge branch 'aci.upstream' of https://github.com/raccoongang/credent…
Browse files Browse the repository at this point in the history
…ials into aci.upstream
  • Loading branch information
Andrii Hantkovskyi committed Jun 17, 2024
2 parents a1c5f39 + 8f949c2 commit bdd5989
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .annotation_safe_list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ auth.Permission:
badges.BadgePenalty:
".. no_pii:": "This model has no PII"
badges.BadgeProgress:
".. no_pii:": "This model has no PII"
".. pii": "Username"
".. pii_types": other
".. pii_retirement": retained
badges.BadgeRequirement:
".. no_pii:": "This model has no PII"
badges.CredlyOrganization:
Expand Down
218 changes: 206 additions & 12 deletions credentials/apps/badges/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
CredlyOrganization,
DataRule,
Fulfillment,
PenaltyDataRule,
)
from credentials.apps.core.models import User

Expand All @@ -29,7 +30,11 @@ def setUp(self):
)
self.site = Site.objects.create(domain="test_domain", name="test_name")
self.badge_template = CredlyBadgeTemplate.objects.create(
organization=self.organization, uuid=uuid.uuid4(), name="test_template", state="draft", site=self.site
organization=self.organization,
uuid=uuid.uuid4(),
name="test_template",
state="draft",
site=self.site,
)
self.requirement = BadgeRequirement.objects.create(
template=self.badge_template,
Expand Down Expand Up @@ -68,7 +73,8 @@ def setUp(self):
uuid=uuid.uuid4(), name="test_template2", state="draft", site=self.site
)
self.badge_requirement = BadgeRequirement.objects.create(
template=self.badge_template1, event_type="org.openedx.learning.course.passing.status.updated.v1"
template=self.badge_template1,
event_type="org.openedx.learning.course.passing.status.updated.v1",
)
self.data_rule1 = DataRule.objects.create(
requirement=self.badge_requirement,
Expand Down Expand Up @@ -103,7 +109,11 @@ def setUp(self):
uuid=uuid.uuid4(), name="test_template", state="draft", site=self.site
)
self.credlybadge_template = CredlyBadgeTemplate.objects.create(
organization=self.organization, uuid=uuid.uuid4(), name="test_template", state="draft", site=self.site
organization=self.organization,
uuid=uuid.uuid4(),
name="test_template",
state="draft",
site=self.site,
)

self.requirement1 = BadgeRequirement.objects.create(
Expand Down Expand Up @@ -154,7 +164,9 @@ def test_fulfill(self):
with patch("credentials.apps.badges.models.notify_requirement_fulfilled") as mock_notify:
created = self.requirement.fulfill(username)
fulfillment = Fulfillment.objects.get(
progress=progress, requirement=self.requirement, blend=self.requirement.blend
progress=progress,
requirement=self.requirement,
blend=self.requirement.blend,
)

self.assertTrue(created)
Expand All @@ -166,6 +178,13 @@ def test_fulfill(self):
fulfillment_id=fulfillment.id,
)

def test_is_active(self):
self.requirement.template.is_active = True
self.assertTrue(self.requirement.is_active)

self.requirement.template.is_active = False
self.assertFalse(self.requirement.is_active)


class RequirementFulfillmentCheckTestCase(TestCase):
def setUp(self):
Expand All @@ -178,7 +197,8 @@ def setUp(self):
)
self.badge_progress = BadgeProgress.objects.create(template=self.badge_template1, username="test1")
self.badge_requirement = BadgeRequirement.objects.create(
template=self.badge_template1, event_type="org.openedx.learning.course.passing.status.updated.v1"
template=self.badge_template1,
event_type="org.openedx.learning.course.passing.status.updated.v1",
)
self.fulfillment = Fulfillment.objects.create(progress=self.badge_progress, requirement=self.badge_requirement)

Expand Down Expand Up @@ -208,7 +228,8 @@ def setUp(self):
blend="group1",
)
self.badge_requirement3 = BadgeRequirement.objects.create(
template=self.badge_template, event_type="org.openedx.learning.course.passing.status.updated.v1"
template=self.badge_template,
event_type="org.openedx.learning.course.passing.status.updated.v1",
)

def test_requirement_group(self):
Expand All @@ -227,7 +248,11 @@ def setUp(self):
uuid=uuid.uuid4(), name="test_template", state="draft", site=self.site
)
self.credlybadge_template = CredlyBadgeTemplate.objects.create(
organization=self.organization, uuid=uuid.uuid4(), name="test_template", state="draft", site=self.site
organization=self.organization,
uuid=uuid.uuid4(),
name="test_template",
state="draft",
site=self.site,
)
self.requirement1 = BadgeRequirement.objects.create(
template=self.badge_template,
Expand Down Expand Up @@ -274,7 +299,11 @@ def setUp(self):
uuid=uuid.uuid4(), name="test_template", state="draft", site=self.site
)
self.credlybadge_template = CredlyBadgeTemplate.objects.create(
organization=self.organization, uuid=uuid.uuid4(), name="test_template", state="draft", site=self.site
organization=self.organization,
uuid=uuid.uuid4(),
name="test_template",
state="draft",
site=self.site,
)
self.requirement1 = BadgeRequirement.objects.create(
template=self.badge_template,
Expand Down Expand Up @@ -307,7 +336,11 @@ def setUp(self):
uuid=uuid.uuid4(), name="test_template", state="draft", site=self.site
)
self.credlybadge_template = CredlyBadgeTemplate.objects.create(
organization=self.organization, uuid=uuid.uuid4(), name="test_template", state="draft", site=self.site
organization=self.organization,
uuid=uuid.uuid4(),
name="test_template",
state="draft",
site=self.site,
)
self.requirement1 = BadgeRequirement.objects.create(
template=self.badge_template,
Expand Down Expand Up @@ -385,7 +418,10 @@ def test_ratio_no_requirements(self):
class CredlyBadgeAsBadgeDataTestCase(TestCase):
def setUp(self):
self.user = User.objects.create_user(
username="test_user", email="[email protected]", full_name="Test User", lms_user_id=1
username="test_user",
email="[email protected]",
full_name="Test User",
lms_user_id=1,
)
self.site = Site.objects.create(domain="test_domain", name="test_name")
self.credential = BadgeTemplate.objects.create(
Expand Down Expand Up @@ -485,7 +521,8 @@ def setUp(self):
blend="group1",
)
self.badge_requirement3 = BadgeRequirement.objects.create(
template=self.badge_template, event_type="org.openedx.learning.course.passing.status.updated.v1"
template=self.badge_template,
event_type="org.openedx.learning.course.passing.status.updated.v1",
)
self.username = "test_user"

Expand Down Expand Up @@ -531,7 +568,10 @@ def test_get_all_organization_ids(self):

def test_get_preconfigured_organizations(self):
preconfigured_organizations = CredlyOrganization.get_preconfigured_organizations()
self.assertEqual(preconfigured_organizations, settings.BADGES_CONFIG["credly"].get("ORGANIZATIONS", {}))
self.assertEqual(
preconfigured_organizations,
settings.BADGES_CONFIG["credly"].get("ORGANIZATIONS", {}),
)

def test_is_preconfigured(self):
with patch(
Expand All @@ -540,3 +580,157 @@ def test_is_preconfigured(self):
mock_get_preconfigured.return_value = {str(self.uuid): "Test Organization"}
self.assertTrue(self.organization.is_preconfigured)
mock_get_preconfigured.assert_called_once()


class BadgeProgressTestCase(TestCase):
def setUp(self):
self.site = Site.objects.create(domain="test_domain", name="test_name")
self.badge_template = BadgeTemplate.objects.create(
uuid=uuid.uuid4(), name="test_template", state="draft", site=self.site
)
self.badge_progress = BadgeProgress.objects.create(template=self.badge_template, username="test_user")

def test_reset_progress(self):
Fulfillment.objects.create(progress=self.badge_progress)
self.assertEqual(Fulfillment.objects.filter(progress=self.badge_progress).count(), 1)

self.badge_progress.reset()

self.assertEqual(Fulfillment.objects.filter(progress=self.badge_progress).count(), 0)


class BadgePenaltyIsActiveTestCase(TestCase):
def setUp(self):
self.organization = CredlyOrganization.objects.create(
uuid=uuid.uuid4(), api_key="test-api-key", name="test_organization"
)
self.site = Site.objects.create(domain="test_domain", name="test_name")
self.badge_template = CredlyBadgeTemplate.objects.create(
organization=self.organization,
uuid=uuid.uuid4(),
name="test_template",
state="draft",
site=self.site,
)
self.requirement = BadgeRequirement.objects.create(
template=self.badge_template,
event_type="org.openedx.learning.course.passing.status.updated.v1",
description="Test description",
)
self.penalty = BadgePenalty.objects.create(
template=self.badge_template,
event_type="org.openedx.learning.course.passing.status.updated.v1",
)

def test_is_active(self):
self.penalty.template.is_active = True
self.assertTrue(self.penalty.is_active)

def test_is_not_active(self):
self.penalty.template.is_active = False
self.assertFalse(self.penalty.is_active)


class DataRuleIsActiveTestCase(TestCase):
def setUp(self):
self.site = Site.objects.create(domain="test_domain", name="test_name")
self.badge_template = BadgeTemplate.objects.create(
uuid=uuid.uuid4(), name="test_template", state="draft", site=self.site
)
self.requirement = BadgeRequirement.objects.create(
template=self.badge_template,
event_type="org.openedx.learning.course.passing.status.updated.v1",
description="Test description",
)
self.rule = DataRule.objects.create(
requirement=self.requirement,
data_path="course_passing_status.user.pii.username",
operator="eq",
value="cucumber1997",
)

def test_is_active(self):
self.rule.requirement.template.is_active = True
self.assertTrue(self.rule.is_active)

self.rule.requirement.template.is_active = False
self.assertFalse(self.rule.is_active)


class BadgeTemplateTestCase(TestCase):
def test_by_uuid(self):
template_uuid = uuid.uuid4()
site = Site.objects.create(domain="test_domain", name="test_name")
badge_template = BadgeTemplate.objects.create(uuid=template_uuid, origin=BadgeTemplate.ORIGIN, site=site)

retrieved_template = BadgeTemplate.by_uuid(template_uuid)

self.assertEqual(retrieved_template, badge_template)


class CredlyBadgeTemplateTestCase(TestCase):
def setUp(self):
uuid4 = uuid.uuid4()
self.organization = CredlyOrganization.objects.create(
uuid=uuid4, api_key="test-api-key", name="test_organization"
)
self.site = Site.objects.create(domain="test_domain", name="test_name")
self.badge_template = CredlyBadgeTemplate.objects.create(
organization=self.organization,
uuid=uuid4,
name="test_template",
state="draft",
site=self.site,
)

def test_management_url(self):
credly_host_base_url = "https://example.com/"
with patch("credentials.apps.badges.models.get_credly_base_url") as mock_get_credly_base_url:
mock_get_credly_base_url.return_value = credly_host_base_url
expected_url = (
f"{credly_host_base_url}mgmt/organizations/"
f"{self.organization.uuid}/badges/templates/{self.badge_template.uuid}/details"
)
self.assertEqual(self.badge_template.management_url, expected_url)
mock_get_credly_base_url.assert_called_with(settings)


class PenaltyDataruleIsActiveTestCase(TestCase):
def setUp(self):
self.organization = CredlyOrganization.objects.create(
uuid=uuid.uuid4(), api_key="test-api-key", name="test_organization"
)
self.site = Site.objects.create(domain="test_domain", name="test_name")
self.badge_template = BadgeTemplate.objects.create(
uuid=uuid.uuid4(), name="test_template", state="draft", site=self.site
)
self.credlybadge_template = CredlyBadgeTemplate.objects.create(
organization=self.organization,
uuid=uuid.uuid4(),
name="test_template",
state="draft",
site=self.site,
)

self.requirement = BadgeRequirement.objects.create(
template=self.badge_template,
event_type="org.openedx.learning.course.passing.status.updated.v1",
description="Test description",
)
self.penalty = BadgePenalty.objects.create(
template=self.badge_template,
event_type="org.openedx.learning.course.passing.status.updated.v1",
)
self.rule = PenaltyDataRule.objects.create(
penalty=self.penalty,
data_path="course_passing_status.user.pii.username",
operator="eq",
value="cucumber1997",
)

def test_is_active(self):
self.requirement.template.is_active = True
self.assertTrue(self.rule.is_active)

self.requirement.template.is_active = False
self.assertFalse(self.rule.is_active)

0 comments on commit bdd5989

Please sign in to comment.