1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.captcha;
17
18 import org.acegisecurity.context.SecurityContextImplTests;
19
20
21 /***
22 * Tests {@link CaptchaSecurityContextImpl}.
23 *
24 * @author marc antoine Garrigue
25 * @version $Id: CaptchaSecurityContextImplTests.java,v 1.4 2005/11/17 00:56:08 benalex Exp $
26 */
27 public class CaptchaSecurityContextImplTests extends SecurityContextImplTests {
28
29
30 public void testDefaultValues() {
31 CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
32 assertEquals("should not be human", false, context.isHuman());
33 assertEquals("should be 0", 0,
34 context.getLastPassedCaptchaDateInMillis());
35 assertEquals("should be 0", 0,
36 context.getHumanRestrictedResourcesRequestsCount());
37 }
38
39 public void testIncrementRequests() {
40 CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
41 context.setHuman();
42 assertEquals("should be human", true, context.isHuman());
43 assertEquals("should be 0", 0,
44 context.getHumanRestrictedResourcesRequestsCount());
45 context.incrementHumanRestrictedRessoucesRequestsCount();
46 assertEquals("should be 1", 1,
47 context.getHumanRestrictedResourcesRequestsCount());
48 }
49
50 public void testResetHuman() {
51 CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
52 context.setHuman();
53 assertEquals("should be human", true, context.isHuman());
54 assertEquals("should be 0", 0,
55 context.getHumanRestrictedResourcesRequestsCount());
56 context.incrementHumanRestrictedRessoucesRequestsCount();
57 assertEquals("should be 1", 1,
58 context.getHumanRestrictedResourcesRequestsCount());
59
60 long now = System.currentTimeMillis();
61 context.setHuman();
62 assertEquals("should be 0", 0,
63 context.getHumanRestrictedResourcesRequestsCount());
64 assertTrue("should be more than 0",
65 (context.getLastPassedCaptchaDateInMillis() - now) >= 0);
66 assertTrue("should be less than 0,1 seconde",
67 (context.getLastPassedCaptchaDateInMillis() - now) < 100);
68 }
69
70 public void testSetHuman() {
71 CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
72 long now = System.currentTimeMillis();
73 context.setHuman();
74 assertEquals("should be human", true, context.isHuman());
75 assertTrue("should be more than 0",
76 (context.getLastPassedCaptchaDateInMillis() - now) >= 0);
77 assertTrue("should be less than 0,1 seconde",
78 (context.getLastPassedCaptchaDateInMillis() - now) < 100);
79 assertEquals("should be 0", 0,
80 context.getHumanRestrictedResourcesRequestsCount());
81 }
82 }