Esempio n. 1
0
    def test_post_redirects_when_single_address_chosen(self, session, mock_address_converter):
        with app.test_request_context():
            self.client.set_cookie('localhost', Session.session_cookie_name, 'cookie_value')
            self.mock_session.return_value.llc1_state = LLC1Search()
            self.mock_session.return_value.user.permissions = [Permissions.request_llc1]

            g.session = MagicMock()
            response = MagicMock()
            response.status_code = 201
            session.return_value.post.return_value = response
            selected_address = {
                'address': 'display address',
                'line_1': 'Flat 1',
                'line_2': 'Place',
                'line_3': 'Holder',
                'line_4': 'Flat 1',
                'line_5': 'Flat 1',
                'line_6': 'Flat 1',
                'postcode': 'postcode',
                'uprn': 123456789
            }

            mock_address_converter.to_charge_address.return_value = selected_address

            response = self.client.post(url_for("create_llc1.llc1_set_description"), data={
                'has-address': 'ProvideAddress',
                'selected-address': json.dumps(selected_address),
            })
            self.assertEqual(self.mock_session.return_value.llc1_state.description, 'Flat 1, Place, Holder, Flat 1, '
                                                                                    'Flat 1, Flat 1 postcode')
            self.assert_status(response, 302)
            self.assertRedirects(response, url_for("create_llc1.llc1_get_result"))
Esempio n. 2
0
    def test_session_expire_success_on_exception(self):
        with app.test_request_context():
            g.requests = MagicMock()
            g.trace_id = '123'
            response = MagicMock()
            response.status_code = 200
            g.requests.delete.side_effect = Exception('test exception')

            SessionAPIService.expire_session("abc")

            self.assertTrue(True)
Esempio n. 3
0
    def test_session_valid_exception(self):
        with app.test_request_context():
            g.requests = MagicMock()
            g.trace_id = '123'
            response = MagicMock()
            response.status_code = 200
            g.requests.get.side_effect = Exception('test exception')

            result = SessionAPIService.session_valid("abc")

            self.assertFalse(result)
Esempio n. 4
0
    def test_session_expire_success_on_non_200(self):
        with app.test_request_context():
            g.requests = MagicMock()
            g.trace_id = '123'
            response = MagicMock()
            response.status_code = 200
            g.requests.delete.return_value = response

            SessionAPIService.expire_session("abc")

            self.assertTrue(True)
Esempio n. 5
0
    def test_session_valid_non_200(self):
        with app.test_request_context():
            g.requests = MagicMock()
            g.trace_id = '123'
            response = MagicMock()
            response.status_code = 500
            g.requests.get.return_value = response

            result = SessionAPIService.session_valid("abc")

            self.assertFalse(result)
Esempio n. 6
0
    def test_get_session_state_none_on_exception(self):
        with app.test_request_context():
            g.requests = MagicMock()
            g.trace_id = '123'
            response = MagicMock()
            response.status_code = 500
            response.json.return_value = {"test": '123'}
            g.requests.get.side_effect = Exception('test exception')

            result = SessionAPIService.get_session_state('abc', 'def')

            self.assertIsNone(result)
Esempio n. 7
0
    def test_create_session_successful(self):
        with app.test_request_context():
            g.requests = MagicMock()
            g.trace_id = '123'
            response = MagicMock()
            response.status_code = 200
            response.text = "session key"
            g.requests.post.return_value = response

            result = SessionAPIService.create_session('abc')

            self.assertIsNotNone(result)
            self.assertEqual('session key', result)
Esempio n. 8
0
    def test_get_session_state_successful(self):
        with app.test_request_context():
            g.requests = MagicMock()
            g.trace_id = '123'
            response = MagicMock()
            response.status_code = 200
            response.json.return_value = {"test": '123'}
            g.requests.get.return_value = response

            result = SessionAPIService.get_session_state('abc', 'def')

            self.assertIsNotNone(result)
            self.assertEqual({"test": '123'}, result)
