1use strict;
2use warnings;
3use Test::More;
4use Captcha::reCAPTCHA;
5
6# Looks real. Isn't.
7use constant PUBKEY => '6LdAAAkAwAAAFJj6ACG3Wlix_GuQJMNGjMQnw5UY';
8
9my @schedule;
10
11BEGIN {
12  my $pubkey = PUBKEY;
13
14  @schedule = (
15    {
16      name => 'Simple',
17      line => __LINE__,
18      args => [$pubkey],
19      expect =>
20       qq{<script src="http://www.google.com/recaptcha/api/challenge?k=$pubkey" }
21       . qq{type="text/javascript"></script>\n}
22       . qq{<noscript><iframe frameborder="0" height="300" }
23       . qq{src="http://www.google.com/recaptcha/api/noscript?k=$pubkey" }
24       . qq{width="500"></iframe><textarea cols="40" name="recaptcha_challenge_field" }
25       . qq{rows="3"></textarea><input name="recaptcha_response_field" type="hidden" }
26       . qq{value="manual_challenge" /></noscript>\n}
27    },
28    {
29      name => 'Error',
30      line => __LINE__,
31      args => [ $pubkey, '<<some random error>>' ],
32      expect =>
33       qq{<script src="http://www.google.com/recaptcha/api/challenge?error=%3c%3csome+random+error%3e%3e&amp;k=$pubkey" }
34       . qq{type="text/javascript"></script>\n}
35       . qq{<noscript><iframe frameborder="0" height="300" }
36       . qq{src="http://www.google.com/recaptcha/api/noscript?error=%3c%3csome+random+error%3e%3e&amp;k=$pubkey" }
37       . qq{width="500"></iframe><textarea cols="40" name="recaptcha_challenge_field" }
38       . qq{rows="3"></textarea><input name="recaptcha_response_field" type="hidden" }
39       . qq{value="manual_challenge" /></noscript>\n}
40    },
41    {
42      name => 'Error in hash',
43      line => __LINE__,
44      args =>
45       [ $pubkey, { is_valid => 0, error => '<<some random error>>' } ],
46      expect =>
47       qq{<script src="http://www.google.com/recaptcha/api/challenge?error=%3c%3csome+random+error%3e%3e&amp;k=$pubkey" }
48       . qq{type="text/javascript"></script>\n}
49       . qq{<noscript><iframe frameborder="0" height="300" }
50       . qq{src="http://www.google.com/recaptcha/api/noscript?error=%3c%3csome+random+error%3e%3e&amp;k=$pubkey" }
51       . qq{width="500"></iframe><textarea cols="40" name="recaptcha_challenge_field" }
52       . qq{rows="3"></textarea><input name="recaptcha_response_field" type="hidden" }
53       . qq{value="manual_challenge" /></noscript>\n}
54    },
55    {
56      name => 'Secure',
57      line => __LINE__,
58      args => [ $pubkey, undef, 1 ],
59      expect =>
60       qq{<script src="https://www.google.com/recaptcha/api/challenge?k=$pubkey" }
61       . qq{type="text/javascript"></script>\n}
62       . qq{<noscript><iframe frameborder="0" height="300" }
63       . qq{src="https://www.google.com/recaptcha/api/noscript?k=$pubkey" }
64       . qq{width="500"></iframe><textarea cols="40" name="recaptcha_challenge_field" }
65       . qq{rows="3"></textarea><input name="recaptcha_response_field" type="hidden" }
66       . qq{value="manual_challenge" /></noscript>\n}
67    },
68    {
69      name => 'Options',
70      line => __LINE__,
71      args =>
72       [ $pubkey, undef, 0, { theme => 'white', tabindex => 3 } ],
73      expect =>
74       qq(<script type="text/javascript">\n//<![CDATA[\nvar RecaptchaOptions = )
75       . qq({"tabindex":3,"theme":"white"};\n//]]>\n</script>\n)
76       . qq{<script src="http://www.google.com/recaptcha/api/challenge?k=$pubkey" }
77       . qq{type="text/javascript"></script>\n}
78       . qq{<noscript><iframe frameborder="0" height="300" }
79       . qq{src="http://www.google.com/recaptcha/api/noscript?k=$pubkey" }
80       . qq{width="500"></iframe><textarea cols="40" name="recaptcha_challenge_field" }
81       . qq{rows="3"></textarea><input name="recaptcha_response_field" type="hidden" }
82       . qq{value="manual_challenge" /></noscript>\n}
83    },
84    {
85       name => 'Use old verion',
86       line => __LINE__,
87       args =>
88        [ $pubkey, undef, 0, { theme => 'white', tabindex => 3 }, 1 ],
89       expect =>
90        qq(<script type="text/javascript">\n//<![CDATA[\nvar RecaptchaOptions = )
91        . qq({"tabindex":3,"theme":"white"};\n//]]>\n</script>\n)
92        . qq{<script src="http://www.google.com/recaptcha/api/challenge?k=$pubkey" }
93        . qq{type="text/javascript"></script>\n}
94        . qq{<noscript><iframe frameborder="0" height="300" }
95        . qq{src="http://www.google.com/recaptcha/api/noscript?k=$pubkey" }
96        . qq{width="500"></iframe><textarea cols="40" name="recaptcha_challenge_field" }
97        . qq{rows="3"></textarea><input name="recaptcha_response_field" type="hidden" }
98        . qq{value="manual_challenge" /></noscript>\n}
99     },
100  );
101  plan tests => 3 * @schedule;
102}
103
104for my $test ( @schedule ) {
105  my $name = $test->{name};
106  ok my $captcha = Captcha::reCAPTCHA->new(), "$name: Created OK at line " . $test->{line};
107  isa_ok $captcha, 'Captcha::reCAPTCHA';
108  my $args = $test->{args};
109  my $html = $captcha->get_html( @$args );
110  is $html, $test->{expect}, "$name: Generate HTML OK "  . $test->{line};
111}
112