Ejemplo n.º 1
0
    def test_send_verification_code(self):
        """ Test users/send_verification_code.
        """
        # Create a user with a phone number, for testing.

        formatted_number = utils.format_phone_number(PHONE_NUMBER)

        User.objects.filter(phone_number=formatted_number).delete()

        user = users.create(phone_number=PHONE_NUMBER)

        # Set up a signal listener to check that the verification message is
        # being sent via Twilio.

        self.twilio_sms_sent   = False # initially.
        self.twilio_from_phone = None  # ditto.
        self.twilio_to_phone   = None  # ditto.

        def twilio_signal_handler(sender, **kwargs):
            self.twilio_sms_sent   = True
            self.twilio_from_phone = kwargs.get("from_phone")
            self.twilio_to_phone   = kwargs.get("to_phone")

        signals.twilio_sms_sent.connect(twilio_signal_handler)

        # Ask the user API to send the verification code.  Note that we disable
        # Twilio so that no actual SMS is sent.

        with self.settings(ENABLE_TWILIO=False):
            users.send_verification_code(user_id=user['id'])

        # Check that the Twilio gateway sent the verification message.

        self.assertTrue(self.twilio_sms_sent)
        self.assertEqual(self.twilio_from_phone, settings.SYSTEM_PHONE_NUMBER)
        self.assertEqual(self.twilio_to_phone, formatted_number)

        # Run the test again, this time supplying the phone number rather than
        # the user ID.

        self.twilio_sms_sent = False

        with self.settings(ENABLE_TWILIO=False):
            users.send_verification_code(phone_number=PHONE_NUMBER)

        self.assertTrue(self.twilio_sms_sent)

        # Finally, clean everything up.

        signals.twilio_sms_sent.disconnect(twilio_signal_handler)
Ejemplo n.º 2
0
    def test_stop_and_restart(self):
        """ Test conversations/stop and conversations/restart.

            Note that this is combined into a single unit test because most of
            the complexity is in setting up the conversation -- it makes sense
            to test both at once so we don't have to set up the conversation
            twice.
        """
        # Create two random users for testing.

        username_1 = utils.random_username()
        password_1 = utils.random_password()

        username_2 = utils.random_username()
        password_2 = utils.random_password()

        user_1_id = users.create(username=username_1,
                                 password=password_1,
                                 phone_number=PHONE_NUMBER)['id']
        user_2_id = users.create(username=username_2,
                                 password=password_2,
                                 phone_number=PHONE_NUMBER_2)['id']

        # Calculate a verification code for the two users.

        with self.settings(ENABLE_TWILIO=False):
            users.send_verification_code(phone_number=PHONE_NUMBER)
            users.send_verification_code(phone_number=PHONE_NUMBER_2)

        # Get the underlying User objects.

        user_1 = User.objects.get(id=user_1_id)
        user_2 = User.objects.get(id=user_2_id)

        # Open up two sessions, one for each user.  Note that this also
        # verifies the users' phone numbers.

        session_1 = users.login(phone_number=PHONE_NUMBER,
                                verification_code=user_1.verification_code)

        session_2 = users.login(phone_number=PHONE_NUMBER_2,
                                verification_code=user_2.verification_code)

        # Get the default topic for user 1.  We'll use this as the topic for
        # our conversation.

        topic_id = topics.list(session_1)[0]['id']
        topic    = Topic.objects.get(id=topic_id)

        # Send a message from user_2 to user_1 about the topic.  This creates a
        # conversation between the two users.

        with self.settings(ENABLE_TWILIO=False, ENABLE_PUBNUB=False):
            messages.send(session_2, topic_id=topic.id, message="Hello")

        # Find the Conversation and make sure it isn't stopped.

        conversation = conversationHandler.get(user_1.id, user_2.id, topic.id)
        self.assertFalse(conversation.stopped)

        # Set up a signal listener to check that the "stopped" message is being
        # sent out via Twilio to both users.

        self.twilio_messages = []

        def twilio_signal_handler(sender, **kwargs):
            self.twilio_messages.append(kwargs.get("message"))

        signals.twilio_sms_sent.connect(twilio_signal_handler)

        # Now try stopping the conversation.  This should send an SMS to each
        # party.

        with self.settings(ENABLE_TWILIO=False, ENABLE_PUBNUB=False):
            conversations.stop(session_1,
                               topic_id=topic.id,
                               other_user_id=user_2.id)

        # Check that the conversation was stopped.

        conversation = conversationHandler.get(user_1.id, user_2.id, topic.id)
        self.assertTrue(conversation.stopped)

        # Check that the two SMS messages were sent.

        self.assertEqual(len(self.twilio_messages), 2)

        # Now try restarting the conversation.  Once again, this should send
        # out an SMS message to each party.

        self.twilio_messages = []

        with self.settings(ENABLE_TWILIO=False, ENABLE_PUBNUB=False):
            conversations.restart(session_1,
                                  topic_id=topic.id,
                                  other_user_id=user_2.id)

        # Check that the conversation was restarted.

        conversation = conversationHandler.get(user_1.id, user_2.id, topic.id)
        self.assertFalse(conversation.stopped)

        # Check that the two SMS messages were sent.

        self.assertEqual(len(self.twilio_messages), 2)

        # Finally, clean up.

        signals.twilio_sms_sent.disconnect(twilio_signal_handler)