Ejemplo n.º 1
0
	def setUp(self):
		# First, create an instance of the Testbed class
		self.testbed = testbed.Testbed()
		# Then activate the testbed, which prepares the service stubs for use.
		self.testbed.activate()
		# Next, declare which service stubs you want to use.
		self.testbed.init_datastore_v3_stub()
		self.testbed.init_memcache_stub()

		test_user = users_db.UsersDb( 
			key_name='Laplace',
			pw_hash='pwd',
			parent=users_db.users_db_rootkey()
			)
		test_user.put()
		
		self.test_user = test_user
		recipient_key = test_user.key()
		
		fixture_msg = message_db.MessageDb( parent = message_db.message_db_rootkey(),\
									author = 'anthony',\
									subect = 'test',\
									body = 'this is a test',\
									recipient_keys = [recipient_key])
		fixture_msg.put()		
			
		self.fix_msg = fixture_msg
Ejemplo n.º 2
0
    def post(self):

        logging.warning('task queue triggered')
        recipients = db.Query(UsersDb)
        recipient_keys = db.Query(UsersDb, keys_only=True)

        user_key = self.request.get('user_key')
        curr_user = UsersDb.get(user_key)

        to_store = MessageDb(
         parent=message_db_rootkey(),\
         author=curr_user.key().name(),\
         subject=self.request.get('subject'),\
         body=self.request.get('body'),\
         recipient_keys=list(recipient_keys)
         )
        to_store.put()

        for recipient in recipients:
            curr_file = recipient.msg_file
            curr_file.message_keys.append(to_store.key())
            curr_file.unread_keys.append(to_store.key())
            curr_file.put()

        # add the message to the user's sent message list
        curr_user.msg_file.sent_keys.append(to_store.key())
        curr_user.msg_file.put()
Ejemplo n.º 3
0
	def test_ViewMessage_login_good_address_malicious_user(self):
		
		#
		# Implementation note:
		# --------------------
		# here we test for a malicious user that is attempting to access
		# a message they are not authorized to view. Users can only views
		# messages where they are in the recipient list or they are the 
		# sender  
		
		user_ent, _ = UsersDb.my_get_or_insert('Laplace',pwd='pwd') 
		user_ent.put()
		
		to_store = MessageDb(parent=message_db_rootkey(),\
							author=user_ent.key().name(),\
							subject='test_subject',\
							body='test_body', 
							recipient_keys=[user_ent.key()]
							)
		to_store.put()
		
		
		# test that urls for message ids that don't exist return 404
		response = self.testapp.get('/' + str(to_store.key().id()), 
			headers={'Cookie':'user_name=anth|ce908054e59b3e4b38891dcd7feb93d5'},
			status=400)
		self.assertEqual(response.status_int, 400)
Ejemplo n.º 4
0
    def setUp(self):
        # First, create an instance of the Testbed class
        self.testbed = testbed.Testbed()
        # Then activate the testbed, which prepares the service stubs for use.
        self.testbed.activate()
        # Next, declare which service stubs you want to use.
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()

        test_user = users_db.UsersDb(key_name='Laplace',
                                     pw_hash='pwd',
                                     parent=users_db.users_db_rootkey())
        test_user.put()

        self.test_user = test_user
        recipient_key = test_user.key()

        fixture_msg = message_db.MessageDb( parent = message_db.message_db_rootkey(),\
               author = 'anthony',\
               subect = 'test',\
               body = 'this is a test',\
               recipient_keys = [recipient_key])
        fixture_msg.put()

        self.fix_msg = fixture_msg
Ejemplo n.º 5
0
	def post(self): 
		
		logging.warning('task queue triggered')
		recipients = db.Query(UsersDb)
		recipient_keys = db.Query(UsersDb, keys_only=True)
			
		user_key = self.request.get('user_key')
		curr_user = UsersDb.get(user_key)
			
		to_store = MessageDb(
			parent=message_db_rootkey(),\
			author=curr_user.key().name(),\
			subject=self.request.get('subject'),\
			body=self.request.get('body'),\
			recipient_keys=list(recipient_keys)
			)
		to_store.put()
		
		for recipient in recipients: 
			curr_file = recipient.msg_file
			curr_file.message_keys.append(to_store.key())
			curr_file.unread_keys.append(to_store.key())
			curr_file.put()

		# add the message to the user's sent message list
		curr_user.msg_file.sent_keys.append(to_store.key())
		curr_user.msg_file.put()
