Exemplo n.º 1
0
    def test_basic_last_connect(self):
        client = yield self.quick_register()
        yield client.disconnect()

        # Verify the last_connect is there and the current month
        c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
        eq_(True, has_connected_this_month(c))

        # Move it back
        today = get_month(delta=-1)
        c["last_connect"] = "%s%s020001" % (today.year,
                                            str(today.month).zfill(2))
        yield deferToThread(c.partial_save)
        eq_(False, has_connected_this_month(c))

        # Connect/disconnect again and verify last_connect move
        yield client.connect()
        yield client.hello()
        yield client.disconnect()
        times = 0
        while times < 10:
            c = yield deferToThread(self._settings.router.get_uaid,
                                    client.uaid)
            if has_connected_this_month(c):
                break
            else:  # pragma: nocover
                times += 1
                yield client.sleep(1)
        log.debug("Last connected time: %s", c.get("last_connect", "None"))
        eq_(True, has_connected_this_month(c))
Exemplo n.º 2
0
    def test_basic_last_connect(self):
        client = yield self.quick_register()
        yield client.disconnect()

        # Verify the last_connect is there and the current month
        c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
        eq_(True, has_connected_this_month(c))

        # Move it back
        today = get_month(delta=-1)
        c["last_connect"] = "%s%s020001" % (today.year,
                                            str(today.month).zfill(2))
        yield deferToThread(c.partial_save)
        eq_(False, has_connected_this_month(c))

        # Connect/disconnect again and verify last_connect move
        yield client.connect()
        yield client.hello()
        yield client.disconnect()
        times = 0
        while times < 10:
            c = yield deferToThread(self._settings.router.get_uaid,
                                    client.uaid)
            if has_connected_this_month(c):
                break
            else:  # pragma: nocover
                times += 1
                yield client.sleep(1)
        log.debug("Last connected time: %s", c.get("last_connect", "None"))
        eq_(True, has_connected_this_month(c))
    def test_post_gcm(self):
        self.patch('uuid.uuid4',
                   side_effect=(uuid.uuid4(), dummy_uaid, dummy_chid))

        from autopush.router.gcm import GCMRouter
        sids = {"182931248179192": {"auth": "aailsjfilajdflijdsilfjsliaj"}}
        gcm = GCMRouter(
            self.conf,
            {"dryrun": True, "senderIDs": sids,
             "endpoint": "gcm-http.googleapis.com/gcm/send"},
            SinkMetrics()
        )
        self.routers["gcm"] = gcm
        self.fernet_mock.configure_mock(**{
            'encrypt.return_value': 'abcd123',
        })

        resp = yield self.client.post(
            self.url(router_type="gcm", router_token="182931248179192"),
            headers={"Authorization": self.auth},
            body=json.dumps(dict(
                channelID=str(dummy_chid),
                token="182931248179192",
            ))
        )
        assert resp.get_status() == 200

        payload = json.loads(resp.content)
        assert payload["uaid"] == dummy_uaid.hex
        assert payload["channelID"] == dummy_chid.hex
        assert payload["endpoint"] == "http://localhost/wpush/v1/abcd123"
        calls = self.db.router.register_user.call_args
        call_args = calls[0][0]
        assert has_connected_this_month(call_args) is True
        assert "secret" in payload
Exemplo n.º 4
0
    def _update_last_connect(self, previous):
        """Update last_connect given old router values if needed"""
        if has_connected_this_month(previous):
            return

        self.force_retry(self.ap_settings.router.update_last_connect,
                         self.ps.uaid)
Exemplo n.º 5
0
    def _update_last_connect(self, previous):
        """Update last_connect given old router values if needed"""
        if has_connected_this_month(previous):
            return

        self.force_retry(self.ap_settings.router.update_last_connect,
                         self.ps.uaid)
Exemplo n.º 6
0
    def test_basic_last_connect(self):
        client = yield self.quick_register()
        yield client.disconnect()

        # Verify the last_connect is there and the current month
        c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
        eq_(True, has_connected_this_month(c))

        # Move it back
        today = get_month(delta=-1)
        c["last_connect"] = "%s%s020001" % (today.year, str(today.month).zfill(2))
        yield deferToThread(c.partial_save)
        eq_(False, has_connected_this_month(c))

        # Connect/disconnect again and verify last_connect move
        yield client.connect()
        yield client.hello()
        yield client.disconnect()
        c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
        eq_(True, has_connected_this_month(c))
