def test_create(self): """ Test topics/create. """ # Create a dummy user for testing, and log in. username = utils.random_username() password = utils.random_password() user = users.create(username=username, password=password) session = users.login(username=username, password=password) # Create a new topic without a name. The topic API should allocate a # random name for this topic. topic = topics.create(session) self.assertTrue(topic['user_id'] == user['id']) self.assertTrue(topic['name'] not in [None, ""]) self.assertTrue(topic['active'] == True) self.assertTrue(topic['default'] == False) self.assertTrue(topic['num_views'] == 0) self.assertTrue(topic['num_conversations'] == 0) # Create a named topic. topic = topics.create(session, topic_name="bike") self.assertTrue(topic['name'] == "bike") # Finally, make sure we can't create a second topic with the same name. with self.assertRaises(DuplicateTopicNameException): topic = topics.create(session, topic_name="bike")
def test_update(self): """ Test topics/update. """ # Create a dummy user for testing, log in, and create a topic. username = utils.random_username() password = utils.random_password() user = users.create(username=username, password=password) session = users.login(username=username, password=password) topic = topics.create(session) # Try updating the topic. updated_topic = topics.update(session, topic['id'], name="bike", active=False) self.assertEqual(updated_topic['name'], "bike") self.assertEqual(updated_topic['active'], False) # Finally, log in as a second user and make sure we can't update the # topic for the first user. username = utils.random_username() password = utils.random_password() user = users.create(username=username, password=password) session = users.login(username=username, password=password) with self.assertRaises(UnauthorizedException): updated_topic = topics.update(session, topic['id'], name="car")
def test_increment_num_views(self): """ Test topics/increment_num_views. """ # Create a dummy user for testing, log in, and create a topic. username = utils.random_username() password = utils.random_password() user = users.create(username=username, password=password) session = users.login(username=username, password=password) topic_id = topics.create(session)['id'] topic = Topic.objects.get(id=topic_id) self.assertEqual(topic.num_views, 0) topics.increment_num_views(topic_id) topic = Topic.objects.get(id=topic_id) self.assertEqual(topic.num_views, 1)
def test_list(self): """ Test topics/list. """ # Create a dummy user for testing, log in, and create a randomly-named # topic for that user. username = utils.random_username() password = utils.random_password() user = users.create(username=username, password=password) session = users.login(username=username, password=password) topic = topics.create(session) # Get the list of topics for this user. topic_list = topics.list(session) # Finally, check that we have two topics: the default one, and the one # we created. self.assertTrue(len(topic_list) == 2)
def test_close_old_conversations(self): """ Test the "close_old_conversations" management command. """ # Create three random users for testing. username_1 = utils.random_username() password_1 = utils.random_password() user_1 = users.create(username=username_1, password=password_1) username_2 = utils.random_username() password_2 = utils.random_password() user_2 = users.create(username=username_2, password=password_2) username_3 = utils.random_username() password_3 = utils.random_password() user_3 = users.create(username=username_3, password=password_3) # Create a dummy topic for user 1. session = users.login(username=username_1, password=password_1) topic = topics.create(session) # Create a conversation between user 2 and user 1, about the dummy # topic. This conversation will have a message twelve days old, and a # second message just two days old. conversation_1 = Conversation() conversation_1.user_1_id = user_1['id'] conversation_1.user_2_id = user_2['id'] conversation_1.stopped = False conversation_1.topic_id = topic['id'] conversation_1.save() message = Message() message.conversation = conversation_1 message.sender_id = user_2['id'] message.body = "The quick brown fox" message.created_at = datetime.datetime.utcnow() \ - datetime.timedelta(days=12) message.updated_at = message.created_at message.save() message = Message() message.conversation = conversation_1 message.sender_id = user_1['id'] message.body = "jumps over the lazy dog" message.created_at = datetime.datetime.utcnow() \ - datetime.timedelta(days=2) message.updated_at = message.created_at message.save() # Create a second conversation between user 3 and user 1. This # conversation will have a message five days old. conversation_2 = Conversation() conversation_2.user_1_id = user_1['id'] conversation_2.user_2_id = user_3['id'] conversation_2.stopped = False conversation_2.topic_id = topic['id'] conversation_2.save() message = Message() message.conversation = conversation_2 message.sender_id = user_3['id'] message.body = "Hello world" message.created_at = datetime.datetime.utcnow() \ - datetime.timedelta(days=5) message.updated_at = message.created_at message.save() # We've now set up the data. Call our management command to close all # conversations older than ten days. call_command("close_old_conversations", 10, silent=True) # Finally, check that the first conversation has been closed, but the # second has not. conversation_1 = Conversation.objects.get(id=conversation_1.id) conversation_2 = Conversation.objects.get(id=conversation_2.id) self.assertEqual(conversation_1.stopped, True) self.assertEqual(conversation_2.stopped, False)
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)
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)
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)