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_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)
def home(request): """ Respond to the "/web/home" URL. We display the home page for a logged in user. If the current user is not logged in, we redirect the user back to the welcome page. """ if not webHelpers.is_logged_in(request): return redirect("/") # Calculate the environment we're running in. We use this to load the # appropriate version of the widget javascript for the environment. if ("127.0.0.1" in request.get_host()) or ("local" in request.get_host()): environment = "dev" elif "stage" in request.get_host(): environment = "stage" else: environment = "prod" # Get the name of the widget javascript to use for this environment. if environment == "dev": widget_script = "widget-dev.js" elif environment == "stage": widget_script = "widget-stage.js" elif environment == "prod": widget_script = "widget.js" # Get the base URL to use to access this server. if request.is_secure(): base_url = "https://" + request.get_host() else: base_url = "http://" + request.get_host() # Get the details of the current user. session = request.COOKIES['mm_session'] user = users.get(session) # If the user doesn't have a username, something is wrong -- we can't # proceed. In this case, delete the session cookie and redirect the user # back to the "welcome" page. if "username" not in user: response = redirect("/") response.delete_cookie("mm_session") return response # Ask the 3taps Identity API for the user's profile. profile = profiles.get(session) # Get a list of the user's topics. topic_list = [] for topic in topics.list(session): if not topic['active']: continue topic_info = {} topic_info['id'] = topic['id'] topic_info['hide_username'] = topic['hide_username'] if topic.get("name") in [None, ""]: # Default topic. topic_info['url'] = base_url + "/" + user['username'] topic_info['hidden_url'] = base_url + "/!" + topic['hidden_url'] topic_info['default'] = True else: # A named topic. topic_info['url'] = base_url + "/" + user['username'] \ + "/" + topic['name'] topic_info['hidden_url'] = base_url + "/!" + topic['hidden_url'] \ + "/" + topic['name'] topic_info['default'] = False topic_info['num_views'] = topic['num_views'] topic_info['num_conversations'] = topic['num_conversations'] topic_info['num_messages'] = topic['num_messages'] topic_info['num_via_sms'] = topic['num_via_sms'] topic_info['embed_code'] = linkGenerator.generate_link_html( request, topic) topic_list.append(topic_info) # Get the user's account details. phone_number = user['phone_number'] rate_limit = rateLimiter.get_phone_number_limit(phone_number) period = settings.SMS_RATE_LIMIT_PERIOD_SIZE num_sms_in_period = rateLimiter.get_phone_number_usage(phone_number) account = {} account['rate_limited'] = True # Hardwired for now. account['rate_limit'] = rate_limit account['period'] = period.lower() account['num_sms_in_period'] = num_sms_in_period # Finally, show the home page. if "n=t" in request.get_full_path(): # We're showing the home page for the first time -> display the # "welcome" message. show_welcome = True else: show_welcome = False return render(request, "home.html", {'user' : user, 'profile' : profile, 'topics' : topic_list, 'account' : account, 'show_welcome' : show_welcome, 'base_url' : base_url})
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)