def test_message_structure(): """Make sure that our saved messages contain the types we want.""" app = HelperApp(server.message_app) app.post('/login/', {'username': '******', 'password': '******'}) # Add a bunch of messages for l in string.ascii_lowercase: app.get('/compose/') app.post('/compose/', {'to': 'james', 'subject': l, 'body': l.upper()}) all_messages = message.load_all_messages() sent_messages = message.load_sent_messages('jessie') received_messages = message.load_sent_messages('james') # Check that we're loading messages correctly for messages in (all_messages, sent_messages, received_messages): for m in messages: # Check that our keys match up assert set(m) == {'id', 'from', 'to', 'subject', 'body', 'time'} # Check that load_all_messages loads the correct types assert isinstance(m['to'], str) assert isinstance(m['from'], str) assert isinstance(m['subject'], str) assert isinstance(m['body'], str) assert isinstance(m['time'], datetime)
def test_view_authorization(): """Make sure that we can only view messages to/from us.""" app = HelperApp(server.message_app) # Send a message from Jessie to James app.post('/login/', {'username': '******', 'password': '******'}) app.get('/compose/') app.post('/compose/', {'to': 'james', 'subject': 's', 'body': 'S'}) # Find the message msg, = message.load_sent_messages('jessie') # Make sure Jessie can see it response = app.get('/view/{}/'.format(msg['id'])) assert response.status == "200 OK" # Make sure James can see it app.get('/logout/') app.post('/login/', {'username': '******', 'password': '******'}) response = app.get('/view/{}/'.format(msg['id'])) assert response.status == "200 OK" # Make sure Cassidy can't app.get('/logout/') app.post('/login/', {'username': '******', 'password': '******'}) response = app.get('/view/{}/'.format(msg['id'])) assert response.status == "302 Found" assert urlsplit(response.location).path == "/"
def test_order_of_loaded_messages(): """Make sure that load_messages maintains order for us.""" app = HelperApp(server.message_app) app.post('/login/', {'username': '******', 'password': '******'}) # Add a bunch of messages for i, l in enumerate("abcd"): # Sleep a second so that our messages have different timestamps if i != 0: time.sleep(1) app.get('/compose/') app.post('/compose/', {'to': 'james', 'subject': l, 'body': l.upper()}) all_messages = message.load_all_messages() sent_messages = message.load_sent_messages('jessie') received_messages = message.load_sent_messages('james') # Check that we're loading messages correctly for messages in (all_messages, sent_messages, received_messages): for prev, current in zip(messages, messages[1:]): assert prev['time'] > current['time']
def test_sent_messages(): """Make sure that we're filtering received messages properly.""" app = HelperApp(server.message_app) # Get a list of people to send messages to (everyone who's not Jessie) with open("passwords.json") as f: passwords = json.load(f) people = set(passwords.keys()) # Add a bunch of messages (26) for l in string.ascii_lowercase: sender = random.choice(list(people)) receiver = random.choice(list(people - set(sender))) app.post('/login/', { 'username': sender, 'password': passwords[sender] }) app.get('/compose/') app.post('/compose/', { 'to': receiver, 'subject': l, 'body': l.upper() }) sent = {} for person in people: messages = message.load_sent_messages(person) sent[person] = messages # It's extremely improbable that all of the messages would go to # the same person. assert len(messages) < 26 for m in messages: assert m['from'] == person # We should have seen 26 total messages assert sum(len(l) for l in sent.values()) == 26 # We should have 26 unique messages assert len(set(x['id'] for l in sent.values() for x in l)) == 26
def list_messages(): """Handler for GET requests to ``/`` path. * Lists sent and received messages * Requires users to be logged in * Loads alerts for display * Uses "templates/list_messages.html" as its template This handler returns a context dictionary with the following fields: * ``sent_messages``: A list of loaded messages (dictionaries) in reverse chronological order (from most recent to least recent) that have been *sent* by the current user. * ``received_messages``: A list of loaded messages (dictionaries) in reverse chronological order (from most recent to least recent) that have been *received* by the current user. :returns: a context dictionary (as described above) to be used by @jinja2_view to render a template. :rtype: dict """ dict = {} # Get's users name from cookie user = request.get_cookie("logged_in_as") # Get lists of sent and received messages sent = load_sent_messages(user) received = load_received_messages(user) # Put into dicitonary dict['sent_messages'] = sent dict['received_messages'] = received return dict