def _validate_password(self, password): """Validate GNTP Message against stored password""" self.password = password if password is None: raise errors.AuthError('Missing password') keyHash = self.info.get('keyHash', None) if keyHash is None and self.password is None: return True if keyHash is None: raise errors.AuthError('Invalid keyHash') if self.password is None: raise errors.AuthError('Missing password') keyHashAlgorithmID = self.info.get('keyHashAlgorithmID', 'MD5') password = self.password.encode('utf8') saltHash = self._decode_hex(self.info['salt']) keyBasis = password + saltHash self.key = self.hash_algo[keyHashAlgorithmID](keyBasis).digest() keyHash = self.hash_algo[keyHashAlgorithmID](self.key).hexdigest() if not keyHash.upper() == self.info['keyHash'].upper(): raise errors.AuthError('Invalid Hash') return True
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 _validate_password(self, password): """Validate GNTP Message against stored password""" self.password = password if password is None: raise errors.AuthError('Missing password') keyHash = self.info.get('keyHash', None) if keyHash is None and self.password is None: return True if keyHash is None: raise errors.AuthError('Invalid keyHash') if self.password is None: raise errors.AuthError('Missing password') hash_algo = { 'MD5': hashlib.md5, 'SHA1': hashlib.sha1, 'SHA256': hashlib.sha256, 'SHA512': hashlib.sha512, } if not self.info['keyHashAlgorithmID'] in hash_algo: raise errors.UnsupportedError('INVALID HASH "%s"' % self.encryptAlgo) hashfunction = hash_algo.get(self.info['keyHashAlgorithmID']) password = self.password.encode('utf8') saltHash = self._decode_hex(self.info['salt']) keyBasis = password + saltHash key = hashfunction(keyBasis).digest() keyHash = hashfunction(key).hexdigest() if not keyHash.upper() == self.info['keyHash'].upper(): raise errors.AuthError('Invalid Hash') return True
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # 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")