def alert(self, alert, user_ids): """Will attempt to alert the user with uid to the alert being managed. This will call the alerts before_alert() method, which allows you to change the alert per user. It's not needed though. """ # Check that the manager actually has an alert if not isinstance(alert, BaseAlert): raise ValueError('alert must be a BaseAlert object') # Ensure uids is iterable # Stopped strings being passed in if not isinstance(user_ids, Iterable) or isinstance(user_ids, str) or \ isinstance(user_ids, unicode): raise TypeError('user_ids must be iterable') # Create the alert object r.set(k.ALERT.format(alert.alert_id), jsonpickle.encode(alert)) # Set the 4WK timeout on it r.expire(k.ALERT.format(alert.alert_id), k.EXPIRE_4WKS) for user_id in user_ids: r.zadd(k.USER_ALERTS.format(user_id), alert.timestamp, alert.alert_id)
def test_tokens(self): """Generate and check a few tokens, simple. """ # Test normal token operations token1 = generate_token("token1") self.assertEqual(check_token(token1), "token1") # Check that getting the token again returns nothing self.assertIsNone(check_token(token1)) # Create another token just this time check it initially with preserve token1 = generate_token("token1") self.assertEqual(check_token(token1, preserve=True), "token1") # Get it again with no preserve and check we get the correct answer self.assertEqual(check_token(token1), "token1") # Try creating a token with some Python objects token1 = generate_token({"name": "token1"}) self.assertEqual(check_token(token1).get("name"), "token1") # A token with None stored would not work as the same outcome would # happen as if there was not a token token1 = generate_token(None) # POINTLESS! self.assertIsNone(check_token(token1)) # Try and break check tokens # Check a token that I just made up, not a hex UUID self.assertIsNone(check_token("token1")) # Create a token and mangle the data inside Redis token1 = generate_token("token1") # Not a valid JSON pickle, the dict is invalid r.set(k.TOKEN.format(token1), "{token: 1}") self.assertIsNone(check_token(token1)) # That will have raised our ValueError, I don't know how to trigger a # TypeError from Redis as everything is a string # Check that preserve on works on tokens token1 = generate_token("token1") self.assertEqual(check_token(token1, preserve=True), 'token1') self.assertEqual(check_token(token1), 'token1') self.assertIsNone(check_token(token1))