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())
    def test_state_from_dict(self):

        test = dict()
        test["charge_id"] = 1
        test["entry_number"] = 2
        test["registration_date"] = "abc"

        state = LastCreatedCharge.from_dict(test)

        self.assertIsNotNone(state)
        self.assertEqual(state.charge_id, 1)
        self.assertEqual(state.entry_number, 2)
        self.assertEqual(state.registration_date, "abc")
 def populate_state(self):
     """Populates the add charge from session state."""
     current_app.logger.info(
         "Method called, getting session state from session api")
     response = SessionAPIService.get_session_state(
         self.session_key, Session.session_state_key)
     if response is not None:
         current_app.logger.info("Non-empty session state contents")
         if 'add_charge_state' in response:
             current_app.logger.info("add_charge_state in session state")
             self.add_charge_state = LocalLandChargeItem.from_json(
                 response['add_charge_state'])
         if 'add_lon_charge_state' in response:
             current_app.logger.info(
                 "add_lon_charge_state in session state")
             self.add_lon_charge_state = LightObstructionNoticeItem.from_json(
                 response['add_lon_charge_state'])
         if 'last_created_charge' in response:
             current_app.logger.info("last_created_charge in session state")
             self.last_created_charge = LastCreatedCharge.from_dict(
                 response['last_created_charge'])
         if 'statutory_provision_list' in response:
             current_app.logger.info(
                 "statutory_provision_list in session state")
             self.statutory_provision_list = response[
                 'statutory_provision_list']
         if 'edited_fields' in response:
             current_app.logger.info("edited_fields in session state")
             self.edited_fields = response['edited_fields']
         if 'llc1_state' in response:
             current_app.logger.info("llc1_state in session state")
             self.llc1_state = LLC1Search.from_json(response['llc1_state'])
         if 'redirect_route' in response:
             current_app.logger.info('redirect_route in session state')
             self.redirect_route = response['redirect_route']
         if 'search_extent' in response:
             current_app.logger.info('search_extent in session state')
             self.search_extent = response['search_extent']
         if 'filenames' in response:
             current_app.logger.info('filenames in session state')
             self.filenames = response['filenames']
         if 'previously_selected_address' in response:
             current_app.logger.info(
                 'previously_selected_address in session state')
             self.previously_selected_address = response[
                 'previously_selected_address']
         if 'adding_charge_for_other_authority' in response:
             current_app.logger.info(
                 'adding_charge_for_other_authority in session state')
             self.adding_charge_for_other_authority = response[
                 'adding_charge_for_other_authority']
         if 'submit_token' in response:
             current_app.logger.info('submit token in session state')
             self.submit_token = response['submit_token']
         if 'upload_shapefile_processed' in response:
             current_app.logger.info(
                 'upload_shapefile_processed in session state')
             self.upload_shapefile_processed = response[
                 'upload_shapefile_processed']
         if 'category_details' in response:
             self.category_details = Category.from_dict(
                 response['category_details'])
         if 'category_confirmation' in response:
             self.category_confirmation = response['category_confirmation']
         if 'charge_added_outside_users_authority' in response:
             self.charge_added_outside_users_authority = response[
                 'charge_added_outside_users_authority']
         if 'other_authority_update_permission' in response:
             self.other_authority_update_permission = response[
                 'other_authority_update_permission']
         if 'other_authority_cancel_permission' in response:
             self.other_authority_cancel_permission = response[
                 'other_authority_cancel_permission']
         if 'source_information' in response:
             self.source_information = response['source_information']
         if 'source_information_id' in response:
             self.source_information_id = response['source_information_id']
         if 'send_payment_link_info' in response:
             current_app.logger.info(
                 "send_payment_link_info in session state")
             self.send_payment_link_info = PaymentLink.from_json(
                 response['send_payment_link_info'])
         if 'payment_info' in response:
             current_app.logger.info("payment_info in session state")
             self.payment_info = PaymentInfo.from_json(
                 response['payment_info'])
         if 'search_details' in response:
             self.search_details = SearchDetails.from_json(
                 response['search_details'])
    def test_value_set(self):
        search_extent_geo = {
            'features': [{
                'geometry': {
                    'coordinates': [[[-378838.7455502291, 6966202.685233321],
                                     [159887.69930341933, 6965138.008464836],
                                     [177987.20436767233, 6568013.573819755],
                                     [-456560.1496496685, 6562690.189977327],
                                     [-378838.7455502291, 6966202.685233321]]]
                }
            }]
        }

        category_obj = Category(name="top-level",
                                display_name="Top Level",
                                sub_categories=[],
                                parent=None,
                                statutory_provisions=[],
                                instruments=[])

        add_lon_charge_state_obj = LightObstructionNoticeItem()
        llc1_state_obj = LLC1Search()
        last_created_charge_obj = LastCreatedCharge()

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

            state = LocalLandChargeItem()

            state.statutory_provision = "test_provision"
            state.charge_geographic_description = "test_geo_location"
            state.expiry_date = date(2011, 1, 1)
            state.further_information_location = "test_fi_location"
            state.further_information_reference = "test_fi_reference"

            sess = Session('abc')
            sess.user = user
            sess.add_charge_state = state
            sess.redirect_route = 'some route for redirection'
            sess.two_factor_authentication_passed = True
            sess.two_factor_authentication_code = 123
            sess.two_factor_authentication_redirect_url = 'redirect url'
            sess.two_factor_authentication_generation_time = 'generation time'
            sess.two_factor_authentication_invalid_attempts = 0

            sess.add_lon_charge_state = add_lon_charge_state_obj
            sess.llc1_state = llc1_state_obj
            sess.last_created_charge = last_created_charge_obj
            sess.statutory_provision_list = 'statutory provision list'
            sess.edited_fields = ['field']
            sess.search_extent = search_extent_geo
            sess.filenames = {
                'form_a': '',
                'temporary_lon_cert': '',
                'definitive_lon_cert': ''
            }
            sess.previously_selected_address = {
                "address": "previously selected address"
            }
            sess.adding_charge_for_other_authority = False
            sess.submit_token = 'a submit token'
            sess.category_details = category_obj
            sess.category_confirmation = True
            sess.upload_shapefile_processed = True
            sess.charge_added_outside_users_authority = True
            sess.other_authority_update_permission = True
            sess.other_authority_cancel_permission = True

            g.trace_id = "test_id"

            self.assertIsNotNone(sess)

            result = sess.to_dict()

            self.assertEqual(result['add_charge_state']['statutory-provision'],
                             "test_provision")
            self.assertEqual(
                result['add_charge_state']['charge-geographic-description'],
                "test_geo_location")
            self.assertEqual(result['add_charge_state']['expiry-date'],
                             "2011-01-01")
            self.assertEqual(
                result['add_charge_state']['further-information-location'],
                "test_fi_location")
            self.assertEqual(
                result['add_charge_state']['further-information-reference'],
                "test_fi_reference")
            self.assertEqual(result['redirect_route'],
                             'some route for redirection')
            self.assertEqual(result['add_lon_charge_state'],
                             add_lon_charge_state_obj.to_json())
            self.assertEqual(result['llc1_state'], llc1_state_obj.to_json())
            self.assertEqual(result['last_created_charge'],
                             last_created_charge_obj.__dict__)
            self.assertEqual(result['statutory_provision_list'],
                             'statutory provision list')
            self.assertEqual(result['edited_fields'], ['field'])
            self.assertEqual(result['search_extent'], search_extent_geo)
            self.assertEqual(result['filenames'], {
                'form_a': '',
                'temporary_lon_cert': '',
                'definitive_lon_cert': ''
            })
            self.assertEqual(result['previously_selected_address'],
                             {"address": "previously selected address"})
            self.assertEqual(result['submit_token'], 'a submit token')
            self.assertEqual(result['category_details'],
                             category_obj.to_json())
            self.assertEqual(result['category_confirmation'], True)
            self.assertEqual(result['upload_shapefile_processed'], True)
            self.assertEqual(result['charge_added_outside_users_authority'],
                             True)
            self.assertEqual(result['other_authority_update_permission'], True)
            self.assertEqual(result['other_authority_cancel_permission'], True)

            two_factor_authentication_result = sess.two_factor_authentication_to_dict(
            )

            self.assertEqual(
                two_factor_authentication_result[
                    'two_factor_authentication_passed'], True)
            self.assertEqual(
                two_factor_authentication_result[
                    'two_factor_authentication_code'], 123)
            self.assertEqual(
                two_factor_authentication_result[
                    'two_factor_authentication_redirect_url'], 'redirect url')
            self.assertEqual(
                two_factor_authentication_result[
                    'two_factor_authentication_generation_time'],
                'generation time')
            self.assertEqual(
                two_factor_authentication_result[
                    'two_factor_authentication_invalid_attempts'], 0)