Пример #1
0
    def test_delete(self):
        user_id = 'user'
        work_id = 'work'

        db.table('work').insert({'id': work_id})
        db.table('workRole').insert({
            'userId': user_id,
            'workId': work_id,
            'type': 'author',
        })

        work = db.table('work').where('id', work_id).first()

        self.assertIsNone(work.get('deletedAt'))

        handler(
            {
                'headers': {
                    'Authorization': jwt_encode(user_id),
                },
                'pathParameters': {
                    'id': work_id,
                },
            }, None)

        work = db.table('work').where('id', work_id).first()

        self.assertIsNotNone(work.get('deletedAt'))
Пример #2
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'])
Пример #3
0
    def test_put_nonexistant(self):
        res = handler(
            {
                'headers': {
                    'Authorization': jwt_encode(new_id()),
                },
                'body':
                json.dumps({
                    'props': {
                        'username': '******',
                        'password': '******',
                    },
                }),
            }, None)

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

        self.assertIsNotNone(body['error'])
Пример #4
0
def get(user_id, keys=[], active=True, verbose=False, is_me=False):
    props = get_props(
        user_id,
        keys=keys,
        active=active,
        verbose=verbose,
    )

    user = {
        'id': user_id,
        'props': props,
    }

    if is_me:
        user['ssid'] = jwt_encode(user_id)

        roles = db.table('userRole').where('userId', user_id).get()
        user['roles'] = list(map(lambda r: r.get('type'), roles))

    return user
Пример #5
0
def set_connection(provider, provider_id, access_token, referer, props={}):
    connection = (
        db.table('userConnection')
        .where({
            'provider': provider,
            'providerId': provider_id,
        }).first()
    )

    if connection:
        created = False
        user_id = connection.get('userId')
        (
            db.table('userConnection')
            .where('providerId', provider_id)
            .update({'token': access_token})
        )
    else:
        created = True
        user_id = new_id()

        conn_id = db.table('userConnection').insert_get_id({
            'userId': user_id,
            'provider': provider,
            'providerId': provider_id,
            'token': access_token,
        })

        db.table('user').insert(id=user_id)
        set_props(user_id, props=props)

        connection = db.table('userConnection').where('id', conn_id).first()

    location = assign_query_params(referer, {
        'ssid': jwt_encode(connection.get('userId')),
        'created': int(created),
    })

    return connection, location
Пример #6
0
    def test_put(self):
        user_id = new_id()
        db.table('user').insert(id=user_id)

        res = handler(
            {
                'headers': {
                    'Authorization': jwt_encode(user_id),
                },
                'body':
                json.dumps({
                    'props': {
                        'username': '******',
                        'password': '******',
                    },
                }),
            }, None)

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

        self.assertIsNotNone(body['data']['id'])
        self.assertEqual(body['data']['props']['username'], 'riverleo')
        self.assertFalse('password' in body['data']['props'])