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)
 def populate_user(self):
     """Populates the current authenticated user from session state."""
     current_app.logger.info(
         "Method called, getting session user from session api")
     response = SessionAPIService.get_session_state(
         self.session_key, Session.session_user_key)
     if response is not None:
         current_app.logger.info("User data returned from session state")
         self.user = User.from_dict(response)
    def test_user_initialisation(self):
        user = User()
        user.id = 'id'
        user.first_name = 'joe'
        user.surname = 'bloggs'
        user.email = 'email'
        user.organisation = 'testorg'
        user.roles = ['testrole']
        user.status = 'Active'
        user.jwt = "MOCK.JWT"

        self.assertIsNotNone(user)
        self.assertEqual(user.id, 'id')
        self.assertEqual(user.first_name, 'joe')
        self.assertEqual(user.surname, 'bloggs')
        self.assertEqual(user.email, 'email')
        self.assertEqual(user.organisation, 'testorg')
        self.assertEqual(user.roles, ['testrole'])
        self.assertEqual(user.status, 'Active')
        self.assertEqual(user.jwt, 'MOCK.JWT')
    def test_post_add_land_charge_success(self, mock_app):
        with main.app.test_request_context():
            g.requests = MagicMock()
            g.trace_id = '123'
            g.session = self.mock_session
            user = User()
            user.email = "abc"
            self.mock_session = user
            response = MagicMock()
            response.status_code = 202
            response.json.return_value = {"land_charge_id": "123",
                                          "entry_number": "1",
                                          "registration_date": "2000-01-01"}
            response.text = 'Success'
            g.requests.post.return_value = response

            MaintainApiService.add_charge(LocalLandChargeItem())

            self.assertIsNotNone(g.session.last_created_charge)
            self.assertEqual(g.session.last_created_charge.charge_id, "123")
            self.assertEqual(g.session.last_created_charge.registration_date, "01/01/2000")
            self.assertEqual(g.session.last_created_charge.entry_number, "1")
            self.assertEqual(g.requests.post.call_count, 1)
    def test_put_update_land_charge_success(self, mock_app):
        with main.app.test_request_context():
            g.requests = MagicMock()
            g.trace_id = '123'
            g.session = self.mock_session
            user = User()
            user.username = "******"
            self.mock_session = user
            response = MagicMock()
            response.status_code = 202
            response.json.return_value = {
                "entry_number": "1", "land_charge_id": "4", "registration_date": "2012-12-12"
            }
            response.text = 'Success'
            g.requests.put.return_value = response

            MaintainApiService.update_charge(LocalLandChargeItem.from_json(s8))

            self.assertIsNotNone(g.session.last_created_charge)
            self.assertEqual(g.session.last_created_charge.charge_id, "4")
            self.assertEqual(g.session.last_created_charge.registration_date, "12/12/2012")
            self.assertEqual(g.session.last_created_charge.entry_number, "1")
            self.assertEqual(g.requests.put.call_count, 1)
    def test_user_from_json(self):
        test = dict()
        test["id"] = 'id'
        test["first_name"] = "joe"
        test["surname"] = "bloggs"
        test["email"] = "testemail"
        test["organisation"] = "testorg"
        test["roles"] = ["testrole"]
        test["status"] = "Active"
        test["jwt"] = "MOCK.JWT"

        user = User.from_dict(test)

        self.assertEqual(user.id, 'id')
        self.assertEqual(user.first_name, "joe")
        self.assertEqual(user.surname, "bloggs")
        self.assertEqual(user.email, "testemail")
        self.assertEqual(user.organisation, "testorg")
        self.assertEqual(user.roles, ['testrole'])
        self.assertEqual(user.status, "Active")
        self.assertEqual(user.jwt, "MOCK.JWT")
    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)