Exemplo n.º 7
0
 def handle_finish(value):
     call_args = self.reg.write.call_args
     ok_(call_args is not None)
     args = call_args[0]
     call_arg = json.loads(args[0])
     eq_(call_arg["uaid"], dummy_uaid.hex)
     eq_(call_arg["channelID"], dummy_chid.hex)
     eq_(call_arg["endpoint"], "http://localhost/wpush/v1/abcd123")
     calls = self.reg.ap_settings.router.register_user.call_args
     call_args = calls[0][0]
     eq_(True, has_connected_this_month(call_args))
     ok_("secret" in call_arg)
Exemplo n.º 8
0
 def handle_finish(value):
     call_args = self.reg.write.call_args
     ok_(call_args is not None)
     args = call_args[0]
     call_arg = json.loads(args[0])
     eq_(call_arg["uaid"], dummy_uaid)
     eq_(call_arg["channelID"], dummy_chid)
     eq_(call_arg["endpoint"], "http://localhost/push/abcd123")
     calls = self.reg.ap_settings.router.register_user.call_args
     call_args = calls[0][0]
     eq_(True, has_connected_this_month(call_args))
     ok_("secret" in call_arg)
Exemplo n.º 9
0
    def lookup_user(self, hello):
        # type: (Hello) -> (Optional[JSONDict], JSONDict)
        flags = dict(
            message_month=None,
            check_storage=False,
            reset_uaid=False,
            rotate_message_table=False,
        )
        uaid = hello.uaid.hex
        try:
            record = self.db.router.get_uaid(uaid)
        except ItemNotFound:
            return None, flags

        # All records must have a router_type and connected_at, in some odd
        # cases a record exists for some users without it
        if "router_type" not in record or "connected_at" not in record:
            self.drop_user(uaid, record, 104)
            return None, flags

        # Current month must exist and be a valid prior month
        if ("current_month" not in record) or record["current_month"] \
                not in self.db.message_tables:
            self.drop_user(uaid, record, 105)
            return None, flags

        # If we got here, its a valid user that needs storage checked
        flags["check_storage"] = True

        # Determine if message table rotation is needed
        flags["message_month"] = record["current_month"]
        if record["current_month"] != self.db.current_msg_month:
            flags["rotate_message_table"] = True

        # Include and update last_connect if needed, otherwise exclude
        if has_connected_this_month(record):
            del record["last_connect"]
        else:
            record["last_connect"] = generate_last_connect()

        # Determine if this is missing a record version
        if ("record_version" not in record or
                int(record["record_version"]) < USER_RECORD_VERSION):
            flags["reset_uaid"] = True

        # Update the node_id, connected_at for this node/connected_at
        record["node_id"] = self.conf.router_url
        record["connected_at"] = hello.connected_at
        return record, flags
