Exemple #1
0
 def setUp(self):
     self.resource = autopush.tests.boto_resource
     table = get_rotating_message_tablename(
         prefix="message_int_test",
         boto_resource=self.resource)
     self.real_table = table
     self.uaid = str(uuid.uuid4())
Exemple #2
0
    def test_unregister(self):
        chid = str(uuid.uuid4())
        m = get_rotating_message_tablename(boto_resource=self.resource)
        message = Message(m, boto_resource=self.resource)
        message.register_channel(self.uaid, chid)

        # Verify its in the db
        lm = self.resource.Table(m)
        # Verify it's in the db
        response = lm.query(
            KeyConditions={
                'uaid': {
                    'AttributeValueList': [self.uaid],
                    'ComparisonOperator': 'EQ'
                },
                'chidmessageid': {
                    'AttributeValueList': [" "],
                    'ComparisonOperator': 'EQ'
                },
            },
            ConsistentRead=True,
        )
        results = list(response.get('Items'))
        assert len(results) == 1
        assert results[0]["chids"] == {chid}

        message.unregister_channel(self.uaid, chid)

        # Verify its not in the db
        response = lm.query(
            KeyConditions={
                'uaid': {
                    'AttributeValueList': [self.uaid],
                    'ComparisonOperator': 'EQ'
                },
                'chidmessageid': {
                    'AttributeValueList': [" "],
                    'ComparisonOperator': 'EQ'
                },
            },
            ConsistentRead=True,
        )
        results = list(response.get('Items'))
        assert len(results) == 1
        assert results[0].get("chids") is None

        # Test for the very unlikely case that there's no 'chid'
        mtable = Mock()
        mtable.update_item = Mock(return_value={
            'Attributes': {
                'uaid': self.uaid
            },
            'ResponseMetaData': {}
        })
        message.table = mtable
        r = message.unregister_channel(self.uaid, dummy_chid)
        assert r is False
Exemple #3
0
    def test_message_rotate_table_with_date(self):
        prefix = "message" + uuid.uuid4().hex
        future = (datetime.today() + timedelta(days=32)).date()
        tbl_name = make_rotating_tablename(prefix, date=future)

        m = get_rotating_message_tablename(prefix=prefix, date=future,
                                           boto_resource=self.resource)
        assert m == tbl_name
        # Clean up the temp table.
        _drop_table(tbl_name, boto_resource=self.resource)
    def test_update_rotating_tables_month_end(self):
        """Test that rotating adds next months table

        This test is intended to ensure that if the next day is a new
        month, then update_rotating_tables realizes this and add's
        the new table to the message_tables.

        A pre-requisite is that today cannot be the last day of
        the current month. Therefore, we first sub in _tomorrow to
        ensure it always appears as next month, and then remove
        the new table create_initial_tables made so we can observe
        update_rotating_tables add the new one.

        Note that sorting message table keys to find the last month
        does *not work* since the month digit is not zero-padded.

        """
        today = datetime.date.today()
        next_month = today.month + 1
        next_year = today.year
        if next_month > 12:  # pragma: nocover
            next_month = 1
            next_year += 1
        tomorrow = datetime.datetime(year=next_year,
                                     month=next_month,
                                     day=1)

        conf = AutopushConfig(
            hostname="example.com", resolve_hostname=True)
        db = DatabaseManager.from_config(
            conf,
            resource=autopush.tests.boto_resource)
        db._tomorrow = Mock(return_value=tomorrow)
        db.create_initial_message_tables()

        # We should have 3 tables, one for next/this/last month
        assert len(db.message_tables) == 3

        # Grab next month's table name and remove it
        next_month = get_rotating_message_tablename(
            conf.message_table.tablename,
            delta=1,
            boto_resource=db.resource
        )
        db.message_tables.pop(db.message_tables.index(next_month))

        # Get the deferred back
        d = db.update_rotating_tables()

        def check_tables(result):
            assert len(db.message_tables) == 3
            assert next_month in db.message_tables

        d.addCallback(check_tables)
        return d
Exemple #5
0
    def test_save_channels(self):
        chid = str(uuid.uuid4())
        chid2 = str(uuid.uuid4())
        m = get_rotating_message_tablename(boto_resource=self.resource)
        message = Message(m, boto_resource=self.resource)
        message.register_channel(self.uaid, chid)
        message.register_channel(self.uaid, chid2)

        exists, chans = message.all_channels(self.uaid)
        new_uaid = uuid.uuid4().hex
        message.save_channels(new_uaid, chans)
        _, new_chans = message.all_channels(new_uaid)
        assert chans == new_chans
Exemple #6
0
    def test_preflight_check(self):
        global test_router
        message = Message(
            get_rotating_message_tablename(boto_resource=self.resource),
            boto_resource=self.resource)

        pf_uaid = "deadbeef00000000deadbeef01010101"
        preflight_check(message, test_router, pf_uaid)
        # now check that the database reports no entries.
        _, notifs = message.fetch_messages(uuid.UUID(pf_uaid))
        assert len(notifs) == 0
        with pytest.raises(ItemNotFound):
            self.router.get_uaid(pf_uaid)