Esempio n. 9
0
    def test_update_session_successful(self):
        with app.test_request_context():
            g.requests = MagicMock()
            g.trace_id = '123'
            response = MagicMock()
            response.status_code = 201
            response.text = "session key"
            g.requests.put.return_value = response

            SessionAPIService.update_session_data(
                1,
                {'data': 'some value'},
                TEST_SUBSECTION
            )

            self.assertTrue(True)
Esempio n. 10
0
    def test_create_session_exception_response(self, mock_app):
        with app.test_request_context():
            g.requests = MagicMock()
            g.trace_id = '123'
            response = MagicMock()
            response.status_code = 500
            response.text = "session key"
            g.requests.post.side_effect = Exception('test exception')

            try:
                SessionAPIService.create_session('abc')
            except ApplicationError as ex:
                self.assertEqual(ex.http_code, 500)
                mock_app.logger.exception.assert_called()
                mock_app.logger.exception.assert_called_with(
                    'Failed to create session. TraceID : 123 - Exception - test exception')
                return
            self.fail()
Esempio n. 11
0
    def test_post_redirects_when_no_single_address_chosen_with_description(self, session):
        with app.test_request_context():
            self.client.set_cookie('localhost', Session.session_cookie_name, 'cookie_value')
            self.mock_session.return_value.llc1_state = LLC1Search()
            self.mock_session.return_value.user.permissions = [Permissions.request_llc1]

            g.session = MagicMock()
            response = MagicMock()
            response.status_code = 201
            session.return_value.post.return_value = response

            response = self.client.post(url_for("create_llc1.llc1_set_description"), data={
                'has-address': 'No',
                'charge-geographic-description': 'This is a valid description',
            })

            self.assertEqual(self.mock_session.return_value.llc1_state.description, 'This is a valid description')
            self.assert_status(response, 302)
            self.assertRedirects(response, url_for("create_llc1.llc1_get_result"))
Esempio n. 12
0
    def test_create_session_non_200_response(self, mock_app):
        with app.test_request_context():
            g.requests = MagicMock()
            g.trace_id = '123'
            response = MagicMock()
            response.status_code = 500
            response.text = "session key"
            g.requests.post.return_value = response

            try:
                SessionAPIService.create_session('abc')
            except ApplicationError as ex:
                self.assertEqual(ex.http_code, 500)
                mock_app.logger.exception.assert_called()
                mock_app.logger.exception.assert_called_with(
                    'Failed to create session. TraceID : {} - Status code:{}, message:{}'.format(
                        "123",
                        "500",
                        "session key"))
                return
            self.fail()
Esempio n. 13
0
    def test_update_session_failure_exception(self, mock_app):
        with app.test_request_context():
            g.requests = MagicMock()
            g.trace_id = '123'
            response = MagicMock()
            response.status_code = 200
            response.text = "session key"
            g.requests.put.side_effect = Exception("test")

            try:
                SessionAPIService.update_session_data(
                    1,
                    {'data': 'some value'},
                    TEST_SUBSECTION
                )
            except ApplicationError as ex:
                self.assertEqual(ex.http_code, 500)
                mock_app.logger.exception.assert_called()
                mock_app.logger.exception.assert_called_with(
                    'Failed to update session. TraceID : 123 - Exception - test')
                return
            self.fail()
Esempio n. 14
0
    def test_update_session_failure_non_200(self, mock_app):
        with app.test_request_context():
            g.requests = MagicMock()
            g.trace_id = '123'
            response = MagicMock()
            response.status_code = 500
            response.text = "session key"
            g.requests.put.return_value = response

            try:
                SessionAPIService.update_session_data(
                    1,
                    {'data': 'some value'},
                    TEST_SUBSECTION
                )
            except ApplicationError as ex:
                self.assertEqual(ex.http_code, 500)
                mock_app.logger.exception.assert_called()
                mock_app.logger.exception.assert_called_with(
                    'Error when updating session data for session ID 1. TraceID : 123 - Message - session key')
                return
            self.fail()