Exemple #1
0
    def test_signin_signs_in_player(self):
        signin_url = '/players/signin'
        username = self.player.get_username()
        password = self.PLAYER_PASSWORD
        player_data = {
            'username': username,
            'password': password
        }
        response = self.post(signin_url, data=player_data)
        self.assertEqual(200, response.status_code)
        player = json.loads(response.data)
        self.assertEqual(self.player.get_id(), player.get('id'))
        self.assertEqual(Player.dump_datetime(self.player.get_date_created()), player.get('date_created'))
        self.assertIsNotNone(player.get('date_modified'))
        self.assertEqual(username, player.get('username'))
        self.assertEqual(self.player.get_email(), player.get('email'))
        self.assertIsNone(player.get('password'))
        self.assertEqual(self.player.get_avatar_url(), player.get('avatar_url'))
        self.assertEqual(self.player.get_is_active(), player.get('is_active'))

        # Make sure the player was actually updated in the database
        saved_player = PlayersService.get_instance().get(int(player.get('id')))
        self.assertEqual(saved_player.get_id(), player.get('id'))
        self.assertEqual(Player.dump_datetime(saved_player.get_date_created()), player.get('date_created'))
        self.assertEqual(Player.dump_datetime(saved_player.get_date_modified()), player.get('date_modified'))
        self.assertEqual(saved_player.get_username(), player.get('username'))
        self.assertEqual(saved_player.get_email(), player.get('email'))
        self.assertEqual(saved_player.get_avatar_url(), player.get('avatar_url'))
        self.assertEqual(saved_player.get_is_active(), player.get('is_active'))
    def get(self, uid):
        if uid:
            try:
                player = Player.query({'_id': uid})
            except InvalidId as e:
                return jsonify({
                    'error_type': type(e).__name__,
                    'error_message': e.args[0]
                })

            if self.get_endpoint() == 'matches':
                return self.gen_response(player.get_matches())

            elif self.get_endpoint() == 'current':
                return self.gen_response(player.get_current_match())

            elif self.get_endpoint() == 'tournaments':
                return self.gen_response(player.get_tournaments(TournamentDao().retrieveAll()))

            else:
                #query = Player.query({'_id': uid})
                return self.gen_response(player)

        else:
            query = Player.query()
            return self.gen_response(query)
Exemple #3
0
    def insert_dummy_players(self):
        player = None
        for player_index in range(1, self.NUM_PLAYERS + 1):
            username = get_incremental_username(player_index)
            email = get_incremental_email(player_index)
            avatar_url = get_incremental_avatar_url(player_index) if player_index % 2 == 0 else None
            player = Player(username, self.PLAYER_PASSWORD, email, avatar_url)
            player.save()

        self.player = player
 def post(self):
     try:
         player = Player.create(request.json)
         socketio.emit('new_player', {'player': {'name': player.name, 'life': player.life, '_id': str(player._id)}})
         return self.gen_response(player)
     except MongoModelError as e:
         return self.error_response(e)
    def get_matches(self, uid):
        player = Player.query({'_id': uid})
        matches = []
        for match in MatchDao().retrieveAll():
            if str(player['_id']) in match['player_scores']:
                matches.append(match)

        return self.gen_response(matches)
    def put(self, uid):
        player = Player.query({'_id': uid})

        try:
            player.update(request.json)
        except MongoModelError as e:
            return self.error_response(e)

        socketio.emit('response', {'player_id': str(player._id), 'life': player.life})
        return self.gen_response(player)
Exemple #7
0
    def test_delete_deletes_player(self):
        delete_url = '/players/{}'.format(self.player.get_id())
        self.assertEqual(True, self.player.get_is_active())
        response = self.delete(delete_url)
        self.assertEqual(200, response.status_code)
        player = json.loads(response.data)
        self.assertIsNotNone(player.get('id'))
        self.assertIsNotNone(player.get('date_created'))
        self.assertIsNotNone(player.get('date_modified'))
        self.assertIsNotNone(player.get('username'))
        self.assertIsNotNone(player.get('email'))
        self.assertIsNone(player.get('password'))
        self.assertEqual(False, player.get('is_active'))

        # Make sure the player was actually updated in the database
        saved_player = PlayersService.get_instance().get(int(player.get('id')))
        self.assertEqual(saved_player.get_id(), player.get('id'))
        self.assertEqual(Player.dump_datetime(saved_player.get_date_created()), player.get('date_created'))
        self.assertEqual(Player.dump_datetime(saved_player.get_date_modified()), player.get('date_modified'))
        self.assertEqual(saved_player.get_username(), player.get('username'))
        self.assertEqual(saved_player.get_email(), player.get('email'))
        self.assertEqual(saved_player.get_avatar_url(), player.get('avatar_url'))
        self.assertEqual(saved_player.get_is_active(), player.get('is_active'))
