def test_404_is_raised_when_no_poll_exists(self): backend = Backend(name="test") self.view.get_connection = Mock(return_value=Connection(identity="77777", backend=backend)) self.view.get_backend = Mock(return_value=backend) self.view.contact_exists = Mock(return_value=True) with self.assertRaises(Http404): response = self.get_http_response_from_view(self.build_kwargs("my_backend","77777","12"), self.view)
def test_that_if_there_are_no_polls_for_the_user_it_returns_success_and_an_empty_list(self): backend = Backend(id=89) connection = Connection(identity=999, backend=backend, contact=Contact()) fake_request = self.setup_get_request(backend, connection) self.view.get_polls_for_contact = Mock(return_value=[]) response = self.view.dispatch(fake_request) data = json.loads(response.content) self.assertEqual(True, data['success']) self.assertEqual([], data['poll_topics'])
def test_404_is_raised_when_no_contact_exists(self): self.view.get_backend = Mock(return_value=Backend(name="test")) self.view.get_connection = Mock(return_value=None) self.view.contact_exists = Mock(return_value=False) response = self.get_http_response_from_view( \ self.build_kwargs("my_backend","77777","12"), self.view) data = json.loads(response.content) self.assertEquals(404, response.status_code) self.assertDictEqual({"success": False, "reason": "Ureporter not found"}, data)
def test_that_poll_null_for_a_registered_user_with_no_poll(self): self.setup_fake_connection() self.setup_fake_poll(None) self.view.get_backend = Mock(return_value=Backend(name="my_backend")) response = self.get_http_response_from_view( { "backend": "my_backend", "user_address": "77777" }, self.view) data = json.loads(response.content) self.assertEqual(True, data['success']) self.assertEqual(None, data['poll'])
def test_that_return_not_registered_if_contact_does_not_exist(self): view = ViewUReporter() fake_check = Mock() fake_check.return_value = {"id": "", "language": "", "registered": False} view.get_contact = fake_check kwargs = {"backend": "console", "user_address": "999"} view.get_backend = Mock(return_value=Backend(name="my_backend")) view.get_connection = Mock(return_value=Connection(identity="999")) http_response = self.get_http_response_from_view(kwargs, view) json_string = http_response.content data = json.loads(json_string) self.assertDictEqual({"success": False, "reason": "Ureporter not found"}, data)
def test_that_poll_data_for_a_registered_user_that_has_a_poll(self): self.setup_fake_connection() poll_data = {"id": 1, "name": "Test Poll", "question": "Is working?"} fake_poll = self.setup_fake_poll(poll_data) self.view.get_current_poll_for = fake_poll self.view.get_backend = Mock(return_value=Backend(name="my_backend")) response = self.get_http_response_from_view( { "backend": "my_backend", "user_address": "77777" }, self.view) data = json.loads(response.content) self.assertEqual(True, data['success']) self.assertEqual(poll_data, data['poll'])
def test_that_return_registered_true_if_contact_exists(self): view = ViewUReporter() fake_check = Mock() fake_check.return_value = {"id": 12, "language": "en", "registered": True} view.get_contact = fake_check kwargs = {"backend": "console", "user_address": "999"} view.get_backend = Mock(return_value=Backend(name="my_backend")) view.get_connection = Mock(return_value=Connection(identity="999")) http_response = self.get_http_response_from_view(kwargs, view) json_string = http_response.content data = json.loads(json_string) self.assertEqual(True, data['success']) self.assertEqual(True, data['user']['registered']) self.assertEqual('en', data['user']['language'])
def test_that_valid_get_request_returns_poll_summary(self): backend = Backend(name="test") self.view.get_connection = Mock(return_value=Connection(identity="77777", backend=backend)) self.view.get_backend = Mock(return_value=backend) self.view.contact_exists = Mock(return_value=True) self.view.get_poll = Mock(return_value=Poll(question="Testing?")) poll_summary = {"total": 1, "responses": [{"name": "T", "count": 1}]} self.view.get_poll_summary = Mock(return_value=poll_summary) expected_data = { "success": True, "poll_result": poll_summary } response = self.get_http_response_from_view(self.build_kwargs("my_backend","77777","12"), self.view) data = json.loads(response.content) self.assertEquals(200,response.status_code) self.assertDictEqual(expected_data, data)
def test_that_script_progress_moves_on_when_current_step_is_message(self): self.setup_fake_connection() mock_progress = MagicMock() mock_progress.moveon = MagicMock() self.view.get_script_progress = Mock(return_value=mock_progress) self.view.contact_exists = Mock(return_value=False) self.view.get_current_step = Mock(return_value=ScriptStep( message="Welcome", script=Script( slug='ureport_autoreg2'), order=1)) self.view.get_backend = Mock(return_value=Backend(name="my_backend")) response = self.get_http_response_from_view( { "backend": "my_backend", "user_address": "77777" }, self.view) self.assertEqual(True, mock_progress.moveon.called)
def test_should_insert_a_classifier_message_for_each_category(self): categories = self.create_dummy_categories() self.load_ibm_seed_data.create_ibm_message_category = self.create_ibm_message_category_mock self.load_ibm_seed_data.create_backend = lambda name: Backend(name=name ) self.load_ibm_seed_data.create_connection = lambda backend, identity: Connection( backend=backend, identity=identity) self.load_ibm_seed_data.create_message = lambda connection, text, direction, status: ( Message(connection=connection, text=text, direction=direction, status=status), True) self.load_ibm_seed_data.create_seed_data(categories) self.assertEquals(self.categories_with_messages_inserted, categories)
def test_that_retrieves_poll_data_from_step_of_script_progress(self): fake_connection = Mock() fake_connection.return_value = self.build_connection(None) self.view.get_connection = fake_connection self.view.get_script_progress = Mock(return_value=ScriptProgress( script=Script(slug="ureport_autoreg2"))) fake_get_next_step = Mock(return_value=ScriptStep( poll=Poll(name="test poll", question="Is it working?"))) self.view.get_current_step = fake_get_next_step self.view.get_backend = Mock(return_value=Backend(name="my_backend")) response = self.get_http_response_from_view( { "backend": "my_backend", "user_address": "77777" }, self.view) data = json.loads(response.content) self.assertEqual(True, data['poll']['is_registration'])
def test_that_for_an_unregistered_user_script_progress_object_is_created_if_it_does_not_exist( self): fake_connection = Mock() fake_connection.return_value = self.build_connection(None) self.view.get_connection = fake_connection self.view.get_current_step = Mock(return_value=None) self.view.get_script_progress = Mock(return_value=ScriptProgress( script=Script(slug="ureport_autoreg2"))) self.view.get_backend = Mock(return_value=Backend(name="my_backend")) response = self.get_http_response_from_view( { "backend": "my_backend", "user_address": "77777" }, self.view) data = json.loads(response.content) self.assertEqual(True, data['success']) self.assertEqual("ureport_autoreg2", self.view.script_progress.script.slug)
def build_connection(self, contact=Contact()): return Connection(identity=77777, backend=Backend(name='my backend'), contact=contact)
def test_that_if_the_user_address_does_not_exist_you_get_a_404(self): backend = Backend(id=89) connection = Connection(identity=999, backend=backend) fake_request = self.setup_post_request(backend, connection) with self.assertRaises(Http404): response = self.view.dispatch(fake_request)
def if_exists_delete_backend(name): try: backend = Backend.objects.get(name=name) Backend.delete(backend) except Backend.DoesNotExist: pass