def test_invalid_data(mocker): request.body = dict({'name': 'user1', 'password': '******'}) load_result.errors['password'] = '******' mocker.patch('helpers.schemas.UserSchema.load', return_value=load_result) with pytest.raises(BadRequestError): validate_request_body(request, resource='user', action='create', resource_id=1) UserSchema.load.assert_called_once_with(request.body)
def test_update_user(mocker): # fail, no user found mocker.patch('helpers.schemas.UserSchema.load', return_value=load_result) mocker.patch('models.user.UserModel.find_by_id', return_value=None) with pytest.raises(BadRequestError): validate_request_body(request, resource='user', action='update', resource_id=1) # succeed mocker.patch('models.user.UserModel.find_by_id', return_value='something') try: validate_request_body(request, resource='user', action='update', resource_id=1) except Exception: pytest.fail("Failed to pass update user request")
def test_create_resource(mocker): # fail, duplicated name mocker.patch('helpers.schemas.UserSchema.load', return_value=load_result) mocker.patch('models.user.UserModel.find_by_name', return_value='something') with pytest.raises(BadRequestError): validate_request_body(request, resource='user', action='create', resource_id=1) # succeed mocker.patch('models.user.UserModel.find_by_name', return_value=None) try: validate_request_body(request, resource='user', action='create', resource_id=1) except Exception: pytest.fail("Failed to pass create resource request")
def test_auth_user(mocker): load_result.errors = None load_result.data = request.body # fail mocker.patch('helpers.schemas.UserSchema.load', return_value=load_result) mocker.patch('models.user.UserModel.find_by_name', return_value=None) with pytest.raises(BadRequestError): validate_request_body(request, resource='user', action='auth', resource_id=1) UserModel.find_by_name.assert_called_once_with('user1') # succeed mocker.patch('models.user.UserModel.find_by_name', return_value='something') try: validate_request_body(request, resource='user', action='auth', resource_id=1) except Exception: pytest.fail("Failed to pass create user request")