示例#1
0
 def setUp(self):
     self.survey = Survey(title='A testing survey')
     self.survey.save()
     self.question = Question(body='A Question',
                              kind=Question.TEXT,
                              survey=self.survey)
     self.question.save()
示例#2
0
def _extract_request_body(request, question_kind):
    Question.validate_kind(question_kind)

    if request.is_sms:
        key = 'Body'
    elif question_kind in [Question.YES_NO, Question.NUMERIC]:
        key = 'Digits'
    elif 'TranscriptionText' in request.POST:
        key = 'TranscriptionText'
    else:
        key = 'RecordingUrl'

    return request.POST.get(key)
示例#3
0
class SurveyUnauthenticatedTest(TestCase):
    def setUp(self):
        self.survey = Survey(title='A testing survey')
        self.survey.save()
        self.question = Question(body='A Question',
                                 kind=Question.TEXT,
                                 survey=self.survey)
        self.question.save()

    def test_show_survey_redirect(self):
        response = self.client.get(
            reverse('survey', kwargs={'survey_id': self.survey.id}))

        self.assertEqual(response.status_code, 200)
    def post(self, request):
        form = SetQuestionsFormm(request.POST)
        if form.is_valid():

            # Get the questions for the form
            valid_form_questions = []
            for question_number in range(1, 11):
                if form.cleaned_data['question'+str(question_number)]:
                    valid_form_questions.append({'body': form.cleaned_data['question'+str(question_number)],
                                                 'kind': form.cleaned_data['questiontype'+str(question_number)]})

            # Create a new survey and save it in the database
            new_survey = Survey(title=form.cleaned_data["survey_title"])
            questions = [Question(body=question['body'],
                                  kind=question['kind'])
                         for question in valid_form_questions]
            new_survey.save()
            for question in questions:
                question.survey = new_survey
                question.save()
            new_survey.question_set.add(*questions)

            return render(request, 'set_questions_success.html')

        return render(request, 'set_questions_form.html', { 'form': form })
示例#5
0
    def test_store_response_and_redirect(self):
        self.create_question(Question.NUMERIC)
        question_two = Question(body='Question two', kind=Question.TEXT, survey=self.survey)
        question_two.save()
        question_ids_two = {'survey_id': self.survey.id,
                            'question_id': question_two.id}

        question_store_url_one = reverse('save_response', kwargs=self.question_ids)
        next_question_url = reverse('question', kwargs=question_ids_two)

        request_parameters = {
            'CallSid': 'somerandomuniqueid',
            'From': '324238944',
            'Digits': '4'
        }

        response = self.client.post(question_store_url_one, request_parameters)

        assert '<Redirect method="GET"' in response.content.decode('utf8')
        assert next_question_url in response.content.decode('utf8')
 def load_survey(self):
     new_survey = Survey(title=self.survey['title'])
     questions = [
         Question(body=question['body'], kind=question['kind'])
         for question in self.survey['questions']
     ]
     new_survey.save()
     for question in questions:
         question.survey = new_survey
         question.save()
     new_survey.question_set.add(*questions)
示例#7
0
class SurveyResultsTest(TestCase):
    def setUp(self):
        # Create a new user
        self.credentials = {
            'email': '*****@*****.**',
            'password': '******'
        }
        self.login_credentials = {
            'username': '******',
            'password': '******'
        }
        User = auth.get_user_model()
        user = User.objects.create_user(**self.credentials)
        self.client.login(**self.login_credentials)

        # Setup the survey
        self.survey = Survey.objects.create(title='A testing survey')
        self.question = Question(body='Question one',
                                 kind=Question.TEXT,
                                 survey=self.survey)
        self.question.save()
        QuestionResponse(response='gopher://audio.mp3',
                         call_sid='sup3runiq3',
                         phone_number='+14155552671',
                         question=self.question).save()

    def test_render_context(self):
        survey_results_url = reverse('survey_results',
                                     kwargs={'survey_id': self.survey.id})
        response = self.client.get(survey_results_url)

        expected_responses = [{
            'body': 'Question one',
            'phone_number': '+14155552671',
            'kind': 'text',
            'response': 'gopher://audio.mp3',
            'call_sid': 'sup3runiq3'
        }]

        assert expected_responses == response.context['responses']
        assert self.survey.title == response.context['survey_title']
示例#8
0
    def setUp(self):
        # Create a new user
        self.credentials = {
            'email': '*****@*****.**',
            'password': '******'
        }
        self.login_credentials = {
            'username': '******',
            'password': '******'
        }
        User = auth.get_user_model()
        user = User.objects.create_user(**self.credentials)
        self.client.login(**self.login_credentials)

        # Setup the survey
        self.survey = Survey(title='A testing survey')
        self.survey.save()
        self.question = Question(body='A Question',
                                 kind=Question.TEXT,
                                 survey=self.survey)
        self.question.save()
    def setUp(self):
        # Create a new user
        self.credentials = {'email': '*****@*****.**', 'password': '******'}
        self.login_credentials = {
            'username': '******',
            'password': '******'
        }
        User = auth.get_user_model()
        user = User.objects.create_user(**self.credentials)
        self.client.login(**self.login_credentials)

        self.survey = Survey(title='A testing survey')
        self.survey.save()

        self.question = Question(body='A Question',
                                 kind=Question.TEXT,
                                 survey=self.survey)
        self.question.save()

        self.question_ids = {
            'survey_id': self.survey.id,
            'question_id': self.question.id
        }
