Introduction ============ This is a full-blown functional test. The emphasis here is on testing what the user may input and see, and the system is largely tested as a black box. We use PloneTestCase to set up this test as well, so we have a full Plone site to play with. We *can* inspect the state of the portal, e.g. using self.portal and self.folder, but it is often frowned upon since you are not treating the system as a black box. Also, if you, for example, log in or set roles using calls like self.setRoles(), these are not reflected in the test browser, which runs as a separate session. Being a doctest, we can tell a story here. First, we must perform some setup. We use the testbrowser that is shipped with Five, as this provides proper Zope 2 integration. Most of the documentation, though, is in the underlying zope.testbrower package. >>> from Products.Five.testbrowser import Browser >>> browser = Browser() >>> portal_url = self.portal.absolute_url() The following is useful when writing and debugging testbrowser tests. It lets us see all error messages in the error_log. >>> self.portal.error_log._ignored_exceptions = () With that in place, we can go to the portal front page and log in. We will do this using the default user from PloneTestCase: >>> from Products.PloneTestCase.setup import portal_owner, default_password >>> browser.open(portal_url) We have the login portlet, so let's use that. >>> browser.open('http://nohost/plone/login_form') >>> browser.getControl('Login Name').value = portal_owner >>> browser.getControl('Password').value = default_password >>> browser.getControl('Log in').click() >>> "You are now logged in" in browser.contents True >>> "Login failed" in browser.contents False >>> browser.url 'http://nohost/plone/login_form' -*- extra stuff goes here -*- Test =============================== Enable commenting. >>> from zope.component import queryUtility >>> from plone.registry.interfaces import IRegistry >>> from plone.app.discussion.interfaces import IDiscussionSettings >>> registry = queryUtility(IRegistry) >>> settings = registry.forInterface(IDiscussionSettings, check=False) >>> settings.globally_enabled = True >>> settings.anonymous_comments = True Enable quintagroup.plonecaptchas >>> from quintagroup.plonecaptchas.config import CAPTCHA_NAME >>> settings.captcha = CAPTCHA_NAME >>> import transaction >>> transaction.commit() Create a public page with comments allowed. >>> browser.open(portal_url) >>> browser.getLink(id='document').click() >>> browser.getControl(name='title').value = "index_html" >>> browser.getControl(name='allowDiscussion:boolean').value = True >>> browser.getControl(name='form.button.save').click() >>> browser.open('http://nohost/plone/index_html/content_status_modify?workflow_action=publish') >>> page_url = browser.url Check that the form has been properly submitted >>> browser.url 'http://nohost/plone/index_html' Test captcha image in comment form >>> import re >>> from quintagroup.captcha.core.tests.testWidget import IMAGE_PATT >>> patt = re.compile(IMAGE_PATT % self.portal.absolute_url()) >>> browser.open(portal_url + '/logout') >>> browser.open(page_url) >>> match_obj = patt.search(browser.contents) >>> img_url = portal_url + match_obj.group(1) >>> browser.open(img_url) >>> browser.headers.getheader("content-type") 'image/jpeg' Test submit right captcha >>> from quintagroup.captcha.core.utils import getWord, decrypt, parseKey >>> self.captcha_key = self.portal.captcha_key >>> browser.open(page_url) >>> captcha_path = patt.search(browser.contents).group(1) >>> captcha_hashkey = captcha_path.lstrip('/getCaptchaImage/') >>> key = getWord(int(parseKey(decrypt(self.captcha_key, captcha_hashkey))['key']) - 1) >>> browser.getControl(name='form.widgets.text').value = 'comment' >>> browser.getControl(name='form.widgets.captcha').value = key >>> browser.getControl(name='form.buttons.comment').click() >>> "Please re-enter validation code" in browser.contents False Test submit wrong captcha >>> browser.open(page_url) >>> browser.getControl(name='form.widgets.text').value = 'comment' >>> browser.getControl(name='form.widgets.captcha').value = 'wrong captcha' >>> browser.getControl(name='form.buttons.comment').click() >>> "Please re-enter validation code" in browser.contents True