def test_post_notification_HTTPError(self, mock_post): mock_post.return_value.raise_for_status.side_effect = req_exc.HTTPError( 'hello') notification_status, notification_error = post_notification( None, None, None) self.assertEqual(notification_status, Constants.FAILURE) self.assertEqual(notification_error, 'hello')
def test_post_notification_RequestException(self, mock_post): mock_post.return_value.raise_for_status.side_effect = req_exc.Timeout( 'hello') notification_status, notification_error = post_notification( None, None, None) self.assertEqual(notification_status, Constants.PENDING) self.assertEqual(notification_error, 'hello')
def test_post_notification_HTTPError_408(self, mock_post): mock_post.return_value.raise_for_status.side_effect = req_exc.HTTPError( 'hello') mock_post.return_value.status_code = 408 notification_status, notification_error = post_notification( None, None, None) self.assertEqual(notification_status, Constants.PENDING) self.assertEqual(notification_error, 'hello')
def send_notification(conf): ''' Post the status and any errors of the current completed UDL job referenced by guid_batch to the client via callback_url :param conf: Notification task configuration :return: Notification status and any error messages ''' email_notification_error = None notification_json = conf.get(UdlStatsConstants.NOTIFICATION) notification_conf = json.loads(notification_json if notification_json is not None else '{}') notification = notification_conf if notification_conf is not None and notification_conf else {} guid_batch = conf.get(UdlStatsConstants.BATCH_GUID) call_back = notification.get(Constants.CALLBACK_URL) emailnotification = notification_conf.get(Constants.EMAILNOTIFICATION) mail_server = conf.get(Constants.MAIL_SERVER) mail_sender = conf.get(Constants.MAIL_SENDER) load_status = conf.get(UdlStatsConstants.LOAD_STATUS) load_type = conf.get(UdlStatsConstants.LOAD_TYPE) if load_status is not None: notification_body = create_notification_body(load_type, guid_batch, 'udl_batch' if load_status.startswith('udl') else 'migrate', notification.get(Constants.STUDENT_REG_GUID), notification.get(Constants.REG_SYSTEM_ID), notification.get(Constants.TOTAL_ROWS_LOADED, 0), load_status, notification.get(Constants.UDL_PHASE), notification.get(Constants.ERROR_DESC), notification.get('tsb_error')) notification_status = {} call_back_timestamp = '' email_timestamp = '' callback_notification_status = None callback_notification_error = None email_notification_error = None if call_back is not None: ts = time.time() call_back_timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') callback_notification_status, callback_notification_error = post_notification(call_back, notification.get(Constants.NOTIFICATION_TIMEOUT_INTERVAL), notification_body) notification_status['call_back'] = {'timestamp': call_back_timestamp, 'status': callback_notification_status, 'error': callback_notification_error} if emailnotification is not None: try: subject = 'Data Load - ' + notification_body['status'] + ' - Batch ' + guid_batch send_notification_email(mail_server, mail_sender, emailnotification, subject, notification_body) email_notification_error = Constants.SUCCESS except: email_notification_error = Constants.FAILURE ts = time.time() email_timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') notification_status['email'] = {'timestamp': email_timestamp, 'status': email_notification_error} update_udl_stats_by_batch_guid(guid_batch, {UdlStatsConstants.NOTIFICATION_STATUS: json.dumps(notification_status)})
def test_post_notification_failure(self): # Create the notification request body. notification_body = {'status': Constants.SUCCESS, 'id': 'aaa-bbb-ccc', 'testRegistrationId': '111-222-333', 'message': ''} # Send the status. callback_url = self.register_url([401]) notification_status, notification_error = post_notification(callback_url, 1, notification_body) # Verify results. self.assertEquals('401 Client Error: Unauthorized', notification_error) self.assertEquals(Constants.FAILURE, notification_status)
def test_post_notification_pending(self): # Create the notification request body. notification_body = {'status': Constants.SUCCESS, 'id': 'aaa-bbb-ccc', 'testRegistrationId': '111-222-333', 'message': ''} # Send the status. callback_url = self.register_url([408]) notification_status, notification_error = post_notification(callback_url, 1, notification_body) # Verify results. self.assertEquals(Constants.PENDING, notification_status) self.assertEquals('408 Client Error: Request a Timeout', notification_error)
def test_post_notification_success_no_retries(self): # Create the notification request body. notification_body = {'status': Constants.SUCCESS, 'id': 'aaa-bbb-ccc', 'testRegistrationId': '111-222-333', 'message': ''} # Send the status. callback_url = self.register_url([201]) notification_status, notification_error = post_notification(callback_url, 1, notification_body) # Verify results. self.assertEquals(Constants.SUCCESS, notification_status) self.assertEquals(None, notification_error)
def test_post_notification_pending_connection_error(self): # Create the notification request body. notification_body = {'status': Constants.SUCCESS, 'id': 'aaa-bbb-ccc', 'testRegistrationId': '111-222-333', 'message': ''} # Send the status. callback_url = 'http://SomeBogusurl/SomeBogusEndpoint' notification_status, notification_error = post_notification(callback_url, 1, notification_body) # Verify results. self.assertEquals(Constants.PENDING, notification_status) self.assertRegex(notification_error, "Max retries exceeded with url")
def test_post_notification_success_no_retries(self): # Create the notification request body. notification_body = { 'status': Constants.SUCCESS, 'id': 'aaa-bbb-ccc', 'testRegistrationId': '111-222-333', 'message': '' } # Send the status. callback_url = self.register_url([201]) notification_status, notification_error = post_notification( callback_url, 1, notification_body) # Verify results. self.assertEquals(Constants.SUCCESS, notification_status) self.assertEquals(None, notification_error)
def test_post_notification_pending_connection_error(self): # Create the notification request body. notification_body = { 'status': Constants.SUCCESS, 'id': 'aaa-bbb-ccc', 'testRegistrationId': '111-222-333', 'message': '' } # Send the status. callback_url = 'http://SomeBogusurl/SomeBogusEndpoint' notification_status, notification_error = post_notification( callback_url, 1, notification_body) # Verify results. self.assertEquals(Constants.PENDING, notification_status) self.assertRegex(notification_error, "Max retries exceeded with url")
def test_post_notification_failure(self): # Create the notification request body. notification_body = { 'status': Constants.SUCCESS, 'id': 'aaa-bbb-ccc', 'testRegistrationId': '111-222-333', 'message': '' } # Send the status. callback_url = self.register_url([401]) notification_status, notification_error = post_notification( callback_url, 1, notification_body) # Verify results. self.assertEquals('401 Client Error: Unauthorized', notification_error) self.assertEquals(Constants.FAILURE, notification_status)
def test_post_notification_pending(self): # Create the notification request body. notification_body = { 'status': Constants.SUCCESS, 'id': 'aaa-bbb-ccc', 'testRegistrationId': '111-222-333', 'message': '' } # Send the status. callback_url = self.register_url([408]) notification_status, notification_error = post_notification( callback_url, 1, notification_body) # Verify results. self.assertEquals(Constants.PENDING, notification_status) self.assertEquals('408 Client Error: Request a Timeout', notification_error)
def test_post_notification_RequestException(self, mock_post): mock_post.return_value.raise_for_status.side_effect = req_exc.Timeout('hello') notification_status, notification_error = post_notification(None, None, None) self.assertEqual(notification_status, Constants.PENDING) self.assertEqual(notification_error, 'hello')
def test_post_notification_HTTPError(self, mock_post): mock_post.return_value.raise_for_status.side_effect = req_exc.HTTPError('hello') notification_status, notification_error = post_notification(None, None, None) self.assertEqual(notification_status, Constants.FAILURE) self.assertEqual(notification_error, 'hello')
def test_post_notification_HTTPError_408(self, mock_post): mock_post.return_value.raise_for_status.side_effect = req_exc.HTTPError('hello') mock_post.return_value.status_code = 408 notification_status, notification_error = post_notification(None, None, None) self.assertEqual(notification_status, Constants.PENDING) self.assertEqual(notification_error, 'hello')