コード例 #1
0
ファイル: test_message_api.py プロジェクト: 3taps/MessageMe
    def test_stop_via_sms(self):
        """ Test the stopping of a conversation via sms message.
        """
        # Create two users, giving them a phone number so they can receive
        # messages.

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

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

        user_1 = users.create(username=username_1,
                              password=password_2,
                              phone_number=PHONE_NUMBER)

        user_2 = users.create(username=username_2,
                              password=password_2,
                              phone_number=PHONE_NUMBER_2)

        # Verify both phone numbers.

        user = User.objects.get(id=user_1['id'])
        user.verified = True
        user.save()

        user = User.objects.get(id=user_2['id'])
        user.verified = True
        user.save()

        # Log in as user 2, and create a dummy topic for this user.

        session = users.login(username=username_2,
                              password=password_2)

        topic = topics.create(session, topic_name=None)

        # Log in as user 1, and send a test message to the topic.  This
        # creates a conversation between the two users.

        session = users.login(username=username_1,
                              password=password_1)

        with self.settings(ENABLE_TWILIO=False, ENABLE_PUBNUB=False):
            message = messages.send(session, topic_id=topic['id'],
                                    message=MESSAGE_BODY)

        # Get the created conversation.

        conversation = conversationHandler.get(user_1['id'],
                                               user_2['id'],
                                               topic['id'])

        # Set up a signal listener to check that the "conversation has stopped"
        # messages were sent out.

        self.twilio_sms_messages = []

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

        signals.twilio_sms_sent.connect(twilio_signal_handler)

        # Ask the twilio gateway to calculate the phone number to use for
        # sending SMS messages to user 2.  This opens up an SMS channel for the
        # conversation we're pretending to have.

        sending_phone_number = \
            twilio_gateway.calc_sending_phone_number(conversation, user_2)

        # Simulate an incoming SMS reply being received from user 2, containing
        # the word "stop".  Note that we disable Twilio and PubNub so that
        # nothing will actually get sent out.

        with self.settings(ENABLE_TWILIO=False, ENABLE_PUBNUB=False):
            response = messages.receive(To=sending_phone_number,
                                        From=user_2.phone_number,
                                        Body="stop")

        # Check that the Twilio gateway sent the "conversation has been
        # stopped" message to both parties.

        self.assertEqual(len(twilio_sms_messages), 2)

        # Finally, clean everything up.

        signals.twilio_sms_sent.disconnect(twilio_signal_handler)
コード例 #2
0
ファイル: test_message_api.py プロジェクト: 3taps/MessageMe
    def test_receive(self):
        """ Test messages/receive.
        """
        # Create the two users we'll need for our test.

        user_1_username = utils.random_username()
        user_1_password = utils.random_password()

        user_1_id = users.create(username=user_1_username,
                                 password=user_1_password,
                                 phone_number=PHONE_NUMBER)['id']

        user_2_id = users.create(phone_number=PHONE_NUMBER_2)['id']

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

        # Verify user 1's phone number, so that replies will be forwarded to
        # that number.

        user_1.verified = True
        user_1.save()

        # Log in as user 1, and create a topic for this user.  We'll need this
        # topic for our pseudo-conversation.

        session = users.login(username=user_1_username,
                              password=user_1_password)

        topic_id = topics.create(session, topic_name=None)['id']

        topic = Topic.objects.get(id=topic_id)

        # Create our pseudo-conversation.

        conversation = Conversation()
        conversation.user_1 = user_1
        conversation.user_2 = user_2
        conversation.topic  = topic
        conversation.save()

        # Ask the twilio gateway to calculate the phone number to use for
        # sending SMS messages to user 2.  This opens up an SMS channel for the
        # conversation we're pretending to have.

        sending_phone_number = \
            twilio_gateway.calc_sending_phone_number(conversation, user_2)

        # Set up a signal listener to check that the reply was forwarded via
        # Twilio.

        self.twilio_messages = []

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

        signals.twilio_sms_sent.connect(twilio_signal_handler)

        # Set up a signal listener to check that the reply was forwarded via
        # PubNub.

        self.pubnub_notification_sent = False # initially.
        self.pubnub_message           = None  # ditto.

        def pubnub_signal_handler(sender, **kwargs):
            self.pubnub_notification_sent = True
            self.pubnub_message           = kwargs.get("message")

        signals.pubnub_notification_sent.connect(pubnub_signal_handler)

        # Simulate an incoming SMS reply being received from user 2.  Note that
        # we disable Twilio and PubNub so that the SMS reply isn't actually
        # sent out to the original sender.

        with self.settings(ENABLE_TWILIO=False, ENABLE_PUBNUB=False):
            response = messages.receive(To=sending_phone_number,
                                        From=user_2.phone_number,
                                        Body=REPLY_BODY)

        # Check that the response is what we expect.

        self.assertEqual(response.status_code, 201)
        self.assertEqual(response['Content-Type'], "text/xml")

        # Check that the Twilio gateway sent the message.

        if len(self.twilio_messages) == 0:
            # Whoops!  The reply wasn't forwarded, which means that something
            # went seriously wrong.  Print out the response so we can see the
            # XML message sent back to Twilio.
            print "Error response sent to Twilio:"
            print response.content
            self.fail("Reply not forwarded -> something went wrong.")

        # Check that the PubNub gateway sent the notification.

        self.assertTrue(self.pubnub_notification_sent)
        self.assertEqual(self.pubnub_message.body, REPLY_BODY)
        self.assertEqual(self.pubnub_message.conversation.topic.id, topic_id)

        # Finally, clean everything up.

        signals.twilio_sms_sent.disconnect(twilio_signal_handler)
        signals.pubnub_notification_sent.disconnect(pubnub_signal_handler)