def test_can_delete_location_of_unpublished_game_of_yours(self): maps = MapsMiddleware() creator_middleware = GameCreatorMiddleware(self.david.username) self.assertTrue( creator_middleware.is_authorized_to_access_game("13T2-JFRN")) # delete location # we know that if this delete_location returned true, the location order also got refactored correctly # due to the implementation of this function self.assertTrue( maps.delete_location("13T2-JFRN", "24Q9-72EG"), "ERROR! Cannot delete location of an " "unpublished game of yours")
class DeleteLocationTest(DatabaseRequiredTests): def setUp(self): super(DeleteLocationTest, self).setUp() self.maps = MapsMiddleware() def test_can_delete_location_of_unpublished_game_of_yours(self): maps = MapsMiddleware() creator_middleware = GameCreatorMiddleware(self.david.username) self.assertTrue( creator_middleware.is_authorized_to_access_game("13T2-JFRN")) # delete location # we know that if this delete_location returned true, the location order also got refactored correctly # due to the implementation of this function self.assertTrue( maps.delete_location("13T2-JFRN", "24Q9-72EG"), "ERROR! Cannot delete location of an " "unpublished game of yours") def test_cannot_delete_location_of_live_game_of_yours(self): creator_middleware = GameCreatorMiddleware(self.david.username) self.assertTrue( creator_middleware.is_authorized_to_access_game("9XMQ-FXYJ")) # delete location we know that if can_change_game returns False, the post request wont succeed and the # location cannot be deleted self.assertFalse(creator_middleware.can_change_game("9XMQ-FXYJ"), "ERROR! User can change game even if it's live") def test_cannot_delete_location_of_archived_game_of_yours(self): creator_middleware = GameCreatorMiddleware(self.david.username) self.assertTrue( creator_middleware.is_authorized_to_access_game("9XMQ-FXYJ")) # archive the game creator_middleware.stop_game("9XMQ-FXYJ") # delete location we know that if can_change_game returns False, the post request wont succeed and the # location cannot be deleted self.assertFalse(creator_middleware.can_change_game("9XMQ-FXYJ"), "ERROR! User can change game even if it's archived") def test_cannot_delete_location_of_game_of_another_creator(self): creator_middleware = GameCreatorMiddleware(self.david.username) # must not be able to access the game # the code won't go past this point so we know that he cannot delete the game of another creator # regardless of whether it is live or not self.assertFalse( creator_middleware.is_authorized_to_access_game("WS30-8FA3"), "ERROR! Can delete location of " "a game created by another " "creator") def test_cannot_delete_invalid_location_of_game_of_yours(self): creator_middleware = GameCreatorMiddleware(self.david.username) # must not be able to access the game # the code won't go past this point so we know that he cannot delete the game of another creator # whether it is live or not self.assertFalse( creator_middleware.is_authorized_to_access_game("WS30-8FA3")) # delete invalid location self.assertFalse( self.maps.delete_location("9XMQ-FXYJ", "INVALID CODE"), "ERROR! Can delete invalid location " "of a game of yours") def test_cannot_delete_invalid_location_of_game_of_yours_two(self): creator_middleware = GameCreatorMiddleware(self.david.username) # must not be able to access the game # the code won't go past this point so we know that he cannot delete the game of another creator # whether it is live or not self.assertFalse( creator_middleware.is_authorized_to_access_game("WS30-8FA3")) # delete location of another game in this game self.assertFalse( self.maps.delete_location("9XMQ-FXYJ", "684V-7K25"), "ERROR! Can delete a location of " "another game which is not in your game")
class LocationListView(LoginRequiredMixin, generic.TemplateView): template_name = 'locations.html' login_url = '/start' locations = None player = None creator = None form = ChangeClueForm def get(self, request, game_code, location_code, *args, **kwargs): self.locations = GameCreatorMiddleware(request.user.username) self.game = _GameMiddleware(game_code) self.maps = MapsMiddleware() this_location = self.locations.get_location_by_code(location_code) this_location_copy = self.locations.get_location_by_code(location_code) for x in this_location: location_name = str(x) if not self.locations.is_authorized_to_access_game(game_code): return handler(request, 404) latitude, longitude = self.maps.get_coordinate(location_name) latitude = float(latitude) longitude = float(longitude) return render(request, self.template_name, context={ 'locations_code': this_location, 'game_details': self.game.get_code_and_name(), 'game_player_name': self.locations.get_name(), 'game_player_username': self.locations.get_username(), 'lat_long': [latitude, longitude], 'game_code': self.game.game.code, 'location_code': this_location_copy.first() }) # Handling of the various post request that can be made on the locations page def post(self, request, game_code, location_code, *args, **kwargs): self.locations = GameCreatorMiddleware(request.user.username) self.game = _GameMiddleware(game_code) self.maps = MapsMiddleware() if not self.locations.is_authorized_to_access_game( request.POST['game_code']): return handler(request, 404) if 'delete_location_code' in request.POST.keys( ) and self.locations.can_change_game(request.POST['game_code']): return self._delete_location( request, game_code, request.POST['delete_location_code'], self.locations.get_location_by_code(location_code)) if 'code' in request.POST.keys(): return self._change_clue(request, request.POST['game_code'], request.POST['code'], *args, **kwargs) return handler(request, 404) # Delete a location from a game # Paramaters: # game_code: the game code of game to be modified # location_code: the location code of the location to be deleted def _delete_location(self, request, game_code, location_code, *args, **kwargs): self.maps = MapsMiddleware() self.maps.delete_location(game_code, location_code) return HttpResponseRedirect('/game/create/' + game_code) # Modify the clue of a location # Paramaters: # game_code: the game code of game to be modified # location_code: the location code of the location to be modified # clues: the new clue for the location def _change_clue(self, request, game_code, location_code, *args, **kwargs): self.creator = GameCreatorMiddleware(None) self.creator.user = request.user form = self.form(instance=self.creator.get_location_of_game( game_code=game_code, location_code=location_code), data=request.POST) if form.is_valid(): location = form.save(commit=False) location.clues = request.POST['clues'] location.save() return HttpResponseRedirect('/game/create/' + game_code) return handler(request, 404)