def test_do_login_no_action_url(self): auth_api = AuthAPI() session = self.get_mocked_session(action='') with self.assertRaises(VkParseError) as err: auth_api.do_login(session=session) self.assertIn("Can't parse form action url", str(err))
def test_do_login_require_2fa(self): auth_api = AuthAPI() session = self.get_mocked_session(name='2fa_session') # Mock 'url' property on post to call get_2fa_code url = 'http://test/?act=authcheck&test=1' login_response = mock.MagicMock(name='login_response') form = '<form method="post" action="/login?act=authcheck_code' \ '&hash=1234567890_ff181e72e9db30cbc3"></form>' type(login_response).url = mock.PropertyMock(return_value=url) type(login_response).text = mock.PropertyMock(return_value=form) session.post = mock.Mock(return_value=login_response) with self.patch_api('get_2fa_code') as get_2fa_code: get_2fa_code.return_value = 1234 auth_api.do_login(session=session) self.assertTrue(get_2fa_code.called) call_2fa = dict(tuple(session.post.call_args_list[-1])[1]) # Check that 2fa http call was done with correct url and data self.assertEqual(call_2fa['url'], 'https://m.vk.com/login?act=authcheck_code&' 'hash=1234567890_ff181e72e9db30cbc3') self.assertEqual(call_2fa['data'], {'_ajax': '1', 'code': 1234, 'remember': '1'})
def test_do_login_require_2fa(self): auth_api = AuthAPI() session = self.get_mocked_session(name='2fa_session') # Mock 'url' property on post to call get_2fa_code url = 'http://test/?act=authcheck&test=1' login_response = mock.MagicMock(name='login_response') form = '<form method="post" action="/login?act=authcheck_code' \ '&hash=1234567890_ff181e72e9db30cbc3"></form>' type(login_response).url = mock.PropertyMock(return_value=url) type(login_response).text = mock.PropertyMock(return_value=form) session.post = mock.Mock(return_value=login_response) with self.patch_api('get_2fa_code') as get_2fa_code: get_2fa_code.return_value = 1234 auth_api.do_login(session=session) self.assertTrue(get_2fa_code.called) call_2fa = dict(tuple(session.post.call_args_list[-1])[1]) # Check that 2fa http call was done with correct url and data self.assertEqual( call_2fa['url'], 'https://m.vk.com/login?act=authcheck_code&' 'hash=1234567890_ff181e72e9db30cbc3') self.assertEqual(call_2fa['data'], { '_ajax': '1', 'code': 1234, 'remember': '1' })
def test_require_phone_number_with_auto_resolving_warn(self): """Test require_phone_number with auto resolving security check when vk returns warning message like: 'Incorrect numbers. You can repeat the attempt in 3 hours.' """ auth_api = AuthAPI(phone_number='+123456789') html = get_fixture('require_phone_num_warn_resp.html') session_mock = mock.Mock() with self.assertRaises(VkPageWarningsError) as err: auth_api.require_phone_number(html=html, session=session_mock) self.assertIn( 'Incorrect numbers. You can repeat the attempt in 3 hours', str(err))
def test_require_phone_number_with_auto_resolving(self): """Test require_phone_number with auto resolving security check. Expect that the method will parse given phone number and send confirmation request """ auth_api = AuthAPI(phone_number='+123456789') html = get_fixture('require_phone_num_resp.html') session_mock = mock.Mock() auth_api.require_phone_number(html=html, session=session_mock) self.assertEqual(session_mock.post.call_count, 1) call = tuple(session_mock.post.call_args_list[0])[1] self.assertEqual(call['data']['act'], 'security_check') self.assertEqual(call['data']['code'], '567')
def test_require_phone_number_warn(self): """Test require_phone_number with auto resolving security check when vk returns warning message like: 'Incorrect numbers. You can repeat the attempt in 3 hours.' """ auth_api = AuthAPI(phone_number='+123456789') html = get_fixture('require_phone_num_warn_resp.html') session_mock = mock.Mock() with self.assertRaises(VkPageWarningsError) as err: auth_api.require_phone_number(html=html, session=session_mock) self.assertIn( 'Incorrect numbers. You can repeat the attempt in 3 hours', str(err))
def test_require_phone_number(self): """Test require_phone_number with auto resolving security check. Expect that the method will parse given phone number and send confirmation request """ auth_api = AuthAPI(phone_number='+123456789') html = get_fixture('require_phone_num_resp.html') session_mock = mock.Mock() auth_api.require_phone_number(html=html, session=session_mock) self.assertEqual(session_mock.post.call_count, 1) call = tuple(session_mock.post.call_args_list[0])[1] self.assertEqual(call['data']['act'], 'security_check') self.assertEqual(call['data']['code'], '567')
def test_do_login_require_phone_number(self): auth_api = AuthAPI() session = self.get_mocked_session() # Mock 'url' property on post to call require_phone_number url = 'http://test/?act=security_check&test=1' post_response = mock.MagicMock() type(post_response).url = mock.PropertyMock(return_value=url) session.post = mock.Mock(return_value=post_response) with self.patch_api('require_phone_number') as require_pn: auth_api.do_login(session=session) self.assertTrue(require_pn.called) call_params = dict(tuple(require_pn.call_args_list[0])[1]) keys = ('html', 'session') for k in keys: self.assertIn(k, call_params)
def test_do_login_require_captcha(self): # Prepare mocked parameters auth_api = AuthAPI() session = self.get_mocked_session() # Mock 'url' property on post to call require_captcha url = 'http://test/?sid=test123&test=1' post_response = mock.MagicMock() type(post_response).url = mock.PropertyMock(return_value=url) session.post = mock.Mock(return_value=post_response) # Do login, expect require captcha method being called with mock.patch('vk_requests.auth.AuthAPI.require_auth_captcha') as \ require_captcha: auth_api.do_login(session=session) self.assertTrue(require_captcha.called) call_params = dict(tuple(require_captcha.call_args_list[0])[1]) keys = ('query_params', 'form_text', 'login_form_data', 'session') for k in keys: self.assertIn(k, call_params) self.assertEqual(call_params['query_params'], {'sid': 'test123', 'test': '1'})
def test_do_login_require_captcha(self): # Prepare mocked parameters auth_api = AuthAPI() session = self.get_mocked_session() # Mock 'url' property on post to call require_captcha url = 'http://test/?sid=test123&test=1' post_response = mock.MagicMock() type(post_response).url = mock.PropertyMock(return_value=url) session.post = mock.Mock(return_value=post_response) # Do login, expect require captcha method being called with mock.patch('vk_requests.auth.AuthAPI.require_auth_captcha') as \ require_captcha: auth_api.do_login(session=session) self.assertTrue(require_captcha.called) call_params = dict(tuple(require_captcha.call_args_list[0])[1]) keys = ('query_params', 'form_text', 'login_form_data', 'session') for k in keys: self.assertIn(k, call_params) self.assertEqual(call_params['query_params'], { 'sid': 'test123', 'test': '1' })
def test_init_with_login_param(self): with self.patch_api('renew_access_token') as renew_access_token: auth_api = AuthAPI(user_login='******') self.assertEqual(auth_api._login, 'test') self.assertTrue(renew_access_token.called)
def test_init(self): auth_api = AuthAPI() self.assertIsInstance(auth_api, AuthAPI)