def _load_simple(self): app = AppAccess(secret="test").put() free = Profile(parent=app, name="free", default=True, restrictions=[ # unlimited: BinaryRestriction(action="take_photo", allow=True), # simple: PerTimeRestriction(action="upload_photo", limit_to=10, duration=24 * 60 * 60), # double restriction: PerTimeRestriction(action="share_photo", limit_to=10, duration=24 * 60 * 60), PerTimeRestriction(action="share_photo", limit_to=20, duration=7 * 24 * 60 * 60), ]).put() premium = Profile(parent=app, name="premium", allow_per_default=True, restrictions=[ # simple: PerTimeRestriction(action="upload_photo", limit_to=100, duration=24 * 60 * 60), # endless restriction: TotalAmountRestriction(action="quota", total_max=1000), ]).put() User(id="free_u", parent=app, assigned_profile=free).put() User(id="prem", parent=app, assigned_profile=premium).put() Device(id="AMEI_FREE", parent=app, assigned_profile=free).put() Device(id="MIAU_PREM", parent=app, assigned_profile=premium).put() self.app_access = app.get()
class ApiHandlerMixin(object): handler_cls = None path = "/" def setUp(self): super(ApiHandlerMixin, self).setUp() # Create a WSGI application. app = webapp2.WSGIApplication([(self.path, self.handler_cls)]) # Wrap the app with WebTest’s TestApp. self.testapp = webtest.TestApp(app) self._default_access = AppAccess(secret="meine weihnacht").put() def _sign(self, method, url, params, key=None, secret=None): if not key: key = self._default_access.urlsafe() if not secret: secret = self._default_access.get().secret.encode("utf-8") params["_key"] = key encoded = urllib.urlencode(params) query = "&".join((method.upper(), url, encoded)) encoded += "&_signature=" + urllib.quote(binascii.b2a_base64( hmac.new(secret, query, hashlib.sha256).digest())) return encoded def ResetKindMap(self): # we run on live models pass def get(self, key=None, secret=None, status=None, expect_errors=False, **params): signed_url = self._sign("GET", "http://localhost" + self.path, params, key, secret) return self.testapp.get(self.path, status=status, expect_errors=expect_errors, params=signed_url) def post(self, key=None, secret=None, status=None, expect_errors=False, **params): signed_url = self._sign("POST", "http://localhost" + self.path, params, key, secret) return self.testapp.post(self.path, status=status, expect_errors=expect_errors, params=signed_url)