1use strict;
2use warnings;
3use Test::More;
4
5use lib 't/lib';
6use Test::TCaptcha;
7use Data::Dumper;
8
9# Looks real
10use constant PRIVKEY => '6LdAAAkAwAAAix_GF6AMQnw5UCG3JjWluQJMNGjY';
11
12note "Create object";
13ok my $captcha = Test::TCaptcha->new(), "Captcha::reCAPTCHA: Created OK";
14
15note "croaks on no response";
16eval { $captcha->check_answer_v2() };
17ok $@ =~ /To use reCAPTCHA you must get an API key from/, "Breaks on no arguments";
18
19eval { $captcha->check_answer_v2( PRIVKEY ) };
20ok $@ =~ /To check answer, the user response token must be provided/, "Breaks on no response arg";
21
22$captcha->set_response("\"success\": false");
23my $result = eval { $captcha->check_answer_v2( PRIVKEY, 'fakeresponse' ) };
24ok $result->{is_valid} eq '0', "Google Say's the response is invalid";
25
26
27my @schedule;
28
29BEGIN {
30
31  # Looks real. Isn't.
32  @schedule = (
33    {
34      name => 'Simple correct',
35      args =>
36       [ PRIVKEY, 'abcdefghijklmnopqrstuv', '192.168.0.1' ],
37      response   => '"success": true,',
38      check_args => {
39        privatekey => PRIVKEY,
40        remoteip   => '192.168.0.1',
41        response   => '..response..'
42      },
43      check_url => 'https://www.google.com/recaptcha/api/siteverify',
44      expect    => { is_valid => 1 },
45    },
46    {
47      name => 'Simple incorrect',
48      args =>
49       [ PRIVKEY, 'response', '192.168.0.1' ],
50      response   => "incorrect-captcha-sol",
51      check_args => {
52        privatekey => PRIVKEY,
53        remoteip   => '192.168.0.1',
54        response   => '..response..'
55      },
56      check_url => 'https://www.google.com/recaptcha/api/siteverify',
57      expect    => { is_valid => 0, error => 'incorrect-captcha-sol' },
58    },
59  );
60  plan tests => 6 * @schedule;
61}
62
63
64for my $test ( @schedule ) {
65  my $name = $test->{name};
66
67  ok my $captcha = Test::TCaptcha->new(), "$name: Created OK";
68
69  isa_ok $captcha, 'Captcha::reCAPTCHA';
70
71  $captcha->set_response( $test->{response} );
72
73  ok my $resp = $captcha->check_answer_v2( @{ $test->{args} } ), "$name: got response";
74
75  unless ( is_deeply $resp, $test->{expect}, "$name: result OK" ) {
76    diag( Data::Dumper->Dump( [ $test->{expect} ], ['$expected'] ) );
77    diag( Data::Dumper->Dump( [$resp], ['$got'] ) );
78  }
79}
80
81
82done_testing();
83