Exemplo n.º 10
0
    def test_webpush_monthly_rotation(self):
        from autopush.db import make_rotating_tablename

        client = yield self.quick_register(use_webpush=True)
        yield client.disconnect()

        # Move the client back one month to the past
        last_month = make_rotating_tablename(prefix=self._settings._message_prefix, delta=-1)
        lm_message = self._settings.message_tables[last_month]
        yield deferToThread(self._settings.router.update_message_month, client.uaid, last_month)

        # Verify the move
        c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
        eq_(c["current_month"], last_month)

        # Verify last_connect is current, then move that back
        ok_(has_connected_this_month(c))
        today = get_month(delta=-1)
        c["last_connect"] = "%s%s020001" % (today.year, str(today.month).zfill(2))
        yield deferToThread(c.partial_save)
        eq_(False, has_connected_this_month(c))

        # Move the clients channels back one month
        exists, chans = yield deferToThread(self._settings.message.all_channels, client.uaid)
        eq_(exists, True)
        eq_(len(chans), 1)
        yield deferToThread(lm_message.save_channels, client.uaid, chans)

        # Remove the channels entry entirely from this month
        yield deferToThread(self._settings.message.table.delete_item, uaid=client.uaid, chidmessageid=" ")

        # Verify the channel is gone
        exists, chans = yield deferToThread(self._settings.message.all_channels, client.uaid)
        eq_(exists, False)
        eq_(len(chans), 0)

        # Send in a notification, verify it landed in last months notification
        # table
        data = uuid.uuid4().hex
        yield client.send_notification(data=data)
        notifs = yield deferToThread(lm_message.fetch_messages, client.uaid)
        eq_(len(notifs), 1)

        # Connect the client, verify the migration
        yield client.connect()
        yield client.hello()

        # Pull down the notification
        result = yield client.get_notification()
        chan = client.channels.keys()[0]
        ok_(result is not None)
        eq_(chan, result["channelID"])

        # Check that the client is going to rotate the month
        server_client = self._settings.clients[client.uaid]
        eq_(server_client.ps.rotate_message_table, True)

        # Acknowledge the notification, which triggers the migration
        yield client.ack(chan, result["version"])

        # Wait up to 2 seconds for the table rotation to occur
        start = time.time()
        while time.time() - start < 2:
            c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
            if c["current_month"] == self._settings.current_msg_month:
                break
            else:
                yield deferToThread(time.sleep, 0.2)

        # Verify the month update in the router table
        c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
        eq_(c["current_month"], self._settings.current_msg_month)
        eq_(server_client.ps.rotate_message_table, False)

        # Verify the client moved last_connect
        eq_(True, has_connected_this_month(c))

        # Verify the channels were moved
        exists, chans = yield deferToThread(self._settings.message.all_channels, client.uaid)
        eq_(exists, True)
        eq_(len(chans), 1)

        yield self.shut_down(client)
Exemplo n.º 11
0
    def test_webpush_monthly_rotation_prior_record_exists(self):
        from autopush.db import make_rotating_tablename
        client = yield self.quick_register(use_webpush=True)
        yield client.disconnect()

        # Move the client back one month to the past
        last_month = make_rotating_tablename(
            prefix=self._settings._message_prefix, delta=-1)
        lm_message = self._settings.message_tables[last_month]
        yield deferToThread(
            self._settings.router.update_message_month,
            client.uaid,
            last_month
        )

        # Verify the move
        c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
        eq_(c["current_month"], last_month)

        # Verify last_connect is current, then move that back
        ok_(has_connected_this_month(c))
        today = get_month(delta=-1)
        c["last_connect"] = "%s%s020001" % (today.year,
                                            str(today.month).zfill(2))
        yield deferToThread(c.partial_save)
        eq_(False, has_connected_this_month(c))

        # Move the clients channels back one month
        exists, chans = yield deferToThread(
            self._settings.message.all_channels, client.uaid
        )
        eq_(exists, True)
        eq_(len(chans), 1)
        yield deferToThread(
            lm_message.save_channels,
            client.uaid,
            chans
        )

        # Send in a notification, verify it landed in last months notification
        # table
        data = uuid.uuid4().hex
        yield client.send_notification(data=data)
        notifs = yield deferToThread(lm_message.fetch_messages, client.uaid)
        eq_(len(notifs), 1)

        # Connect the client, verify the migration
        yield client.connect()
        yield client.hello()

        # Pull down the notification
        result = yield client.get_notification()
        chan = client.channels.keys()[0]
        ok_(result is not None)
        eq_(chan, result["channelID"])

        # Check that the client is going to rotate the month
        server_client = self._settings.clients[client.uaid]
        eq_(server_client.ps.rotate_message_table, True)

        # Acknowledge the notification, which triggers the migration
        yield client.ack(chan, result["version"])

        # Wait up to 2 seconds for the table rotation to occur
        start = time.time()
        while time.time()-start < 2:
            c = yield deferToThread(self._settings.router.get_uaid,
                                    client.uaid)
            if c["current_month"] == self._settings.current_msg_month:
                break
            else:
                yield deferToThread(time.sleep, 0.2)

        # Verify the month update in the router table
        c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
        eq_(c["current_month"], self._settings.current_msg_month)
        eq_(server_client.ps.rotate_message_table, False)

        # Verify the client moved last_connect
        eq_(True, has_connected_this_month(c))

        # Verify the channels were moved
        exists, chans = yield deferToThread(
            self._settings.message.all_channels,
            client.uaid
        )
        eq_(exists, True)
        eq_(len(chans), 1)

        yield self.shut_down(client)