[3270] | 1 | Introduction |
---|
| 2 | ============ |
---|
| 3 | |
---|
| 4 | This is a full-blown functional test. The emphasis here is on testing what |
---|
| 5 | the user may input and see, and the system is largely tested as a black box. |
---|
| 6 | We use PloneTestCase to set up this test as well, so we have a full Plone site |
---|
| 7 | to play with. We *can* inspect the state of the portal, e.g. using |
---|
| 8 | self.portal and self.folder, but it is often frowned upon since you are not |
---|
| 9 | treating the system as a black box. Also, if you, for example, log in or set |
---|
| 10 | roles using calls like self.setRoles(), these are not reflected in the test |
---|
| 11 | browser, which runs as a separate session. |
---|
| 12 | |
---|
| 13 | Being a doctest, we can tell a story here. |
---|
| 14 | |
---|
| 15 | First, we must perform some setup. We use the testbrowser that is shipped |
---|
| 16 | with Five, as this provides proper Zope 2 integration. Most of the |
---|
| 17 | documentation, though, is in the underlying zope.testbrower package. |
---|
| 18 | |
---|
| 19 | >>> from Products.Five.testbrowser import Browser |
---|
| 20 | >>> browser = Browser() |
---|
| 21 | >>> portal_url = self.portal.absolute_url() |
---|
| 22 | |
---|
| 23 | The following is useful when writing and debugging testbrowser tests. It lets |
---|
| 24 | us see all error messages in the error_log. |
---|
| 25 | |
---|
| 26 | >>> self.portal.error_log._ignored_exceptions = () |
---|
| 27 | |
---|
| 28 | With that in place, we can go to the portal front page and log in. We will |
---|
| 29 | do this using the default user from PloneTestCase: |
---|
| 30 | |
---|
| 31 | >>> from Products.PloneTestCase.setup import portal_owner, default_password |
---|
| 32 | >>> browser.open(portal_url) |
---|
| 33 | |
---|
| 34 | We have the login portlet, so let's use that. |
---|
| 35 | |
---|
| 36 | >>> browser.open('http://nohost/plone/login_form') |
---|
| 37 | >>> browser.getControl('Login Name').value = portal_owner |
---|
| 38 | >>> browser.getControl('Password').value = default_password |
---|
| 39 | >>> browser.getControl('Log in').click() |
---|
| 40 | >>> "You are now logged in" in browser.contents |
---|
| 41 | True |
---|
| 42 | >>> "Login failed" in browser.contents |
---|
| 43 | False |
---|
| 44 | >>> browser.url |
---|
| 45 | 'http://nohost/plone/login_form' |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | -*- extra stuff goes here -*- |
---|
| 50 | Test |
---|
| 51 | =============================== |
---|
| 52 | |
---|
| 53 | Enable commenting. |
---|
| 54 | |
---|
| 55 | >>> from zope.component import queryUtility |
---|
| 56 | >>> from plone.registry.interfaces import IRegistry |
---|
| 57 | >>> from plone.app.discussion.interfaces import IDiscussionSettings |
---|
| 58 | >>> registry = queryUtility(IRegistry) |
---|
| 59 | >>> settings = registry.forInterface(IDiscussionSettings, check=False) |
---|
| 60 | >>> settings.globally_enabled = True |
---|
| 61 | >>> settings.anonymous_comments = True |
---|
| 62 | |
---|
| 63 | Enable quintagroup.plonecaptchas |
---|
| 64 | |
---|
| 65 | >>> from quintagroup.plonecaptchas.config import CAPTCHA_NAME |
---|
| 66 | >>> settings.captcha = CAPTCHA_NAME |
---|
| 67 | |
---|
| 68 | >>> import transaction |
---|
| 69 | >>> transaction.commit() |
---|
| 70 | |
---|
| 71 | Create a public page with comments allowed. |
---|
| 72 | |
---|
| 73 | >>> browser.open(portal_url) |
---|
| 74 | >>> browser.getLink(id='document').click() |
---|
| 75 | >>> browser.getControl(name='title').value = "index_html" |
---|
| 76 | >>> browser.getControl(name='allowDiscussion:boolean').value = True |
---|
| 77 | >>> browser.getControl(name='form.button.save').click() |
---|
| 78 | >>> browser.open('http://nohost/plone/index_html/content_status_modify?workflow_action=publish') |
---|
| 79 | >>> page_url = browser.url |
---|
| 80 | |
---|
| 81 | Check that the form has been properly submitted |
---|
| 82 | |
---|
| 83 | >>> browser.url |
---|
| 84 | 'http://nohost/plone/index_html' |
---|
| 85 | |
---|
| 86 | Test captcha image in comment form |
---|
| 87 | |
---|
| 88 | >>> import re |
---|
| 89 | >>> from quintagroup.captcha.core.tests.testWidget import IMAGE_PATT |
---|
| 90 | >>> patt = re.compile(IMAGE_PATT % self.portal.absolute_url()) |
---|
| 91 | |
---|
| 92 | >>> browser.open(portal_url + '/logout') |
---|
| 93 | >>> browser.open(page_url) |
---|
| 94 | >>> match_obj = patt.search(browser.contents) |
---|
| 95 | >>> img_url = portal_url + match_obj.group(1) |
---|
| 96 | >>> browser.open(img_url) |
---|
| 97 | >>> browser.headers.getheader("content-type") |
---|
| 98 | 'image/jpeg' |
---|
| 99 | |
---|
| 100 | Test submit right captcha |
---|
| 101 | |
---|
| 102 | >>> from quintagroup.captcha.core.utils import getWord, decrypt, parseKey |
---|
| 103 | >>> self.captcha_key = self.portal.captcha_key |
---|
| 104 | >>> browser.open(page_url) |
---|
| 105 | >>> captcha_path = patt.search(browser.contents).group(1) |
---|
| 106 | >>> captcha_hashkey = captcha_path.lstrip('/getCaptchaImage/') |
---|
| 107 | >>> key = getWord(int(parseKey(decrypt(self.captcha_key, captcha_hashkey))['key']) - 1) |
---|
| 108 | |
---|
| 109 | >>> browser.getControl(name='form.widgets.text').value = 'comment' |
---|
| 110 | >>> browser.getControl(name='form.widgets.captcha').value = key |
---|
| 111 | >>> browser.getControl(name='form.buttons.comment').click() |
---|
| 112 | >>> "Please re-enter validation code" in browser.contents |
---|
| 113 | False |
---|
| 114 | |
---|
| 115 | Test submit wrong captcha |
---|
| 116 | |
---|
| 117 | >>> browser.open(page_url) |
---|
| 118 | >>> browser.getControl(name='form.widgets.text').value = 'comment' |
---|
| 119 | >>> browser.getControl(name='form.widgets.captcha').value = 'wrong captcha' |
---|
| 120 | >>> browser.getControl(name='form.buttons.comment').click() |
---|
| 121 | >>> "Please re-enter validation code" in browser.contents |
---|
| 122 | True |
---|