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. assert badge_class.criteria == 'https://example.com/syllabus' assert badge_class.display_name == 'Test Badge' assert badge_class.description == "Yay! It's a test badge." # File name won't always be the same. assert badge_class.image.path == premade_badge_class.image.path
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_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_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': str(course_key)}) self.assertEqual(len(response['results']), 3) unused_course = CourseFactory.create() response = self.get_json( self.url(), data={'course_id': str(unused_course.location.course_key)}) 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, ) assert badge_class.id != course_badge_class.id assert course_badge_class.id == premade_badge_class.id