class PollSummaryTest(TestCase):
    def test_that_get_the_response_summary_fo_a_give_poll(self):
        expected_data = [{"name":u"Choice 1", "count":2}, {"name":u"Choice 2", "count":1}]
        data = self.view.get_responses_summary_for_poll(self.poll)
        self.assertListEqual(expected_data, data)

    def setUp(self):
        self.view = PollSummary()
        backend, backend_created = Backend.objects.get_or_create(name="test_backend")
        self.connection, connection_created = Connection.objects.get_or_create(identity="7777", backend=backend)
        self.first_contact, contact_created = Contact.objects.get_or_create(name='John Jonny')
        second_contact, contact_created = Contact.objects.get_or_create(name='Johanna J')
        message, message_created = Message.objects.get_or_create(connection=self.connection, direction="I", text="yes")
        self.user, user_created = User.objects.get_or_create(username="******")
        an_hour_ago = datetime.datetime.now() - datetime.timedelta(hours=1)
        self.poll, poll_created = Poll.objects.get_or_create(name="Test Poll", question="Is working?", user=self.user, start_date=an_hour_ago)
        first_category, first_category_created = Category.objects.get_or_create(name="Choice 1", poll=self.poll)
        second_category, second_category_created = Category.objects.get_or_create(name="Choice 2", poll=self.poll)
        first_response = Response.objects.create(message=message, poll=self.poll, contact=self.first_contact)
        second_response = Response.objects.create(message=message, poll=self.poll, contact=second_contact)
        first_response.update_categories([first_category, second_category], self.user)
        second_response.update_categories([first_category], self.user)

    def test_that_response_with_no_category_is_counted(self):
        message, message_created = Message.objects.get_or_create(connection=self.connection, direction="I", text="me")
        response = Response.objects.create(message=message, poll=self.poll, contact=self.first_contact)
        expected_data = [{"name":u"Choice 1", "count":2}, {"name":u"Choice 2", "count":1}, {"name":u"uncategorized", "count":1}]
        data = self.view.get_responses_summary_for_poll(self.poll)
        self.assertListEqual(expected_data, data)
 def setUp(self):
     self.view = PollSummary()
     backend, backend_created = Backend.objects.get_or_create(name="test_backend")
     self.connection, connection_created = Connection.objects.get_or_create(identity="7777", backend=backend)
     self.first_contact, contact_created = Contact.objects.get_or_create(name='John Jonny')
     second_contact, contact_created = Contact.objects.get_or_create(name='Johanna J')
     message, message_created = Message.objects.get_or_create(connection=self.connection, direction="I", text="yes")
     self.user, user_created = User.objects.get_or_create(username="******")
     an_hour_ago = datetime.datetime.now() - datetime.timedelta(hours=1)
     self.poll, poll_created = Poll.objects.get_or_create(name="Test Poll", question="Is working?", user=self.user, start_date=an_hour_ago)
     first_category, first_category_created = Category.objects.get_or_create(name="Choice 1", poll=self.poll)
     second_category, second_category_created = Category.objects.get_or_create(name="Choice 2", poll=self.poll)
     first_response = Response.objects.create(message=message, poll=self.poll, contact=self.first_contact)
     second_response = Response.objects.create(message=message, poll=self.poll, contact=second_contact)
     first_response.update_categories([first_category, second_category], self.user)
     second_response.update_categories([first_category], self.user)
