Esempio n. 1
0
    def test_no_continuation(self, get_student_keys_mock, get_group_broadcast_task_mock, insert_tasks_mock):
        """Verify a continuation task is not inserted if there are not more
        students.
        """
        from sosbeacon.event.message import broadcast_to_group

        group_key = object()
        event_key = object()
        message_key = object()
        cursor = object()

        get_student_keys_mock.return_value = ((), cursor, False)

        continuation_marker = object()
        get_group_broadcast_task_mock.return_value = continuation_marker

        broadcast_to_group(
            group_key=group_key,
            event_key=event_key,
            message_key=message_key,
            batch_id="alpha",
            iteration=17,
            cursor=cursor,
        )

        get_student_keys_mock.assert_called_once_with(group_key, cursor)

        self.assertEqual(get_group_broadcast_task_mock.call_count, 0)
        self.assertEqual(insert_tasks_mock.call_count, 0)
Esempio n. 2
0
    def test_continuation(self, get_student_keys_mock, get_group_broadcast_task_mock, insert_tasks_mock):
        """Verify a continuation task is inserted if there are more
        students.
        """
        from sosbeacon.event.message import GROUP_TX_QUEUE
        from sosbeacon.event.message import broadcast_to_group

        group_key = object()
        event_key = object()
        message_key = object()
        cursor = object()

        get_student_keys_mock.return_value = ((), cursor, True)

        continuation_marker = object()
        get_group_broadcast_task_mock.return_value = continuation_marker

        broadcast_to_group(
            group_key=group_key,
            event_key=event_key,
            message_key=message_key,
            batch_id="alpha",
            iteration=17,
            cursor=cursor,
        )

        get_student_keys_mock.assert_called_once_with(group_key, cursor)

        get_group_broadcast_task_mock.assert_called_once_with(group_key, event_key, message_key, "alpha", 18, cursor)

        insert_tasks_mock.assert_called_once_with((continuation_marker,), GROUP_TX_QUEUE)
Esempio n. 3
0
    def test_no_student_task_returned(self, get_student_broadcast_task_mock, get_student_keys_mock, insert_tasks_mock):
        """Verify a continuation task is not inserted if there are not more
        students.
        """
        from sosbeacon.event.message import broadcast_to_group

        group_key = object()
        event_key = object()
        message_key = object()

        get_student_keys_mock.return_value = ((object(),), None, False)

        get_student_broadcast_task_mock.return_value = None

        broadcast_to_group(group_key=group_key, event_key=event_key, message_key=message_key, batch_id="alpha")

        get_student_keys_mock.assert_called_once_with(group_key, None)

        self.assertEqual(insert_tasks_mock.call_count, 0)
Esempio n. 4
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

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

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

        broadcast_to_group(group_key, event_key, message_key, batch_id)