Пример #1
0
    def test_delete_account_basic(self):
        """
        Test delete_account()

        Posts and comments: Ensures that all data related to posting and
        commenting is removed
        """
        # Create test user
        user1 = create_user('user1', '*****@*****.**', 'Password')
        self.assertIsNotNone(user1)
        # Lets just delete a fresh account
        delete_account(user1)
        # Lets check that has got rid of everything
        self.assertIsNone(get_user(user1))
        # Ensure the user can not be looked up
        self.assertIsNone(get_uid_username('user1'))
        self.assertIsNone(get_uid_email('*****@*****.**'))
        # Ensure the underlying Redis is correct
        # Ensure the user account has gone
        self.assertIsNone(r.get(K.USER.format(user1)))
        # Ensure the username maps to -1
        self.assertEqual(r.get(K.UID_USERNAME.format('user1')), K.NIL_VALUE)
        # Ensure the usernames TTL has been set
        self.assertNotEqual(int(r.ttl(K.UID_USERNAME.format('user1'))), -1)
        # Ensure the email maps to -1
        self.assertEqual(
            r.get(K.UID_EMAIL.format('*****@*****.**')), K.NIL_VALUE)
        # Ensure the email TTL has been set
        self.assertNotEqual(
            int(r.ttl(K.UID_EMAIL.format('*****@*****.**'))), -1)
        # Try and authenticate a user now that it has been deleted.
        # I have seen this cause issues during development
        self.assertFalse(authenticate('user1', 'Password'))
        self.assertIsNone(get_uid_username('user1'))
        self.assertIsNone(get_uid_email('*****@*****.**'))
Пример #2
0
def check_token(tid, preserve=False):
    """Look up token with tid and return the data which was stored or None.

    """
    # Stop malformed tokens making calls to Redis
    if not TOKEN_RE.match(tid):
        return None

    # Get the pickled data from Redis
    data = r.get(K.TOKEN.format(tid))

    if data:
        try:
            # Attempt to get the pickled object back
            data = jsonpickle.decode(data)
            return data
        except (TypeError, ValueError):
            # There was a problem pulling the data out of Redis.
            return None
        finally:
            # What the actual f**k coverage? It says this is a partial yet
            # there is a specific test for this. Must be something to do with
            # the finally block. The else is POINTLESS but works.
            if not preserve:
                # Delete the token if preserve is False
                r.delete(K.TOKEN.format(tid))
            else:
                pass

    # If we didn't get data in the first place no need to delete.
    return None
Пример #3
0
def check_token(tid, preserve=False):
    """Look up token with tid and return the data which was stored or None.

    """
    # Stop malformed tokens making calls to Redis
    if not TOKEN_RE.match(tid):
        return None

    # Get the pickled data from Redis
    data = r.get(K.TOKEN.format(tid))

    if data:
        try:
            # Attempt to get the pickled object back
            data = jsonpickle.decode(data)
            return data
        except (TypeError, ValueError):
            # There was a problem pulling the data out of Redis.
            return None
        finally:
            # What the actual f**k coverage? It says this is a partial yet
            # there is a specific test for this. Must be something to do with
            # the finally block. The else is POINTLESS but works.
            if not preserve:
                # Delete the token if preserve is False
                r.delete(K.TOKEN.format(tid))
            else:
                pass

    # If we didn't get data in the first place no need to delete.
    return None
Пример #4
0
def check_token(tid, preserve=False):
    """Look up token with tid and return the data which was stored or None.

    """
    # Stop malformed tokens making calls to Redis
    if not TOKEN_RE.match(tid):
        return None

    # Get the pickled data from Redis
    data = r.get(K.TOKEN.format(tid))

    if data:
        try:
            # Attempt to get the pickled object back
            data = jsonpickle.decode(data)
            result = data
        except (TypeError, ValueError):
            # There was a problem pulling the data out of Redis.
            result = None
        finally:
            if not preserve:
                # Delete the token if preserve is False
                r.delete(K.TOKEN.format(tid))
        return result

    # If we didn't get data in the first place no need to delete.
    return None
Пример #5
0
def get_uid_email(email):
    """Get the uid for user with email.

    :param username: The email to lookup
    :type username: str
    :returns: The users UID
    :rtype: str or None

    """
    # Attemp to get a uid from Redis with a lowercase email
    uid = r.get(K.UID_EMAIL.format(email.lower()))

    # Check that something was returned and it was not our defined None value
    if uid is not None and uid != K.NIL_VALUE:
        return uid

    return None
Пример #6
0
def get_uid_username(username):
    """Get the uid for user with username.

    :param username: The username to lookup
    :type username: str
    :returns: The users UID
    :rtype: str or None

    """
    # Attempt to get a uid from Redis with a lowercase username
    uid = r.get(K.UID_USERNAME.format(username.lower()))

    # Check that something was returned and it was not our defined None value
    if uid is not None and uid != K.NIL_VALUE:
        return uid

    return None
Пример #7
0
    def get(self, aid):
        """Attempts to load an Alert from Redis and unpickle it.

        """
        # Try the unpickling process
        try:
            pickled_alert = r.get(k.ALERT.format(aid))
            alert = jsonpickle.decode(pickled_alert)
        except (TypeError, ValueError):
            # We failed to get an alert for whateva reason
            return None

        # Ensure we got an alert and that it verifies.
        if alert.verify():
            # Return the alert object
            return alert
        else:
            # If the alert did not verify delete it
            # This will stop this always being called
            r.delete(k.ALERT.format(aid))
            return None
Пример #8
0
 def test_change_email(self):
     """
     Test change_email().
     """
     # Create user
     user1 = create_user('user1', '*****@*****.**', 'Password')
     # Test email lookup key
     self.assertEqual(get_uid_email('*****@*****.**'), user1)
     # Check correct email
     self.assertEqual(get_email(user1), '*****@*****.**')
     # Change e-mail
     self.assertIsNotNone(change_email(user1, '*****@*****.**'))
     # Check new lookup key
     self.assertEqual(get_uid_email('*****@*****.**'), user1)
     # Check old lookup key has been nulled
     self.assertIsNone(get_uid_email('*****@*****.**'))
     # Check the old key is set to NIL_VALUE and the expiration has been set
     self.assertEqual(
         r.get(K.UID_EMAIL.format('*****@*****.**')), K.NIL_VALUE)
     self.assertNotEqual(
         int(r.ttl(K.UID_EMAIL.format('*****@*****.**'))), -1)
Пример #9
0
    def get(self, aid):
        """Attempts to load an Alert from Redis and unpickle it.

        """
        # Try the unpickling process
        try:
            pickled_alert = r.get(k.ALERT.format(aid))
            alert = jsonpickle.decode(pickled_alert)
        except (TypeError, ValueError):
            # We failed to get an alert for whateva reason
            return None

        # Ensure we got an alert and that it verifies.
        if alert.verify():
            # Return the alert object
            return alert
        else:
            # If the alert did not verify delete it
            # This will stop this always being called
            r.delete(k.ALERT.format(aid))
            return None