def test_get_user_client_initialization_failure(self, mock_init): """ Verify that the function 'get_user' raises exception when API client fails to initialize. """ mock_init.side_effect = Exception with self.assertRaises(Exception): get_user_data(self.username)
def test_get_user_data_retrieval_failure(self): """ Verify that the data can't be retrieved from User API if there is a server error. """ self.mock_user_api_500(username=self.username) with self.assertRaises(exceptions.HttpServerError): get_user_data(self.username)
def test_get_user_with_no_data(self): """ Verify that the function 'get_user' raises exception if the User API gives 404. """ username = '******' self.mock_user_api_404(username=username) with self.assertRaises(exceptions.HttpNotFoundError): get_user_data(username)
def test_get_user_caching(self): """ Verify that when the value is set, the cache is used for getting a user. """ self.mock_user_api(username=self.username) # hit the Organizations API twice with the test org for _ in range(2): get_user_data(self.username) # verify that only one request has been made self.assertEqual(len(httpretty.httpretty.latest_requests), 1)
def get_certificate_context(self, user_credential): """ Returns the context data necessary to render the certificate. Arguments: user_credential (UserCredential): UserCredential being rendered Returns: dict, representing a data returned by the Program service, lms service and template path. """ program_details = user_credential.credential.program_details # pylint: disable=no-member return { 'credential_type': _('{program_type} Certificate').format( program_type=program_details.type), 'credential_title': user_credential.credential.title, 'user_data': get_user_data(user_credential.username), 'program_details': program_details, 'credential_template': 'credentials/program_certificate.html', }
def test_get_user(self): """ Verify that the user data can be retrieved. """ self.mock_user_api(username=self.username) actual_user_api_response = get_user_data(self.username) self.assertEqual( actual_user_api_response, self.USER_API_RESPONSE ) # verify the API was actually hit (not the cache) self.assertEqual(len(httpretty.httpretty.latest_requests), 1)