Exemplo n.º 1
0
 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()
     new_survey.question_set.add(*questions)
    def test_render_context(self):
        survey = Survey(title='A testing survey')
        survey.save()

        question_one = Question(body='Question one', kind=Question.VOICE, survey=survey)
        question_one.save()

        question_response = QuestionResponse(response='gopher://someaudio.mp3',
                                             call_sid='sup3runiq3',
                                             phone_number='+14155552671',
                                             question=question_one)

        question_response.save()

        redirect = self.client.get(reverse('app_root'))
        survey_results_url = reverse('survey_results', kwargs={'survey_id': survey.id})

        assert survey_results_url in redirect.url

        response = self.client.get(survey_results_url)

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

        assert expected_responses == response.context['responses']
        assert survey.title == response.context['survey_title']
Exemplo n.º 3
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()
    def test_entry_point_redirection(self):
        survey = Survey(title='A testing survey')
        survey.save()

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

        assert expected_url in response.url
Exemplo n.º 5
0
 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()
     new_survey.question_set.add(*questions)
    def test_show_survey(self):
        survey = Survey(title='A testing survey')
        survey.save()
        Question(body='A Question', kind=Question.VOICE, survey=survey).save()

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

        assert survey.title in response.content.decode('utf8')
    def load_survey(self):
        survey = json.loads(self.survey_content)
        new_survey = Survey(title=survey['title'])
        questions = map(
            lambda q: Question(
                body=q['body'],
                kind=q['kind']),
            survey['questions'])

        new_survey.save()
        new_survey.question_set.add(*questions)
Exemplo n.º 8
0
class SurveyRedirectionTest(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_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')
    def test_redirect_to_first_question(self):
        survey = Survey(title='A testing survey')
        survey.save()

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

        question_ids = {'survey_id': survey.id, 'question_id': question.id}
        question_url = reverse('question', kwargs=question_ids)

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

        assert question_url in response.content.decode('utf8')
class SurveyRedirectionTest(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_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')
    def setUp(self):
        self.survey = Survey(title="A testing survey")
        self.survey.save()

        self.question = Question(body="A Question", kind=Question.VOICE, survey=self.survey)
        self.question.save()

        self.question_ids = {"survey_id": self.survey.id, "question_id": self.question.id}
    def setUp(self):
        self.survey = Survey(title='A testing survey')
        self.survey.save()

        self.question_one = Question(body='Question one', kind=Question.VOICE, survey=self.survey)
        self.question_one.save()
        self.question_ids_one = {'survey_id': self.survey.id,
                                 'question_id': self.question_one.id}
    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()

        self.question_ids = {'survey_id': self.survey.id,
                             'question_id': self.question.id}
class ShowQuestionTest(TestCase):
    def setUp(self):
        self.survey = Survey(title="A testing survey")
        self.survey.save()

        self.question = Question(body="A Question", kind=Question.VOICE, survey=self.survey)
        self.question.save()

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

    def test_show_voice_question(self):
        question_store_url = reverse("question", kwargs=self.question_ids)

        self.question.kind = Question.VOICE
        self.question.save()

        voice_response = self.client.get(reverse("question", kwargs=self.question_ids))

        assert self.question.body in voice_response.content.decode("utf8")
        assert "Record" in voice_response.content.decode("utf8")
        assert question_store_url in voice_response.content.decode("utf8")

    def test_show_numeric_question(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(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")
class ShowQuestionTest(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()

        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
 def setUp(self):
     self.survey = Survey(title='A testing survey')
     self.survey.save()
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 pytest.raises(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
class StoreQuestionResponseTest(TestCase):

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

        self.question_one = Question(body='Question one', kind=Question.VOICE, survey=self.survey)
        self.question_one.save()
        self.question_ids_one = {'survey_id': self.survey.id,
                                 'question_id': self.question_one.id}

    def test_store_response(self):
        question_store_url = reverse('record_response', kwargs=self.question_ids_one) + '?Kind=voice'

        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_one.id)

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

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

        question_store_url_one = reverse('record_response', kwargs=self.question_ids_one) + '?Kind=numeric'
        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 response.status_code == 303
        assert next_question_url in response.url

    def test_validate_question_kind(self):
        invalid_question_store_url = reverse('record_response', kwargs=self.question_ids_one) + '?Kind=lol'
        request_parameters = {
            'CallSid': 'somerandomuniqueid',
            'From': '324238944',
            'Digits': '4'
        }
        with pytest.raises(NoSuchQuestionKindException):
            self.client.post(invalid_question_store_url, request_parameters)

    def test_last_question(self):
        question_store_url_one = reverse('record_response', kwargs=self.question_ids_one) + '?Kind=numeric'
        request_parameters = {
            'CallSid': 'somerandomuniqueid',
            'From': '324238944',
            'Digits': '4'
        }

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

        assert response.status_code == 200
        assert 'Redirect' not in response.content.decode('utf8')
 def setUp(self):
     self.survey = Survey(title='A testing survey')
     self.survey.save()
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 pytest.raises(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