PageRenderTime 30ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/app/vendors/simpletest/docs/en/authentication_documentation.html

https://github.com/cgajardo/repositorium
HTML | 355 lines | 349 code | 6 blank | 0 comment | 0 complexity | d5af774d4a053126e5b75637c30ec848 MD5 | raw file
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. <title>SimpleTest documentation for testing log-in and authentication</title>
  5. <link rel="stylesheet" type="text/css" href="docs.css" title="Styles">
  6. </head>
  7. <body>
  8. <div class="menu_back"><div class="menu">
  9. <a href="index.html">SimpleTest</a>
  10. |
  11. <a href="overview.html">Overview</a>
  12. |
  13. <a href="unit_test_documentation.html">Unit tester</a>
  14. |
  15. <a href="group_test_documentation.html">Group tests</a>
  16. |
  17. <a href="mock_objects_documentation.html">Mock objects</a>
  18. |
  19. <a href="partial_mocks_documentation.html">Partial mocks</a>
  20. |
  21. <a href="reporter_documentation.html">Reporting</a>
  22. |
  23. <a href="expectation_documentation.html">Expectations</a>
  24. |
  25. <a href="web_tester_documentation.html">Web tester</a>
  26. |
  27. <a href="form_testing_documentation.html">Testing forms</a>
  28. |
  29. <span class="chosen">Authentication</span>
  30. |
  31. <a href="browser_documentation.html">Scriptable browser</a>
  32. </div></div>
  33. <h1>Authentication documentation</h1>
  34. This page...
  35. <ul>
  36. <li>
  37. Getting through <a href="#basic">Basic HTTP authentication</a>
  38. </li>
  39. <li>
  40. Testing <a href="#cookies">cookie based authentication</a>
  41. </li>
  42. <li>
  43. Managing <a href="#session">browser sessions</a> and timeouts
  44. </li>
  45. </ul>
  46. <div class="content">
  47. <p>
  48. One of the trickiest, and yet most important, areas
  49. of testing web sites is the security.
  50. Testing these schemes is one of the core goals of
  51. the SimpleTest web tester.
  52. </p>
  53. <p><a class="target" name="basic"><h2>Basic HTTP authentication</h2></a></p>
  54. <p>
  55. If you fetch a page protected by basic authentication then
  56. rather than receiving content, you will instead get a 401
  57. header.
  58. We can illustrate this with this test...
  59. <pre>
  60. class AuthenticationTest extends WebTestCase {<strong>
  61. function test401Header() {
  62. $this-&gt;get('http://www.lastcraft.com/protected/');
  63. $this-&gt;showHeaders();
  64. }</strong>
  65. }
  66. </pre>
  67. This allows us to see the challenge header...
  68. <div class="demo">
  69. <h1>File test</h1>
  70. <pre style="background-color: lightgray; color: black">
  71. HTTP/1.1 401 Authorization Required
  72. Date: Sat, 18 Sep 2004 19:25:18 GMT
  73. Server: Apache/1.3.29 (Unix) PHP/4.3.4
  74. WWW-Authenticate: Basic realm="SimpleTest basic authentication"
  75. Connection: close
  76. Content-Type: text/html; charset=iso-8859-1
  77. </pre>
  78. <div style="padding: 8px; margin-top: 1em; background-color: green; color: white;">1/1 test cases complete.
  79. <strong>0</strong> passes, <strong>0</strong> fails and <strong>0</strong> exceptions.</div>
  80. </div>
  81. We are trying to get away from visual inspection though, and so SimpleTest
  82. allows to make automated assertions against the challenge.
  83. Here is a thorough test of our header...
  84. <pre>
  85. class AuthenticationTest extends WebTestCase {
  86. function test401Header() {
  87. $this-&gt;get('http://www.lastcraft.com/protected/');<strong>
  88. $this-&gt;assertAuthentication('Basic');
  89. $this-&gt;assertResponse(401);
  90. $this-&gt;assertRealm('SimpleTest basic authentication');</strong>
  91. }
  92. }
  93. </pre>
  94. Any one of these tests would normally do on it's own depending
  95. on the amount of detail you want to see.
  96. </p>
  97. <p>
  98. One theme that runs through SimpleTest is the ability to use
  99. <span class="new_code">SimpleExpectation</span> objects wherever a simple
  100. match is not enough.
  101. If you want only an approximate match to the realm for
  102. example, you can do this...
  103. <pre>
  104. class AuthenticationTest extends WebTestCase {
  105. function test401Header() {
  106. $this-&gt;get('http://www.lastcraft.com/protected/');
  107. $this-&gt;assertRealm(<strong>new PatternExpectation('/simpletest/i')</strong>);
  108. }
  109. }
  110. </pre>
  111. Most of the time we are not interested in testing the
  112. authentication itself, but want to get past it to test
  113. the pages underneath.
  114. As soon as the challenge has been issued we can reply with
  115. an authentication response...
  116. <pre>
  117. class AuthenticationTest extends WebTestCase {
  118. function testCanAuthenticate() {
  119. $this-&gt;get('http://www.lastcraft.com/protected/');<strong>
  120. $this-&gt;authenticate('Me', 'Secret');</strong>
  121. $this-&gt;assertTitle(...);
  122. }
  123. }
  124. </pre>
  125. The username and password will now be sent with every
  126. subsequent request to that directory and subdirectories.
  127. You will have to authenticate again if you step outside
  128. the authenticated directory, but SimpleTest is smart enough
  129. to merge subdirectories into a common realm.
  130. </p>
  131. <p>
  132. You can shortcut this step further by encoding the log in
  133. details straight into the URL...
  134. <pre>
  135. class AuthenticationTest extends WebTestCase {
  136. function testCanReadAuthenticatedPages() {
  137. $this-&gt;get('http://<strong>Me:Secret@</strong>www.lastcraft.com/protected/');
  138. $this-&gt;assertTitle(...);
  139. }
  140. }
  141. </pre>
  142. If your username or password has special characters, then you
  143. will have to URL encode them or the request will not be parsed
  144. correctly.
  145. Also this header will not be sent on subsequent requests if
  146. you request a page with a fully qualified URL.
  147. If you navigate with relative URLs though, the authentication
  148. information will be preserved.
  149. </p>
  150. <p>
  151. Only basic authentication is currently supported and this is
  152. only really secure in tandem with HTTPS connections.
  153. This is usually enough to protect test server from prying eyes,
  154. however.
  155. Digest authentication and NTLM authentication may be added
  156. in the future.
  157. </p>
  158. <p><a class="target" name="cookies"><h2>Cookies</h2></a></p>
  159. <p>
  160. Basic authentication doesn't give enough control over the
  161. user interface for web developers.
  162. More likely this functionality will be coded directly into
  163. the web architecture using cookies and complicated timeouts.
  164. </p>
  165. <p>
  166. Starting with a simple log-in form...
  167. <pre>
  168. &lt;form&gt;
  169. Username:
  170. &lt;input type="text" name="u" value="" /&gt;&lt;br /&gt;
  171. Password:
  172. &lt;input type="password" name="p" value="" /&gt;&lt;br /&gt;
  173. &lt;input type="submit" value="Log in" /&gt;
  174. &lt;/form&gt;
  175. </pre>
  176. Which looks like...
  177. </p>
  178. <p>
  179. <form class="demo">
  180. Username:
  181. <input type="text" name="u" value=""><br>
  182. Password:
  183. <input type="password" name="p" value=""><br>
  184. <input type="submit" value="Log in">
  185. </form>
  186. </p>
  187. <p>
  188. Let's suppose that in fetching this page a cookie has been
  189. set with a session ID.
  190. We are not going to fill the form in yet, just test that
  191. we are tracking the user.
  192. Here is the test...
  193. <pre>
  194. class LogInTest extends WebTestCase {
  195. function testSessionCookieSetBeforeForm() {
  196. $this-&gt;get('http://www.my-site.com/login.php');<strong>
  197. $this-&gt;assertCookie('SID');</strong>
  198. }
  199. }
  200. </pre>
  201. All we are doing is confirming that the cookie is set.
  202. As the value is likely to be rather cryptic it's not
  203. really worth testing this with...
  204. <pre>
  205. class LogInTest extends WebTestCase {
  206. function testSessionCookieIsCorrectPattern() {
  207. $this-&gt;get('http://www.my-site.com/login.php');
  208. $this-&gt;assertCookie('SID', <strong>new PatternExpectation('/[a-f0-9]{32}/i')</strong>);
  209. }
  210. }
  211. </pre>
  212. The rest of the test would be the same as any other form,
  213. but we might want to confirm that we still have the same
  214. cookie after log-in as before we entered.
  215. We wouldn't want to lose track of this after all.
  216. Here is a possible test for this...
  217. <pre>
  218. class LogInTest extends WebTestCase {
  219. ...
  220. function testSessionCookieSameAfterLogIn() {
  221. $this-&gt;get('http://www.my-site.com/login.php');<strong>
  222. $session = $this-&gt;getCookie('SID');
  223. $this-&gt;setField('u', 'Me');
  224. $this-&gt;setField('p', 'Secret');
  225. $this-&gt;click('Log in');
  226. $this-&gt;assertText('Welcome Me');
  227. $this-&gt;assertCookie('SID', $session);</strong>
  228. }
  229. }
  230. </pre>
  231. This confirms that the session identifier is maintained
  232. afer log-in.
  233. </p>
  234. <p>
  235. We could even attempt to spoof our own system by setting
  236. arbitrary cookies to gain access...
  237. <pre>
  238. class LogInTest extends WebTestCase {
  239. ...
  240. function testSessionCookieSameAfterLogIn() {
  241. $this-&gt;get('http://www.my-site.com/login.php');<strong>
  242. $this-&gt;setCookie('SID', 'Some other session');
  243. $this-&gt;get('http://www.my-site.com/restricted.php');</strong>
  244. $this-&gt;assertText('Access denied');
  245. }
  246. }
  247. </pre>
  248. Is your site protected from this attack?
  249. </p>
  250. <p><a class="target" name="session"><h2>Browser sessions</h2></a></p>
  251. <p>
  252. If you are testing an authentication system a critical piece
  253. of behaviour is what happens when a user logs back in.
  254. We would like to simulate closing and reopening a browser...
  255. <pre>
  256. class LogInTest extends WebTestCase {
  257. ...
  258. function testLoseAuthenticationAfterBrowserClose() {
  259. $this-&gt;get('http://www.my-site.com/login.php');
  260. $this-&gt;setField('u', 'Me');
  261. $this-&gt;setField('p', 'Secret');
  262. $this-&gt;click('Log in');
  263. $this-&gt;assertText('Welcome Me');<strong>
  264. $this-&gt;restart();
  265. $this-&gt;get('http://www.my-site.com/restricted.php');
  266. $this-&gt;assertText('Access denied');</strong>
  267. }
  268. }
  269. </pre>
  270. The <span class="new_code">WebTestCase::restart()</span> method will
  271. preserve cookies that have unexpired timeouts, but throw away
  272. those that are temporary or expired.
  273. You can optionally specify the time and date that the restart
  274. happened.
  275. </p>
  276. <p>
  277. Expiring cookies can be a problem.
  278. After all, if you have a cookie that expires after an hour,
  279. you don't want to stall the test for an hour while the
  280. cookie passes it's timeout.
  281. </p>
  282. <p>
  283. To push the cookies over the hour limit you can age them
  284. before you restart the session...
  285. <pre>
  286. class LogInTest extends WebTestCase {
  287. ...
  288. function testLoseAuthenticationAfterOneHour() {
  289. $this-&gt;get('http://www.my-site.com/login.php');
  290. $this-&gt;setField('u', 'Me');
  291. $this-&gt;setField('p', 'Secret');
  292. $this-&gt;click('Log in');
  293. $this-&gt;assertText('Welcome Me');
  294. <strong>
  295. $this-&gt;ageCookies(3600);</strong>
  296. $this-&gt;restart();
  297. $this-&gt;get('http://www.my-site.com/restricted.php');
  298. $this-&gt;assertText('Access denied');
  299. }
  300. }
  301. </pre>
  302. After the restart it will appear that cookies are an
  303. hour older and any that pass their expiry will have
  304. disappeared.
  305. </p>
  306. </div>
  307. References and related information...
  308. <ul>
  309. <li>
  310. SimpleTest project page on <a href="http://sourceforge.net/projects/simpletest/">SourceForge</a>.
  311. </li>
  312. <li>
  313. SimpleTest download page on <a href="http://www.lastcraft.com/simple_test.php">LastCraft</a>.
  314. </li>
  315. <li>
  316. The <a href="http://simpletest.org/api/">developer's API for SimpleTest</a>
  317. gives full detail on the classes and assertions available.
  318. </li>
  319. </ul>
  320. <div class="menu_back"><div class="menu">
  321. <a href="index.html">SimpleTest</a>
  322. |
  323. <a href="overview.html">Overview</a>
  324. |
  325. <a href="unit_test_documentation.html">Unit tester</a>
  326. |
  327. <a href="group_test_documentation.html">Group tests</a>
  328. |
  329. <a href="mock_objects_documentation.html">Mock objects</a>
  330. |
  331. <a href="partial_mocks_documentation.html">Partial mocks</a>
  332. |
  333. <a href="reporter_documentation.html">Reporting</a>
  334. |
  335. <a href="expectation_documentation.html">Expectations</a>
  336. |
  337. <a href="web_tester_documentation.html">Web tester</a>
  338. |
  339. <a href="form_testing_documentation.html">Testing forms</a>
  340. |
  341. <span class="chosen">Authentication</span>
  342. |
  343. <a href="browser_documentation.html">Scriptable browser</a>
  344. </div></div>
  345. <div class="copyright">
  346. Copyright<br>Marcus Baker 2006
  347. </div>
  348. </body>
  349. </html>