async def test_not_list_finished_invitations(self, prepare_vfolder,
                                                 get_headers, folder_mount,
                                                 folder_host, user_keypair):
        app, client, create_vfolder = prepare_vfolder

        folder_info = await create_vfolder()

        async with app['dbpool'].acquire() as conn:
            query = (vfolder_invitations.insert().values({
                'id':
                uuid.uuid4().hex,
                'permission':
                'ro',
                'vfolder':
                folder_info['id'],
                'inviter':
                '*****@*****.**',
                'invitee':
                '*****@*****.**',
                'state':
                'rejected',
            }))
            await conn.execute(query)

        url = f'/v3/folders/invitations/list'
        req_bytes = json.dumps({}).encode()
        headers = get_headers('GET', url, req_bytes, keypair=user_keypair)
        ret = await client.get(url, data=req_bytes, headers=headers)
        rsp_json = await ret.json()

        assert len(rsp_json['invitations']) == 0
    async def test_accept_invitation(self, prepare_vfolder, get_headers,
                                     folder_mount, folder_host, user_keypair):
        app, client, create_vfolder = prepare_vfolder

        folder_info = await create_vfolder()

        inv_id = uuid.uuid4().hex
        async with app['dbpool'].acquire() as conn:
            query = (vfolder_invitations.insert().values({
                'id':
                inv_id,
                'permission':
                'ro',
                'vfolder':
                folder_info['id'],
                'inviter':
                '*****@*****.**',
                'invitee':
                '*****@*****.**',
                'state':
                'pending',
            }))
            await conn.execute(query)

        url = f'/v3/folders/invitations/accept'
        req_bytes = json.dumps({
            'inv_id': inv_id,
            'inv_ak': user_keypair['access_key']
        }).encode()
        headers = get_headers('POST', url, req_bytes, keypair=user_keypair)
        ret = await client.post(url, data=req_bytes, headers=headers)

        async with app['dbpool'].acquire() as conn:
            query = (sa.select('*').select_from(vfolder_permissions).where(
                vfolder_permissions.c.access_key == user_keypair['access_key'])
                     )
            result = await conn.execute(query)
            perm = await result.first()

            query = (sa.select('*').select_from(vfolder_invitations).where(
                vfolder_invitations.c.invitee == '*****@*****.**'))
            result = await conn.execute(query)
            invitations = await result.fetchall()

        assert ret.status == 201
        assert perm.permission == 'ro'
        assert len(invitations) == 1
        assert invitations[0]['state'] == 'accepted'
    async def test_cannot_accept_invitation_with_duplicated_vfolder_name(
            self, prepare_vfolder, get_headers, folder_mount, folder_host,
            user_keypair):
        app, client, create_vfolder = prepare_vfolder

        folder_info = await create_vfolder()

        inv_id = uuid.uuid4().hex
        async with app['dbpool'].acquire() as conn:
            query = (vfolder_invitations.insert().values({
                'id':
                inv_id,
                'permission':
                'ro',
                'vfolder':
                folder_info['id'],
                'inviter':
                '*****@*****.**',
                'invitee':
                '*****@*****.**',
                'state':
                'pending',
            }))
            await conn.execute(query)

            # Invitee already has vfolder with the same name as invitation.
            query = (vfolders.insert().values({
                'id':
                uuid.uuid4().hex,
                'name':
                folder_info['name'],
                'host':
                'azure-shard01',
                'last_used':
                None,
                'belongs_to':
                user_keypair['access_key'],
            }))
            await conn.execute(query)

        url = f'/v3/folders/invitations/accept'
        req_bytes = json.dumps({
            'inv_id': inv_id,
            'inv_ak': user_keypair['access_key']
        }).encode()
        headers = get_headers('POST', url, req_bytes, keypair=user_keypair)
        ret = await client.post(url, data=req_bytes, headers=headers)

        async with app['dbpool'].acquire() as conn:
            query = (sa.select('*').select_from(vfolder_permissions).where(
                vfolder_permissions.c.access_key == user_keypair['access_key'])
                     )
            result = await conn.execute(query)
            perm = await result.fetchall()

            query = (sa.select('*').select_from(vfolder_invitations).where(
                vfolder_invitations.c.invitee == '*****@*****.**'))
            result = await conn.execute(query)
            invitations = await result.fetchall()

        assert ret.status == 400
        assert len(perm) == 0
        assert len(invitations) == 1
        assert invitations[0]['state'] == 'pending'