One of the trickiest, and yet most important, areas of testing web sites is the security. Testing these schemes is one of the core goals of the SimpleTest web tester.
If you fetch a page protected by basic authentication then
rather than receiving content, you will instead get a 401
header.
We can illustrate this with this test...
One theme that runs through SimpleTest is the ability to use
SimpleExpectation objects wherever a simple
match is not enough.
If you want only an approximate match to the realm for
example, you can do this...
You can shortcut this step further by encoding the log in
details straight into the URL...
Only basic authentication is currently supported and this is only really secure in tandem with HTTPS connections. This is usually enough to protect test server from prying eyes, however. Digest authentication and NTLM authentication may be added in the future.
Basic authentication doesn't give enough control over the user interface for web developers. More likely this functionality will be coded directly into the web architecture using cookies and complicated timeouts.
Starting with a simple log-in form...
Username:
Password:
]]>
Which looks like...
Let's suppose that in fetching this page a cookie has been
set with a session ID.
We are not going to fill the form in yet, just test that
we are tracking the user.
Here is the test...
We could even attempt to spoof our own system by setting
arbitrary cookies to gain access...
If you are testing an authentication system a critical piece
of behaviour is what happens when a user logs back in.
We would like to simulate closing and reopening a browser...
WebTestCase::restart() method will
preserve cookies that have unexpired timeouts, but throw away
those that are temporary or expired.
You can optionally specify the time and date that the restart
happened.
Expiring cookies can be a problem. After all, if you have a cookie that expires after an hour, you don't want to stall the test for an hour while the cookie passes it's timeout.
To push the cookies over the hour limit you can age them
before you restart the session...