diff --git a/setup.cfg b/setup.cfg index 4a71cad013ae5febaa627863eff0c2715a232827..7fb18a7966f636adbd0fe0433a1a5323ed8297c5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -13,7 +13,9 @@ tests = weboob.tools.capabilities.bank.transactions, weboob.tools.tokenizer, weboob.browser.browsers, weboob.browser.pages, - weboob.browser.filters.standard + weboob.browser.filters.standard, + weboob.browser.tests_class_URL, + weboob.browser_tests_class_Form [isort] known_first_party=weboob diff --git a/weboob/browser/tests_class_Form.py b/weboob/browser/tests_class_Form.py new file mode 100644 index 0000000000000000000000000000000000000000..abdcbe6b11b7d92f87dce428bee32679c321d436 --- /dev/null +++ b/weboob/browser/tests_class_Form.py @@ -0,0 +1,106 @@ +# -*- coding: utf-8 -*- +# Copyright(C) 2014 Julia Leven +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . +from unittest import TestCase +from weboob.browser import URL +from weboob.browser.pages import Form, FormSubmitWarning, HTMLPage +import collections +import lxml.html +import warnings + + +# Mock that allows to represent a Page +class MyMockPage(): + url = URL("http://httpbin.org") + + +# Class that tests different methods from the class URL +class FormTest(TestCase): + + # Initialization of the objects needed by the tests + def setUp(self): + self.page = MyMockPage() + self.el = lxml.html.fromstring( + """
+ + + + + +
""") + self.elMoreSubmit = lxml.html.fromstring( + """
+ + + + + + +
""") + + # Checks that the dictionary is correctly initialised + def test_init_nominal_case(self): + form = Form(self.page, self.el, None) + self.assertDictEqual(form, collections.OrderedDict([ + ('nom', 'Dupont'), ('prenom', ''), ('mySelect', 'item2'), + ('mySelectNotSelected', 'item1'), ('submitForm', u'')])) + + # Checks that submit fields are not added to the dictionary when the + # attribute submit_el is set to False + def test_no_submit(self): + formNoSubmit = Form(self.page, self.el, False) + self.assertDictEqual(formNoSubmit, collections.OrderedDict([ + ('nom', 'Dupont'), ('prenom', ''), ('mySelect', 'item2'), + ('mySelectNotSelected', 'item1')])) + + # Checks that the right warning is issued when there are several submit + # fields + def test_warning_more_submit(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always') + Form(self.page, self.elMoreSubmit) + warningMsg = "Form has more than one submit input, you" + \ + " should chose the correct one" + assert len(w) == 1 + assert issubclass(w[-1].category, FormSubmitWarning) + assert warningMsg in str(w[-1].message) + + # Checks that a warning is raised when the submit passed as a parameter + # does not exist in the form + def test_warning_submit_not_find(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always') + Form(self.page, self.el, lxml.html.fromstring( + "")) + warningMsg = "Form had a submit element provided, but" + \ + " it was not found" + assert len(w) == 1 + assert issubclass(w[-1].category, FormSubmitWarning) + assert warningMsg in str(w[-1].message) + diff --git a/weboob/browser/tests_class_URL.py b/weboob/browser/tests_class_URL.py new file mode 100644 index 0000000000000000000000000000000000000000..bde40bdcd327b8842a5063bd350615d41e8e6170 --- /dev/null +++ b/weboob/browser/tests_class_URL.py @@ -0,0 +1,143 @@ +# -*- coding: utf-8 -*- +# Copyright(C) 2014 Julia Leven +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . +from unittest import TestCase +from weboob.browser import URL, PagesBrowser +from weboob.browser.pages import Page +from weboob.browser.url import UrlNotResolvable + + +class MyMockBrowserWithoutBrowser(): + BASEURL = "http://weboob.org" + url = URL("http://test.org") + + +# Mock that allows to represent a Page +class myMockPage(Page): + pass + + +# Mock that allows to represent a Browser +class MyMockBrowser(PagesBrowser): + BASEURL = "http://weboob.org" + + # URL used by method match + urlNotRegex = URL("http://test.org", "http://test2.org") + urlRegex = URL("http://test.org", "http://weboob2.org") + urlRegWithoutHttp = URL("news") + urlNotRegWithoutHttp = URL("youtube") + + # URL used by method build + urlValue = URL("http://test.com/(?P\d+)") + urlParams = URL("http://test.com\?id=(?P\d+)&name=(?P.+)") + + # URL used by method is_here + urlIsHere = URL('http://weboob.org/(?P)', myMockPage) + urlIsHereDifKlass = URL('http://free.fr', myMockPage) + + +# Class that tests different methods from the class URL +class URLTest(TestCase): + + # Initialization of the objects needed by the tests + def setUp(self): + self.myBrowser = MyMockBrowser() + self.myBrowserWithoutBrowser = MyMockBrowserWithoutBrowser() + +# TESTS FOR MATCH METHOD + + # Check that an assert is sent if both base and browser are none + def test_match_base_none_browser_none(self): + self.assertRaises(AssertionError, + self.myBrowserWithoutBrowser.url.match, + "http://weboob.org") + + # Check that no assert is raised when browser is none and a base is indeed + # instanciated when given as a parameter + def test_match_base_not_none_browser_none(self): + try: + self.myBrowserWithoutBrowser.url.match("http://weboob.org/news", + "http://weboob.org") + except AssertionError: + self.fail("Method match returns an AssertionError while" + + " base parameter is not none!") + + # Check that none is returned when none of the defined urls is a regex for + # the given url + def test_match_url_pasregex_baseurl(self): + # Test + res = self.myBrowser.urlNotRegex.match("http://weboob.org/news") + # Assertions + self.assertIsNone(res) + + # Check that true is returned when one of the defined urls is a regex + # for the given url + def test_match_url_regex_baseurl(self): + # Test + res = self.myBrowser.urlRegex.match("http://weboob2.org/news") + # Assertions + self.assertTrue(res) + + # Successful test with relatives url + def test_match_url_without_http(self): + # Test + res = self.myBrowser.urlRegWithoutHttp.match("http://weboob.org/news") + # Assertions + self.assertTrue(res) + + # Unsuccessful test with relatives url + def test_match_url_without_http_fail(self): + # Test + browser = self.myBrowser + res = browser.urlNotRegWithoutHttp.match("http://weboob.org/news") + # Assertions + self.assertIsNone(res) + +# TESTS FOR BUILD METHOD + + # Checks that build returns the right url when it needs to add + # the value of a parameter + def test_build_nominal_case(self): + res = self.myBrowser.urlValue.build(id=2) + self.assertEquals(res, "http://test.com/2") + + # Checks that build returns the right url when it needs to add + # identifiers and values of some parameters + def test_build_urlParams_OK(self): + res = self.myBrowser.urlParams.build(id=2, name="weboob") + self.assertEquals(res, "http://test.com?id=2&name=weboob") + + # Checks that an exception is raised when a parameter is missing + # (here, the parameter name) + def test_build_urlParams_KO_missedparams(self): + self.assertRaises(UrlNotResolvable, self.myBrowser.urlParams.build, + id=2) + + # Checks that an exception is raised when there is an extra parameter + # added to the build function (here, the parameter title) + def test_build_urlParams_KO_moreparams(self): + self.assertRaises(UrlNotResolvable, self.myBrowser.urlParams.build, + id=2, name="weboob", title="test") + +# TESTS FOR IS_HERE METHOD + + # Check that an assert is sent if both klass is none + def test_ishere_klass_none(self): + self.assertRaisesRegexp(AssertionError, "You can use this method" + + " only if there is a Page class handler.", + self.myBrowser.urlRegex.is_here, id=2) +