File: 40.errors.t

package info (click to toggle)
libcaptcha-recaptcha-perl 0.94-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 112 kB
  • sloc: perl: 183; makefile: 2
file content (97 lines) | stat: -rw-r--r-- 2,194 bytes parent folder | download | duplicates (3)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use strict;
use warnings;
use Test::More;
use Captcha::reCAPTCHA;

use constant PUBKEY  => '6LdAAAkAwAAAFJj6ACG3Wlix_GuQJMNGjMQnw5UY';
use constant PRIVKEY => '6LdAAAkAwAAAix_GF6AMQnw5UCG3JjWluQJMNGjY';

my @schedule;

BEGIN {
  @schedule = (
    {
      name  => 'new: Bad args',
      class => 'T::Captcha::reCAPTCHA',
      try   => sub {
        my $c = Captcha::reCAPTCHA->new( PUBKEY );
      },
      expect => qr/reference to a hash/
    },
    {
      name  => 'get_html: No args',
      class => 'T::Captcha::reCAPTCHA',
      try   => sub {
        my $c = shift;
        $c->get_html();
      },
      expect => qr/To use reCAPTCHA you must get an API key from/
    },
    {
      name  => 'get_html: No key',
      class => 'T::Captcha::reCAPTCHA',
      try   => sub {
        my $c = shift;
        $c->get_html( '' );
      },
      expect => qr/To use reCAPTCHA you must get an API key from/
    },
    {
      name  => 'check_answer: No args',
      class => 'T::Captcha::reCAPTCHA',
      try   => sub {
        my $c = shift;
        $c->check_answer();
      },
      expect => qr/To use reCAPTCHA you must get an API key from/
    },
    {
      name  => 'check_answer: no ip',
      class => 'T::Captcha::reCAPTCHA',
      try   => sub {
        my $c = shift;
        $c->check_answer( PRIVKEY );
      },
      expect => qr/you must pass the remote ip/
    },
  );

  plan tests => 3 * @schedule;
}

package T::Captcha::reCAPTCHA;

our @ISA = qw(Captcha::reCAPTCHA);
use Captcha::reCAPTCHA;

sub _post_request {
  my $self = shift;
  my $url  = shift;
  my $args = shift;

  # Just keep the args
  $self->{t_url}  = $url;
  $self->{t_args} = $args;

  return HTTP::Response->new( 200, 'OK',
    [ 'Content-type:' => 'text/plain' ], "true\n" );
}

sub get_url  { shift->{t_url} }
sub get_args { shift->{t_args} }

package main;

for my $test ( @schedule ) {
  my $name  = $test->{name};
  my $class = $test->{class};
  ok my $captcha = $class->new, "$name: create OK";
  isa_ok $captcha, $class;
  eval { $test->{try}->( $captcha ); };
  if ( my $expect = $test->{expect} ) {
    like $@, $expect, "$name: error OK";
  }
  else {
    ok !$@, "$name: no error OK";
  }
}