def testChangePassword(self): oldTime = PlayerPassword.objects.get(player__id=3).time_set newPassword = "******" response = self.doPost('/udj/users/6/players/3/password', {'password': newPassword}) self.assertEqual(response.status_code, 200, "Error: " + response.content) playerPassword = PlayerPassword.objects.get(player__id=3) self.assertEqual(playerPassword.password_hash, hashPlayerPassword(newPassword)) self.assertTrue(oldTime < playerPassword.time_set)
def testSetPassword(self): newPassword = '******' response = self.doPost('/udj/users/2/players/1/password', {'password': newPassword}) self.assertEqual(response.status_code, 200, "Error: " + response.content) playerPassword = PlayerPassword.objects.get(player__id=1) self.assertEqual(playerPassword.password_hash, hashPlayerPassword(newPassword))
def participateWithPlayer(request, player_id, user_id, activePlayer): playerPassword = PlayerPassword.objects.filter(player=activePlayer) if playerPassword.exists(): if DJANGO_PLAYER_PASSWORD_HEADER in request.META: hashedPassword = hashPlayerPassword(request.META[DJANGO_PLAYER_PASSWORD_HEADER]) if hashedPassword == playerPassword[0].password_hash: return onSuccessfulPlayerAuth(activePlayer, user_id) toReturn = HttpResponse(status=401) toReturn['WWW-Authenticate'] = 'player-password' return toReturn else: return onSuccessfulPlayerAuth(activePlayer, user_id)
def participateWithPlayer(request, player_id, user_id, activePlayer): playerPassword = PlayerPassword.objects.filter(player=activePlayer) if playerPassword.exists(): if DJANGO_PLAYER_PASSWORD_HEADER in request.META: hashedPassword = hashPlayerPassword( request.META[DJANGO_PLAYER_PASSWORD_HEADER]) if hashedPassword == playerPassword[0].password_hash: return onSuccessfulPlayerAuth(activePlayer, user_id) toReturn = HttpResponse(status=401) toReturn['WWW-Authenticate'] = 'player-password' return toReturn else: return onSuccessfulPlayerAuth(activePlayer, user_id)
def setPlayerPassword(request, user_id, player_id, player): givenPassword = request.POST['password'] if givenPassword == '': return HttpResponseBadRequest("Bad password") hashedPassword = hashPlayerPassword(givenPassword) playerPassword, created = PlayerPassword.objects.get_or_create( player=player, defaults={'password_hash': hashedPassword}) if not created: playerPassword.password_hash = hashedPassword playerPassword.save() return HttpResponse()
def setPlayerPassword(request, user_id, player_id, player): givenPassword = request.POST['password'] if givenPassword == '': return HttpResponseBadRequest("Bad password") hashedPassword = hashPlayerPassword(givenPassword) playerPassword , created = PlayerPassword.objects.get_or_create( player=player, defaults={'password_hash': hashedPassword}) if not created: playerPassword.password_hash = hashedPassword playerPassword.save() return HttpResponse()
def testCreatePasswordPlayer(self): playerName = "Yunyoung Player" password = '******' passwordHash = hashPlayerPassword(password) payload = {'name' : playerName, 'password' : password} response = self.doJSONPut('/udj/users/7/players/player', json.dumps(payload)) self.assertEqual(response.status_code, 201, "Error: " + response.content) self.isJSONResponse(response) givenPlayerId = json.loads(response.content)['player_id'] addedPlayer = Player.objects.get(pk=givenPlayerId) self.assertEqual(addedPlayer.name, playerName) self.assertEqual(addedPlayer.owning_user.id, 7) self.assertFalse(PlayerLocation.objects.filter(player=addedPlayer).exists()) addedPassword = PlayerPassword.objects.get(player=addedPlayer) self.assertEqual(addedPassword.password_hash, passwordHash)
def createPlayer(request, user_id): user = User.objects.get(pk=user_id) try: newPlayerJSON = json.loads(request.raw_post_data) except ValueError: return HttpResponseBadRequest('Bad JSON') #Ensure the name attribute was provided with the JSON newPlayerName = "" try: newPlayerName = newPlayerJSON['name'] except KeyError: return HttpResponseBadRequest('No name given') #Ensure that the suers doesn't already have a player with the given name conflictingPlayer = Player.objects.filter(owning_user=user, name=newPlayerName) if conflictingPlayer.exists(): return HttpResponse('A player with that name already exists', status=409) #Create and save new player newPlayer = Player(owning_user=user, name=newPlayerName) newPlayer.save() #If password provided, create and save password if 'password' in newPlayerJSON: PlayerPassword(player=newPlayer, password_hash=hashPlayerPassword( newPlayerJSON['password'])).save() #If locaiton provided, geocode it and save it if 'location' in newPlayerJSON: location = newPlayerJSON['location'] if isValidLocation(location): try: doLocationSet(location['address'], location['city'], location['state'], location['zipcode'], newPlayer) except LocationNotFoundError: return HttpResponseBadRequest('Location not found') else: return HttpResponseBadRequest('Bad location') return HttpResponse(json.dumps({'player_id': newPlayer.id}), status=201, content_type="text/json")
def testCreatePasswordPlayer(self): playerName = "Yunyoung Player" password = '******' passwordHash = hashPlayerPassword(password) payload = {'name': playerName, 'password': password} response = self.doJSONPut('/udj/users/7/players/player', json.dumps(payload)) self.assertEqual(response.status_code, 201, "Error: " + response.content) self.isJSONResponse(response) givenPlayerId = json.loads(response.content)['player_id'] addedPlayer = Player.objects.get(pk=givenPlayerId) self.assertEqual(addedPlayer.name, playerName) self.assertEqual(addedPlayer.owning_user.id, 7) self.assertFalse( PlayerLocation.objects.filter(player=addedPlayer).exists()) addedPassword = PlayerPassword.objects.get(player=addedPlayer) self.assertEqual(addedPassword.password_hash, passwordHash)
def createPlayer(request, user_id): user = User.objects.get(pk=user_id) try: newPlayerJSON = json.loads(request.raw_post_data) except ValueError: return HttpResponseBadRequest('Bad JSON') #Ensure the name attribute was provided with the JSON newPlayerName = "" try: newPlayerName = newPlayerJSON['name'] except KeyError: return HttpResponseBadRequest('No name given') #Ensure that the suers doesn't already have a player with the given name conflictingPlayer = Player.objects.filter(owning_user=user, name=newPlayerName) if conflictingPlayer.exists(): return HttpResponse('A player with that name already exists', status=409) #Create and save new player newPlayer = Player(owning_user=user, name=newPlayerName) newPlayer.save() #If password provided, create and save password if 'password' in newPlayerJSON: PlayerPassword(player=newPlayer, password_hash=hashPlayerPassword(newPlayerJSON['password'])).save() #If locaiton provided, geocode it and save it if 'location' in newPlayerJSON: location = newPlayerJSON['location'] if isValidLocation(location): try: doLocationSet(location['address'], location['city'], location['state'], location['zipcode'], newPlayer) except LocationNotFoundError: return HttpResponseBadRequest('Location not found') else: return HttpResponseBadRequest('Bad location') return HttpResponse(json.dumps({'player_id' : newPlayer.id}), status=201, content_type="text/json")