From 6547e75a19f2caae4610567d4d3335e617915e7e Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Fri, 27 Mar 2020 17:26:35 +0100 Subject: [PATCH] Adds user exist restriction to fill/free slots and update comments/docs --- teos/gatekeeper.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/teos/gatekeeper.py b/teos/gatekeeper.py index 4807be3..0d5131b 100644 --- a/teos/gatekeeper.py +++ b/teos/gatekeeper.py @@ -3,7 +3,6 @@ import re from common.cryptographer import Cryptographer -# TODO: UNITTEST class NotEnoughSlots(ValueError): """Raise this when trying to subtract more slots than a user has available.""" @@ -103,12 +102,16 @@ class Gatekeeper: Args: user_pk(:obj:`str`): the public key that identifies the user (33-bytes hex str). n: the number of slots to fill. + n (:obj:`int`): the number of slots to fill. Raises: :obj:``: if the user subscription does not have enough slots. """ - if n <= self.registered_users.get(user_pk): + # We are not making sure the value passed is a integer, but the value is computed by the API and rounded before + # passing it to the gatekeeper. + # DISCUSS: we may want to return a different exception if teh user does not exist + if user_pk in self.registered_users and n <= self.registered_users.get(user_pk): self.registered_users[user_pk] -= n else: raise NotEnoughSlots(user_pk, n) @@ -122,4 +125,8 @@ class Gatekeeper: n: the number of slots to free. """ - self.registered_users[user_pk] += n + # We are not making sure the value passed is a integer, but the value is computed by the API and rounded before + # passing it to the gatekeeper. + # DISCUSS: if the user does not exist we may want to log or return an exception. + if user_pk in self.registered_users: + self.registered_users[user_pk] += n