def test_type_filter(self, mock_get_edx_api_data): get_credentials(self.user, credential_type='program') mock_get_edx_api_data.assert_called_once() call = mock_get_edx_api_data.mock_calls[0] __, __, kwargs = call querystring = { 'username': self.user.username, 'status': 'awarded', 'type': 'program', } self.assertEqual(kwargs['querystring'], querystring)
def test_type_filter(self, mock_get_edx_api_data): get_credentials(self.user, credential_type='program') mock_get_edx_api_data.assert_called_once() call = mock_get_edx_api_data.mock_calls[0] __, __, kwargs = call querystring = { 'username': self.user.username, 'status': 'awarded', 'type': 'program', } self.assertEqual(kwargs['querystring'], querystring)
def test_get_credentials(self): """Verify user credentials data can be retrieve.""" self.create_credentials_config() self.mock_credentials_api(self.user) actual = get_credentials(self.user) self.assertEqual(actual, self.CREDENTIALS_API_RESPONSE['results'])
def get_certificates(user, extended_program): """ Find certificates a user has earned related to a given program. Arguments: user (User): The user whose enrollments to inspect. extended_program (dict): The program for which to locate certificates. This is expected to be an "extended" program whose course runs already have certificate URLs attached. Returns: list: Contains dicts representing course run and program certificates the given user has earned which are associated with the given program. """ certificates = [] for course in extended_program['courses']: for course_run in course['course_runs']: url = course_run.get('certificate_url') if url: certificates.append({ 'type': 'course', 'title': course_run['title'], 'url': url, }) # We only want one certificate per course to be returned. break # A user can only have earned a program certificate if they've earned certificates # in associated course runs. If they haven't earned any course run certificates, # they can't have earned a program certificate, and we can save a network call # to the credentials service. if certificates: program_credentials = get_credentials( user, program_uuid=extended_program['uuid']) if program_credentials: certificates.append({ 'type': 'program', 'title': extended_program['title'], 'url': program_credentials[0]['certificate_url'], }) return certificates
def get_certificates(user, extended_program): """ Find certificates a user has earned related to a given program. Arguments: user (User): The user whose enrollments to inspect. extended_program (dict): The program for which to locate certificates. This is expected to be an "extended" program whose course runs already have certificate URLs attached. Returns: list: Contains dicts representing course run and program certificates the given user has earned which are associated with the given program. """ certificates = [] for course in extended_program['courses']: for course_run in course['course_runs']: url = course_run.get('certificate_url') if url and course_run.get('may_certify'): certificates.append({ 'type': 'course', 'title': course_run['title'], 'url': url, }) # We only want one certificate per course to be returned. break program_credentials = get_credentials( user, program_uuid=extended_program['uuid'], credential_type='program') # only include a program certificate if a certificate is available for every course if program_credentials and (len(certificates) == len( extended_program['courses'])): enabled_force_program_cert_auth = configuration_helpers.get_value( 'force_program_cert_auth', True) cert_url = program_credentials[0]['certificate_url'] url = get_logged_in_program_certificate_url( cert_url) if enabled_force_program_cert_auth else cert_url certificates.append({ 'type': 'program', 'title': extended_program['title'], 'url': url, }) return certificates
def test_get_many(self, mock_get_edx_api_data): expected = factories.UserCredential.create_batch(3) mock_get_edx_api_data.return_value = expected actual = get_credentials(self.user) mock_get_edx_api_data.assert_called_once() call = mock_get_edx_api_data.mock_calls[0] __, __, kwargs = call querystring = { 'username': self.user.username, 'status': 'awarded', } self.assertEqual(kwargs['querystring'], querystring) self.assertEqual(actual, expected)
def get_certificates(user, extended_program): """ Find certificates a user has earned related to a given program. Arguments: user (User): The user whose enrollments to inspect. extended_program (dict): The program for which to locate certificates. This is expected to be an "extended" program whose course runs already have certificate URLs attached. Returns: list: Contains dicts representing course run and program certificates the given user has earned which are associated with the given program. """ certificates = [] for course in extended_program['courses']: for course_run in course['course_runs']: url = course_run.get('certificate_url') if url and course_run.get('may_certify'): certificates.append({ 'type': 'course', 'title': course_run['title'], 'url': url, }) # We only want one certificate per course to be returned. break program_credentials = get_credentials(user, program_uuid=extended_program['uuid'], credential_type='program') # only include a program certificate if a certificate is available for every course if program_credentials and (len(certificates) == len(extended_program['courses'])): enabled_force_program_cert_auth = configuration_helpers.get_value( 'force_program_cert_auth', True ) cert_url = program_credentials[0]['certificate_url'] url = get_logged_in_program_certificate_url(cert_url) if enabled_force_program_cert_auth else cert_url certificates.append({ 'type': 'program', 'title': extended_program['title'], 'url': url, }) return certificates
def get_certified_programs(student): """ Find the UUIDs of all the programs for which the student has already been awarded a certificate. Args: student: User object representing the student Returns: str[]: UUIDs of the programs for which the student has been awarded a certificate """ certified_programs = [] for credential in get_credentials(student, credential_type='program'): certified_programs.append(credential['credential']['program_uuid']) return certified_programs
def get_certified_programs(student): """ Find the UUIDs of all the programs for which the student has already been awarded a certificate. Args: student: User object representing the student Returns: str[]: UUIDs of the programs for which the student has been awarded a certificate """ certified_programs = [] for credential in get_credentials(student, credential_type='program'): certified_programs.append(credential['credential']['program_uuid']) return certified_programs
def test_get_one(self, mock_get_edx_api_data): expected = factories.UserCredential() mock_get_edx_api_data.return_value = expected program_uuid = str(uuid.uuid4()) actual = get_credentials(self.user, program_uuid=program_uuid) mock_get_edx_api_data.assert_called_once() call = mock_get_edx_api_data.mock_calls[0] __, __, kwargs = call querystring = { 'username': self.user.username, 'status': 'awarded', 'program_uuid': program_uuid, } self.assertEqual(kwargs['querystring'], querystring) self.assertEqual(actual, expected)
def test_get_many(self, mock_get_edx_api_data): expected = factories.UserCredential.create_batch(3) mock_get_edx_api_data.return_value = expected actual = get_credentials(self.user) mock_get_edx_api_data.assert_called_once() call = mock_get_edx_api_data.mock_calls[0] __, __, kwargs = call querystring = { 'username': self.user.username, 'status': 'awarded', } cache_key = '{}.{}'.format(self.credentials_config.CACHE_KEY, self.user.username) self.assertEqual(kwargs['querystring'], querystring) self.assertEqual(kwargs['cache_key'], cache_key) self.assertEqual(actual, expected)
def test_get_many(self, mock_get_edx_api_data): expected = factories.UserCredential.create_batch(3) mock_get_edx_api_data.return_value = expected actual = get_credentials(self.user) mock_get_edx_api_data.assert_called_once() call = mock_get_edx_api_data.mock_calls[0] __, __, kwargs = call querystring = { 'username': self.user.username, 'status': 'awarded', 'only_visible': 'True', } cache_key = '{}.{}'.format(self.credentials_config.CACHE_KEY, self.user.username) self.assertEqual(kwargs['querystring'], querystring) self.assertEqual(kwargs['cache_key'], cache_key) self.assertEqual(actual, expected)
def test_get_many(self, mock_get_edx_api_data): expected = factories.UserCredential.create_batch(3) mock_get_edx_api_data.return_value = expected actual = get_credentials(self.user) mock_get_edx_api_data.assert_called_once() call = mock_get_edx_api_data.mock_calls[0] __, __, kwargs = call querystring = { 'username': self.user.username, 'status': 'awarded', 'only_visible': 'True', } cache_key = f'{self.credentials_config.CACHE_KEY}.{self.user.username}' assert kwargs['querystring'] == querystring assert kwargs['cache_key'] == cache_key assert actual == expected
def test_get_one(self, mock_get_edx_api_data): expected = factories.UserCredential() mock_get_edx_api_data.return_value = expected program_uuid = str(uuid.uuid4()) actual = get_credentials(self.user, program_uuid=program_uuid) mock_get_edx_api_data.assert_called_once() call = mock_get_edx_api_data.mock_calls[0] __, __, kwargs = call querystring = { 'username': self.user.username, 'status': 'awarded', 'program_uuid': program_uuid, } cache_key = '{}.{}.{}'.format(self.credentials_config.CACHE_KEY, self.user.username, program_uuid) self.assertEqual(kwargs['querystring'], querystring) self.assertEqual(kwargs['cache_key'], cache_key) self.assertEqual(actual, expected)
def test_get_one(self, mock_get_edx_api_data): expected = factories.UserCredential() mock_get_edx_api_data.return_value = expected program_uuid = str(uuid.uuid4()) actual = get_credentials(self.user, program_uuid=program_uuid) mock_get_edx_api_data.assert_called_once() call = mock_get_edx_api_data.mock_calls[0] __, __, kwargs = call querystring = { 'username': self.user.username, 'status': 'awarded', 'only_visible': 'True', 'program_uuid': program_uuid, } cache_key = f'{self.credentials_config.CACHE_KEY}.{self.user.username}.{program_uuid}' assert kwargs['querystring'] == querystring assert kwargs['cache_key'] == cache_key assert actual == expected
def get_certificates(user, extended_program): """ Find certificates a user has earned related to a given program. Arguments: user (User): The user whose enrollments to inspect. extended_program (dict): The program for which to locate certificates. This is expected to be an "extended" program whose course runs already have certificate URLs attached. Returns: list: Contains dicts representing course run and program certificates the given user has earned which are associated with the given program. """ certificates = [] for course in extended_program['courses']: for course_run in course['course_runs']: url = course_run.get('certificate_url') if url: certificates.append({ 'type': 'course', 'title': course_run['title'], 'url': url, }) # We only want one certificate per course to be returned. break program_credentials = get_credentials(user, program_uuid=extended_program['uuid']) if program_credentials: certificates.append({ 'type': 'program', 'title': extended_program['title'], 'url': program_credentials[0]['certificate_url'], }) return certificates
def test_get_credentials_caching(self): """Verify that when enabled, the cache is used for non-staff users.""" self.create_credentials_config(cache_ttl=1) self.mock_credentials_api(self.user) # Warm up the cache. get_credentials(self.user) # Hit the cache. get_credentials(self.user) # Verify only one request was made. self.assertEqual(len(httpretty.httpretty.latest_requests), 1) staff_user = UserFactory(is_staff=True) # Hit the Credentials API twice. for _ in range(2): get_credentials(staff_user) # Verify that three requests have been made (one for student, two for staff). self.assertEqual(len(httpretty.httpretty.latest_requests), 3)