async def test_async_try_login_basic_digest_ntlm_wrong_credentials(): target_url = "http://perdu.com/" respx.get(target_url).mock(return_value=httpx.Response( 200, text= "<html><head><title>Vous Etes Perdu ?</title></head><body><h1>Perdu sur l'Internet ?</h1> \ <h2>Pas de panique, on va vous aider</h2> \ <strong><pre> * <----- vous êtes ici</pre></strong><a href='http://perdu.com/foobar/'></a> \ <a href='http://perdu.com/foobar/signout'></a> \ <div><a href='http://perdu.com/a/b/signout'></a></div></body></html>" )) auth_urls = [["http://perdu.com/login1", 401], ["http://perdu.com/login2", 403], ["http://perdu.com/login3", 404]] crawler = AsyncCrawler(Request(target_url), timeout=1) crawler._auth_credentials = ["username", "password"] for auth_url, status_code in auth_urls: respx.get(auth_url).mock( return_value=httpx.Response(status_code, text="KO")) is_logged_in, form, disconnect_urls = await crawler._async_try_login_basic_digest_ntlm( auth_url) assert is_logged_in is False assert len(form) == 0 assert len(disconnect_urls) == 0
async def test_async_try_login_post_form_not_detected(): target_url = "http://perdu.com/" respx.get(target_url).mock(return_value=httpx.Response( 200, text= "<html><head><title>Vous Etes Perdu ?</title></head><body><h1>Perdu sur l'Internet ?</h1> \ <h2>Pas de panique, on va vous aider</h2> \ <strong><pre> * <----- vous êtes ici</pre></strong><a href='http://perdu.com/foobar/'></a> \ <a href='http://perdu.com/foobar/signout'></a> \ <div><a href='http://perdu.com/a/b/signout'></a></div></body></html>" )) crawler = AsyncCrawler(Request(target_url), timeout=1) crawler._auth_credentials = ["username", "password"] is_logged_in, form, disconnect_urls = await crawler._async_try_login_post( "username", "password", target_url) assert form == {} assert len(disconnect_urls) == 0 assert is_logged_in is False
async def test_async_try_login_post_good_credentials(): target_url = "http://perdu.com/" respx.get(target_url).mock(return_value=httpx.Response( 200, text= "<html><head><title>Vous Etes Perdu ?</title></head><body><h1>Perdu sur l'Internet ?</h1> \ <h2>Pas de panique, on va vous aider</h2> \ <strong><pre> * <----- vous êtes ici</pre></strong><a href='http://perdu.com/foobar/'></a> \ <a href='http://perdu.com/foobar/signout'></a> \ <form name='loginform' method='post' action='userinfo.php'> \ <table cellpadding='4' cellspacing='1'> \ <tbody><tr><td>Username : </td><td><input name='uname' type='text' size='20'></td></tr> \ <tr><td>Password : </td><td><input name='pass' type='password' size='20'></td></tr> \ <tr><td colspan='2' align='right'><input type='submit' value='login' style='width:75px;'></td></tr> \ </tbody></table> \ </form> \ <div><a href='http://perdu.com/a/b/signout'></a></div></body></html>" )) respx.post(target_url + 'userinfo.php').mock(return_value=httpx.Response( 200, text= "<html><head><title>Vous Etes Perdu ?</title></head><body><h1>Perdu sur l'Internet ?</h1> \ <h2>Pas de panique, on va vous aider</h2> \ <strong><pre> * <----- vous êtes ici</pre></strong><a href='http://perdu.com/foobar/'></a> \ <a href='http://perdu.com/foobar/signout'>disconnect</a> \ <div><a href='http://perdu.com/a/b/signout'></a></div></body></html>" )) crawler = AsyncCrawler(Request(target_url), timeout=1) crawler._auth_credentials = ["username", "password"] is_logged_in, form, disconnect_urls = await crawler._async_try_login_post( "username", "password", target_url) assert form == {'login_field': 'uname', 'password_field': 'pass'} assert len(disconnect_urls) == 2 assert "http://perdu.com/foobar/signout" in disconnect_urls assert "http://perdu.com/a/b/signout" in disconnect_urls assert is_logged_in is True