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(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(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(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(self.username) # verify that only one request has been made self.assertEqual(len(httpretty.httpretty.latest_requests), 1)
def get_program_certificate_context(self, user_credential): """ Get the program certificate related data from database. Arguments: user_credential (User): UserCredential object Returns: dict, representing a data returned by the Program service, lms service and template path. """ programs_data = self._get_program_data(user_credential.credential.program_id) organization_data = get_organization(programs_data['organization_key']) organization_name = organization_data['short_name'] if user_credential.credential.use_org_name: organization_name = organization_data['name'] return { 'credential_type': _(u'XSeries Certificate'), 'credential_title': user_credential.credential.title, 'user_data': get_user(user_credential.username), 'programs_data': programs_data, 'organization_data': organization_data, 'organization_name': organization_name, '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(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)