From 782aab3048887dde99e652782df63e752b21046c Mon Sep 17 00:00:00 2001 From: Vangelis Banos Date: Mon, 13 Apr 2020 19:16:10 +0000 Subject: [PATCH] Add unit tests for try_login behavior Add unit tests for the code that detects and tries to use login forms automatically (`Browser.try_login`). Add `htdocs/favicon.ico` because it is loaded automatically when the browser tries to use the test web server and it causes a "missing" warning. Create a new dir `tests/htdocs/site11` which is used for login related test html files. --- tests/htdocs/favicon.ico | Bin 0 -> 4286 bytes tests/htdocs/site11/form-no-login.html | 12 ++++++ tests/htdocs/site11/form1.html | 12 ++++++ tests/test_brozzling.py | 50 +++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 tests/htdocs/favicon.ico create mode 100644 tests/htdocs/site11/form-no-login.html create mode 100644 tests/htdocs/site11/form1.html diff --git a/tests/htdocs/favicon.ico b/tests/htdocs/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..363e30d6567d62ef9179fcb8f103a19664812ae9 GIT binary patch literal 4286 zcmc(h%S%^V6vuy+_V6NHC9aPYa;9c!&U_5+v`j>FRzd`6rcHtd4SbZP1B0Lt!MSk3 zfl~M=A4rl`P8+0US~=_=2&&cZv%25I?p)2dT)F$l`mKG|UVDAlYoD_di9N~R(W43e z6My}lNc@pVBnp$16ea#l5^^8OwankX(|18~ZOmR$Q!3H#U-4s&oIWa23) zDfTl;O-(iEXF9CKb+j+^Lzs${XU%t$%Ln1}ynp|G$A0kOfxUnK-qzRGZDV7@N%ME_ z-q}vXo~?A&*4FIp+qYI+T(@O-)jF&{^&k)M!oos3e*CzdJbBVNH9L0fnCGsvwA9MV%Dj%`k~Q?1 zBS){OsK|*mWb8qnk&)pT#H$=A=aS(AXJuq$#6EufXxFY?^F7F`s;cb6hY#MLm6er| zoIFbZ_U+q|yt=yDHa9nYPenzA%ds7=w|r0z3JMCmhpEHV)RY}Mbjba^a^*@?8s>^2 z)SsW9x3sjhE%m`UaNvNi2@|fl@bmKW?A5DRjs-q@@cH7!i}vNq7nirRv_x^{Cz?p{Q0we{rc6Ko10y>UG3Ei;2b$}4whfBxL+>gwEY>Zx~a z=nr$gox3%zUcKu0>H%sR;uAmLPVPw^9UV?xU0v4Q-R*Sa#trAlnCm&d&NFN1VF!Di zot;)*Uheq94Y8FEa&!0YUAuq(zVrV6e(US&vs<@rSx--o-Mo3zZNozn9+>F%_V)U| zJ9qBby?gg;U|_&boH!BThdsg^U+7Kf)TvY6d)R#P+h0ePClW)|ezP{duhljo2_}*~d8E~HIxuKyU@3X^)4@c)Yw57Q))&JtGD-ZZ5 z{W#~H%lnP`z!h6uk7sOS6AmH6c8Do|q^~tNy3)svzDIE^_e>2`N5sgrT{f!9W&b|Bahu|KIoqt>?SS literal 0 HcmV?d00001 diff --git a/tests/htdocs/site11/form-no-login.html b/tests/htdocs/site11/form-no-login.html new file mode 100644 index 0000000..3ab17bf --- /dev/null +++ b/tests/htdocs/site11/form-no-login.html @@ -0,0 +1,12 @@ + + + Form without proper username/password fields + + +
+ + + +
+ + diff --git a/tests/htdocs/site11/form1.html b/tests/htdocs/site11/form1.html new file mode 100644 index 0000000..0072efc --- /dev/null +++ b/tests/htdocs/site11/form1.html @@ -0,0 +1,12 @@ + + + Detect login form and use it + + +
+ + + +
+ + diff --git a/tests/test_brozzling.py b/tests/test_brozzling.py index 9738eb4..0e83089 100755 --- a/tests/test_brozzling.py +++ b/tests/test_brozzling.py @@ -72,6 +72,16 @@ def httpd(request): else: super().do_GET() + def do_POST(self): + if self.path == '/login-action': + self.send_response(200) + payload = b'login successfull\n' + self.send_header('Content-Type', 'text/plain;charset=utf-8') + self.send_header('Content-Length', len(payload)) + self.end_headers() + self.wfile.write(payload) + + # SimpleHTTPRequestHandler always uses CWD so we have to chdir os.chdir(os.path.join(os.path.dirname(__file__), 'htdocs')) @@ -247,3 +257,43 @@ def test_proxy_down(): with pytest.raises(brozzler.ProxyError): worker.brozzle_page(browser, site, page) +def test_try_login(httpd): + """Test try_login behavior. + """ + response_urls = [] + def on_response(msg): + response_urls.append(msg['params']['response']['url']) + chrome_exe = brozzler.suggest_default_chrome_exe() + form_url = 'http://localhost:%s/site11/form1.html' % httpd.server_port + favicon_url = 'http://localhost:%s/favicon.ico' % httpd.server_port + login_url = 'http://localhost:%s/login-action' % httpd.server_port + # When username and password are defined and initial page has login form, + # detect login form, submit login, and then return to the initial page. + username = 'user1' + password = 'pass1' + with brozzler.Browser(chrome_exe=chrome_exe) as browser: + browser.browse_page(form_url, username=username, password=password, + on_response=on_response) + assert len(response_urls) == 4 + assert response_urls[0] == form_url + assert response_urls[1] == favicon_url + assert response_urls[2] == login_url + assert response_urls[3] == form_url + + # When username and password are not defined, just load the initial page. + response_urls = [] + with brozzler.Browser(chrome_exe=chrome_exe) as browser: + browser.browse_page(form_url, on_response=on_response) + assert len(response_urls) == 2 + assert response_urls[0] == form_url + assert response_urls[1] == favicon_url + + # when the page doesn't have a form with username/password, don't submit it + response_urls = [] + form_without_login_url = 'http://localhost:%s/site11/form-no-login.html' % httpd.server_port + with brozzler.Browser(chrome_exe=chrome_exe) as browser: + browser.browse_page(form_without_login_url, username=username, + password=password, on_response=on_response) + assert len(response_urls) == 2 + assert response_urls[0] == form_without_login_url + assert response_urls[1] == favicon_url