[4085] | 1 | WAeUP portal authentication |
---|
| 2 | *************************** |
---|
| 3 | |
---|
| 4 | :Test-Layer: functional |
---|
| 5 | |
---|
| 6 | We need to protect most pieces of our portals from unauthenticated |
---|
| 7 | access. |
---|
| 8 | |
---|
| 9 | Therefore users have to login to access main functionality and they |
---|
| 10 | are able to log out afterwards. |
---|
| 11 | |
---|
| 12 | Before we can check access we have to create an app: |
---|
| 13 | |
---|
[4921] | 14 | >>> from waeup.sirp.app import University |
---|
[4085] | 15 | >>> root = getRootFolder() |
---|
| 16 | >>> u = University() |
---|
| 17 | >>> root['app'] = u |
---|
| 18 | |
---|
| 19 | To make sure, we can 'watch' pages, we first have to initialize our |
---|
| 20 | test browser: |
---|
| 21 | |
---|
| 22 | >>> from zope.testbrowser.testing import Browser |
---|
| 23 | >>> browser = Browser() |
---|
| 24 | >>> browser.handleErrors = False |
---|
| 25 | |
---|
[4092] | 26 | Creating users (principals) |
---|
| 27 | =========================== |
---|
[4085] | 28 | |
---|
[4092] | 29 | Before we can login, we have to provide a user (``principal`` in Zope |
---|
[4093] | 30 | terms) with a password (and optional a title or description): |
---|
[4092] | 31 | |
---|
[4744] | 32 | >>> root['app']['users'].addUser('bob', 'bobsecret', |
---|
[4093] | 33 | ... title='Bob', description='A sample user') |
---|
[4092] | 34 | |
---|
| 35 | We can also add complete `Account` objects. An `Account` stores the |
---|
| 36 | user credentials and some metadata persistently: |
---|
| 37 | |
---|
[4921] | 38 | >>> from waeup.sirp.authentication import Account |
---|
[4092] | 39 | >>> alice = Account('alice', 'alicesecret') |
---|
[4744] | 40 | >>> root['app']['users'].addAccount(alice) |
---|
[4092] | 41 | |
---|
[4093] | 42 | See ``users.txt`` for details about the UserContainer we use here. |
---|
[4092] | 43 | |
---|
[4093] | 44 | |
---|
[4092] | 45 | Logging in via side bar |
---|
| 46 | ======================= |
---|
| 47 | |
---|
[4085] | 48 | We can access the front page without restrictions: |
---|
| 49 | |
---|
| 50 | >>> browser.open('http://localhost/app') |
---|
| 51 | >>> print browser.headers['Status'] |
---|
| 52 | 200 Ok |
---|
| 53 | |
---|
[5404] | 54 | We have to go to one of the login pages first: |
---|
[4086] | 55 | |
---|
[5404] | 56 | >>> browser.open('http://localhost/app/@@loginstaff') |
---|
| 57 | >>> print browser.headers['Status'] |
---|
| 58 | 200 Ok |
---|
| 59 | |
---|
| 60 | There is a login form on tis page: |
---|
| 61 | |
---|
[4086] | 62 | >>> 'form.login' in browser.contents |
---|
| 63 | True |
---|
| 64 | |
---|
[4093] | 65 | >>> 'form.logout' in browser.contents |
---|
| 66 | False |
---|
| 67 | |
---|
[4086] | 68 | We use this form: |
---|
| 69 | |
---|
[4093] | 70 | >>> browser.getControl(name='form.login').value = 'bob' |
---|
| 71 | >>> browser.getControl(name='form.password').value = 'bobsecret' |
---|
[4086] | 72 | >>> browser.getControl('Login').click() |
---|
[4092] | 73 | |
---|
[4093] | 74 | Now the login form is gone. Instead we have the opportunity to logout: |
---|
| 75 | |
---|
| 76 | >>> 'form.login' in browser.contents |
---|
| 77 | False |
---|
| 78 | |
---|
[4617] | 79 | >>> logout = browser.getLink('Logout') |
---|
| 80 | >>> logout |
---|
| 81 | <Link text='Logout' url='http://localhost/app/@@logout'> |
---|
[4093] | 82 | |
---|
[4613] | 83 | The user title is also displayed in the sidebar: |
---|
[4093] | 84 | |
---|
[4613] | 85 | >>> 'Bob' in browser.contents |
---|
[4094] | 86 | True |
---|
[4093] | 87 | |
---|
[4094] | 88 | We can also log out afterwards: |
---|
| 89 | |
---|
[4617] | 90 | >>> logout.click() |
---|
[5404] | 91 | >>> print browser.contents |
---|
| 92 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"... |
---|
| 93 | ...Staff Login |
---|
| 94 | ... |
---|
[4094] | 95 | |
---|