async def test_missing_value(loop): SCHEMA = schema.Schema( required_value=schema.REQUIRED ) values = await SCHEMA.load('''{}''') assert values.error == 'Missing required input: requiredValue'
async def test_unknown_value(loop): SCHEMA = schema.Schema( required_value=schema.REQUIRED ) values = await SCHEMA.load('''{ "requiredValue": "required value", "anotherValue": "should not be set" }''') assert values.error == 'Unknown input: anotherValue'
async def test_schema(loop): SCHEMA = schema.Schema( prompt_user_hash=schema.HASH | schema.REQUIRED, timestamp=(schema.TIMESTAMP, lambda: datetime.datetime.utcnow()), expire=(schema.EXPIRE, 90), encrypted_data=schema.BASE64 ) values = await SCHEMA.load('''{ "promptUserHash": "ebd25cfe070ab282250533a201e38c83249d489a3bf1c8f9718bad6369f59994", "encryptedData": "dGVzdA==" }''') assert not values.error assert values.prompt_user_hash == 'ebd25cfe070ab282250533a201e38c83249d489a3bf1c8f9718bad6369f59994' assert isinstance(values.timestamp, datetime.datetime) assert values.expire == 90 assert base64.b64decode(values.encrypted_data.encode('utf-8')).decode() == 'test'
from app.shared import schema from app.shared.data import load from app.shared.handler import lambda_handler SCHEMA = schema.Schema( seed_hash=schema.HASH | schema.REQUIRED ) @lambda_handler(SCHEMA) async def handler(values=None, **kwargs): backup_data = await load('backup', values.seed_hash, decode=False) if backup_data: return 200, {'state': 'saved', 'backupData': backup_data.decode('utf-8')} return 404, {'error': 'No such seedHash'}
from app.shared import schema from app.shared.data import store from app.shared.handler import lambda_handler SCHEMA = schema.Schema(seed_hash=schema.HASH | schema.REQUIRED, backup_data=schema.BASE64 | schema.REQUIRED) @lambda_handler(SCHEMA) async def handler(values=None, **kwargs): await store('backup', values.seed_hash, values.backup_data, save_previous=True) return 200, {'state': 'saved', 'backupData': values.backup_data}
from app.shared import schema from app.shared.data import load, store from app.shared.handler import lambda_handler from app.shared.utils import is_expired SCHEMA = schema.Schema(prompt_identifier=schema.UUID | schema.REQUIRED) @lambda_handler(SCHEMA) async def handler(values=None, **kwargs): stored_data = await load('prompt', values.prompt_identifier) if not stored_data: return 404, {'error': 'No such promptIdentifier'} state = stored_data.get('state') if stored_data.get('state') in ('pending', 'received') and is_expired( stored_data.get('expireAt')): prompt_user_hash = stored_data.get('promptUserHash') state = 'expired' store_data = dict(stored_data) store_data['state'] = state await store('prompt', values.prompt_identifier, store_data) await store('user', prompt_user_hash, store_data) return 200, { 'state': state, 'responseHash': stored_data.get('responseHash'), 'timestamp': stored_data.get('timestamp'),
import datetime from app.shared import schema from app.shared.data import delete, load, store from app.shared.handler import lambda_handler from app.shared.utils import is_expired SCHEMA = schema.Schema( prompt_user_hash=schema.HASH | schema.REQUIRED, unique_identifier=schema.UUID | schema.REQUIRED, approve=schema.BOOLEAN | schema.REQUIRED, response_hash=schema.HASH, webhook_url=schema.URL, ) @lambda_handler(SCHEMA) async def handler(values=None, **kwargs): stored_data = await load('user', values.prompt_user_hash) if not stored_data: return 404, {'error': 'No available prompt'} if stored_data.get('uniqueIdentifier') != values.unique_identifier: return 404, {'error': 'No available prompt'} if stored_data.get('state') not in ('pending', 'received'): return 404, {'error': 'No available prompt'} if is_expired(stored_data.get('expireAt')): return 404, {'error': 'No available prompt'}
import datetime from app.shared import schema from app.shared.data import delete, load, store from app.shared.handler import lambda_handler from app.shared.utils import convert_timestamp, is_expired SCHEMA = schema.Schema(prompt_user_hash=schema.HASH | schema.REQUIRED, ) @lambda_handler(SCHEMA) async def handler(values=None, **kwargs): stored_data = await load('user', values.prompt_user_hash) if not stored_data: return 404, {'error': 'No available prompt'} prompt_identifier = stored_data.get('promptIdentifier') state = 'pending' if stored_data.get('state') in ( 'pending', 'received') else stored_data.get('state') expire_at = convert_timestamp(stored_data.get('expireAt')) if state not in ('pending', 'expired', 'aborted'): await delete('user', values.prompt_user_hash) return 404, {'error': 'No available prompt'} if stored_data.get('state') in ('pending', 'received') and is_expired(expire_at): state = 'expired' store_data = dict(stored_data) store_data['state'] = state
import datetime import uuid from app.shared import schema from app.shared.data import load, store from app.shared.handler import lambda_handler from app.shared.utils import is_expired, sha3 SCHEMA = schema.Schema(prompt_user_hash=schema.HASH | schema.REQUIRED, timestamp=(schema.TIMESTAMP, lambda: datetime.datetime.utcnow()), expire=(schema.EXPIRE, 90), encrypted_data=schema.BASE64) @lambda_handler(SCHEMA) async def handler(values=None, **kwargs): try: expire_at = values.timestamp + datetime.timedelta( seconds=int(values.expire)) except Exception: return 400, {'error': 'Invalid value for expire'} try: stored_data = await load('user', values.prompt_user_hash) if stored_data: if stored_data.get('state') in ('pending', 'received') and not is_expired( stored_data.get('expireAt')): return 406, {'error': 'Prompt already pending'} if values.encrypted_data and sha3(
from app.shared import schema from app.shared.data import delete, load, store from app.shared.handler import lambda_handler SCHEMA = schema.Schema(prompt_user_hash=schema.HASH | schema.REQUIRED, device_token=schema.DEVICE_TOKEN | schema.REQUIRED, platform=schema.PLATFORM | schema.REQUIRED) @lambda_handler(SCHEMA) async def handler(values=None, **kwargs): subscription_data = await load('subscription', values.prompt_user_hash) if not subscription_data or not isinstance(subscription_data, list): await delete('subscription', values.prompt_user_hash) else: store_data = [] for subscription in subscription_data: if subscription.get('deviceToken') != values.device_token: store_data.append(subscription) if not store_data: await delete('subscription', values.prompt_user_hash) else: await store('subscription', values.prompt_user_hash, store_data) return 200, { 'promptUserHash': values.prompt_user_hash, 'deviceToken': values.device_token, 'platform': values.platform, 'state': 'deleted' }