def test_handle_role_with_timeout(self, mock_post): mock_post.side_effect = Timeout(self.error_msg) with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): with self.assertRaises(ApplicationError) as context: AccountAPI.handle_role(self, { 'groups': { 'nps_sample': True }, 'ldap_id': '11-22' }) self.assertTrue(ApplicationError in str(context.exception)) self.assertEqual( context.exception.message, 'Connection to account_api timed out: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E711') self.assertEqual(context.exception.http_code, 500)
def test_handle_role_http_error(self, mock_post): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): mock_post.side_effect = HTTPError(self.error_msg) with self.assertRaises(ApplicationError) as context: AccountAPI.handle_role(self, { 'groups': { 'nps_sample': True }, 'ldap_id': '11-22' }) self.assertEqual( context.exception.message, 'Received the following response from account_api: {}'. format(self.error_msg)) self.assertEqual(context.exception.code, 'E709') self.assertEqual(context.exception.http_code, 500)
def test_handle_role(self, mock_post): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): mock_post.return_value.text = 'Success' mock_post.return_value.status_code = 200 response = AccountAPI.handle_role(self, { 'groups': { 'nps_sample': True }, 'ldap_id': '11-22' }) assert response == {'message': 'success'}
def _handle_ldap_group(licence, ldap_id, agreed): account = AccountAPI() payload = {'ldap_id': ldap_id, 'groups': {licence: agreed}} return account.handle_role(payload)