Beispiel #1
0
def test_validation_failed():
    def handler(method, url, *args, **kwargs):
        urlp = urlparse(url)
        query = dict(parse_qsl(urlp.query))

        if urlp.path.startswith('/authorize'):
            with open('tests/static/blank.html', 'rb') as f:
                rurl = 'https://oauth.vk.com/blank.html#fail=1'
                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):

            auth = ClientAuth(disable_cache=True,
                              input=Fake,
                              username='******',
                              app_id=1234)

            with pytest.raises(pyvk.exceptions.AuthError):
                auth.auth()
Beispiel #2
0
def test_incorrect_info():
    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/auth_page_error.html', 'rb') as f:
                return Response(url, 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):

            auth = ClientAuth(disable_cache=True,
                              input=Fake,
                              username='******',
                              app_id=1234)

            with pytest.raises(pyvk.exceptions.AuthError):
                auth.auth()
Beispiel #3
0
 def run():
     auth = ClientAuth(input=Fake,
                       username='******',
                       app_id=1234,
                       scope=p_all)
     auth.auth(state='exit')
     assert auth.token is None
     assert fshelve.open.call_args[0][0] == cache_path
     assert auth.scope is None
Beispiel #4
0
    def run():
        auth = ClientAuth(disable_cache=True,
                          input=Fake,
                          username='******',
                          app_id=1234)

        auth.auth('grant_access', 'https://login.vk.com/?act=grant_access')
        assert auth.token == token
        assert not session.cookies
Beispiel #5
0
 def run():
     auth = ClientAuth(input=Fake,
                       username='******',
                       app_id=1234,
                       scope=p_basic)
     auth.auth()
     assert auth.token == token
     assert auth.scope == p_all
     assert auth.http.cookies == cache['cookies']
     assert fshelve.open.call_args[0][0] == cache_path
Beispiel #6
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
Beispiel #7
0
def test_auth_unexpected_json():
    def handler(method, url, *args, **kwargs):
        with open('tests/static/token_valid_all.json', 'rb') as f:
            return Response(url, f.read())

    session = Session(handler)
    with mock.patch('pyvk.auth.requests.Session', new=selector(session)):
        with mock.patch('pyvk.auth.requests.get', new=session.get):
            auth = ClientAuth(app_id=123, username='******', disable_cache=True)
            with pytest.raises(pyvk.exceptions.AuthError):
                auth.auth()
Beispiel #8
0
def test_auth_user_app_id_input():
    def handler(method, url, *args, **kwargs):
        pytest.fail('No requests expected here')

    session = Session(handler)

    with mock.patch('pyvk.auth.requests.Session', new=selector(session)):
        with mock.patch('pyvk.auth.requests.get', new=session.get):
            auth = ClientAuth(disable_cache=True, input=Fake)
            assert auth.username == Fake.ask('username')
            assert auth.app_id == Fake.ask('app_id')
Beispiel #9
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']
Beispiel #10
0
 def run():
     auth = ClientAuth(input=Fake,
                       username='******',
                       app_id=1234,
                       scope=p_basic)
     auth.auth()
Beispiel #11
0
 def run():
     auth = ClientAuth(input=Fake,
                       username='******',
                       app_id=1234,
                       scope=p_all)
     return (auth, auth._cache_path)
Beispiel #12
0
import pytest
from pyvk import ClientAuth, p_wall, p_friends
from pyvk.utils import zip
from pyvk.helpers import reqn
from tests.utils import EnvInput

auth = ClientAuth(input=EnvInput, scope=p_wall | p_friends, disable_cache=True)
auth.auth()
api = auth.api()


def fetch(method, args, n, batch_size):
    rb = reqn(method, n=n, batch_size=batch_size, **args)
    rn = method(count=n, **args)
    return rb, rn


def fetch_and_compare(method, args, n, batch_size):

    rb, rn = fetch(method, args, n, batch_size)

    if type(rb) is dict:
        assert set(rb.keys()) == set(rn.keys())

        for k in list(rb.keys()):
            if type(rb[k]) is list:
                try:
                    ids_b = [e['id'] for e in rb[k]]
                    ids_n = [e['id'] for e in rn[k]]
                except TypeError:
                    ids_b = rb[k]