示例#10
0
    def setUp(self):
        # Create a new user
        self.credentials = {
            'email': '*****@*****.**',
            'password': '******'
        }
        self.login_credentials = {
            'username': '******',
            'password': '******'
        }
        User = auth.get_user_model()
        user = User.objects.create_user(**self.credentials)
        self.client.login(**self.login_credentials)

        # Setup the survey
        self.survey = Survey.objects.create(title='A testing survey')
        self.question = Question(body='Question one',
                                 kind=Question.TEXT,
                                 survey=self.survey)
        self.question.save()
        QuestionResponse(response='gopher://audio.mp3',
                         call_sid='sup3runiq3',
                         phone_number='+14155552671',
                         question=self.question).save()
class ShowQuestionTest(TestCase):
    def setUp(self):
        # Create a new user
        self.credentials = {'email': '*****@*****.**', 'password': '******'}
        self.login_credentials = {
            'username': '******',
            'password': '******'
        }
        User = auth.get_user_model()
        user = User.objects.create_user(**self.credentials)
        self.client.login(**self.login_credentials)

        self.survey = Survey(title='A testing survey')
        self.survey.save()

        self.question = Question(body='A Question',
                                 kind=Question.TEXT,
                                 survey=self.survey)
        self.question.save()

        self.question_ids = {
            'survey_id': self.survey.id,
            'question_id': self.question.id
        }

    def test_show_text_question_during_a_call(self):
        question_store_url = reverse('question', kwargs=self.question_ids)

        text_response = self.client.get(
            reverse('question', kwargs=self.question_ids))

        assert self.question.body in text_response.content.decode('utf8')
        assert '<Record' in text_response.content.decode('utf8')
        assert question_store_url in text_response.content.decode('utf8')

    def test_transcription_is_enabled(self):
        save_url = reverse('save_response', kwargs=self.question_ids)
        expected_attribute = 'transcribeCallback="%s"' % (save_url)

        text_response = self.client.get(
            reverse('question', kwargs=self.question_ids))

        assert expected_attribute in text_response.content.decode('utf8')

    def test_show_numeric_question_during_a_call(self):
        self.question.kind = Question.NUMERIC
        self.question.save()

        numeric_response = self.client.get(
            reverse('question', kwargs=self.question_ids))

        assert 'Gather' in numeric_response.content.decode('utf8')

    def test_show_yesno_question_during_a_call(self):
        self.question.kind = Question.YES_NO
        self.question.save()

        yesno_response = self.client.get(
            reverse('question', kwargs=self.question_ids))

        assert 'Gather' in yesno_response.content.decode('utf8')

    def test_uses_proper_verbs_for_sms(self):
        sms_parameters = {'MessageSid': 'SMS123'}

        text_response = self.client.get(
            reverse('question', kwargs=self.question_ids), sms_parameters)

        assert self.question.body in text_response.content.decode('utf8')
        assert '<Message' in text_response.content.decode('utf8')
        assert '<Record' not in text_response.content.decode('utf8')
        assert '<Say' not in text_response.content.decode('utf8')

    def test_sms_creates_a_web_session(self):
        sms_parameters = {'MessageSid': 'SMS123'}

        self.client.get(reverse('question', kwargs=self.question_ids),
                        sms_parameters)

        assert self.client.session["answering_question_id"] == self.question.id
