def test_session_commit(self, mock_session_api):
        with main.app.test_request_context():
            user = User()
            user.id = 'id'
            user.email = 'email'
            user.first_name = 'joe'
            user.surname = 'bloggs'
            user.organisation = 'testorg'
            user.roles = ['testrole']
            user.status = 'Active'

            lcc = LastCreatedCharge()

            lcc.charge_id = 1
            lcc.entry_number = 2
            lcc.registration_date = "abc"

            sess = Session('abc')
            sess.user = user
            sess.last_created_charge = lcc

            g.trace_id = "test_id"

            sess.commit()

            mock_session_api.update_session_data. \
                assert_called_with(sess.session_key,
                                   sess.to_dict(),
                                   Session.session_state_key)
Ejemplo n.º 2
0
    def update_charge(land_charge):
        current_app.logger.info("Attempting to update a charge")
        try:
            # Update Author Information
            land_charge.author = g.session.user.get_author_info()
            charge_json = land_charge.to_json()
            headers = {
                'Content-Type': 'application/json',
                'X-Trace-ID': g.trace_id
            }

            current_app.logger.info(
                "Putting to maintain-api/local-land-charge/{}".format(
                    charge_json['local-land-charge']))
            response = g.requests.put('{}/local-land-charge/{}'.format(
                MAINTAIN_API_URL, charge_json['local-land-charge']),
                                      json=charge_json,
                                      headers=headers)
        except Exception as ex:
            error_message = 'Failed to send land charge to maintain-api. ' \
                            'TraceID : {} - Exception - {}' \
                .format(g.trace_id, ex)
            current_app.logger.exception(error_message)
            AuditAPIService.audit_event(
                "Failed to send land charge to maintain-api",
                supporting_info={
                    'id': calc_display_id(land_charge.local_land_charge)
                })
            raise ApplicationError(500)

        if response.status_code != 202:
            current_app.logger.exception(
                'Failed to send land charge to maintain-api. '
                'TraceID : {} - Status: {}, Message: {}'.format(
                    g.trace_id, response.status_code, response.text))
            AuditAPIService.audit_event(
                "Failed to send land charge to maintain-api",
                supporting_info={
                    'id': calc_display_id(land_charge.local_land_charge)
                })
            raise ApplicationError(500)

        result = response.json()

        current_app.logger.info(
            "User ID '{}' updated charge {}. Entry number: {}, registration date: {}.  TraceID={}"
            .format(g.session.user.id, result['land_charge_id'],
                    result['entry_number'], result['registration_date'],
                    g.trace_id))

        last_charge = LastCreatedCharge()
        last_charge.charge_id = result['land_charge_id']
        last_charge.entry_number = result['entry_number']
        last_charge.registration_date = datetime.strptime(
            result['registration_date'], "%Y-%m-%d").strftime("%d/%m/%Y")
        g.session.last_created_charge = last_charge
        g.session.commit()
    def test_state_initialisation(self):
        state = LastCreatedCharge()

        state.charge_id = 1
        state.entry_number = 2
        state.registration_date = "abc"

        self.assertIsNotNone(state)
        self.assertEqual(state.charge_id, 1)
        self.assertEqual(state.entry_number, 2)
        self.assertEqual(state.registration_date, "abc")
    def test_confirmation_renders(self):
        self.client.set_cookie('localhost', Session.session_cookie_name,
                               'cookie_value')

        lcc = LastCreatedCharge()
        lcc.entry_number = 1
        lcc.charge_id = 123456789
        lcc.registration_date = "01/01/2000"

        self.mock_session.return_value.last_created_charge = lcc
        self.mock_session.return_value.user.permissions = [Permissions.add_llc]

        response = self.client.get(url_for('add_land_charge.get_confirmation'))

        self.assert_status(response, 200)
        self.assert_template_used('confirmation.html')
        self.assertIn('123456789', response.data.decode())