def test_add_and_pop(self):
        '''Adds a password and pops it again.'''
        database = MemcachedDatabase()

        database.add_password("test_add_and_pop_8341")

        database.pop_password("test_add_and_pop_8341")
    def test_double_pop(self):
        '''Tries to pop a password twice. Should raise an exception.'''
        database = MemcachedDatabase()

        database.add_password("test_double_pop_9864")

        database.pop_password("test_double_pop_9864")

        with self.assertRaises(InvalidPasswordError):
            database.pop_password("test_double_pop_9864")
Exemplo n.º 3
0
class Authenticator:

    def __init__(self):
        self._database = MemcachedDatabase()
        self._configs = Configs()

    def authenticate_robot(self, robot_object, password):
        '''Authenticate the robot access and its password.'''

        # Ensuring that password is a string.
        if not isinstance(password, str):
            raise AuthenticationFailedError("Wrong password for Robot {0}".format(robot_object.get_id()))

        if password != robot_object.get_password():
            raise AuthenticationFailedError("Wrong password for Robot {0}".format(robot_object.get_id()))

        if not robot_object.get_alive():
            raise AuthenticationFailedError("Robot {0} is dead!".format(robot_object.get_id()))

    def authenticate_new_robot(self, password):
        '''Authenticate if this password is valid for a new robot to join the game
        (e.g. born).
        It remove the password from the database. i.e. the password can use for
        only one born.

        @raise InvalidPasswordError: If password wasn't valid.
        '''
        self._database.pop_password(password)

    def authenticate_admin(self, password):
        '''Authenticates an admin. Admins are the ones who can see
        things like world statistics.
        '''
        admin_password = self._configs.get_server_admin_password()

        if admin_password is None or admin_password.isspace():
            raise AuthenticationFailedError("Invalid Admin password.")

        if not isinstance(password, str):
            raise AuthenticationFailedError("Invalid Admin password.")

        if admin_password != password:
            raise AuthenticationFailedError("Invalid Admin password.")
    def test_not_exist_password(self):
        '''Tries to pop a password that does not exists. Should raise an exception.'''
        database = MemcachedDatabase()

        with self.assertRaises(InvalidPasswordError):
            database.pop_password("not_exist_password_1873")