def test_validation_failed_when_amount_field_contains_pound_symbol(self):
     result = LandCompensationPaymentValidator.validate(
         '£123.45', '900', False)
     self.assertEqual(1, len(result.errors))
     self.assertEqual(
         'Advance payment must be a positive number',
         result.errors['land-compensation-paid'].summary_message)
    def test_validation_passes_with_blank_amount_type(self):
        land_compensation_paid = '100'
        amount_of_compensation = '900'

        result = LandCompensationPaymentValidator.validate(
            land_compensation_paid, amount_of_compensation, False)
        self.assertEqual(0, len(result.errors))
Пример #3
0
def post_land_compensation_payment():
    land_compensation_paid = NumberConverter.format_number_string(
        request.form.get('land-compensation-paid'),
        leading_char='£',
        force_two_dp=True)

    amount_of_compensation = NumberConverter.format_number_string(
        request.form.get('amount-of-compensation'),
        leading_char='£',
        force_two_dp=True)
    land_compensation_amount_type = request.form.get(
        'land-compensation-amount-type')

    current_app.logger.info(
        "Endpoint called with land-compensation-paid = '%s', land-compensation-amount-type = '%s'",
        land_compensation_paid, amount_of_compensation,
        land_compensation_amount_type)
    if g.session.add_charge_state is None:
        current_app.logger.info("Redirecting to: %s",
                                url_for("add_land_charge.new"))
        return redirect(url_for("add_land_charge.new"))

    current_app.logger.info("Running validation")
    validation_errors = LandCompensationPaymentValidator.validate(
        land_compensation_paid, amount_of_compensation, False)
    if validation_errors.errors:
        current_app.logger.warning("Validation errors occurred")
        return render_template(
            'land_compensation_payment.html',
            validation_errors=validation_errors.errors,
            validation_summary_heading=validation_errors.summary_heading_text,
            request_body=request.form,
            submit_url=url_for(
                'add_land_charge.post_land_compensation_payment')), 400

    current_app.logger.info(
        "Updating session object with land compensation amount: '%s' and \
                            land compensation amount type: '%s'",
        land_compensation_paid, land_compensation_amount_type)

    ReviewRouter.update_edited_field('land_compensation_paid',
                                     land_compensation_paid)
    ReviewRouter.update_edited_field('amount_of_compensation',
                                     amount_of_compensation)
    ReviewRouter.update_edited_field('land_compensation_amount_type',
                                     land_compensation_amount_type)

    g.session.add_charge_state.land_compensation_paid = land_compensation_paid
    g.session.add_charge_state.amount_of_compensation = amount_of_compensation
    g.session.add_charge_state.land_compensation_amount_type = land_compensation_amount_type
    g.session.commit()

    # Force the flow to go to the next page if that value is not in the session (in case browser back button is used)
    if not g.session.add_charge_state.land_capacity_description:
        next_url = url_for("add_land_charge.get_land_compensation_owned")
    else:
        next_url = ReviewRouter.get_redirect_url(
            "add_land_charge.get_land_compensation_owned")
    current_app.logger.info("Redirecting to next step: %s", next_url)
    return redirect(next_url)
    def test_validation_passes_with_blank_amount_of_compensation_when_varying(
            self):
        land_compensation_paid = '100'
        amount_of_compensation = ''

        result = LandCompensationPaymentValidator.validate(
            land_compensation_paid, amount_of_compensation, True)
        self.assertEqual(0, len(result.errors))
 def test_validation_failed_when_advance_higher_than_total(self):
     result = LandCompensationPaymentValidator.validate('300', '100', False)
     self.assertEqual(1, len(result.errors))
     self.assertEqual(
         'Advance payment cannot be more than total compensation',
         result.errors['land-compensation-paid'].summary_message)
     self.assertEqual(
         'Advance payment cannot be more than total compensation',
         result.errors['land-compensation-paid'].inline_message)
 def test_validation_failed_when_input_is_blank(self):
     result = LandCompensationPaymentValidator.validate('', '', False)
     self.assertEqual(2, len(result.errors))
     self.assertEqual(
         'Enter the advance payment',
         result.errors['land-compensation-paid'].summary_message)
     self.assertEqual(
         'Enter the total compensation',
         result.errors['amount-of-compensation'].summary_message)
     self.assertEqual(
         'This is part of the compensation (paid in advance)',
         result.errors['land-compensation-paid'].inline_message)
     self.assertEqual(
         'This is the total compensation',
         result.errors['amount-of-compensation'].inline_message)
 def test_validation_failed_when_amount_fields_are_more_than_2dp(self):
     result = LandCompensationPaymentValidator.validate(
         '1.999', '2.999', False)
     self.assertEqual(2, len(result.errors))
     self.assertEqual(
         'Advance payment can only have 2 numbers after the decimal place',
         result.errors['land-compensation-paid'].summary_message)
     self.assertEqual(
         'Compensation payment can only have 2 numbers after the decimal place',
         result.errors['amount-of-compensation'].summary_message)
     self.assertEqual(
         'Advance payment can only have 2 numbers after the decimal place',
         result.errors['land-compensation-paid'].inline_message)
     self.assertEqual(
         'Compensation payment can only have 2 numbers after the decimal place',
         result.errors['amount-of-compensation'].inline_message)
 def test_validation_failed_when_amount_fields_are_invalid(self):
     result = LandCompensationPaymentValidator.validate(
         'abcdefg', 'defghijk', False)
     self.assertEqual(2, len(result.errors))
     self.assertEqual(
         'Advance payment must be a positive number',
         result.errors['land-compensation-paid'].summary_message)
     self.assertEqual(
         'Compensation must be a positive number',
         result.errors['amount-of-compensation'].summary_message)
     self.assertEqual(
         'Advance payment must be a positive number',
         result.errors['land-compensation-paid'].inline_message)
     self.assertEqual(
         'Compensation must be a positive number',
         result.errors['amount-of-compensation'].inline_message)