示例#12
0
class StoreQuestionResponseTest(TestCase):

    def setUp(self):
        self.survey = Survey(title='A testing survey')
        self.survey.save()

    def create_question(self, kind):
        self.question = Question(body='Question one', kind=kind,
                                 survey=self.survey)
        self.question.save()
        self.question_ids = {'survey_id': self.survey.id,
                             'question_id': self.question.id}

    def test_store_response_during_a_call(self):
        self.create_question(Question.TEXT)
        question_store_url = reverse('save_response', kwargs=self.question_ids)

        request_parameters = {
            'CallSid': 'somerandomuniqueid',
            'From': '324238944',
            'RecordingUrl': 'gopher://recording.mp3'
        }

        self.client.post(question_store_url, request_parameters)
        new_response = QuestionResponse.objects.get(question_id=self.question.id)

        assert new_response.call_sid == 'somerandomuniqueid'
        assert new_response.phone_number == '324238944'
        assert new_response.response == 'gopher://recording.mp3'

    def test_store_transcription_update(self):
        self.create_question(Question.TEXT)
        question_store_url = reverse('save_response', kwargs=self.question_ids)

        request_parameters = {
            'CallSid': 'somerandomuniqueid',
            'From': '324238944',
            'RecordingUrl': 'gopher://recording.mp3'
        }

        self.client.post(question_store_url, request_parameters)

        request_parameters['TranscriptionText'] = 'Do you hear me?'
        self.client.post(question_store_url, request_parameters)

        new_response = QuestionResponse.objects.get(question_id=self.question.id)

        assert new_response.call_sid == 'somerandomuniqueid'
        assert new_response.phone_number == '324238944'
        assert new_response.response == 'Do you hear me?'

    def test_store_SMS_response(self):
        self.create_question(Question.TEXT)
        question_store_url = reverse('save_response', kwargs=self.question_ids)

        request_parameters = {
            'MessageSid': 'somerandomuniqueid',
            'From': '324238944',
            'Body': 'I agree with you'
        }

        self.client.post(question_store_url, request_parameters)
        new_response = QuestionResponse.objects.get(question_id=self.question.id)

        assert new_response.call_sid == 'somerandomuniqueid'
        assert new_response.phone_number == '324238944'
        assert new_response.response == 'I agree with you'

    def test_store_response_and_redirect(self):
        self.create_question(Question.NUMERIC)
        question_two = Question(body='Question two', kind=Question.TEXT, survey=self.survey)
        question_two.save()
        question_ids_two = {'survey_id': self.survey.id,
                            'question_id': question_two.id}

        question_store_url_one = reverse('save_response', kwargs=self.question_ids)
        next_question_url = reverse('question', kwargs=question_ids_two)

        request_parameters = {
            'CallSid': 'somerandomuniqueid',
            'From': '324238944',
            'Digits': '4'
        }

        response = self.client.post(question_store_url_one, request_parameters)

        assert '<Redirect method="GET"' in response.content.decode('utf8')
        assert next_question_url in response.content.decode('utf8')

    def test_validate_question_kind(self):
        self.create_question('invalid')
        invalid_question_store_url = reverse('save_response', kwargs=self.question_ids)
        request_parameters = {
            'CallSid': 'somerandomuniqueid',
            'From': '324238944',
            'Digits': '4'
        }
        with self.assertRaises(ValidationError):
            self.client.post(invalid_question_store_url, request_parameters)

    def test_last_question_during_a_call(self):
        self.create_question(Question.NUMERIC)
        question_store_url_one = reverse('save_response', kwargs=self.question_ids)
        request_parameters = {
            'CallSid': 'somerandomuniqueid',
            'From': '324238944',
            'Digits': '4'
        }

        response = self.client.post(question_store_url_one, request_parameters)
        response = response.content.decode('utf8')

        assert 'Good-bye' in response
        assert '<Say>' in response
        assert '<Hangup' in response

    def test_last_question_over_sms(self):
        self.create_question(Question.NUMERIC)
        question_store_url_one = reverse('save_response', kwargs=self.question_ids)
        request_parameters = {
            'MessageSid': 'somerandomuniqueid',
            'From': '324238944',
            'Body': '4'
        }

        response = self.client.post(question_store_url_one, request_parameters)
        response = response.content.decode('utf8')

        assert 'Good-bye' in response
        assert '<Message>' in response
        assert 'Redirect' not in response
示例#13
0
 def create_question(self, kind):
     self.question = Question(body='Question one', kind=kind,
                              survey=self.survey)
     self.question.save()
     self.question_ids = {'survey_id': self.survey.id,
                          'question_id': self.question.id}
示例#14
0
class SurveyRedirectionTest(TestCase):
    def setUp(self):
        # Create a new user
        self.credentials = {
            'email': '*****@*****.**',
            'password': '******'
        }
        self.login_credentials = {
            'username': '******',
            'password': '******'
        }
        User = auth.get_user_model()
        user = User.objects.create_user(**self.credentials)
        self.client.login(**self.login_credentials)

        # Setup the survey
        self.survey = Survey(title='A testing survey')
        self.survey.save()
        self.question = Question(body='A Question',
                                 kind=Question.TEXT,
                                 survey=self.survey)
        self.question.save()

    def test_default_entry_point_redirection(self):
        response = self.client.post(reverse('first_survey'))
        expected_url = reverse('survey', kwargs={'survey_id': self.survey.id})

        assert expected_url in response.url

    def test_redirects_to_save_response_due_session_variable(self):
        session = self.client.session
        session['answering_question_id'] = self.question.id
        session.save()

        response = self.client.post(reverse('first_survey'))

        expected_url = reverse('save_response',
                               kwargs={
                                   'survey_id': self.question.survey.id,
                                   'question_id': self.question.id
                               })
        assert expected_url in response.url

    def test_show_message_verb_on_sms(self):
        response = self.client.get(
            reverse('survey', kwargs={'survey_id': self.survey.id}),
            {'MessageSid': '123'})

        assert '<Message>' in response.content.decode('utf8')
        assert '<Say>' not in response.content.decode('utf8')

    def test_show_survey(self):
        response = self.client.get(
            reverse('survey', kwargs={'survey_id': self.survey.id}))

        assert self.survey.title in response.content.decode('utf8')

    def test_redirect_to_first_question(self):
        response = self.client.get(
            reverse('survey', kwargs={'survey_id': self.survey.id}))

        url_parameters = {
            'survey_id': self.survey.id,
            'question_id': self.question.id
        }
        redirect_url = reverse('question', kwargs=url_parameters)

        assert redirect_url in response.content.decode('utf8')