Example #1
0
def test_all_stages():

    token = 'toooooo'

    def handler(method, url, *args, **kwargs):
        urlp = urlparse(url)
        query = dict(parse_qsl(urlp.query))

        if urlp.path.startswith('/authorize'):
            with open('tests/static/auth_page_normal.html', 'rb') as f:
                return Response(url, f.read())

        elif query.get('act', None) == 'login':
            with open('tests/static/secret_code_normal.html', 'rb') as f:
                return Response(url, f.read())

        elif query.get('act', None) == 'authcheck_code':
            with open('tests/static/security_check.html', 'rb') as f:
                return Response(url, f.read())

        elif query.get('act', None) == 'security_check':
            with open('tests/static/grant_access.html', 'rb') as f:
                return Response(url, f.read())

        elif query.get('act', None) == 'grant_access':
            with open('tests/static/grant_access.html', 'rb') as f:
                rurl = 'https://oauth.vk.com/blank.html#access_token=%s' \
                       '&expires_in=86400&user_id=101010' % token
                return Response(rurl, f.read())

        else:
            pytest.fail('Request is either not recognised '
                        'or not expected: %s' % url)

    session = Session(handler)

    with mock.patch('pyvk.auth.requests.Session', new=selector(session)):
        with mock.patch('pyvk.auth.requests.get', new=session.get):

            version = '5.0'

            auth = ClientAuth(disable_cache=True,
                              input=Fake,
                              username='******',
                              app_id=1234,
                              version=version)
            auth.auth()
            assert auth.token == token

            # Test .get_api()
            api = auth.api(lang='ru')
            # Config propagation
            assert api.config.version == version
            assert api.config.lang == 'ru'
            assert api.token == token
Example #2
0
from io import BytesIO
from pyvk import ClientAuth, p_basic, p_market, p_docs
from pyvk.helpers.uploaders import *
from tests.utils import EnvInput
import requests


auth = ClientAuth(input=EnvInput, scope=p_basic|p_market|p_docs,
                  disable_cache=True)
auth.auth()
api = auth.api()


def get_random_photo():
    photo_obj, = api.photos.get(album_id=237720036, count=1, rev=True)['items']
    photo_url = photo_obj['photo_604']
    return requests.get(photo_url).content


def test_video():
    up = VideoUploader(api, link='https://www.youtube.com/watch?v=dQw4w9WgXcQ',
                       wallpost=True)
    result = up.upload()
    assert 'response' in result


def test_album_photo():
    photo = get_random_photo()
    up = AlbumPhotoUploader(api, album_id=237720036)
    result = up.upload(BytesIO(photo), caption='shakal')
    assert 'jpg' in result[0]['photo_604']