2016-11-04 19:14:48 +08:00
|
|
|
from test_helper import *
|
2015-11-11 19:28:24 +08:00
|
|
|
from mockredis import MockRedis
|
2016-01-29 18:54:50 +08:00
|
|
|
from cartodb_services.metrics import QuotaService
|
2016-12-23 00:01:18 +08:00
|
|
|
from cartodb_services.metrics import *
|
2015-11-11 19:28:24 +08:00
|
|
|
from unittest import TestCase
|
|
|
|
from nose.tools import assert_raises
|
2016-01-22 01:07:27 +08:00
|
|
|
from datetime import datetime, date
|
2015-11-11 19:28:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestQuotaService(TestCase):
|
|
|
|
|
2016-01-22 01:07:27 +08:00
|
|
|
# single user
|
|
|
|
# user:<username>:<service>:<metric>:YYYYMM:DD
|
|
|
|
# organization user
|
|
|
|
# org:<orgname>:<service>:<metric>:YYYYMM:DD
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.redis_conn = MockRedis()
|
|
|
|
|
|
|
|
def test_should_return_true_if_user_quota_with_no_use(self):
|
2016-04-14 23:50:46 +08:00
|
|
|
qs = self.__build_geocoder_quota_service('test_user')
|
2016-01-22 01:07:27 +08:00
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
|
|
|
|
def test_should_return_true_if_org_quota_with_no_use(self):
|
2016-04-14 23:50:46 +08:00
|
|
|
qs = self.__build_geocoder_quota_service('test_user',
|
|
|
|
orgname='test_org')
|
2016-01-22 01:07:27 +08:00
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
|
|
|
|
def test_should_return_true_if_user_quota_is_not_completely_used(self):
|
2016-04-14 23:50:46 +08:00
|
|
|
qs = self.__build_geocoder_quota_service('test_user')
|
2016-11-04 19:14:48 +08:00
|
|
|
increment_service_uses(self.redis_conn, 'test_user')
|
2016-01-22 01:07:27 +08:00
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
|
|
|
|
def test_should_return_true_if_org_quota_is_not_completely_used(self):
|
2016-04-14 23:50:46 +08:00
|
|
|
qs = self.__build_geocoder_quota_service('test_user',
|
|
|
|
orgname='test_org')
|
2016-11-04 19:14:48 +08:00
|
|
|
increment_service_uses(self.redis_conn, 'test_user',
|
2016-12-20 00:52:38 +08:00
|
|
|
orgname='test_org')
|
2016-01-22 01:07:27 +08:00
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
|
|
|
|
def test_should_return_false_if_user_quota_is_surpassed(self):
|
2016-04-14 23:50:46 +08:00
|
|
|
qs = self.__build_geocoder_quota_service('test_user')
|
2016-11-04 19:14:48 +08:00
|
|
|
increment_service_uses(self.redis_conn, 'test_user',
|
2016-12-20 00:52:38 +08:00
|
|
|
amount=300)
|
2016-01-22 01:07:27 +08:00
|
|
|
assert qs.check_user_quota() is False
|
|
|
|
|
|
|
|
def test_should_return_false_if_org_quota_is_surpassed(self):
|
2016-04-14 23:50:46 +08:00
|
|
|
qs = self.__build_geocoder_quota_service('test_user',
|
|
|
|
orgname='test_org')
|
2016-11-04 19:14:48 +08:00
|
|
|
increment_service_uses(self.redis_conn, 'test_user',
|
2016-12-20 00:52:38 +08:00
|
|
|
orgname='test_org', amount=400)
|
2016-01-22 01:07:27 +08:00
|
|
|
assert qs.check_user_quota() is False
|
|
|
|
|
|
|
|
def test_should_return_true_if_user_quota_is_surpassed_but_soft_limit_is_enabled(self):
|
2016-04-14 23:50:46 +08:00
|
|
|
qs = self.__build_geocoder_quota_service('test_user', soft_limit=True)
|
2016-11-04 19:14:48 +08:00
|
|
|
increment_service_uses(self.redis_conn, 'test_user',
|
2016-12-20 00:52:38 +08:00
|
|
|
amount=300)
|
2016-01-22 01:07:27 +08:00
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
|
|
|
|
def test_should_return_true_if_org_quota_is_surpassed_but_soft_limit_is_enabled(self):
|
2016-04-14 23:50:46 +08:00
|
|
|
qs = self.__build_geocoder_quota_service('test_user',
|
|
|
|
orgname='test_org',
|
|
|
|
soft_limit=True)
|
2016-11-04 19:14:48 +08:00
|
|
|
increment_service_uses(self.redis_conn, 'test_user',
|
2016-12-20 00:52:38 +08:00
|
|
|
orgname='test_org', amount=400)
|
2016-01-22 01:07:27 +08:00
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
|
|
|
|
def test_should_check_user_increment_and_quota_check_correctly(self):
|
2016-04-14 23:50:46 +08:00
|
|
|
qs = self.__build_geocoder_quota_service('test_user', quota=2)
|
2016-03-07 22:40:37 +08:00
|
|
|
qs.increment_success_service_use()
|
2016-01-29 18:54:50 +08:00
|
|
|
assert qs.check_user_quota() is True
|
2016-03-07 22:40:37 +08:00
|
|
|
qs.increment_success_service_use(amount=2)
|
2016-01-29 18:54:50 +08:00
|
|
|
assert qs.check_user_quota() is False
|
2016-01-22 01:07:27 +08:00
|
|
|
|
|
|
|
def test_should_check_org_increment_and_quota_check_correctly(self):
|
2016-04-14 23:50:46 +08:00
|
|
|
qs = self.__build_geocoder_quota_service('test_user', quota=2,
|
|
|
|
orgname='test_org')
|
2016-03-07 22:40:37 +08:00
|
|
|
qs.increment_success_service_use()
|
2016-01-29 18:54:50 +08:00
|
|
|
assert qs.check_user_quota() is True
|
2016-03-07 22:40:37 +08:00
|
|
|
qs.increment_success_service_use(amount=2)
|
2016-01-29 18:54:50 +08:00
|
|
|
assert qs.check_user_quota() is False
|
2016-01-22 01:07:27 +08:00
|
|
|
|
2016-04-14 23:50:46 +08:00
|
|
|
def test_should_check_user_mapzen_geocoder_quota_correctly(self):
|
2016-12-23 00:01:18 +08:00
|
|
|
qs = self.__build_geocoder_quota_service('test_user',
|
|
|
|
provider='mapzen')
|
2016-04-14 23:50:46 +08:00
|
|
|
qs.increment_success_service_use()
|
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
qs.increment_success_service_use(amount=1500000)
|
|
|
|
assert qs.check_user_quota() is False
|
|
|
|
|
|
|
|
def test_should_check_org_mapzen_geocoder_quota_correctly(self):
|
2016-12-23 00:01:18 +08:00
|
|
|
qs = self.__build_geocoder_quota_service('test_user',
|
|
|
|
orgname='testorg',
|
|
|
|
provider='mapzen')
|
2016-04-14 23:50:46 +08:00
|
|
|
qs.increment_success_service_use()
|
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
qs.increment_success_service_use(amount=1500000)
|
|
|
|
assert qs.check_user_quota() is False
|
|
|
|
|
2018-01-02 23:21:57 +08:00
|
|
|
def test_should_check_user_mapbox_geocoder_quota_correctly(self):
|
|
|
|
qs = self.__build_geocoder_quota_service('test_user',
|
|
|
|
provider='mapbox')
|
|
|
|
qs.increment_success_service_use()
|
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
qs.increment_success_service_use(amount=1500000)
|
|
|
|
assert qs.check_user_quota() is False
|
|
|
|
|
|
|
|
def test_should_check_org_mapbox_geocoder_quota_correctly(self):
|
|
|
|
qs = self.__build_geocoder_quota_service('test_user',
|
|
|
|
orgname='testorg',
|
|
|
|
provider='mapbox')
|
|
|
|
qs.increment_success_service_use()
|
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
qs.increment_success_service_use(amount=1500000)
|
|
|
|
assert qs.check_user_quota() is False
|
|
|
|
|
2018-01-04 19:30:58 +08:00
|
|
|
def test_should_check_user_mapzen_routing_quota_correctly(self):
|
|
|
|
qs = self.__build_routing_quota_service('test_user', provider='mapzen')
|
2016-04-14 23:50:46 +08:00
|
|
|
qs.increment_success_service_use()
|
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
qs.increment_success_service_use(amount=1500000)
|
|
|
|
assert qs.check_user_quota() is False
|
|
|
|
|
2018-01-04 19:30:58 +08:00
|
|
|
def test_should_check_org_mapzen_routing_quota_correctly(self):
|
|
|
|
qs = self.__build_routing_quota_service('test_user', provider='mapzen',
|
|
|
|
orgname='testorg')
|
2016-04-14 23:50:46 +08:00
|
|
|
qs.increment_success_service_use()
|
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
qs.increment_success_service_use(amount=1500000)
|
|
|
|
assert qs.check_user_quota() is False
|
|
|
|
|
2018-01-04 19:30:58 +08:00
|
|
|
def test_should_check_user_mapbox_routing_quota_correctly(self):
|
|
|
|
qs = self.__build_routing_quota_service('test_user', provider='mapbox')
|
|
|
|
qs.increment_success_service_use()
|
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
qs.increment_success_service_use(amount=1500000)
|
|
|
|
assert qs.check_user_quota() is False
|
|
|
|
|
|
|
|
def test_should_check_org_mapbox_routing_quota_correctly(self):
|
|
|
|
qs = self.__build_routing_quota_service('test_user', provider='mapbox',
|
|
|
|
orgname='testorg')
|
|
|
|
qs.increment_success_service_use()
|
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
qs.increment_success_service_use(amount=1500000)
|
|
|
|
assert qs.check_user_quota() is False
|
|
|
|
|
|
|
|
def test_should_check_user_mapzen_isolines_quota_correctly(self):
|
|
|
|
qs = self.__build_isolines_quota_service('test_user',
|
|
|
|
provider='mapzen')
|
|
|
|
qs.increment_isolines_service_use()
|
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
qs.increment_isolines_service_use(amount=1500000)
|
|
|
|
assert qs.check_user_quota() is False
|
|
|
|
|
|
|
|
def test_should_check_org_mapzen_isolines_quota_correctly(self):
|
|
|
|
qs = self.__build_isolines_quota_service('test_user',
|
|
|
|
provider='mapzen',
|
|
|
|
orgname='testorg')
|
|
|
|
qs.increment_isolines_service_use()
|
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
qs.increment_isolines_service_use(amount=1500000)
|
|
|
|
assert qs.check_user_quota() is False
|
|
|
|
|
|
|
|
def test_should_check_user_mapbox_isolines_quota_correctly(self):
|
|
|
|
qs = self.__build_isolines_quota_service('test_user',
|
|
|
|
provider='mapbox')
|
2016-12-23 00:01:18 +08:00
|
|
|
qs.increment_isolines_service_use()
|
2016-07-21 19:46:57 +08:00
|
|
|
assert qs.check_user_quota() is True
|
2016-12-23 00:01:18 +08:00
|
|
|
qs.increment_isolines_service_use(amount=1500000)
|
2016-07-21 19:46:57 +08:00
|
|
|
assert qs.check_user_quota() is False
|
|
|
|
|
2018-01-04 19:30:58 +08:00
|
|
|
def test_should_check_org_mapbox_isolines_quota_correctly(self):
|
2016-12-23 00:01:18 +08:00
|
|
|
qs = self.__build_isolines_quota_service('test_user',
|
2018-01-04 19:30:58 +08:00
|
|
|
provider='mapbox',
|
2016-12-23 00:01:18 +08:00
|
|
|
orgname='testorg')
|
|
|
|
qs.increment_isolines_service_use()
|
2016-07-21 19:46:57 +08:00
|
|
|
assert qs.check_user_quota() is True
|
2016-12-23 00:01:18 +08:00
|
|
|
qs.increment_isolines_service_use(amount=1500000)
|
2016-07-21 19:46:57 +08:00
|
|
|
assert qs.check_user_quota() is False
|
|
|
|
|
2017-08-24 20:35:20 +08:00
|
|
|
# Quick workaround so we don't take into account numer of credits
|
|
|
|
# spent for users that have defined the quota.
|
|
|
|
# See https://github.com/CartoDB/bigmetadata/issues/215
|
2016-12-23 00:01:18 +08:00
|
|
|
def test_should_check_org_obs_quota_correctly(self):
|
|
|
|
qs = self.__build_obs_quota_service('test_user',
|
|
|
|
orgname='testorg')
|
|
|
|
qs.increment_success_service_use()
|
|
|
|
assert qs.check_user_quota() is True
|
|
|
|
qs.increment_success_service_use(amount=100000)
|
2017-08-24 20:35:20 +08:00
|
|
|
assert qs.check_user_quota() is True
|
2016-12-23 00:01:18 +08:00
|
|
|
|
2016-12-20 00:52:38 +08:00
|
|
|
def __prepare_quota_service(self, username, service, quota, provider,
|
|
|
|
orgname, soft_limit, end_date):
|
|
|
|
build_redis_user_config(self.redis_conn, username, service,
|
|
|
|
quota=quota, provider=provider,
|
|
|
|
soft_limit=soft_limit, end_date=end_date)
|
2016-01-22 01:07:27 +08:00
|
|
|
if orgname:
|
2016-12-20 00:52:38 +08:00
|
|
|
build_redis_org_config(self.redis_conn, orgname, service,
|
|
|
|
quota=quota, provider=provider,
|
|
|
|
end_date=end_date)
|
2016-04-14 23:50:46 +08:00
|
|
|
|
|
|
|
def __build_geocoder_quota_service(self, username, quota=100,
|
2016-12-20 00:52:38 +08:00
|
|
|
provider='heremaps', orgname=None,
|
2016-04-14 23:50:46 +08:00
|
|
|
soft_limit=False,
|
|
|
|
end_date=datetime.today()):
|
2016-12-20 00:52:38 +08:00
|
|
|
self.__prepare_quota_service(username, 'geocoding', quota,
|
|
|
|
provider, orgname, soft_limit, end_date)
|
2016-11-04 19:14:48 +08:00
|
|
|
geocoder_config = GeocoderConfig(self.redis_conn, plpy_mock,
|
2016-03-08 19:24:27 +08:00
|
|
|
username, orgname)
|
2016-04-14 23:50:46 +08:00
|
|
|
return QuotaService(geocoder_config, redis_connection=self.redis_conn)
|
|
|
|
|
2018-01-04 19:30:58 +08:00
|
|
|
def __build_routing_quota_service(self, username, provider,
|
2016-04-22 22:43:28 +08:00
|
|
|
orgname=None, soft_limit=False,
|
|
|
|
quota=100, end_date=datetime.today()):
|
2016-12-20 00:52:38 +08:00
|
|
|
self.__prepare_quota_service(username, 'routing', quota, provider,
|
|
|
|
orgname, soft_limit, end_date)
|
2016-11-04 19:14:48 +08:00
|
|
|
routing_config = RoutingConfig(self.redis_conn, plpy_mock,
|
2016-04-14 23:50:46 +08:00
|
|
|
username, orgname)
|
|
|
|
return QuotaService(routing_config, redis_connection=self.redis_conn)
|
2016-04-20 00:45:45 +08:00
|
|
|
|
2018-01-04 19:30:58 +08:00
|
|
|
def __build_isolines_quota_service(self, username, provider,
|
2016-07-21 19:46:57 +08:00
|
|
|
orgname=None, soft_limit=False,
|
|
|
|
quota=100, end_date=datetime.today()):
|
2016-12-20 00:52:38 +08:00
|
|
|
self.__prepare_quota_service(username, 'isolines', quota, provider,
|
|
|
|
orgname, soft_limit, end_date)
|
2016-11-04 19:14:48 +08:00
|
|
|
isolines_config = IsolinesRoutingConfig(self.redis_conn, plpy_mock,
|
2016-07-21 19:46:57 +08:00
|
|
|
username, orgname)
|
|
|
|
return QuotaService(isolines_config, redis_connection=self.redis_conn)
|
|
|
|
|
2016-12-23 00:01:18 +08:00
|
|
|
def __build_obs_quota_service(self, username, quota=100,
|
|
|
|
provider='obs_general',
|
|
|
|
orgname=None,
|
|
|
|
soft_limit=False,
|
|
|
|
end_date=datetime.today()):
|
|
|
|
self.__prepare_quota_service(username, 'data_observatory', quota,
|
|
|
|
None, orgname, soft_limit, end_date)
|
|
|
|
do_config = ObservatoryConfig(self.redis_conn, plpy_mock,
|
|
|
|
username, orgname)
|
|
|
|
return QuotaService(do_config, redis_connection=self.redis_conn)
|