001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com> and
003: * Steven Grimm <koreth[remove] at midwinter dot com>
004: * Distributed under the terms of either:
005: * - the common development and distribution license (CDDL), v1.0; or
006: * - the GNU Lesser General Public License, v2.1 or later
007: * $Id: TestMemoryAuthenticated.java 3634 2007-01-08 21:42:24Z gbevin $
008: */
009: package com.uwyn.rife.authentication.elements;
010:
011: import com.meterware.httpunit.GetMethodWebRequest;
012: import com.meterware.httpunit.WebConversation;
013: import com.meterware.httpunit.WebForm;
014: import com.meterware.httpunit.WebRequest;
015: import com.meterware.httpunit.WebResponse;
016: import com.uwyn.rife.TestCaseServerside;
017: import com.uwyn.rife.engine.SiteBuilder;
018: import com.uwyn.rife.resources.ResourceFinderClasspath;
019: import com.uwyn.rife.test.MockConversation;
020: import com.uwyn.rife.test.MockForm;
021: import com.uwyn.rife.test.MockRequest;
022: import com.uwyn.rife.test.MockResponse;
023: import com.uwyn.rife.test.ParsedHtml;
024: import com.uwyn.rife.tools.ExceptionUtils;
025:
026: public class TestMemoryAuthenticated extends TestCaseServerside {
027: public TestMemoryAuthenticated(int siteType, String name) {
028: super (siteType, name);
029: }
030:
031: public void testMemoryAuthenticatedBasicInput() throws Exception {
032: setupSite("site/authentication_memory_input.xml");
033:
034: WebConversation conversation = new WebConversation();
035: WebRequest request = null;
036: WebResponse response = null;
037: WebForm form = null;
038:
039: request = new GetMethodWebRequest(
040: "http://localhost:8181/authentication/memory/basic");
041: response = conversation.getResponse(request);
042: form = response.getForms()[0];
043: form.setParameter("login", "guest");
044: form.setParameter("password", "guestpass");
045: response = form.submit();
046:
047: assertEquals(0, response.getForms().length);
048:
049: request = new GetMethodWebRequest(
050: "http://localhost:8181/authentication/memory/basic");
051: response = conversation.getResponse(request);
052: form = response.getForms()[0];
053: form.setParameter("login", "gbevin");
054: form.setParameter("password", "yeolpass");
055: response = form.submit();
056:
057: assertEquals(0, response.getForms().length);
058:
059: request = new GetMethodWebRequest(
060: "http://localhost:8181/authentication/memory/basic");
061: response = conversation.getResponse(request);
062: form = response.getForms()[0];
063: form.setParameter("login", "invalid");
064: form.setParameter("password", "invalid");
065: response = form.submit();
066:
067: assertEquals(1, response.getForms().length);
068: }
069:
070: public void testMemoryAuthenticatedBasicCookie() throws Exception {
071: setupSite("site/authentication_memory_cookie.xml");
072:
073: WebConversation conversation = new WebConversation();
074: WebRequest request = null;
075: WebResponse response = null;
076: WebForm form = null;
077:
078: request = new GetMethodWebRequest(
079: "http://localhost:8181/authentication/memory/basic");
080: response = conversation.getResponse(request);
081: form = response.getForms()[0];
082: form.setParameter("login", "guest");
083: form.setParameter("password", "guestpass");
084: response = form.submit();
085:
086: assertEquals("authid", response.getNewCookieNames()[0]);
087: assertEquals(0, response.getForms().length);
088:
089: request = new GetMethodWebRequest(
090: "http://localhost:8181/authentication/memory/basic");
091: assertEquals(0, response.getForms().length);
092:
093: conversation = new WebConversation();
094: request = new GetMethodWebRequest(
095: "http://localhost:8181/authentication/memory/basic");
096: response = conversation.getResponse(request);
097: form = response.getForms()[0];
098: form.setParameter("login", "gbevin");
099: form.setParameter("password", "yeolpass");
100: response = form.submit();
101:
102: assertEquals("authid", response.getNewCookieNames()[0]);
103: assertEquals(0, response.getForms().length);
104:
105: conversation = new WebConversation();
106: request = new GetMethodWebRequest(
107: "http://localhost:8181/authentication/memory/basic");
108: response = conversation.getResponse(request);
109: form = response.getForms()[0];
110: form.setParameter("login", "invalid");
111: form.setParameter("password", "invalid");
112: response = form.submit();
113:
114: assertEquals(0, response.getNewCookieNames().length);
115: assertEquals(1, response.getForms().length);
116: }
117:
118: public void testMemoryAuthenticatedXhtmlInput() throws Exception {
119: setupSite("site/authentication_memory_input.xml");
120:
121: WebConversation conversation = new WebConversation();
122: WebRequest request = null;
123: WebResponse response = null;
124: WebForm form = null;
125:
126: request = new GetMethodWebRequest(
127: "http://localhost:8181/authentication/memory/xhtml");
128: response = conversation.getResponse(request);
129: assertTrue(response.getText().indexOf("This is XHtml :") != -1);
130: form = response.getForms()[0];
131: form.setParameter("login", "guest");
132: form.setParameter("password", "guestpass");
133: response = form.submit();
134:
135: assertEquals(0, response.getForms().length);
136:
137: request = new GetMethodWebRequest(
138: "http://localhost:8181/authentication/memory/xhtml");
139: response = conversation.getResponse(request);
140: form = response.getForms()[0];
141: form.setParameter("login", "gbevin");
142: form.setParameter("password", "yeolpass");
143: response = form.submit();
144:
145: assertEquals(0, response.getForms().length);
146:
147: request = new GetMethodWebRequest(
148: "http://localhost:8181/authentication/memory/xhtml");
149: response = conversation.getResponse(request);
150: form = response.getForms()[0];
151: form.setParameter("login", "invalid");
152: form.setParameter("password", "invalid");
153: response = form.submit();
154:
155: assertEquals(1, response.getForms().length);
156: }
157:
158: public void testMemoryAuthenticatedXhtmlCookie() throws Exception {
159: setupSite("site/authentication_memory_cookie.xml");
160:
161: WebConversation conversation = new WebConversation();
162: WebRequest request = null;
163: WebResponse response = null;
164: WebForm form = null;
165:
166: request = new GetMethodWebRequest(
167: "http://localhost:8181/authentication/memory/xhtml");
168: response = conversation.getResponse(request);
169: assertTrue(response.getText().indexOf("This is XHtml :") != -1);
170: form = response.getForms()[0];
171: form.setParameter("login", "guest");
172: form.setParameter("password", "guestpass");
173: response = form.submit();
174:
175: assertEquals("authid", response.getNewCookieNames()[0]);
176: assertEquals(0, response.getForms().length);
177:
178: request = new GetMethodWebRequest(
179: "http://localhost:8181/authentication/memory/xhtml");
180: assertEquals(0, response.getForms().length);
181:
182: conversation = new WebConversation();
183: request = new GetMethodWebRequest(
184: "http://localhost:8181/authentication/memory/xhtml");
185: response = conversation.getResponse(request);
186: form = response.getForms()[0];
187: form.setParameter("login", "gbevin");
188: form.setParameter("password", "yeolpass");
189: response = form.submit();
190:
191: assertEquals("authid", response.getNewCookieNames()[0]);
192: assertEquals(0, response.getForms().length);
193:
194: conversation = new WebConversation();
195: request = new GetMethodWebRequest(
196: "http://localhost:8181/authentication/memory/xhtml");
197: response = conversation.getResponse(request);
198: form = response.getForms()[0];
199: form.setParameter("login", "invalid");
200: form.setParameter("password", "invalid");
201: response = form.submit();
202:
203: assertEquals(0, response.getNewCookieNames().length);
204: assertEquals(1, response.getForms().length);
205: }
206:
207: public void testMemoryAuthenticatedEncryptedInput()
208: throws Exception {
209: setupSite("site/authentication_memory_input.xml");
210:
211: WebConversation conversation = new WebConversation();
212: WebRequest request = null;
213: WebResponse response = null;
214: WebForm form = null;
215:
216: request = new GetMethodWebRequest(
217: "http://localhost:8181/authentication/memory/basic");
218: response = conversation.getResponse(request);
219: form = response.getForms()[0];
220: form.setParameter("login", "guest");
221: form.setParameter("password", "guestpass");
222: response = form.submit();
223:
224: assertEquals(0, response.getForms().length);
225:
226: request = new GetMethodWebRequest(
227: "http://localhost:8181/authentication/memory/basic");
228: response = conversation.getResponse(request);
229: form = response.getForms()[0];
230: form.setParameter("login", "gbevinencrypted");
231: form.setParameter("password", "yeolpass");
232: response = form.submit();
233:
234: assertEquals(0, response.getForms().length);
235:
236: request = new GetMethodWebRequest(
237: "http://localhost:8181/authentication/memory/basic");
238: response = conversation.getResponse(request);
239: form = response.getForms()[0];
240: form.setParameter("login", "guestencrypted");
241: form.setParameter("password", "guestpass");
242: response = form.submit();
243:
244: assertEquals(0, response.getForms().length);
245:
246: request = new GetMethodWebRequest(
247: "http://localhost:8181/authentication/memory/basic");
248: response = conversation.getResponse(request);
249: form = response.getForms()[0];
250: form.setParameter("login", "gbevin");
251: form.setParameter("password", "yeolpass");
252: response = form.submit();
253:
254: assertEquals(0, response.getForms().length);
255:
256: request = new GetMethodWebRequest(
257: "http://localhost:8181/authentication/memory/basic");
258: response = conversation.getResponse(request);
259: form = response.getForms()[0];
260: form.setParameter("login", "invalid");
261: form.setParameter("password", "invalid");
262: response = form.submit();
263:
264: assertEquals(1, response.getForms().length);
265: }
266:
267: public void testMemoryAuthenticatedEncryptedCookie()
268: throws Exception {
269: setupSite("site/authentication_memory_cookie.xml");
270:
271: WebConversation conversation = new WebConversation();
272: WebRequest request = null;
273: WebResponse response = null;
274: WebForm form = null;
275:
276: request = new GetMethodWebRequest(
277: "http://localhost:8181/authentication/memory/basic");
278: response = conversation.getResponse(request);
279: form = response.getForms()[0];
280: form.setParameter("login", "guest");
281: form.setParameter("password", "guestpass");
282: response = form.submit();
283:
284: assertEquals("authid", response.getNewCookieNames()[0]);
285: assertEquals(0, response.getForms().length);
286:
287: request = new GetMethodWebRequest(
288: "http://localhost:8181/authentication/memory/basic");
289: assertEquals(0, response.getForms().length);
290:
291: conversation = new WebConversation();
292: request = new GetMethodWebRequest(
293: "http://localhost:8181/authentication/memory/basic");
294: response = conversation.getResponse(request);
295: form = response.getForms()[0];
296: form.setParameter("login", "gbevinencrypted");
297: form.setParameter("password", "yeolpass");
298: response = form.submit();
299:
300: assertEquals("authid", response.getNewCookieNames()[0]);
301: assertEquals(0, response.getForms().length);
302:
303: conversation = new WebConversation();
304: request = new GetMethodWebRequest(
305: "http://localhost:8181/authentication/memory/basic");
306: response = conversation.getResponse(request);
307: form = response.getForms()[0];
308: form.setParameter("login", "guestencrypted");
309: form.setParameter("password", "guestpass");
310: response = form.submit();
311:
312: assertEquals("authid", response.getNewCookieNames()[0]);
313: assertEquals(0, response.getForms().length);
314:
315: conversation = new WebConversation();
316: request = new GetMethodWebRequest(
317: "http://localhost:8181/authentication/memory/basic");
318: response = conversation.getResponse(request);
319: form = response.getForms()[0];
320: form.setParameter("login", "gbevin");
321: form.setParameter("password", "yeolpass");
322: response = form.submit();
323:
324: assertEquals("authid", response.getNewCookieNames()[0]);
325: assertEquals(0, response.getForms().length);
326:
327: conversation = new WebConversation();
328: request = new GetMethodWebRequest(
329: "http://localhost:8181/authentication/memory/basic");
330: response = conversation.getResponse(request);
331: form = response.getForms()[0];
332: form.setParameter("login", "invalid");
333: form.setParameter("password", "invalid");
334: response = form.submit();
335:
336: assertEquals(0, response.getNewCookieNames().length);
337: assertEquals(1, response.getForms().length);
338: }
339:
340: public void testMemoryAuthenticatedOtheridInput() throws Exception {
341: setupSite("site/authentication_memory_input.xml");
342:
343: WebConversation conversation = new WebConversation();
344: WebRequest request = null;
345: WebResponse response = null;
346: WebForm form = null;
347: String auth_id;
348:
349: request = new GetMethodWebRequest(
350: "http://localhost:8181/authentication/memory/basic");
351: response = conversation.getResponse(request);
352: form = response.getForms()[0];
353: form.setParameter("login", "guest");
354: form.setParameter("password", "guestpass");
355: response = form.submit();
356:
357: assertEquals(0, response.getForms().length);
358: auth_id = response.getTitle();
359:
360: request = new GetMethodWebRequest(
361: "http://localhost:8181/authentication/memory/basic");
362: request.setParameter("authid", auth_id);
363: response = conversation.getResponse(request);
364:
365: assertEquals(0, response.getForms().length);
366: assertEquals(auth_id, response.getTitle());
367:
368: request = new GetMethodWebRequest(
369: "http://localhost:8181/authentication/memory/otherid");
370: request.setParameter("authid", auth_id);
371: response = conversation.getResponse(request);
372:
373: assertEquals(1, response.getForms().length);
374: assertTrue(!auth_id.equals(response.getTitle()));
375:
376: request = new GetMethodWebRequest(
377: "http://localhost:8181/authentication/memory/otherid");
378: response = conversation.getResponse(request);
379: form = response.getForms()[0];
380: form.setParameter("login", "guest");
381: form.setParameter("password", "guestpass");
382: response = form.submit();
383:
384: assertEquals(0, response.getForms().length);
385: auth_id = response.getTitle();
386:
387: request = new GetMethodWebRequest(
388: "http://localhost:8181/authentication/memory/otherid");
389: request.setParameter("authid", auth_id);
390: response = conversation.getResponse(request);
391:
392: assertEquals(0, response.getForms().length);
393: assertEquals(auth_id, response.getTitle());
394:
395: request = new GetMethodWebRequest(
396: "http://localhost:8181/authentication/memory/basic");
397: request.setParameter("authid", auth_id);
398: response = conversation.getResponse(request);
399:
400: assertEquals(1, response.getForms().length);
401: assertTrue(!auth_id.equals(response.getTitle()));
402: }
403:
404: public void testMemoryAuthenticatedOtheridCookie() throws Exception {
405: SiteBuilder builder = new SiteBuilder(
406: "site/authentication_memory_cookie.xml",
407: ResourceFinderClasspath.getInstance());
408: MockConversation conversation = new MockConversation(builder
409: .getSite());
410:
411: MockResponse response;
412: MockForm form;
413: ParsedHtml parsed;
414:
415: String auth_id;
416:
417: response = conversation
418: .doRequest("/authentication/memory/basic");
419: parsed = response.getParsedHtml();
420: form = parsed.getForms().get(0);
421: form.setParameter("login", "guest");
422: form.setParameter("password", "guestpass");
423: response = form.submit();
424: parsed = response.getParsedHtml();
425:
426: assertEquals(0, parsed.getForms().size());
427: assertEquals("authid", response.getNewCookieNames().get(0));
428: auth_id = conversation.getCookieValue("authid");
429:
430: conversation = new MockConversation(builder.getSite());
431: conversation.addCookie("authid", auth_id);
432: response = conversation
433: .doRequest("/authentication/memory/basic");
434: parsed = response.getParsedHtml();
435:
436: assertEquals(0, parsed.getForms().size());
437: auth_id = parsed.getTitle();
438:
439: conversation = new MockConversation(builder.getSite());
440: conversation.addCookie("authid", auth_id);
441: response = conversation
442: .doRequest("/authentication/memory/otherid");
443: parsed = response.getParsedHtml();
444:
445: assertEquals(0, response.getNewCookieNames().size());
446: assertEquals(1, parsed.getForms().size());
447:
448: conversation = new MockConversation(builder.getSite());
449: conversation.addCookie("authid", auth_id);
450: response = conversation
451: .doRequest("/authentication/memory/otherid");
452: parsed = response.getParsedHtml();
453: form = parsed.getForms().get(0);
454: form.setParameter("login", "guest");
455: form.setParameter("password", "guestpass");
456: response = form.submit();
457: parsed = response.getParsedHtml();
458:
459: assertEquals(0, parsed.getForms().size());
460: assertEquals("authid", response.getNewCookieNames().get(0));
461: auth_id = conversation.getCookieValue("authid");
462:
463: conversation = new MockConversation(builder.getSite());
464: conversation.addCookie("authid", auth_id);
465: response = conversation
466: .doRequest("/authentication/memory/otherid");
467: parsed = response.getParsedHtml();
468:
469: assertEquals(0, parsed.getForms().size());
470:
471: response = conversation
472: .doRequest("/authentication/memory/basic");
473: parsed = response.getParsedHtml();
474:
475: assertEquals(1, parsed.getForms().size());
476: }
477:
478: public void testMemoryAuthenticatedRoleInput() throws Exception {
479: setupSite("site/authentication_memory_input.xml");
480:
481: WebConversation conversation = new WebConversation();
482: WebRequest request = null;
483: WebResponse response = null;
484: WebForm form = null;
485:
486: request = new GetMethodWebRequest(
487: "http://localhost:8181/authentication/memory/role");
488: response = conversation.getResponse(request);
489: form = response.getForms()[0];
490: form.setParameter("login", "guest");
491: form.setParameter("password", "guestpass");
492: response = form.submit();
493:
494: assertEquals(1, response.getForms().length);
495:
496: request = new GetMethodWebRequest(
497: "http://localhost:8181/authentication/memory/role");
498: response = conversation.getResponse(request);
499: form = response.getForms()[0];
500: form.setParameter("login", "gbevin");
501: form.setParameter("password", "yeolpass");
502: response = form.submit();
503:
504: assertEquals(0, response.getForms().length);
505:
506: request = new GetMethodWebRequest(
507: "http://localhost:8181/authentication/memory/role2");
508: response = conversation.getResponse(request);
509: form = response.getForms()[0];
510: form.setParameter("login", "gbevin");
511: form.setParameter("password", "yeolpass");
512: response = form.submit();
513:
514: assertEquals(0, response.getForms().length);
515:
516: request = new GetMethodWebRequest(
517: "http://localhost:8181/authentication/memory/role");
518: response = conversation.getResponse(request);
519: form = response.getForms()[0];
520: form.setParameter("login", "johndoe");
521: form.setParameter("password", "thepassofbass");
522: response = form.submit();
523:
524: assertEquals(1, response.getForms().length);
525:
526: request = new GetMethodWebRequest(
527: "http://localhost:8181/authentication/memory/role2");
528: response = conversation.getResponse(request);
529: form = response.getForms()[0];
530: form.setParameter("login", "johndoe");
531: form.setParameter("password", "thepassofbass");
532: response = form.submit();
533:
534: assertEquals(0, response.getForms().length);
535:
536: request = new GetMethodWebRequest(
537: "http://localhost:8181/authentication/memory/role");
538: response = conversation.getResponse(request);
539: form = response.getForms()[0];
540: form.setParameter("login", "invalid");
541: form.setParameter("password", "invalid");
542: response = form.submit();
543:
544: assertEquals(1, response.getForms().length);
545: }
546:
547: public void testMemoryAuthenticatedRoleCookie() throws Exception {
548: setupSite("site/authentication_memory_cookie.xml");
549:
550: WebConversation conversation = new WebConversation();
551: WebRequest request = null;
552: WebResponse response = null;
553: WebForm form = null;
554:
555: request = new GetMethodWebRequest(
556: "http://localhost:8181/authentication/memory/role");
557: response = conversation.getResponse(request);
558: form = response.getForms()[0];
559: form.setParameter("login", "guest");
560: form.setParameter("password", "guestpass");
561: response = form.submit();
562:
563: assertEquals(0, response.getNewCookieNames().length);
564: assertEquals(1, response.getForms().length);
565:
566: request = new GetMethodWebRequest(
567: "http://localhost:8181/authentication/memory/role");
568: response = conversation.getResponse(request);
569: form = response.getForms()[0];
570: form.setParameter("login", "gbevin");
571: form.setParameter("password", "yeolpass");
572: response = form.submit();
573:
574: assertEquals("authid", response.getNewCookieNames()[0]);
575: assertEquals(0, response.getForms().length);
576:
577: request = new GetMethodWebRequest(
578: "http://localhost:8181/authentication/memory/role");
579: assertEquals(0, response.getForms().length);
580:
581: conversation = new WebConversation();
582: request = new GetMethodWebRequest(
583: "http://localhost:8181/authentication/memory/role2");
584: response = conversation.getResponse(request);
585: form = response.getForms()[0];
586: form.setParameter("login", "gbevin");
587: form.setParameter("password", "yeolpass");
588: response = form.submit();
589:
590: assertEquals("authid", response.getNewCookieNames()[0]);
591: assertEquals(0, response.getForms().length);
592:
593: conversation = new WebConversation();
594: request = new GetMethodWebRequest(
595: "http://localhost:8181/authentication/memory/role");
596: response = conversation.getResponse(request);
597: form = response.getForms()[0];
598: form.setParameter("login", "johndoe");
599: form.setParameter("password", "thepassofbass");
600: response = form.submit();
601:
602: assertEquals(0, response.getNewCookieNames().length);
603: assertEquals(1, response.getForms().length);
604:
605: conversation = new WebConversation();
606: request = new GetMethodWebRequest(
607: "http://localhost:8181/authentication/memory/role2");
608: response = conversation.getResponse(request);
609: form = response.getForms()[0];
610: form.setParameter("login", "johndoe");
611: form.setParameter("password", "thepassofbass");
612: response = form.submit();
613:
614: assertEquals("authid", response.getNewCookieNames()[0]);
615: assertEquals(0, response.getForms().length);
616:
617: conversation = new WebConversation();
618: request = new GetMethodWebRequest(
619: "http://localhost:8181/authentication/memory/role");
620: response = conversation.getResponse(request);
621: form = response.getForms()[0];
622: form.setParameter("login", "invalid");
623: form.setParameter("password", "invalid");
624: response = form.submit();
625:
626: assertEquals(0, response.getNewCookieNames().length);
627: assertEquals(1, response.getForms().length);
628: }
629:
630: public void testMemoryAuthenticatedRoleIsolationInput()
631: throws Exception {
632: setupSite("site/authentication_memory_input.xml");
633:
634: WebConversation conversation = new WebConversation();
635: WebRequest request = null;
636: WebResponse response = null;
637: WebForm form = null;
638: String auth_id = null;
639:
640: request = new GetMethodWebRequest(
641: "http://localhost:8181/authentication/memory/role");
642: response = conversation.getResponse(request);
643: form = response.getForms()[0];
644: form.setParameter("login", "gbevin");
645: form.setParameter("password", "yeolpass");
646: response = form.submit();
647:
648: assertEquals(0, response.getForms().length);
649: auth_id = response.getTitle();
650:
651: request = new GetMethodWebRequest(
652: "http://localhost:8181/authentication/memory/role");
653: request.setParameter("authid", auth_id);
654: response = conversation.getResponse(request);
655:
656: assertEquals(auth_id, response.getTitle());
657:
658: request = new GetMethodWebRequest(
659: "http://localhost:8181/authentication/memory/role2");
660: request.setParameter("authid", auth_id);
661: response = conversation.getResponse(request);
662:
663: assertEquals(auth_id, response.getTitle());
664:
665: request = new GetMethodWebRequest(
666: "http://localhost:8181/authentication/memory/role2");
667: response = conversation.getResponse(request);
668: form = response.getForms()[0];
669: form.setParameter("login", "johndoe");
670: form.setParameter("password", "thepassofbass");
671: response = form.submit();
672:
673: assertEquals(0, response.getForms().length);
674: auth_id = response.getTitle();
675:
676: request = new GetMethodWebRequest(
677: "http://localhost:8181/authentication/memory/role2");
678: request.setParameter("authid", auth_id);
679: response = conversation.getResponse(request);
680:
681: assertEquals(auth_id, response.getTitle());
682:
683: request = new GetMethodWebRequest(
684: "http://localhost:8181/authentication/memory/role");
685: request.setParameter("authid", auth_id);
686: response = conversation.getResponse(request);
687:
688: assertEquals(1, response.getForms().length);
689: }
690:
691: public void testMemoryAuthenticatedRoleIsolationCookie()
692: throws Exception {
693: SiteBuilder builder = new SiteBuilder(
694: "site/authentication_memory_cookie.xml",
695: ResourceFinderClasspath.getInstance());
696: MockConversation conversation = new MockConversation(builder
697: .getSite());
698:
699: MockResponse response;
700: MockForm form;
701: ParsedHtml parsed;
702:
703: String auth_id;
704:
705: response = conversation
706: .doRequest("/authentication/memory/role");
707: parsed = response.getParsedHtml();
708: form = parsed.getForms().get(0);
709: form.setParameter("login", "gbevin");
710: form.setParameter("password", "yeolpass");
711: response = form.submit();
712: parsed = response.getParsedHtml();
713:
714: assertEquals(0, parsed.getForms().size());
715: assertEquals("authid", response.getNewCookieNames().get(0));
716: auth_id = conversation.getCookieValue("authid");
717:
718: conversation = new MockConversation(builder.getSite());
719: conversation.addCookie("authid", auth_id);
720: response = conversation
721: .doRequest("/authentication/memory/role");
722: parsed = response.getParsedHtml();
723:
724: assertEquals(0, parsed.getForms().size());
725:
726: conversation = new MockConversation(builder.getSite());
727: conversation.addCookie("authid", auth_id);
728: response = conversation
729: .doRequest("/authentication/memory/role2");
730: parsed = response.getParsedHtml();
731:
732: assertEquals(0, parsed.getForms().size());
733:
734: conversation = new MockConversation(builder.getSite());
735: response = conversation
736: .doRequest("/authentication/memory/role2");
737: parsed = response.getParsedHtml();
738: form = parsed.getForms().get(0);
739: form.setParameter("login", "johndoe");
740: form.setParameter("password", "thepassofbass");
741: response = form.submit();
742: parsed = response.getParsedHtml();
743:
744: assertEquals(0, parsed.getForms().size());
745: assertEquals("authid", response.getNewCookieNames().get(0));
746: auth_id = conversation.getCookieValue("authid");
747:
748: conversation = new MockConversation(builder.getSite());
749: conversation.addCookie("authid", auth_id);
750: response = conversation
751: .doRequest("/authentication/memory/role2");
752: parsed = response.getParsedHtml();
753:
754: assertEquals(0, parsed.getForms().size());
755:
756: conversation = new MockConversation(builder.getSite());
757: conversation.addCookie("authid", auth_id);
758: response = conversation
759: .doRequest("/authentication/memory/role");
760: parsed = response.getParsedHtml();
761:
762: assertEquals(1, parsed.getForms().size());
763: }
764:
765: public void testMemoryAuthenticatedSessiondurationInput()
766: throws Exception {
767: setupSite("site/authentication_memory_input.xml");
768:
769: WebConversation conversation = new WebConversation();
770: WebRequest request = null;
771: WebResponse response = null;
772: WebForm form = null;
773: String auth_id;
774:
775: request = new GetMethodWebRequest(
776: "http://localhost:8181/authentication/memory/sessionduration");
777: response = conversation.getResponse(request);
778: form = response.getForms()[0];
779: form.setParameter("login", "guest");
780: form.setParameter("password", "guestpass");
781: response = form.submit();
782:
783: assertEquals(0, response.getForms().length);
784: auth_id = response.getTitle();
785:
786: request = new GetMethodWebRequest(
787: "http://localhost:8181/authentication/memory/sessionduration");
788: request.setParameter("authid", auth_id);
789: response = conversation.getResponse(request);
790:
791: assertEquals(0, response.getForms().length);
792: assertEquals(auth_id, response.getTitle());
793:
794: try {
795: Thread.sleep(2000);
796: } catch (InterruptedException e) {
797: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
798: }
799:
800: request = new GetMethodWebRequest(
801: "http://localhost:8181/authentication/memory/sessionduration");
802: request.setParameter("authid", auth_id);
803: response = conversation.getResponse(request);
804:
805: assertEquals(1, response.getForms().length);
806: }
807:
808: public void testMemoryAuthenticatedSessiondurationCookie()
809: throws Exception {
810: SiteBuilder builder = new SiteBuilder(
811: "site/authentication_memory_cookie.xml",
812: ResourceFinderClasspath.getInstance());
813: MockConversation conversation = new MockConversation(builder
814: .getSite());
815:
816: MockResponse response;
817: MockForm form;
818: ParsedHtml parsed;
819:
820: String auth_id;
821:
822: response = conversation
823: .doRequest("/authentication/memory/sessionduration");
824: parsed = response.getParsedHtml();
825: form = parsed.getForms().get(0);
826: form.setParameter("login", "guest");
827: form.setParameter("password", "guestpass");
828: response = form.submit();
829: parsed = response.getParsedHtml();
830:
831: assertEquals(0, parsed.getForms().size());
832: assertEquals("authid", response.getNewCookieNames().get(0));
833: auth_id = conversation.getCookieValue("authid");
834:
835: conversation = new MockConversation(builder.getSite());
836: conversation.addCookie("authid", auth_id);
837: response = conversation
838: .doRequest("/authentication/memory/sessionduration");
839: parsed = response.getParsedHtml();
840:
841: assertEquals(0, parsed.getForms().size());
842:
843: try {
844: Thread.sleep(2000);
845: } catch (InterruptedException e) {
846: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
847: }
848:
849: conversation = new MockConversation(builder.getSite());
850: conversation.addCookie("authid", auth_id);
851: response = conversation
852: .doRequest("/authentication/memory/sessionduration");
853: parsed = response.getParsedHtml();
854:
855: assertEquals(1, parsed.getForms().size());
856: }
857:
858: /**
859: * Tests that we can get through an authenticated element that has its
860: * "enforce_authenticated" flag set to false.
861: */
862: public void testMemoryAuthenticatedNotEnforcedInput()
863: throws Exception {
864: SiteBuilder builder = new SiteBuilder(
865: "site/authentication_memory_input.xml",
866: ResourceFinderClasspath.getInstance());
867: MockConversation conversation = new MockConversation(builder
868: .getSite());
869:
870: MockResponse response;
871: MockRequest request;
872: MockForm form;
873: ParsedHtml parsed;
874: String auth_id;
875:
876: /* Can we get through the page as an anonymous user? */
877: response = conversation
878: .doRequest("/authentication/memory/enforce");
879: parsed = response.getParsedHtml();
880:
881: assertEquals("forms", 0, parsed.getForms().size());
882: assertEquals("login name", "(none)", parsed.getLinkWithId(
883: "userLogin").getText());
884:
885: /* Now hit a protected page so we can get an authid cookie. */
886: response = conversation
887: .doRequest("/authentication/memory/enforce2");
888: parsed = response.getParsedHtml();
889: form = parsed.getForms().get(0);
890: form.setParameter("login", "guest");
891: form.setParameter("password", "guestpass");
892: response = form.submit();
893: parsed = response.getParsedHtml();
894:
895: assertEquals("forms (after login)", 0, parsed.getForms().size());
896: assertEquals("login name (after login)", "guest", parsed
897: .getLinkWithId("userLogin").getText());
898:
899: auth_id = parsed.getTitle();
900:
901: /* And hit the unprotected page again to be sure it recognizes us. */
902: conversation = new MockConversation(builder.getSite());
903: request = new MockRequest();
904: request.setParameter("authid", auth_id);
905: response = conversation.doRequest(
906: "/authentication/memory/enforce", request);
907: parsed = response.getParsedHtml();
908:
909: assertEquals("forms (unenforced after login)", 0, parsed
910: .getForms().size());
911: assertEquals("login name (unenforced after login)", "guest",
912: parsed.getLinkWithId("userLogin").getText());
913: }
914:
915: /**
916: * Tests that we can get through an authenticated element that has its
917: * "enforce_authenticated" flag set to false.
918: */
919: public void testMemoryAuthenticatedNotEnforcedCookie()
920: throws Exception {
921: SiteBuilder builder = new SiteBuilder(
922: "site/authentication_memory_cookie.xml",
923: ResourceFinderClasspath.getInstance());
924: MockConversation conversation = new MockConversation(builder
925: .getSite());
926:
927: MockResponse response;
928: MockForm form;
929: ParsedHtml parsed;
930: String auth_id;
931:
932: /* Can we get through the page as an anonymous user? */
933: response = conversation
934: .doRequest("/authentication/memory/enforce");
935: parsed = response.getParsedHtml();
936:
937: assertEquals("forms", 0, parsed.getForms().size());
938: assertEquals("login name", "(none)", parsed.getLinkWithId(
939: "userLogin").getText());
940:
941: /* Now hit a protected page so we can get an authid cookie. */
942: response = conversation
943: .doRequest("/authentication/memory/enforce2");
944: parsed = response.getParsedHtml();
945: form = parsed.getForms().get(0);
946: form.setParameter("login", "guest");
947: form.setParameter("password", "guestpass");
948: response = form.submit();
949: parsed = response.getParsedHtml();
950:
951: assertEquals("forms (after login)", 0, parsed.getForms().size());
952: assertEquals("login name (after login)", "guest", parsed
953: .getLinkWithId("userLogin").getText());
954:
955: auth_id = conversation.getCookieValue("authid");
956:
957: /* And hit the unprotected page again to be sure it recognizes us. */
958: conversation = new MockConversation(builder.getSite());
959: conversation.addCookie("authid", auth_id);
960: response = conversation
961: .doRequest("/authentication/memory/enforce");
962: parsed = response.getParsedHtml();
963:
964: assertEquals("forms (unenforced after login)", 0, parsed
965: .getForms().size());
966: assertEquals("login name (unenforced after login)", "guest",
967: parsed.getLinkWithId("userLogin").getText());
968: }
969: }
|