Ejemplo n.º 6
0
    def test_ViewMessage_login_good_address_malicious_user(self):

        #
        # Implementation note:
        # --------------------
        # here we test for a malicious user that is attempting to access
        # a message they are not authorized to view. Users can only views
        # messages where they are in the recipient list or they are the
        # sender

        user_ent, _ = UsersDb.my_get_or_insert('Laplace', pwd='pwd')
        user_ent.put()

        to_store = MessageDb(parent=message_db_rootkey(),\
             author=user_ent.key().name(),\
             subject='test_subject',\
             body='test_body',
             recipient_keys=[user_ent.key()]
             )
        to_store.put()

        # test that urls for message ids that don't exist return 404
        response = self.testapp.get(
            '/' + str(to_store.key().id()),
            headers={
                'Cookie': 'user_name=anth|ce908054e59b3e4b38891dcd7feb93d5'
            },
            status=400)
        self.assertEqual(response.status_int, 400)
Ejemplo n.º 7
0
    def test_ViewMessage_login_good_address(self):

        #
        # Implementation note:
        # --------------------
        # we have to put a message in the database so we can test the
        # MessageView handler. The message view only renders when there
        # is an actual message.


        to_store = MessageDb(parent=message_db_rootkey(),\
             author=self.user.key().name(),\
             subject='test_subject',\
             body='test_body',
             recipient_keys=[self.user.key()]
             )
        to_store.put()

        # test that urls for message ids that don't exist return 404
        response = self.testapp.get(
            '/' + str(to_store.key().id()),
            headers={
                'Cookie': 'user_name=anth|ce908054e59b3e4b38891dcd7feb93d5'
            })
        self.assertEqual(response.status_int, 200)
Ejemplo n.º 8
0
	def test_message_db_by_id(self):
		
		recipient_key = self.test_user.key()
		
		test_msg = message_db.MessageDb( parent = message_db.message_db_rootkey(),\
									author = 'anthony',\
									subect = 'test',\
									body = 'this is a test',\
									recipient_keys = [recipient_key])
		test_msg.put()
		test_msg_id = test_msg.key().id()
		res = message_db.MessageDb.db_by_id(test_msg_id)
		self.assertEqual( res.author, 'anthony')
Ejemplo n.º 9
0
    def test_message_db_by_id(self):

        recipient_key = self.test_user.key()

        test_msg = message_db.MessageDb( parent = message_db.message_db_rootkey(),\
               author = 'anthony',\
               subect = 'test',\
               body = 'this is a test',\
               recipient_keys = [recipient_key])
        test_msg.put()
        test_msg_id = test_msg.key().id()
        res = message_db.MessageDb.db_by_id(test_msg_id)
        self.assertEqual(res.author, 'anthony')
Ejemplo n.º 10
0
	def test_ViewMessage_login_good_address(self):
		
		#
		# Implementation note:
		# --------------------
		# we have to put a message in the database so we can test the 
		# MessageView handler. The message view only renders when there
		# is an actual message. 
		
		
		to_store = MessageDb(parent=message_db_rootkey(),\
							author=self.user.key().name(),\
							subject='test_subject',\
							body='test_body', 
							recipient_keys=[self.user.key()]
							)
		to_store.put()
		
		
		# test that urls for message ids that don't exist return 404
		response = self.testapp.get('/' + str(to_store.key().id()), headers={'Cookie':'user_name=anth|ce908054e59b3e4b38891dcd7feb93d5'})
		self.assertEqual(response.status_int, 200)
