def test_no_student(self):
        """Ensure the method does not error if the student is not found."""
        from sosbeacon.event.message import broadcast_to_student

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

        event_key = Mock()
        message_key = Mock()

        broadcast_to_student(student_key, event_key, message_key)
    def test_no_contacts(self, get_contact_broadcast_task_mock):
        """Ensure the method does not error if no contacts are found."""
        from google.appengine.ext import ndb

        from sosbeacon.event.event import Event
        from sosbeacon.event.message import broadcast_to_student

        student_key = Mock()
        student_key.get.return_value.name = "Joe Blow"
        student_key.get.return_value.contacts = ()
        student_key.get.return_value.key.urlsafe.return_value = "STUDENTKEY"

        event_key = ndb.Key(Event, "EVENTKEY")

        message_key = Mock()

        broadcast_to_student(student_key, event_key, message_key)

        self.assertFalse(get_contact_broadcast_task_mock.called)
    def test_student_marker_inserted(self, put_mock, insert_tasks_mock):
        """Ensure the method creates the student marker."""
        from google.appengine.ext import ndb

        from sosbeacon.event.message import broadcast_to_student

        student_key = Mock()
        student_key.get.return_value.name = "Joe Blow"
        student_key.get.return_value.contacts = ()
        student_key.id.return_value = 211
        student_key.get.return_value.event.id.return_value = 919
        student_key.get.return_value.key.urlsafe.return_value = "STUDENTKEY"

        event_key = Mock(spec=ndb.Key)
        event_key.kind.return_value = "Event"
        event_key.urlsafe.return_value = "EVENTKEY"

        message_key = Mock()

        broadcast_to_student(student_key, event_key, message_key)

        self.assertEqual(1, put_mock.call_count)
    def test_broadcast_to_contacts(self, get_contact_broadcast_task_mock, insert_tasks_mock):
        """Ensure the method does not error if no contacts are found."""
        from google.appengine.ext import ndb

        from sosbeacon.event.event import Event
        from sosbeacon.event.message import broadcast_to_student

        contacts = ({"t": "test", "name": "me"},)

        student_key = Mock()
        student_key.get.return_value.name = "Joe Blow"
        student_key.get.return_value.contacts = contacts
        student_key.get.return_value.key.urlsafe.return_value = "STUDENTKEY"

        event_key = ndb.Key(Event, "EVENTKEY")

        message_key = Mock()

        broadcast_to_student(student_key, event_key, message_key)

        get_contact_broadcast_task_mock.assert_called_once_with(event_key, message_key, student_key, contacts[0], "")

        self.assertEqual(1, insert_tasks_mock.call_count)
Example #5
0
    def post(self):
        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)

        broadcast_to_student(student_key, event_key, message_key, batch_id)