Logging in to the client after forgetting your password
=======================================================

Before we can start we must set up our test environment by
creating an dummy survey.

>>> from euphorie.client.tests.utils import addSurvey
>>> from euphorie.client.tests.utils import addAccount
>>> from plone import api
>>> app = layer['portal']
>>> portal = layer['portal']
>>> from plone.testing.zope import Browser
>>> browser = Browser(app)
>>> browser.handleErrors=True
>>> xml_survey="""\
... <sector xmlns="http://xml.simplon.biz/euphorie/survey/1.0">
...    <title>Sector title</title>
...    <survey>
...      <title>Survey title</title>
...    </survey>
...  </sector>"""
>>> with api.env.adopt_user("admin"):
...     addSurvey(portal, xml_survey)
...     account = addAccount()
>>> from transaction import commit
>>> commit()


First login attempt
-------------------
We start our story when the user tries to look at the surveys for his country.
Note that we bypass the country selection stage to make sure our test stays in
an English user interface. When we try to look at the login we are prompted for
a login:

>>> browser.open(portal.client.nl.absolute_url())
>>> "@@login" in browser.url
True

Lets try to login:

>>> browser.getControl(name="__ac_name").value="jane@example.com"
>>> browser.getControl(name="__ac_password:utf8:ustring").value="john"
>>> browser.getControl(name="login").click()
>>> browser.contents
'...Your login name and/or password were entered incorrectly...'

Oops. Our login failed.


Request a password reset
------------------------

All is not lost! Lets try the password reset feature.

First of all we verify that we have no requests in the password reset tool
>>> from plone import api
>>> ppr = api.portal.get_tool('portal_password_reset')
>>> len(ppr._requests)
0

>>> def send(self, *args, **kw):
...     pass
>>> from zope.sendmail.mailer import SMTPMailer
>>> original_send=SMTPMailer.send
>>> SMTPMailer.send=send
>>> browser.getLink("reset your password").click()
>>> browser.getControl(name="form.widgets.email").value="jane@example.com"
>>> browser.getControl(name="form.buttons.save").click()

We now have a request and we use it to reset the password
>>> len(ppr._requests)
1
>>> for key in ppr._requests:
...     reset_url = '{}/@@passwordreset/{}'.format(
...         browser.url.partition('@@')[0],
...         key,
...     )
>>> browser.open(reset_url)

>>> NEW_PASSWORD = "Ølé123-Ølé123"
>>> browser.getControl(name="form.widgets.new_password").value = NEW_PASSWORD
>>> browser.getControl(name="form.widgets.new_password_confirmation").value = NEW_PASSWORD
>>> browser.getControl(label="Save changes").click()

Password does not comply
>>> browser.contents
'...The password needs to be at least 12 characters long and needs to contain at least one lower case letter, one upper case letter and one digit...'


>>> NEW_PASSWORD = "ØLé123-Ølé123"
>>> browser.getControl(name="form.widgets.new_password").value = NEW_PASSWORD
>>> browser.getControl(name="form.widgets.new_password_confirmation").value = NEW_PASSWORD
>>> browser.getControl(label="Save changes").click()

We are now back at the login form:

>>> browser.url
'http://nohost/plone/client/nl/@@login...'

Given the password has been reset,
we can use it to login again:

>>> browser.getControl(name="__ac_name").value="jane@example.com"
>>> browser.getControl(name="__ac_password:utf8:ustring").value = NEW_PASSWORD
>>> browser.getControl(name="login").click()

And we are now indeed at the session screen:

>>> browser.url
'http://nohost/plone/client/nl'
>>> browser.contents
'...Assessments...'