Ejemplo n.º 11
0
	def post(self,path):
		if not self.user:
			self.error(400)
			return
		
		# retreive the field named "subject" and the field 
		# named "content" from the form submission
		msg_recipient = self.request.get("recipient")
		msg_subject = self.request.get("subject")
		msg_body = self.request.get("body")
		
		q_params = {}
		q_params['recipient'] = msg_recipient 
		q_params['subject'] = msg_subject
		q_params['body'] = msg_body
		q_params['user_key'] = self.user.key()
		
		
		# check if the message is a global broadcast
		if msg_recipient.lower() == "all": 
			
			# send to taskqueue to manage distribution 
			taskqueue.add( params=q_params )
			self.redirect("/")
					
		group_qry = UserGroup.db_by_name(msg_recipient)	
		if group_qry: 
			
			# create a new Message entity
			to_store = MessageDb(
							parent=message_db_rootkey(),\
							author=self.user.key().name(),\
							subject=msg_subject,\
							body=msg_body,\
							recipient_keys=group_qry.group_keys)
		
			to_store.put()
			
			for recipient_key in group_qry.group_keys:
				msg_file = UsersDb.get(recipient_key).msg_file
				msg_file.message_keys.append(to_store.key())
				msg_file.unread_keys.append(to_store.key())
				msg_file.put()

			self.user.msg_file.sent_keys.append(to_store.key())
			self.user.msg_file.put()
			
			self.redirect("/")
	
		
		# Query the database for the recipient
		recipient_entity = UsersDb.db_by_name(msg_recipient) 
		
		if recipient_entity:
			# create a new Message entity
			to_store = MessageDb(parent=message_db_rootkey(),\
							author=self.user.key().name(),\
							subject=msg_subject,\
							body=msg_body, 
							recipient_keys=[recipient_entity.key()]
							)
				
			# store the message object
			to_store.put()
			
			# retrieve the recipient's message file and
			# add the message to their message list
			# and unread message list
			msg_file = recipient_entity.msg_file
			msg_file.message_keys.append(to_store.key())
			msg_file.unread_keys.append(to_store.key())
			msg_file.put()
			
			# add the message to the user's sent message list
			self.user.msg_file.sent_keys.append(to_store.key())
			self.user.msg_file.put()
			
			self.redirect("/")
			
		else: 
			error = "That recipient doesn't exist"
			
			# pass the error message to the render fuction
			# the function then passes 'error' to the form
			self.render("composeMsg.html",\
						recipient=msg_recipient,\
						subject=msg_subject,\
						body=msg_body,\
						num_msgs=len(self.inbox),\
						num_sent_msgs=len(self.outbox),\
						fallback_error=error
						)
Ejemplo n.º 12
0
    def post(self, path):
        if not self.user:
            self.error(400)
            return

        # retreive the field named "subject" and the field
        # named "content" from the form submission
        msg_recipient = self.request.get("recipient")
        msg_subject = self.request.get("subject")
        msg_body = self.request.get("body")

        q_params = {}
        q_params['recipient'] = msg_recipient
        q_params['subject'] = msg_subject
        q_params['body'] = msg_body
        q_params['user_key'] = self.user.key()

        # check if the message is a global broadcast
        if msg_recipient.lower() == "all":

            # send to taskqueue to manage distribution
            taskqueue.add(params=q_params)
            self.redirect("/")

        group_qry = UserGroup.db_by_name(msg_recipient)
        if group_qry:

            # create a new Message entity
            to_store = MessageDb(
                parent=message_db_rootkey(),\
                author=self.user.key().name(),\
                subject=msg_subject,\
                body=msg_body,\
                recipient_keys=group_qry.group_keys)

            to_store.put()

            for recipient_key in group_qry.group_keys:
                msg_file = UsersDb.get(recipient_key).msg_file
                msg_file.message_keys.append(to_store.key())
                msg_file.unread_keys.append(to_store.key())
                msg_file.put()

            self.user.msg_file.sent_keys.append(to_store.key())
            self.user.msg_file.put()

            self.redirect("/")

        # Query the database for the recipient
        recipient_entity = UsersDb.db_by_name(msg_recipient)

        if recipient_entity:
            # create a new Message entity
            to_store = MessageDb(parent=message_db_rootkey(),\
                author=self.user.key().name(),\
                subject=msg_subject,\
                body=msg_body,
                recipient_keys=[recipient_entity.key()]
                )

            # store the message object
            to_store.put()

            # retrieve the recipient's message file and
            # add the message to their message list
            # and unread message list
            msg_file = recipient_entity.msg_file
            msg_file.message_keys.append(to_store.key())
            msg_file.unread_keys.append(to_store.key())
            msg_file.put()

            # add the message to the user's sent message list
            self.user.msg_file.sent_keys.append(to_store.key())
            self.user.msg_file.put()

            self.redirect("/")

        else:
            error = "That recipient doesn't exist"

            # pass the error message to the render fuction
            # the function then passes 'error' to the form
            self.render("composeMsg.html",\
               recipient=msg_recipient,\
               subject=msg_subject,\
               body=msg_body,\
               num_msgs=len(self.inbox),\
               num_sent_msgs=len(self.outbox),\
               fallback_error=error
               )