File: 21.check_answer_v2.t

package info (click to toggle)
libcaptcha-recaptcha-perl 0.98%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 176 kB
  • sloc: perl: 274; makefile: 2
file content (81 lines) | stat: -rw-r--r-- 2,181 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use strict;
use warnings;
use Test::More;

use lib 't/lib';
use Test::TCaptcha;
use Data::Dumper;

# Looks real
use constant PRIVKEY => '6LdAAAkAwAAAix_GF6AMQnw5UCG3JjWluQJMNGjY';

note "Create object";
ok my $captcha = Test::TCaptcha->new(), "Captcha::reCAPTCHA: Created OK";

note "croaks on no response";
eval { $captcha->check_answer_v2() };
ok $@ =~ /To use reCAPTCHA you must get an API key from/, "Breaks on no arguments";

eval { $captcha->check_answer_v2( PRIVKEY ) };
ok $@ =~ /To check answer, the user response token must be provided/, "Breaks on no response arg";

my $result = eval { $captcha->check_answer_v2( PRIVKEY, 'fakeresponse' ) };
ok $result->{is_valid} eq '0', "Google Say's the response is invalid";


my @schedule;

BEGIN {

  # Looks real. Isn't.
  @schedule = (
    {
      name => 'Simple correct',
      args =>
       [ PRIVKEY, 'abcdefghijklmnopqrstuv', '192.168.0.1' ],
      response   => '"success": true,',
      check_args => {
        privatekey => PRIVKEY,
        remoteip   => '192.168.0.1',
        response   => '..response..'
      },
      check_url => 'https://www.google.com/recaptcha/api/siteverify',
      expect    => { is_valid => 1 },
    },
    {
      name => 'Simple incorrect',
      args =>
       [ PRIVKEY, 'response', '192.168.0.1' ],
      response   => "incorrect-captcha-sol",
      check_args => {
        privatekey => PRIVKEY,
        remoteip   => '192.168.0.1',
        response   => '..response..'
      },
      check_url => 'https://www.google.com/recaptcha/api/siteverify',
      expect    => { is_valid => 0, error => 'incorrect-captcha-sol' },
    },
  );
  plan tests => 6 * @schedule;
}


for my $test ( @schedule ) {
  my $name = $test->{name};

  ok my $captcha = Test::TCaptcha->new(), "$name: Created OK";

  isa_ok $captcha, 'Captcha::reCAPTCHA';

  $captcha->set_response( $test->{response} );

  ok my $resp = $captcha->check_answer_v2( @{ $test->{args} } ), "$name: got response";

  unless ( is_deeply $resp, $test->{expect}, "$name: result OK" ) {
    diag( Data::Dumper->Dump( [ $test->{expect} ], ['$expected'] ) );
    diag( Data::Dumper->Dump( [$resp], ['$got'] ) );
  }
}


done_testing();