Beispiel #3
0
    url(r"^pulse/(?P<period>\w+)/$", pulse, name='pulse_json'),
    url(r"^map-cloud/$", map_cloud, name='map_cloud'),
    url(r"^upload-contacts", upload_users, name='upload_users'),
    url(r"^access/dashboards/$", access_dashboards, name='access_dashboards'),
    url(r"^assign-group", assign_group, name="assign_group"),
    url(r'^start_poll_export/(\d+)/$',
        start_poll_export,
        name="start_poll_export"),
    url(r"^backend/vumi/$", VumiBackendView.as_view(backend_name="vumi")),
    url(r"^api/v1/ureporters/registration/steps$",
        RegistrationStepsView.as_view()),
    url(r"^api/v1/ureporters/(?P<backend>\w+)/(?P<user_address>\+?\w+)$",
        ViewUReporter.as_view(),
        name="view_ureporter_api"),
    url(r"^api/v1/ureporters/(?P<backend>\w+)/(?P<user_address>\+?\w+)/polls/current$",
        ViewCurrentPoll.as_view(),
        name="view_current_poll_api"),
    url(r"^api/v1/ureporters/(?P<backend>\w+)/(?P<user_address>\+?\w+)/poll/(?P<poll_id>\d+)/responses/$",
        SubmitPollResponses.as_view(),
        name="submit_poll_response_api"),
    url(r"^api/v1/ureporters/(?P<backend>\w+)/(?P<user_address>\+?\w+)/polls/topics$",
        PollTopicsApiView.as_view(),
        name="poll_topics_api"),
    url(r"^api/v1/ureporters/(?P<backend>\w+)/(?P<user_address>\+?\w+)/reports/$",
        SubmitReportApiView.as_view(),
        name="submit_report_api"),
    url(
        r"^api/v1/ureporters/(?P<backend>\w+)/(?P<user_address>\+?\w+)/poll/(?P<poll_id>\d+)/summary$",
        PollSummary.as_view()),
)
Beispiel #4
0
                                       ('', False, '', None)],
                       }, name="ureport-contact2"),
                       (r'^api/', include(v1_api.urls)),
                       url(r'^comfirm/(?P<key>.+)/$', comfirm_message_sending, name="comfirm"),
                       url(r'^comfirmmessages/(?P<key>.+)/$', comfirmmessages, name="comfirm-messages"),
                       url(r"^dumpreport/(\d+)/$", generate_poll_dump_report),
                       url(r"^districtreport/(\d+)/$", generate_per_district_report),
                       url(r"^pulse/$", pulse, name='pulse_json'),
                       url(r"^pulse/(?P<period>\w+)/$", pulse, name='pulse_json'),
                       url(r"^map-cloud/$", map_cloud, name='map_cloud'),
                       url(r"^upload-contacts", upload_users, name='upload_users'),
                       url(r"^access/dashboards/$", access_dashboards, name='access_dashboards'),
                       url(r"^assign-group", assign_group, name="assign_group"),
                       url(r'^start_poll_export/(\d+)/$', start_poll_export, name="start_poll_export"),
                       url(r"^backend/vumi/$", VumiBackendView.as_view(backend_name="vumi")),
                       url(r"^api/v1/ureporters/registration/steps$", RegistrationStepsView.as_view()),
                       url(r"^api/v1/ureporters/(?P<backend>\w+)/(?P<user_address>\+?\w+)$", ViewUReporter.as_view(),
                           name="view_ureporter_api"),
                       url(r"^api/v1/ureporters/(?P<backend>\w+)/(?P<user_address>\+?\w+)/polls/current$",
                           ViewCurrentPoll.as_view(), name="view_current_poll_api"),
                       url(
                           r"^api/v1/ureporters/(?P<backend>\w+)/(?P<user_address>\+?\w+)/poll/(?P<poll_id>\d+)/responses/$",
                           SubmitPollResponses.as_view(), name="submit_poll_response_api"),
                       url(r"^api/v1/ureporters/(?P<backend>\w+)/(?P<user_address>\+?\w+)/polls/topics$",
                           PollTopicsApiView.as_view(), name="poll_topics_api"),
                       url(r"^api/v1/ureporters/(?P<backend>\w+)/(?P<user_address>\+?\w+)/reports/$",
                           SubmitReportApiView.as_view(), name="submit_report_api"),
                       url(r"^api/v1/ureporters/(?P<backend>\w+)/(?P<user_address>\+?\w+)/poll/(?P<poll_id>\d+)/summary$", PollSummary.as_view()),

)
Beispiel #5
0
 def setUp(self):
     self.view = PollSummary()
Beispiel #6
0
class PollSummaryTest(TestCase):
    
    def setUp(self):
        self.view = PollSummary()

    def build_kwargs(self, backend_name, user_address, poll_id):
        return {"backend": backend_name, "user_address": user_address, "poll_id": poll_id}

    def get_http_response_from_view(self, kwargs, view):
        request_factory = RequestFactory()
        auth_string = base64.b64encode("who:why")
        fake_request = request_factory.get('/', **{"HTTP_AUTHORIZATION": ("Basic %s" % auth_string)})
        view.validate_credentials = Mock(return_value=True)
        return view.dispatch(fake_request, None, **kwargs)

    def test_404_is_raised_if_backend_does_not_exist(self):
        with self.assertRaises(Http404):
            response = self.get_http_response_from_view(self.build_kwargs("my_backend","77777","12"), self.view)

    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_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_post_returns_405_status_code(self):
        request_factory = RequestFactory()
        fake_request = request_factory.post("/")
        response = self.view.post(fake_request)
        self.assertEqual(405, response.status_code)

    def test_that_gets_poll_summary_if_poll_exists(self):
        poll = mock.Mock(spec=Poll)
        poll.id = 12
        poll.responses = mock.Mock()
        poll.responses.count = Mock(return_value=2)
        fake_summary_responses = [{"name": "Choice 1", "count": 2}, { "name": "Choice 2", "count": 1}]
        self.view.get_responses_summary_for_poll = Mock(return_value=fake_summary_responses)
        self.view.get_total_categorized_responses = Mock(return_value=3)
        data = self.view.get_poll_summary(poll)
        expected_data = {"total_responses": 2,
                         "total_categorized_responses":3,
                          "responses": [
                              {
                                "name": "Choice 1",
                                "count": 2
                              },
                              {
                                "name": "Choice 2",
                                "count": 1
                              }
                          ]
                        }
        self.assertDictEqual(expected_data, data)

    def test_that_get_responses_total_count_returns_the_sum_of_the_responses_summary(self):
        fake_summary_responses = [{"name": "Choice 1", "count": 2}, { "name": "Choice 2", "count": 1}]
        self.assertEquals(3,self.view.get_total_categorized_responses(fake_summary_responses))

    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)