def validate(self): '''Validate required headers and validate notification headers''' for header in self._requiredHeaders: if not self.headers.get(header, False): raise errors.ParseError('Missing Registration Header: ' + header) for notice in self.notifications: for header in self._requiredNotificationHeaders: if not notice.get(header, False): raise errors.ParseError('Missing Notification Header: ' + header)
def _decode_binary(self, rawIdentifier, identifier): rawIdentifier += '\r\n\r\n' dataLength = int(identifier['Length']) pointerStart = self.raw.find(rawIdentifier) + len(rawIdentifier) pointerEnd = pointerStart + dataLength data = self.raw[pointerStart:pointerEnd] if not len(data) == dataLength: raise errors.ParseError('INVALID_DATA_LENGTH Expected: %s Recieved %s' % (dataLength, len(data))) return data
def parse_gntp(data, password=None): """Attempt to parse a message as a GNTP message :param string data: Message to be parsed :param string password: Optional password to be used to verify the message """ match = GNTP_INFO_LINE_SHORT.match(data) if not match: raise errors.ParseError('INVALID_GNTP_INFO') info = match.groupdict() if info['messagetype'] == 'REGISTER': return GNTPRegister(data, password=password) elif info['messagetype'] == 'NOTIFY': return GNTPNotice(data, password=password) elif info['messagetype'] == 'SUBSCRIBE': return GNTPSubscribe(data, password=password) elif info['messagetype'] == '-OK': return GNTPOK(data) elif info['messagetype'] == '-ERROR': return GNTPError(data) raise errors.ParseError('INVALID_GNTP_MESSAGE')
def test_plugin_growl_exception_handling(mock_gntp): """ NotifyGrowl() Exception Handling """ TEST_GROWL_EXCEPTIONS = ( errors.NetworkError(0, 'gntp.ParseError() not handled'), errors.AuthError(0, 'gntp.AuthError() not handled'), errors.ParseError(0, 'gntp.ParseError() not handled'), errors.UnsupportedError(0, 'gntp.UnsupportedError() not handled'), ) mock_notifier = mock.Mock() mock_gntp.return_value = mock_notifier mock_notifier.notify.return_value = True # First we test the growl.register() function for exception in TEST_GROWL_EXCEPTIONS: mock_notifier.register.side_effect = exception # instantiate our object obj = apprise.Apprise.instantiate('growl://growl.server.hostname', suppress_exceptions=False) # Verify Growl object was instantiated assert obj is not None # We will fail to send the notification because our registration # would have failed assert obj.notify(title='test', body='body', notify_type=apprise.NotifyType.INFO) is False # Now we test the growl.notify() function mock_notifier.register.side_effect = None for exception in TEST_GROWL_EXCEPTIONS: mock_notifier.notify.side_effect = exception # instantiate our object obj = apprise.Apprise.instantiate('growl://growl.server.hostname', suppress_exceptions=False) # Verify Growl object was instantiated assert obj is not None # We will fail to send the notification because of the underlining # notify() call throws an exception assert obj.notify(title='test', body='body', notify_type=apprise.NotifyType.INFO) is False
def _parse_info(self, data): """Parse the first line of a GNTP message to get security and other info values :param string data: GNTP Message :return dict: Parsed GNTP Info line """ match = GNTP_INFO_LINE.match(data) if not match: raise errors.ParseError('ERROR_PARSING_INFO_LINE') info = match.groupdict() if info['encryptionAlgorithmID'] == 'NONE': info['encryptionAlgorithmID'] = None return info
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. import sys import mock import six import pytest import apprise try: from gntp import errors TEST_GROWL_EXCEPTIONS = ( errors.NetworkError(0, 'gntp.ParseError() not handled'), errors.AuthError(0, 'gntp.AuthError() not handled'), errors.ParseError(0, 'gntp.ParseError() not handled'), errors.UnsupportedError(0, 'gntp.UnsupportedError() not handled'), ) except ImportError: # no problem; gntp isn't available to us pass # Disable logging for a cleaner testing output import logging logging.disable(logging.CRITICAL) @pytest.mark.skipif('gntp' in sys.modules, reason="Requires that gntp NOT be installed") def test_plugin_growl_gntp_import_error():
def validate(self): """Verify required headers""" for header in self._requiredHeaders: if not self.headers.get(header, False): raise errors.ParseError('Missing Notification Header: ' + header)