Exemple #7
0
    def test_message_delete_fail_condition(self):
        notif = make_webpush_notification(dummy_uaid, dummy_chid)
        notif.message_id = notif.update_id = dummy_uaid
        m = get_rotating_message_tablename(boto_resource=self.resource)
        message = Message(m, boto_resource=self.resource)

        def raise_condition(*args, **kwargs):
            raise ClientError({}, 'delete_item')

        m_de = Mock()
        m_de.delete_item = Mock(side_effect=raise_condition)
        message.table = m_de
        result = message.delete_message(notif)
        assert result is False
Exemple #8
0
    def test_all_channels_fail(self):

        m = get_rotating_message_tablename(boto_resource=self.resource)
        message = Message(m, boto_resource=self.resource)

        mtable = Mock()
        mtable.get_item.return_value = {
            "ResponseMetadata": {
                "HTTPStatusCode": 400
            },
        }
        message.table = mtable
        res = message.all_channels(self.uaid)
        assert res == (False, set([]))
Exemple #9
0
    def test_preflight_check_fail(self):
        router = Router(self.table_conf, SinkMetrics(), resource=self.resource)
        message = Message(
            get_rotating_message_tablename(boto_resource=self.resource),
            boto_resource=self.resource)

        def raise_exc(*args, **kwargs):  # pragma: no cover
            raise Exception("Oops")

        router.clear_node = Mock()
        router.clear_node.side_effect = raise_exc

        with pytest.raises(Exception):
            preflight_check(message, router, self.resource)
Exemple #10
0
    def test_all_channels(self):
        chid = str(uuid.uuid4())
        chid2 = str(uuid.uuid4())
        m = get_rotating_message_tablename(boto_resource=self.resource)
        message = Message(m, boto_resource=self.resource)
        message.register_channel(self.uaid, chid)
        message.register_channel(self.uaid, chid2)

        _, chans = message.all_channels(self.uaid)
        assert chid in chans
        assert chid2 in chans

        message.unregister_channel(self.uaid, chid2)
        _, chans = message.all_channels(self.uaid)
        assert chid2 not in chans
        assert chid in chans
Exemple #11
0
    def test_message_storage(self):
        chid = str(uuid.uuid4())
        chid2 = str(uuid.uuid4())
        m = get_rotating_message_tablename(boto_resource=self.resource)
        message = Message(m, boto_resource=self.resource)
        message.register_channel(self.uaid, chid)
        message.register_channel(self.uaid, chid2)

        # Ensure that sort keys are fetched from DB in order.
        notifs = [make_webpush_notification(self.uaid, chid) for x in range(3)]
        keys = [notif.sort_key for notif in notifs]
        for msg in notifs:
            message.store_message(msg)

        _, all_messages = message.fetch_timestamp_messages(
            uuid.UUID(self.uaid), " ")
        assert len(all_messages) == len(notifs)
        assert keys == [msg.sort_key for msg in all_messages]
Exemple #12
0
    def test_message_storage_overwrite(self):
        """Test that store_message can overwrite existing messages which
        can occur in some reconnect cases but shouldn't error"""
        chid = str(uuid.uuid4())
        chid2 = str(uuid.uuid4())
        notif1 = make_webpush_notification(self.uaid, chid)
        notif2 = make_webpush_notification(self.uaid, chid)
        notif3 = make_webpush_notification(self.uaid, chid2)
        notif2.message_id = notif1.message_id
        m = get_rotating_message_tablename(boto_resource=self.resource)
        message = Message(m, boto_resource=self.resource)
        message.register_channel(self.uaid, chid)
        message.register_channel(self.uaid, chid2)

        message.store_message(notif1)
        message.store_message(notif2)
        message.store_message(notif3)

        all_messages = list(message.fetch_messages(uuid.UUID(self.uaid)))
        assert len(all_messages) == 2
Exemple #13
0
    def test_register(self):
        chid = str(uuid.uuid4())

        m = get_rotating_message_tablename(boto_resource=self.resource)
        message = Message(m, boto_resource=self.resource)
        message.register_channel(self.uaid, chid)
        lm = self.resource.Table(m)
        # Verify it's in the db
        response = lm.query(
            KeyConditions={
                'uaid': {
                    'AttributeValueList': [self.uaid],
                    'ComparisonOperator': 'EQ'
                },
                'chidmessageid': {
                    'AttributeValueList': ['02'],
                    'ComparisonOperator': 'LT'
                }
            },
            ConsistentRead=True,
        )
        assert len(response.get('Items'))
Exemple #14
0
 def test_all_channels_no_uaid(self):
     m = get_rotating_message_tablename(boto_resource=self.resource)
     message = Message(m, boto_resource=self.resource)
     exists, chans = message.all_channels(dummy_uaid)
     assert chans == set([])