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()
def init_jerry_db(): from google.appengine.ext.ndb import Key from models import AppAccess, Profile app = AppAccess(key=Key(urlsafe="agxkZXZ-ai1lcnJpY29yEAsSCUFwcEFjY2VzcxjpBww"), name="jerrico", secret="982b3800288b452a888e3bd31d982adf") app.put() Profile(name="free", default=True, parent=app.key).put() Profile(name="premium", default=False, parent=app.key).put() Profile(name="megalon", allow_per_default=True, parent=app.key).put()
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()
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)
def _add_item(self, params): name = params.get("name", None) template = params.get("template", None) if not name: webapp2.abort(400, "No app name given") app = AppAccess.create(name, owner=self.user) app.put() if template: self._setup_template(app, template) self._post_add(app, params) return app.prepare_json()