Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
bug: fix _expiry() function to use seconds
Browse files Browse the repository at this point in the history
Also enforce expiry on router records

Closes #1073
  • Loading branch information
jrconlin committed Mar 6, 2018
1 parent 0eddf9b commit cfcad77
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
7 changes: 6 additions & 1 deletion autopush/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def _make_table(


def _expiry(ttl):
return int(time.time() + ttl)
return int(time.time()) + ttl


def get_router_table(tablename="router", read_throughput=5,
Expand Down Expand Up @@ -776,6 +776,9 @@ def get_uaid(self, uaid):
# Incomplete record, drop it.
self.drop_user(uaid)
raise ItemNotFound("uaid not found")
if item.get("expiry") < _expiry(0):
self.drop_user(uaid)
raise ItemNotFound("uaid expired")
return item
except Boto3Error: # pragma: nocover
# We trap JSONResponseError because Moto returns text instead of
Expand Down Expand Up @@ -805,6 +808,8 @@ def register_user(self, data):
# AWS.
raise AutopushException("data is missing router_type "
"or connected_at")
if "expiry" not in data:
data["expiry"] = _expiry(MAX_EXPIRY)
# Generate our update expression
expr = "SET " + ", ".join(["%s=:%s" % (x, x) for x in data.keys()])
expr_values = {":%s" % k: v for k, v in data.items()}
Expand Down
12 changes: 12 additions & 0 deletions autopush/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,18 @@ def raise_error(*args, **kwargs):
router_type="webpush"))
assert res == (False, {})

def test_register_user_expired(self):
from time import time

router = Router(self.table_conf, SinkMetrics(), resource=self.resource)
expired = self._create_minimal_record()
uaid = expired["uaid"]
expired["expiry"] = int(time()) - 100
self.router.register_user(expired)
with pytest.raises(ItemNotFound) as ex:
router.get_uaid(uaid)
assert ex.value.message == "uaid expired"

def test_clear_node_provision_failed(self):
router = Router(self.table_conf, SinkMetrics(),
resource=self.resource)
Expand Down
2 changes: 1 addition & 1 deletion autopush/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ def test_ttl_batch_expired_and_good_one(self):
for x in range(0, 12):
yield client.send_notification(data=data, ttl=1)

yield client.send_notification(data=data2)
yield client.send_notification(data=data2, ttl=200)
time.sleep(1.5)
yield client.connect()
yield client.hello()
Expand Down
1 change: 0 additions & 1 deletion autopush/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,6 @@ def from_message_table(cls, uaid, item):
sortkey_timestamp=key_info.get("sortkey_timestamp"),
source="Stored"
)

# Ensure we generate the sort-key properly for legacy messges
if key_info["api_ver"] == "00":
notif.legacy = True
Expand Down

0 comments on commit cfcad77

Please sign in to comment.