def test_get_assertions(self, check_course, wildcard): """ Verify we can get assertions via the badge class and username. """ badge_class = self.create_badge_class(check_course) for dummy in range(3): BadgeAssertionFactory.create(user=self.user, badge_class=badge_class) if badge_class.course_id: # Also create a version of this badge under a different course. alt_class = BadgeClassFactory.create( slug=badge_class.slug, issuing_component=badge_class.issuing_component, course_id=CourseFactory.create().location.course_key, ) BadgeAssertionFactory.create(user=self.user, badge_class=alt_class) # Same badge class, but different user. Should not show up in the list. for dummy in range(5): BadgeAssertionFactory.create(badge_class=badge_class) # Different badge class AND different user. Certainly shouldn't show up in the list! for dummy in range(6): BadgeAssertionFactory.create() response = self.get_json(self.url(), data=self.get_qs_args(check_course, wildcard, badge_class)) if wildcard: expected_length = 4 else: expected_length = 3 # pylint: disable=no-member self.assertEqual(len(response["results"]), expected_length) unused_class = self.create_badge_class(check_course, slug="unused_slug", issuing_component="unused_component") response = self.get_json(self.url(), data=self.get_qs_args(check_course, wildcard, unused_class)) # pylint: disable=no-member self.assertEqual(len(response["results"]), 0)
def test_award(self, mock_award): """ Verify that the award command calls the award function on the backend with the right parameters. """ user = UserFactory.create() badge_class = BadgeClassFactory.create() badge_class.award(user, evidence_url='http://example.com/evidence') self.assertTrue(mock_award.called) mock_award.assert_called_with(badge_class, user, evidence_url='http://example.com/evidence')
def test_get_for_user(self): """ Make sure we can get an assertion for a user if there is one. """ user = UserFactory.create() badge_class = BadgeClassFactory.create() self.assertFalse(badge_class.get_for_user(user)) assertion = BadgeAssertionFactory.create(badge_class=badge_class, user=user) self.assertEqual(list(badge_class.get_for_user(user)), [assertion])
def test_assertion_structure(self): """ Verify the badge assertion structure is as expected when a course is involved. """ course_key = self.course.location.course_key badge_class = BadgeClassFactory.create(course_id=course_key) assertion = BadgeAssertionFactory.create(badge_class=badge_class, user=self.user) response = self.get_json(self.url()) self.check_assertion_structure(assertion, response['results'][0])
def setUp(self): """ Create a course and user to test with. """ super(BadgrBackendTestCase, self).setUp() # Need key to be deterministic to test slugs. self.course = CourseFactory.create( org='edX', course='course_test', run='test_run', display_name='Badged', start=datetime(year=2015, month=5, day=19), end=datetime(year=2015, month=5, day=20) ) self.user = UserFactory.create(email='*****@*****.**') CourseEnrollmentFactory.create(user=self.user, course_id=self.course.location.course_key, mode='honor') # Need to empty this on each run. BadgrBackend.badges = [] self.badge_class = BadgeClassFactory.create(course_id=self.course.location.course_key) self.legacy_badge_class = BadgeClassFactory.create( course_id=self.course.location.course_key, issuing_component='' ) self.no_course_badge_class = BadgeClassFactory.create()
def test_get_badge_class(self): runtime = self.create_runtime() badge_service = runtime.service(self.mock_block, 'badging') premade_badge_class = BadgeClassFactory.create() # Ignore additional parameters. This class already exists. # We should get back the first class we created, rather than a new one. badge_class = badge_service.get_badge_class( slug='test_slug', issuing_component='test_component', description='Attempted override', criteria='test', display_name='Testola', image_file_handle=get_image('good') ) # These defaults are set on the factory. self.assertEqual(badge_class.criteria, 'https://example.com/syllabus') self.assertEqual(badge_class.display_name, 'Test Badge') self.assertEqual(badge_class.description, "Yay! It's a test badge.") # File name won't always be the same. self.assertEqual(badge_class.image.path, premade_badge_class.image.path)
def test_get_badge_class_preexisting(self): """ Verify fetching a badge first grabs existing badges. """ premade_badge_class = BadgeClassFactory.create() # Ignore additional parameters. This class already exists. badge_class = BadgeClass.get_badge_class( slug='test_slug', issuing_component='test_component', description='Attempted override', criteria='test', display_name='Testola', image_file_handle=get_image('good') ) # These defaults are set on the factory. self.assertEqual(badge_class.criteria, 'https://example.com/syllabus') self.assertEqual(badge_class.display_name, 'Test Badge') self.assertEqual(badge_class.description, "Yay! It's a test badge.") # File name won't always be the same. self.assertEqual(badge_class.image.path, premade_badge_class.image.path)
def test_unique_for_course(self): """ Verify that the course_id is used in fetching existing badges or creating new ones. """ course_key = CourseFactory.create().location.course_key premade_badge_class = BadgeClassFactory.create(course_id=course_key) badge_class = BadgeClass.get_badge_class( slug='test_slug', issuing_component='test_component', description='Attempted override', criteria='test', display_name='Testola', image_file_handle=get_image('good') ) course_badge_class = BadgeClass.get_badge_class( slug='test_slug', issuing_component='test_component', description='Attempted override', criteria='test', display_name='Testola', image_file_handle=get_image('good'), course_id=course_key, ) self.assertNotEqual(badge_class.id, course_badge_class.id) self.assertEqual(course_badge_class.id, premade_badge_class.id)
def test_get_assertions(self): """ Verify we can get assertions via the course_id and username. """ course_key = self.course.location.course_key badge_class = BadgeClassFactory.create(course_id=course_key) for dummy in range(3): BadgeAssertionFactory.create(user=self.user, badge_class=badge_class) # Should not be included, as they don't share the target badge class. for dummy in range(3): BadgeAssertionFactory.create(user=self.user) # Also should not be included, as they don't share the same user. for dummy in range(6): BadgeAssertionFactory.create(badge_class=badge_class) response = self.get_json(self.url(), data={'course_id': course_key}) self.assertEqual(len(response['results']), 3) unused_course = CourseFactory.create() response = self.get_json(self.url(), data={'course_id': unused_course.location.course_key}) self.assertEqual(len(response['results']), 0)
def test_get_assertions(self): """ Verify we can get assertions via the course_id and username. """ course_key = self.course.location.course_key badge_class = BadgeClassFactory.create(course_id=course_key) for dummy in range(3): BadgeAssertionFactory.create(user=self.user, badge_class=badge_class) # Should not be included, as they don't share the target badge class. for dummy in range(3): BadgeAssertionFactory.create(user=self.user) # Also should not be included, as they don't share the same user. for dummy in range(6): BadgeAssertionFactory.create(badge_class=badge_class) response = self.get_json(self.url(), data={'course_id': course_key}) # pylint: disable=no-member self.assertEqual(len(response['results']), 3) unused_course = CourseFactory.create() response = self.get_json( self.url(), data={'course_id': unused_course.location.course_key}) # pylint: disable=no-member self.assertEqual(len(response['results']), 0)
def test_get_assertions(self, check_course, wildcard): """ Verify we can get assertions via the badge class and username. """ badge_class = self.create_badge_class(check_course) for dummy in range(3): BadgeAssertionFactory.create(user=self.user, badge_class=badge_class) if badge_class.course_id: # Also create a version of this badge under a different course. alt_class = BadgeClassFactory.create( slug=badge_class.slug, issuing_component=badge_class.issuing_component, course_id=CourseFactory.create().location.course_key ) BadgeAssertionFactory.create(user=self.user, badge_class=alt_class) # Same badge class, but different user. Should not show up in the list. for dummy in range(5): BadgeAssertionFactory.create(badge_class=badge_class) # Different badge class AND different user. Certainly shouldn't show up in the list! for dummy in range(6): BadgeAssertionFactory.create() response = self.get_json( self.url(), data=self.get_qs_args(check_course, wildcard, badge_class), ) if wildcard: expected_length = 4 else: expected_length = 3 self.assertEqual(len(response['results']), expected_length) unused_class = self.create_badge_class(check_course, slug='unused_slug', issuing_component='unused_component') response = self.get_json( self.url(), data=self.get_qs_args(check_course, wildcard, unused_class), ) self.assertEqual(len(response['results']), 0)
def test_unique_for_course(self): """ Verify that the course_id is used in fetching existing badges or creating new ones. """ course_key = CourseFactory.create().location.course_key premade_badge_class = BadgeClassFactory.create(course_id=course_key) badge_class = BadgeClass.get_badge_class( slug='test_slug', issuing_component='test_component', description='Attempted override', criteria='test', display_name='Testola', image_file_handle=get_image('good')) course_badge_class = BadgeClass.get_badge_class( slug='test_slug', issuing_component='test_component', description='Attempted override', criteria='test', display_name='Testola', image_file_handle=get_image('good'), course_id=course_key, ) self.assertNotEqual(badge_class.id, course_badge_class.id) self.assertEqual(course_badge_class.id, premade_badge_class.id)