Exemple #8
0
    def test_update_updates_player(self):
        update_url = '/players/{}'.format(self.player.get_id())
        player_number = self.player.get_id() * 100
        username = get_incremental_username(player_number)
        email = get_incremental_email(player_number)
        password = '******'.format(player_number)
        avatar_url = get_incremental_avatar_url(player_number)
        player_data = {
            'username': username,
            'password': password,
            'confirm': password,
            'email': email,
            'avatar_url': avatar_url,
            'is_active': False
        }
        response = self.put(update_url, data=player_data)
        self.assertEqual(200, response.status_code)
        player = json.loads(response.data)
        self.assertIsNotNone(player.get('id'))
        self.assertIsNotNone(player.get('date_created'))
        self.assertIsNotNone(player.get('date_modified'))
        self.assertEqual(username, player.get('username'))
        self.assertEqual(email, player.get('email'))
        self.assertIsNone(player.get('password'))
        self.assertEqual(avatar_url, player.get('avatar_url'))
        self.assertEqual(False, player.get('is_active'))

        # Make sure the player was actually updated in the database
        saved_player = PlayersService.get_instance().get(int(player.get('id')))
        self.assertEqual(saved_player.get_id(), player.get('id'))
        self.assertEqual(Player.dump_datetime(saved_player.get_date_created()), player.get('date_created'))
        self.assertEqual(Player.dump_datetime(saved_player.get_date_modified()), player.get('date_modified'))
        self.assertEqual(saved_player.get_username(), player.get('username'))
        self.assertEqual(saved_player.get_email(), player.get('email'))
        self.assertEqual(saved_player.get_avatar_url(), player.get('avatar_url'))
        self.assertEqual(saved_player.get_is_active(), player.get('is_active'))
Exemple #9
0
    def test_create_returns_created_status(self):
        player_id = self.NUM_PLAYERS + 1
        create_url = '/players'
        username = get_incremental_username(player_id)
        email = get_incremental_email(player_id)
        avatar_url = get_incremental_avatar_url(player_id)
        player_data = {
            'username': username,
            'password': self.PLAYER_PASSWORD,
            'confirm': self.PLAYER_PASSWORD,
            'email': email,
            'avatar_url': avatar_url
        }
        response = self.post(create_url, data=player_data)
        self.assertEqual(201, response.status_code)
        player = json.loads(response.data)
        self.assertIsNotNone(player.get('id'))
        self.assertIsNotNone(player.get('date_created'))
        self.assertIsNotNone(player.get('date_modified'))
        self.assertEqual(username, player.get('username'))
        self.assertEqual(email, player.get('email'))
        self.assertIsNone(player.get('password'))
        self.assertEqual(avatar_url, player.get('avatar_url'))
        self.assertEqual(True, player.get('is_active'))
        self.assertIsNotNone(player.get('auth_token'))

        # Make sure the player was actually saved to the database
        saved_player = PlayersService.get_instance().get(int(player.get('id')))
        self.assertEqual(saved_player.get_id(), player.get('id'))
        self.assertEqual(Player.dump_datetime(saved_player.get_date_created()), player.get('date_created'))
        self.assertEqual(Player.dump_datetime(saved_player.get_date_modified()), player.get('date_modified'))
        self.assertEqual(saved_player.get_username(), player.get('username'))
        self.assertEqual(saved_player.get_email(), player.get('email'))
        self.assertEqual(saved_player.get_avatar_url(), player.get('avatar_url'))
        self.assertEqual(saved_player.get_is_active(), player.get('is_active'))
        self.assertEqual(saved_player.get_auth_token(), player.get('auth_token'))
Exemple #10
0
def create():
    """
    Request:
    {
        "username": "******",
        "password": "******",
        "confirm": "confirm",
        "email": "email",
        "avatar_url": "avatar_url"
    }

    Response [422] (invalid parameters):
    {
        "errors": {
            "name of parameter that failed validation": [
                "Reason for validation failure"
            ],
            "name of another parameter that failed validation": [
                "Reason for validation failure"
            ],
        },
        "inputs": {
            "username": "******",
            "password": NULL,
            "confirm": NULL,
            "email": "value passed in. empty string if missing",
            "avatar_url": "value passed in. empty string if missing"
        }
    }

    Response [422] (save failure):
    {
        "errors": {
            "IntegrityError": [
                "Reason saving to the db failed, such as username/email uniqueness"
            ]
        },
        "inputs": {
            "username": "******",
            "password": NULL,
            "confirm": NULL,
            "email": "value passed in. empty string if missing",
            "avatar_url": "value passed in. empty string if missing"
        }
    }

    Response [200] (success):
    {
        "id": "current value",
        "date_created": "current value",
        "date_modified": "current value",
        "username": "******",
        "email": "current value",
        "avatar_url": "current value",
        "is_active": "current value",
        "auth_token": "current value"
    }
    """
    # Get the input validator
    inputs = CreateInputs(get_inputs())

    # Verify the player creation inputs
    if inputs.validate_on_submit():

        avatar_url = inputs.avatar_url.data if inputs.avatar_url.data else None
        player = Player(inputs.username.data, inputs.password.data, inputs.email.data, avatar_url)
        try:
            player.save()
            player_data = dict(player.serialized.items() + {'auth_token': player.get_auth_token()}.items())
            return render_view('players/show', 201, player=player_data)
        except Exception as e:
            return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=inputs.obfuscated())

    return render_view('422', 422, errors=inputs.errors, inputs=inputs.obfuscated())
 def decreaseLife(data):
     player = Player.query({'_id': data['player_id']})
     player.update({'life': player.life - data['amount']})
 def delete(self, uid):
     query = Player.destroy({'_id': uid})
     return '200'
Exemple #13
0
 def test_new_player_defaults(self):
     player = Player({'_id': '1', 'name': 'Paulo'})
     for field_id in player.get_fields_with_defaults():
         assert getattr(player, field_id) is player.get_default_value(field_id)