示例#1
0
def test_logout_auto(set_mock, as_guest, user):
    testutil.guest_mode = False
    assert user.update_userinfo(user.urlopen('/').read()) == 'test'
    assert user.username == 'test'

    testutil.guest_mode = True
    assert user.update_userinfo(user.urlopen('/').read()) is None
    assert user.username is None
    assert user.rating is None
    assert user.skill is None
示例#2
0
def test_login_ok(set_mock, as_guest, user):
    set_mock({'/login/ajax-login': '******'})
    assert user.username is None
    assert user.login('test', '123456') is None
    testutil.guest_mode = False
    assert user.update_userinfo(user.urlopen('/').read()) == 'test'
    assert user.username == 'test'
示例#3
0
def test_login_fail(set_mock, as_guest, user):
    set_mock({'/login/ajax-login': '******'})
    assert user.username is None
    with pytest.raises(api.TabunResultError):
        user.login('test', '123456')
    assert user.username is None
    assert user.update_userinfo(user.urlopen('/').read()) is None
    assert user.username is None
示例#4
0
def test_send_request_with_interval(user):
    user.query_interval = 5

    import time

    now = [100]
    sleeps = []
    def sleep(n):
        sleeps.append(n)
        now[0] += n
        now[0] += 1  # имитация технических задержек и неточности time.time()
    def get_time():
        return now[0]

    old_sleep = time.sleep
    old_time = time.time
    time.sleep = sleep
    time.time = get_time

    try:
        user.urlopen('/')
        assert sleeps == []
        assert user.last_query_time == now[0]

        now[0] += 2  # 100 + 2 = 102
        user.urlopen('/comments/')  # 102 + (5 - 2) + 1 = 106
        assert sleeps == [3]
        assert user.last_query_time == now[0] - 1  # встроена компенсация технических задержек

        now[0] += 1  # 106 + 1 = 107
        user.urlopen('/', nowait=True)  # 107 + 0 = 107
        assert sleeps == [3]
        assert user.last_query_time == now[0]

        now[0] += 1  # 107 + 1 = 108
        user.urlopen('/stream/all/')  # 108 + (5 - 1) + 1 = 113 (предыдущий запрос сбросил last_query_time)
        assert sleeps == [3, 4]
        assert user.last_query_time == now[0] - 1

        now[0] += 100  # 113 + 100 = 213
        user.urlopen('/')  # 213 + 0 = 213
        assert sleeps == [3, 4]
        assert user.last_query_time == now[0]
    finally:
        time.sleep = old_sleep
        time.time = old_time
示例#5
0
def test_user_partially_preloaded_cookies(session_id, security_ls_key, key):
    user = UserTest(phpsessid=session_id, security_ls_key=security_ls_key, key=key)
    assert user.username == None if security_ls_key else 'test'
    assert user.phpsessid == session_id
    assert user.security_ls_key == '0123456789abcdef0123456789abcdef'
    assert user.key in (None, '00000000000000000000000000000000')

    assert user.update_userinfo(user.urlopen('/').read()) == 'test'
    assert user.phpsessid == session_id
    assert user.security_ls_key == '0123456789abcdef0123456789abcdef'
    assert user.key in (None, '00000000000000000000000000000000')
示例#6
0
def test_cloudflare_solution_ok_again(set_mock, as_guest, intercept):
    # Отличие от предыдущего теста в том, что печеньки уже стоят заранее
    cfdata = _get_cf_data()

    set_mock({
        '/': cfdata['cf_mock'],
        '/login/': cfdata['cf_mock'],
        cfdata['answer_url']: cfdata['cf_mock_solved'],
    })

    calls = {'page': 0, 'solve': 0}

    @intercept('/')
    @intercept('/login/')
    def get_page(data, headers):
        calls['page'] += 1

    @intercept(cfdata['answer_url'])
    def cf_solve(data, headers):
        calls['solve'] += 1
        set_mock({
            '/': 'index.html',
            '/login/': 'login.html',
        })

    user = UserTest(security_ls_key='N/A', session_id='abcdef9876543210abcdef9876543210', avoid_cf=True)
    user.extra_cookies.update({
        '__cfduid': cfdata['cookvalue1'],
        'cf_clearance': 'foobarblablablathisisoldcookie',
    })

    resp = user.urlopen('/')
    assert resp.code == 200
    user.update_userinfo(resp.read())

    assert calls == {'page': 2, 'solve': 1}
    assert user.sleeps == [1, 4.0]

    assert user.extra_cookies == {
        '__cfduid': cfdata['cookvalue1'],
        'cf_clearance': cfdata['cookvalue2'],
    }
    assert user.session_id == 'abcdef9876543210abcdef9876543210'
    assert user.security_ls_key == '0123456789abcdef0123456789abcdef'
示例#7
0
def test_send_request_without_interval(user):
    import time

    now = [100]
    sleeps = []
    def sleep(n):
        sleeps.append(n)
        now[0] += n
        now[0] += 1  # всякие техничские задержки
    def get_time():
        return now[0]

    old_sleep = time.sleep
    old_time = time.time
    time.sleep = sleep
    time.time = get_time

    try:
        user.urlopen('/')
        assert sleeps == []
        assert user.last_query_time == now[0]

        now[0] += 2
        user.urlopen('/comments/', nowait=True)
        assert sleeps == []
        assert user.last_query_time == now[0]

        now[0] += 2
        user.urlopen('/stream/all/')
        assert sleeps == []
        assert user.last_query_time == now[0]

        now[0] += 100
        user.urlopen('/')
        assert sleeps == []
        assert user.last_query_time == now[0]
    finally:
        time.sleep = old_sleep
        time.time = old_time
示例#8
0
def test_login_init_ok(set_mock, as_guest):
    set_mock({'/login/ajax-login': '******'})
    user = UserTest('test', '123456')
    testutil.guest_mode = False
    assert user.update_userinfo(user.urlopen('/').read()) == 'test'
    assert user.username == 'test'