Ejemplo n.º 1
0
    def test_edit_item(self, client, app):
        # Prepare testing
        edit_info = {
            'name': 'edited item',
            'description': 'desc',
            'category_id': 1
        }
        with app.app_context():
            item = db.session.query(Item).first()
            other_item = db.session.query(Item)\
                .filter(Item.id != item.id,
                        Item.category_id == item.category_id).first()

            auth_user = item.user
            auth_token = get_access_token(auth_user)
            unauth_user = db.session.query(User).filter(
                User.id != auth_user.id).first()
            unauth_token = get_access_token(unauth_user)

        # Case anonymous user
        response = edit_item(client, item.id, edit_info)
        assert response.status_code == 401

        # Case unauthorized user
        response = edit_item(client, item.id, edit_info, unauth_token)
        assert response.status_code == 401

        # Case authorized user
        response = edit_item(client, item.id, edit_info, auth_token)
        data = response.get_json()
        assert response.status_code == 200
        assert data['item']['name'] == edit_info['name']
        with app.app_context():
            db_item = Item.get_by_id(item.id)
        assert db_item.name == edit_info['name']

        # Case duplicate item
        edit_info['name'] = other_item.name
        response = edit_item(client, item.id, edit_info, auth_token)
        assert response.status_code == 400

        # Case invalid info
        edit_info['name'] = '  '
        response = edit_item(client, item.id, edit_info, auth_token)
        assert response.status_code == 400
        assert 'error' in response.get_json()
Ejemplo n.º 2
0
    def test_delete_item(self, client, app):
        # Prepare testing
        with app.app_context():
            item = db.session.query(Item).first()
            auth_user = item.user
            auth_token = get_access_token(auth_user)
            unauth_user = db.session.query(User).filter(
                User.id != auth_user.id).first()
            unauth_token = get_access_token(unauth_user)

        # Case anonymous user
        response = delete_item(client, item.id)
        assert response.status_code == 401

        # Case unauthorized user
        response = delete_item(client, item.id, unauth_token)
        assert response.status_code == 401

        # Case authorized user
        response = delete_item(client, item.id, auth_token)
        assert response.status_code == 204
        with app.app_context():
            db_item = Item.get_by_id(item.id)
        assert db_item is None
Ejemplo n.º 3
0
 def decorated_func(item_id, **kwargs):
     item = Item.get_by_id(item_id)
     if item is None:
         return render_json_error('Item not found', 404)
     return f(item_id, item, **kwargs)