def test_no_searchable_methods(self, insert_tasks_mock):
        """Ensure the method does not error if no contact is passed."""
        from sosbeacon.event.message import broadcast_to_contact

        event_key = Mock()
        event_key.get.return_value = None

        message_key = Mock()
        message_key.get.return_value = None

        student_key = Mock()
        student_key.get.return_value = None

        contact = {"methods": [{"type": "p", "value": "1233211234"}]}

        ret_value = broadcast_to_contact(event_key, message_key, student_key, contact)

        self.assertFalse(insert_tasks_mock.called)
        self.assertIsNone(ret_value)
示例#2
0
    def post(self):
        import json

        from google.appengine.api import namespace_manager

        # batch_id is used so that we can force resend of notices for an event.
        batch_id = self.request.get('batch', '')

        event_urlsafe = self.request.get('event')
        if not event_urlsafe:
            logging.error('No event key given.')
            return

        # TODO: Use event id rather than key here for namespacing purposes?
        event_key = ndb.Key(urlsafe=event_urlsafe)
        event = event_key.get()
        if not event:
            logging.error('Event %s not found!', event_key)
            return

        if event.status == EVENT_STATUS_CLOSED:
            logging.error('Event %s closed!', event_key)
            return

        message_urlsafe = self.request.get('message')
        if not message_urlsafe:
            logging.error('No message key given.')
            return

        # TODO: Use message id rather than key here for namespacing purposes?
        message_key = ndb.Key(urlsafe=message_urlsafe)

        # TODO: Check namespace here.
        current_namespae = unicode(namespace_manager.get_namespace())
        if message_key.namespace() != current_namespae:
            logging.error('Message %s not in namespace %s!',
                          message_key, current_namespae)
            return

        message = message_key.get()
        if not message:
            logging.error('Message %s not found!', message_key)
            return

        # We don't want to send the wrong message to the wrong groups.
        if message.event != event.key:
            logging.error('Message %s not belong to Event %s!',
                          message_key, event_key)
            return

        if message.message_type == 'c' :
            logging.error('Message %s is not a broadcast!', message_key)
            return

        student_urlsafe = self.request.get('student')
        if not student_urlsafe:
            logging.error('No student key given.')
            return

        # TODO: Use group id rather than key here for namespacing purposes?
        student_key = ndb.Key(urlsafe=student_urlsafe)

        contact = self.request.get('contact')
        if not contact:
            logging.error('No contact given.')
            return
        contact = json.loads(contact)

        broadcast_to_contact(event_key, message_key, student_key, contact,
                             batch_id)