def register_user(self, username, password, email=None): if User.select().where(User.username==username).exists(): return False user = User(username=username, password=UserController.hash_password(password), email=email) user.save() return user
def add_user(username, password): '''Add a user to the database. If it already exists, delete it first. Also mark it for cleanup later.''' delete_user(username, password) User(username=username, password=password) session.commit()
def authenticate(self, username, password): '''Compares a username/password pair against that in the database. If they match, return True. Else, return False.''' # Do some database stuff here to verify the user. user = User.get(username=username) if not user: return False return UserController.check_password(plaintext=password, hashed=user.password)
def test_everything(self): '''This is a test version of the 'idealclient.py' file for ease of automation.''' # Make sure the server is up. response = self.fetch('/ping').body expected = 'pong' self.assertEqual(expected, response) # Insert our user. user = User.query.filter_by( username=settings.DEFAULT_USERNAME, password=settings.DEFAULT_PASSWORD).first() if not user: User(username=settings.DEFAULT_USERNAME, password=settings.DEFAULT_PASSWORD) session.commit() # Log in. data = { 'username': settings.DEFAULT_USERNAME, 'password': settings.DEFAULT_PASSWORD } response = self.fetch('/login', body=urlencode(data), method="POST") result = response.body expected = "Login successful." self.assertEqual(expected, result) cookie = response.headers['Set-Cookie'] self.assertTrue(cookie) headers = {'Cookie': cookie} # Get our characters. response = self.fetch('/characters', headers=headers) result = json.loads(response.body) self.assertTrue(len(result) > 0) character = result[0] # Get the zone our character is in: response = self.fetch('/zone/%s/zone' % character, headers=headers) result = json.loads(response.body) expected = {'zone': 'playerinstance-GhibliHills-%s' % (character, )} self.assertEqual(result, expected) zone = result['zone'] # Normally, the client would ask the masterzoneserver for the # url of the zone. This is not necessary for this test # since we already know where it is. # Get the zone's objects. response = self.fetch('/objects', headers=headers) self.fetch('/logout', headers=headers)
def create(self, character_name): logging.info("Creating a character named %s" % character_name) try: user = User.get(username=self.get_current_user()) except User.DoesNotExist: return if Character.select().where(Character.name==character_name).exists(): return False character = Character(user=user, name=character_name) character.save() logging.info("Created a character %s" % character) return character.name
def test_init(self): '''Initting a User should work.''' tests = {'username': '******', 'password':'******', 'email': '*****@*****.**'} with test_database(test_db, (User, Character)): result = User.create(username="******", password="******", email="*****@*****.**") character = Character.create(name="Char", user=result) for attr, value in tests.iteritems(): self.assertEqual(getattr(result, attr), value) self.assertEqual(result.characters[0], character)
def test_init(self): '''Initting a User should work.''' tests = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } with test_database(test_db, (User, Character)): result = User.create(username="******", password="******", email="*****@*****.**") character = Character.create(name="Char", user=result) for attr, value in tests.iteritems(): self.assertEqual(getattr(result, attr), value) self.assertEqual(result.characters[0], character)
def test___repr__(self): user = User(username="******", characters=[Character(name="The Enchanter")]) expected = '<User "Tim" owning character: The Enchanter.>' self.assertEqual(expected, user.__repr__())
def get_characters(self, username): '''Queries the database for all characters owned by a particular user.''' user = User.get(username=username) logging.info(user.characters) return [c.name for c in user.characters]
def get_characters(self, username): '''Queries the database for all characters owned by a particular user.''' user = User.get(username=username) logging.info(user.characters) return [c.name for c in user.characters] if __name__ == "__main__": handlers = [] handlers.append((r"/", lambda x, y: SimpleHandler(__doc__, x, y))) handlers.append((r"/register", RegistrationHandler)) handlers.append((r"/login", AuthHandler)) handlers.append((r"/logout", LogoutHandler)) handlers.append((r"/characters", CharacterHandler)) server = BaseServer(handlers) server.listen(settings.AUTHSERVERPORT) try: user = User.get(username=settings.DEFAULT_USERNAME) except User.DoesNotExist: password = UserController.hash_password(settings.DEFAULT_PASSWORD) user = User(username=settings.DEFAULT_USERNAME, password=password) user.save() print "Starting up Authserver..." try: server.start() except KeyboardInterrupt: print "Exiting Authserver."