Esempio n. 1
0
    def test_set_props(self):
        set_props('id', props={'key': 'value'})
        prop = db.table('userProperty').first()

        self.assertEqual(prop.get('key'), 'key')
        self.assertEqual(prop.get('value'), 'value')
        self.assertTrue(prop.get('active'))
        self.assertIsNone(prop.get('unique'))
Esempio n. 2
0
    def test_exists(self):
        set_props(new_id(), props={'username': '******'}, unique=True)

        event = {'body': json.dumps({'props': {'username': '******'}})}
        res = handler(event, None)

        body = json.loads(res['body'])

        self.assertEqual(res['statusCode'], 200)
        self.assertTrue(body['data']['username']['exists'])
        self.assertEqual(body['data']['username']['value'], 'riverleo')
Esempio n. 3
0
    def test_set_props_by_unique(self):
        set_props('user1', props={'key': 'unique_value'}, unique=True)

        self.assertEqual(
            db.table('userProperty').where({
                'userId': 'user1',
                'value': 'unique_value',
            }).count(), 1)

        # 다른 사용자가 유니크한 값을 사용할 수 없다.
        with self.assertRaises(QueryException):
            set_props('user2', props={'key': 'unique_value'}, unique=True)

        # 기존 사용자는 값을 변경할 수 있다.
        set_props('user1', props={'key': 'unique_another_value'}, unique=True)
        self.assertEqual(
            db.table('userProperty').where({
                'userId': 'user1',
                'value': 'unique_another_value',
            }).count(), 1)

        # 기존 사용자가 해당 값을 더이상 사용하지 않은 경우 변경 가능하다
        set_props('user2', props={'key': 'unique_value'}, unique=True)
        self.assertEqual(
            db.table('userProperty').where({
                'userId': 'user2',
                'value': 'unique_value',
            }).count(), 1)
Esempio n. 4
0
    def test_me_by_jwt(self):
        user_id = new_id()
        db.table('user').insert(id=user_id)
        set_props(user_id, props={'key': 'value'})
        encoded = jwt_encode(user_id)

        res = handler({
            'body': json.dumps({'props': ['key', 'anonymous']}),
            'headers': {'Authorization': 'Bearer {}'.format(encoded)},
        }, None)

        body = json.loads(res['body'])

        self.assertEqual(res['statusCode'], 200)
        self.assertEqual(body['data']['id'], user_id)
        self.assertEqual(body['data']['roles'], [])
        self.assertEqual(body['data']['props']['key'], 'value')
        self.assertIsNone(body['data']['props']['anonymous'])
Esempio n. 5
0
    def test_get(self):
        user_id = new_id()
        db.table('user').insert(id=user_id)
        set_props(user_id, props={'key': 'value'})

        res = handler({
            'queryStringParameters': {
                'props': 'key,anonymous'
            },
        }, None)

        body = json.loads(res['body'])

        self.assertEqual(res['statusCode'], 200)
        self.assertEqual(body['data'][0]['id'], user_id)
        self.assertEqual(body['data'][0]['roles'], [])
        self.assertEqual(body['data'][0]['props']['key'], 'value')
        self.assertIsNone(body['data'][0]['props']['anonymous'])
Esempio n. 6
0
def handler(event, context):
    headers = event.get('headers') or {}
    authorization = headers.get('Authorization')

    try:
        body = json.loads(event.get('body'))
    except:
        body = {}

    try:
        decoded = jwt_decode(authorization)
    except:
        decoded = {}

    props = body.get('props') or {}
    verbose = body.get('verbose')
    user_id = decoded.get('user_id')
    unique_props = {k: v for k, v in props.items() if k in UNIQUE_KEYS}
    custom_props = {k: v for k, v in props.items() if k not in unique_props}

    if db.table('user').where('id', user_id).first() is None:
        return abort(401, new_error('invalid token', 1))

    if 'password' in custom_props:
        custom_props['password'] = password(custom_props['password'])

    try:
        set_props(user_id, props=unique_props, unique=True)
        set_props(user_id, props=custom_props)
    except Exception as e:
        return abort(400, parse_sql_error(e))

    return {
        'body': json.dumps({
            'data': get(
                user_id,
                keys=props.keys(),
                verbose=verbose,
            ),
        }),
        'headers': {'Access-Control-Allow-Origin': '*'},
        'statusCode': 200,
    }
Esempio n. 7
0
def handler(event, context):
    user_id = new_id()

    while db.table('user').where({'id': user_id}).first():
        user_id = new_id()

    try:
        body = json.loads(event.get('body'))
    except:
        body = {}

    props = body.get('props') or {}
    verbose = body.get('verbose')
    unique_props = {k: v for k, v in props.items() if k in UNIQUE_KEYS}
    custom_props = {k: v for k, v in props.items() if k not in unique_props}

    if 'password' in custom_props:
        custom_props['password'] = password(custom_props['password'])

    try:
        set_props(user_id, props=unique_props, unique=True)
        set_props(user_id, props=custom_props)
        db.table('user').insert(id=user_id)
    except Exception as e:
        return abort(400, parse_sql_error(e))

    return {
        'body':
        dumps({
            'data':
            get(
                user_id,
                keys=props.keys(),
                verbose=verbose,
                is_me=True,
            )
        }),
        'headers': {
            'Access-Control-Allow-Origin': '*'
        },
        'statusCode':
        201,
    }
Esempio n. 8
0
    def test_me_by_email_and_password(self):
        user_id = new_id()
        db.table('user').insert(id=user_id)
        set_props(user_id, props={
            'key': 'value',
            'email': '*****@*****.**',
            'password': password('password'),
        }, unique=True)

        res = handler({
            'body': json.dumps({
                'email': '*****@*****.**',
                'password': '******',
                'props': ['key', 'anonymous'],
            }),
        }, None)

        body = json.loads(res['body'])

        self.assertEqual(res['statusCode'], 200)
        self.assertEqual(body['data']['id'], user_id)
        self.assertIsNotNone(body['data']['ssid'])
        self.assertEqual(body['data']['props']['key'], 'value')
        self.assertIsNone(body['data']['props']['anonymous'])

        res = handler({
            'queryStringParameters': {
                'email': '*****@*****.**',
                'password': '******',
                'props': 'key,anonymous',
            },
        }, None)

        body = json.loads(res['body'])

        self.assertEqual(res['statusCode'], 200)
        self.assertEqual(body['data']['id'], user_id)
        self.assertIsNotNone(body['data']['ssid'])
        self.assertEqual(body['data']['props']['key'], 'value')
        self.assertIsNone(body['data']['props']['anonymous'])
Esempio n. 9
0
    def test_get_props(self):
        set_props('id', props={'key': 'value'})
        prop = get_props('id', ['key', 'anonymous'])

        self.assertEqual(prop['key'], 'value')
        self.assertIsNone(prop['anonymous'])