def create_hang(obj, info, hang): id = str(uuid.uuid4()) hang['id'] = id table().put_item(Item=hang) return { 'hang': hang, 'message': 'success', 'code': 200, 'success': True }
def test_get_me(): setup_mocks() user = { 'pk': 'user#123', 'sk': phone, } table().put_item(Item=user) auth = jwt.encode(user, os.environ['API_SECRET'], algorithm='HS256') token = auth.decode('utf8') info = MockObject info.context = MockObject info.context.headers = {"Auth": token} resp = get_me(None, info) assert resp['id'] == '123' assert resp['phone'] == phone
def update_song(obj, info, song): attributes_to_update = build_update_attributes_dictionary(song) update_expression = build_update_expression(song) table().update_item( Key={'id': song['id']}, UpdateExpression=update_expression, ExpressionAttributeValues=attributes_to_update, ) updated_song = table().query( KeyConditionExpression=Key('id').eq(song['id']))['Items'][0] return { 'song': updated_song, 'message': 'success', 'code': 200, 'success': True }
def delete_old_codes(phone): old_codes = table().query( KeyConditionExpression=f'pk = :pk AND begins_with(sk, :sk)', ExpressionAttributeValues={ ':pk': phone, ':sk': 'verificationCode#' } )['Items'] if (len(old_codes) > 0): return [delete_code(phone, code['sk'].split('#')[1]) for code in old_codes]
def send_verification(obj, info, phone, message='Hello! your verification code is'): try: number = phonenumbers.parse(phone, None) e164_number = phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.E164) if not phonenumbers.is_valid_number(number) and phone != '+15551234567': return { 'message': f'Phone number is not E164', 'status': 400, 'success': False } except Exception as e: return { 'message': f'Phone number is not E164', 'status': 400, 'success': False } delete_old_codes(phone) id = str(uuid.uuid4()) verification_code = ''.join(["{}".format(randint(0, 9)) for num in range(0, 6)]) verification = { 'pk': phone, 'sk': f'verificationCode#{verification_code}', 'expiration': str(int(time.time() + 1200)) } # check for phone number table().put_item(Item=verification) client = boto3.client( "sns", region_name="us-east-1" ) client.publish( PhoneNumber = phone, Message = f'{message} {verification_code}' ) return { 'message': 'success', 'status': 200, 'success': True }
def get_me(obj, info): request = info.context token = request.headers["Auth"] values = jwt.decode(token, os.environ['API_SECRET'], algorithm='HS256') user = table().query(KeyConditionExpression=f'pk = :pk AND sk = :sk', ExpressionAttributeValues={ ':pk': values['pk'], ':sk': values['sk'] })['Items'][0] return {'id': user['pk'].split('#')[1], 'phone': user['sk']}
def verify_user(obj, info, verification): phone = verification['phone'] code = verification['code'] verification_query = table().query( KeyConditionExpression=f'pk = :pk AND begins_with(sk, :sk)', ExpressionAttributeValues={ ':pk': phone, ':sk': f'verificationCode#{code}' })['Items'] if (len(verification_query) != 0 and int( float(verification_query[0]['expiration'])) > int(time.time())): delete_code(phone, code) user_from_table = table().query( KeyConditionExpression=f'pk = :pk AND begins_with(sk, :sk)', ExpressionAttributeValues={ ':pk': phone, ':sk': 'user#' })['Items'] if (len(user_from_table) == 1): refreshed_user = table().query( KeyConditionExpression=f'pk = :pk AND begins_with(sk, :sk)', ExpressionAttributeValues={ ':pk': user_from_table[0]['sk'], ':sk': phone })['Items'] auth = jwt.encode(refreshed_user[0], os.environ['API_SECRET'], algorithm='HS256') return { 'auth': auth.decode("utf-8"), 'message': 'success, reauthenticated user', 'status': 200, 'success': True } user_id = str(uuid.uuid4()) phone_record = {'pk': phone, 'sk': f'user#{user_id}'} user = { 'pk': f'user#{user_id}', 'sk': phone, } table().put_item(Item=user) table().put_item(Item=phone_record) auth = jwt.encode(user, os.environ['API_SECRET'], algorithm='HS256') return { 'auth': auth.decode("utf-8"), 'message': 'success, user created', 'status': 200, 'success': True } return {'message': 'not found', 'status': 400, 'success': True}
def test_table(): db = table() assert db != None
def resolve_song(obj, info, id): song = table().query(KeyConditionExpression=Key('id').eq(id))['Items'][0] return song
def get_songs(obj, info, last_evaluated_key): songs = table().scan(Limit=10, Select='ALL_ATTRIBUTES', ExclusiveStartKey={'id': last_evaluated_key}, FilterExpression='attribute_exists(title)')['Items'] return songs
def test_verify_user(): setup_mocks() table().put_item(Item=mock_verification) resp = verify_user(None, None, {'phone': phone, 'code': '123456'}) assert resp['status'] == 200 assert resp['auth'] != None
def delete_code(phone, code): return table().delete_item(Key={ 'pk': phone, 'sk': f'verificationCode#{code}' })