Exemplo n.º 1
0
    def test_merge(self):
        """ Test users/merge.
        """
        # Create a user who will be the recipient of our test conversation.

        username = utils.random_username()
        password = utils.random_password()

        user_1    = users.create(username=username, password=password)
        session_1 = users.login(username=username, password=password)

        # Get the default topic for this user.

        topic = topics.list(session_1)[0]

        # Create a second, ad hoc user.

        user_2    = users.create()
        session_2 = users.login(user_id=user_2['id'])

        # Create a third, non-ad hoc user.  This will be the user we merge the
        # ad hoc user into.

        username = utils.random_username()
        password = utils.random_password()

        user_3    = users.create(username=username, password=password)
        session_3 = users.login(username=username, password=password)

        # Send a test message from the 'user_2' to 'user_1's default topic.
        # This will create a conversation between these two users.

        messages.send(session_2, topic_id=topic['id'],
                      message="Test Message 1")

        # Get the conversation we've created.

        try:
            conversation_2_to_1 = \
                Conversation.objects.get(user_1=user_1['id'],
                                         user_2=user_2['id'])
        except Conversation.DoesNotExist:
            conversation_2_to_1 = \
                Conversation.objects.get(user_1=user_2['id'],
                                         user_2=user_1['id'])

        # Get the message for this conversation.

        message_2_to_1 = Message.objects.get(conversation=conversation_2_to_1)

        # Send a test message from 'user_3' to 'user_1's default topic.  This
        # will create a conversation between these two users.

        messages.send(session_3, topic_id=topic['id'],
                      message="Test Message 2")

        # Get the conversation we've created.

        try:
            conversation_3_to_1 = \
                Conversation.objects.get(user_1=user_1['id'],
                                         user_2=user_3['id'])
        except Conversation.DoesNotExist:
            conversation_3_to_1 = \
                Conversation.objects.get(user_1=user_3['id'],
                                         user_2=user_1['id'])

        # Get the message for this conversation.

        message_3_to_1 = Message.objects.get(conversation=conversation_3_to_1)

        # It's now time to pretend that the ad hoc user has identified
        # themselves as actually being the third user.  We merge 'user_2' into
        # 'user_3'.

        users.merge(session_2, session_3)

        # Check that the conversation between user_2 and user_1 no longer
        # exists.

        n = Conversation.objects.filter(user_1=user_1['id'],
                                        user_2=user_2['id']).count()
        n = n + Conversation.objects.filter(user_1=user_2['id'],
                                            user_2=user_1['id']).count()

        self.assertEqual(n, 0)

        # Check that we have exactly one conversation between user_3 and
        # user_1.

        n = Conversation.objects.filter(user_1=user_1['id'],
                                        user_2=user_3['id']).count()
        n = n + Conversation.objects.filter(user_1=user_3['id'],
                                            user_2=user_1['id']).count()

        self.assertEqual(n, 1)

        # Get the conversation between user_3 and user_1.  Note that this might
        # have changed due to the merge -- we don't assume that the same
        # conversation record is used.

        try:
            conversation_3_to_1 = \
                Conversation.objects.get(user_1=user_1['id'],
                                         user_2=user_3['id'])
        except Conversation.DoesNotExist:
            conversation_3_to_1 = \
                Conversation.objects.get(user_1=user_3['id'],
                                         user_2=user_1['id'])

        # Finally, check that the message that was part of conversation_2_to_1
        # is now part of conversation_3_to_1.

        updated_message = Message.objects.get(id=message_2_to_1.id)
        self.assertEqual(updated_message.conversation, conversation_3_to_1)
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
    def test_send(self):
        """ Test messages/send.
        """
        # Create the sender and recipient users, giving the recipient a phone
        # number so it can receive messages.

        sender_username = utils.random_username()
        sender_password = utils.random_password()

        sender = users.create(username=sender_username,
                              password=sender_password)

        recipient_username = utils.random_username()
        recipient_password = utils.random_password()

        recipient = users.create(username=recipient_username,
                                 password=recipient_password,
                                 phone_number=PHONE_NUMBER)

        # Verify the recipient's phone number.

        recipient_obj = User.objects.get(id=recipient['id'])
        recipient_obj.verified = True
        recipient_obj.save()

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

        session = users.login(username=recipient_username,
                              password=recipient_password)

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

        # Set up a signal listener to check that the message is being sent 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 message is being sent 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)

        # Log in as the sender, and send a test message to the topic.  Note
        # that we disable Twilio and PubNub so no actual notifications will get
        # sent out.

        session = users.login(username=sender_username,
                              password=sender_password)

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

        # Check that the Twilio gateway sent the message.

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

        # Check that the PubNub gateway sent the notification.

        self.assertTrue(self.pubnub_notification_sent)
        self.assertEqual(self.pubnub_message.body, MESSAGE_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)
Exemplo n.º 4
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)