def test_get_organizations_client_initialization_failure(self, mock_init): """ Verify that the function 'get_organization' raises exception when API client fails to initialize. """ mock_init.side_effect = Exception with self.assertRaises(Exception): get_organization(self.organization_key)
def test_get_organization_data_retrieval_failure(self): """ Verify that the data can't be retrieved from Organizations API if there is a server error. """ self.mock_organizations_api_500(organization_key=self.organization_key) with self.assertRaises(exceptions.HttpServerError): get_organization(self.organization_key)
def test_get_organization_with_no_data(self): """ Verify that the function 'get_organization' raises exception if the Organizations API gives 404. """ organization_key = 'invalid-org-key' self.mock_organizations_api_404(organization_key) with self.assertRaises(exceptions.HttpNotFoundError): get_organization(organization_key)
def test_get_organization_caching(self): """ Verify that when the value is set, the cache is used for getting an organization. """ self.mock_organizations_api(organization_key=self.organization_key) # hit the Organizations API twice with the test org for _ in range(2): get_organization(self.organization_key) # 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_organization(self): """ Verify that the organization data can be retrieved. """ self.mock_organizations_api(organization_key=self.organization_key) actual_organizations_api_response = get_organization(self.organization_key) self.assertEqual( actual_organizations_api_response, self.ORGANIZATIONS_API_RESPONSE ) # verify the API was actually hit (not the cache) self.assertEqual(len(httpretty.httpretty.latest_requests), 1)