def post_land_compensation_payment():
    current_app.logger.info("Endpoint called")
    if g.session.add_charge_state is None:
        current_app.logger.error(
            "Charge state not found in session - Returning error")
        raise ApplicationError(500)

    land_compensation_paid = NumberConverter.format_number_string(
        request.form.get('land-compensation-paid'),
        leading_char='£',
        force_two_dp=True)

    amount_of_compensation = NumberConverter.format_number_string(
        request.form.get('amount-of-compensation'),
        leading_char='£',
        force_two_dp=True)
    land_compensation_amount_type = request.form.get(
        'land-compensation-amount-type')

    current_app.logger.info("Running validation")
    validation_errors = LandCompensationPaymentValidator.validate(
        land_compensation_paid, amount_of_compensation, True)
    if validation_errors.errors:
        current_app.logger.warning("Validation errors occurred")
        return render_template(
            'land_compensation_payment.html',
            validation_errors=validation_errors.errors,
            validation_summary_heading=validation_errors.summary_heading_text,
            request_body=request.form,
            submit_url=url_for(
                'modify_land_charge.post_land_compensation_payment')), 400

    current_app.logger.info(
        "Updating session object with land compensation amount: '%s' , \
                                    total compensation: '%s' and land compensation amount type: '%s'",
        land_compensation_paid, amount_of_compensation,
        land_compensation_amount_type)

    charge_display_id = calc_display_id(
        g.session.add_charge_state.local_land_charge)
    edited = False
    if has_value_changed(g.session.add_charge_state.land_compensation_paid,
                         land_compensation_paid):
        g.session.edited_fields.append('land_compensation_paid')
        g.session.add_charge_state.land_compensation_paid = land_compensation_paid
        edited = True
    if has_value_changed(g.session.add_charge_state.amount_of_compensation,
                         amount_of_compensation):
        g.session.edited_fields.append('amount_of_compensation')
        g.session.add_charge_state.amount_of_compensation = amount_of_compensation
        edited = True
    if has_value_changed(
            g.session.add_charge_state.land_compensation_amount_type,
            land_compensation_amount_type):
        g.session.edited_fields.append('land_compensation_amount_type')
        g.session.add_charge_state.land_compensation_amount_type = land_compensation_amount_type
        edited = True
    if edited:
        g.session.commit()

    current_app.logger.info(
        "Redirecting to next step: %s",
        url_for("modify_land_charge.modify_land_charge",
                local_land_charge=charge_display_id))
    return redirect(
        url_for("modify_land_charge.modify_land_charge",
                local_land_charge=charge_display_id))
    def test_params_passed(self, mock_field_validator, mock_error_builder):
        """should pass the given parameter to the fieldset validator and call the expected validations"""

        land_compensation_paid = '100'
        amount_of_compensation = '900'

        LandCompensationPaymentValidator.validate(land_compensation_paid,
                                                  amount_of_compensation,
                                                  False)

        calls = [
            call(land_compensation_paid,
                 'land-compensation-paid',
                 'Advance payment amount',
                 mock_error_builder(),
                 summary_message='Enter the advance payment',
                 inline_message=
                 'This is part of the compensation (paid in advance)'),
            call().is_required(),
            call(amount_of_compensation,
                 'amount-of-compensation',
                 'Total compensation amount',
                 mock_error_builder(),
                 summary_message='Enter the total compensation',
                 inline_message='This is the total compensation'),
            call().is_required(),
            call(amount_of_compensation,
                 'amount-of-compensation',
                 'Total compensation amount',
                 mock_error_builder(),
                 summary_message='Compensation must be a positive number',
                 inline_message='Compensation must be a positive number'),
            call().is_positive_number(),
            call(land_compensation_paid,
                 'land-compensation-paid',
                 'Advance payment amount',
                 mock_error_builder(),
                 summary_message=
                 'Advance payment cannot be more than total compensation',
                 inline_message=
                 'Advance payment cannot be more than total compensation'),
            call().is_less_than_or_equal_to(amount_of_compensation),
            call(
                amount_of_compensation,
                'amount-of-compensation',
                'Total compensation amount',
                mock_error_builder(),
                summary_message=
                'Compensation payment can only have 2 numbers after the decimal place',
                inline_message=
                'Compensation payment can only have 2 numbers after the decimal place'
            ),
            call().is_number_with_zero_or_x_decimal_places(2),
            call(land_compensation_paid,
                 'land-compensation-paid',
                 'Advance payment amount',
                 mock_error_builder(),
                 summary_message='Advance payment must be a positive number',
                 inline_message='Advance payment must be a positive number'),
            call().is_positive_number(),
            call(
                land_compensation_paid,
                'land-compensation-paid',
                'Advance payment amount',
                mock_error_builder(),
                summary_message=
                'Advance payment can only have 2 numbers after the decimal place',
                inline_message=
                'Advance payment can only have 2 numbers after the decimal place'
            ),
            call().is_number_with_zero_or_x_decimal_places(2)
        ]
        mock_field_validator.assert_has_calls(calls)