diff --git a/lemonldap-ng-portal/t/41-Captcha-old-api.t b/lemonldap-ng-portal/t/41-Captcha-old-api.t new file mode 100644 index 000000000..96894bb32 --- /dev/null +++ b/lemonldap-ng-portal/t/41-Captcha-old-api.t @@ -0,0 +1,114 @@ +use Test::More; +use strict; +use IO::String; +use JSON; +use Lemonldap::NG::Portal::Main::Constants 'PE_CAPTCHAEMPTY'; + +require 't/test-lib.pm'; + +my $res; + +my $maintests = 0; +SKIP: { + eval 'use GD::SecurityImage; use Image::Magick;'; + if ($@) { + skip 'Image::Magick not found', $maintests; + } + + my $client = LLNG::Manager::Test->new( { + ini => { + logLevel => 'error', + portalMainLogo => 'common/logos/logo_llng_old.png', + customPlugins => 't::CaptchaOldApi', + } + } + ); + my ( $data, $json ); + + # check setCaptcha + $data = ''; + $json = expectJSON( + $client->_post( + '/setCaptcha', + IO::String->new($data), + length => length($data), + ) + ); + like( $json->{token}, qr/.+/ ); + like( $json->{img}, qr#^data:image/png;base64,.{10}# ); + like( $json->{answer}, qr#^\d{6}$# ); + count(3); + + # check getCaptcha + $data = ''; + $json = expectJSON( + $client->_post( + '/getCaptcha', + IO::String->new($data), + length => length($data), + ) + ); + like( $json->{token}, qr/.+/ ); + like( $json->{img}, qr#^data:image/png;base64,.{10}# ); + like( $json->{answer}, qr#^\d{6}$# ); + count(3); + + my $token = $json->{token}; + my $answer = $json->{answer}; + + # validate: wrong token + $data = buildForm( { token => 111, answer => $answer } ); + $json = expectJSON( + $client->_post( + '/validateCaptcha', IO::String->new($data), + length => length($data), + ) + ); + is( $json->{result}, 0, 'Wrong token failed' ); + count(1); + + # validate: wrong answer + $data = buildForm( { token => $token, answer => 999 } ); + $json = expectJSON( + $client->_post( + '/validateCaptcha', IO::String->new($data), + length => length($data), + ) + ); + is( $json->{result}, 0, 'Wrong captcha failed' ); + count(1); + + # Get Fresh token/answer pair + $data = ''; + $json = expectJSON( + $client->_post( + '/getCaptcha', + IO::String->new($data), + length => length($data), + ) + ); + like( $json->{token}, qr/.+/ ); + like( $json->{img}, qr#^data:image/png;base64,.{10}# ); + like( $json->{answer}, qr#^\d{6}$# ); + count(3); + + $token = $json->{token}; + $answer = $json->{answer}; + + # validate: correct values + $data = buildForm( { token => $token, answer => $answer } ); + $json = expectJSON( + $client->_post( + '/validateCaptcha', IO::String->new($data), + length => length($data), + ) + ); + is( $json->{result}, 1, 'Captcha successfully verified' ); + count(1); + +} +count($maintests); + +clean_sessions(); + +done_testing( count() ); diff --git a/lemonldap-ng-portal/t/CaptchaOldApi.pm b/lemonldap-ng-portal/t/CaptchaOldApi.pm new file mode 100644 index 000000000..a1637e657 --- /dev/null +++ b/lemonldap-ng-portal/t/CaptchaOldApi.pm @@ -0,0 +1,57 @@ +package t::CaptchaOldApi; + +use Mouse; +use Lemonldap::NG::Portal::Main::Constants; +extends 'Lemonldap::NG::Portal::Main::Plugin'; + +has 'captcha' => ( is => 'rw' ); + +sub init { + my $self = shift; + + $self->addUnauthRoute( validateCaptcha => 'validateCaptcha', ['POST'] ); + $self->addUnauthRoute( setCaptcha => 'setCaptcha', ['POST'] ); + $self->addUnauthRoute( getCaptcha => 'getCaptcha', ['POST'] ); + $self->captcha( $self->p->loadModule('::Lib::Captcha') ); + return 1; +} + +sub setCaptcha { + my ( $self, $req ) = @_; + + $self->captcha->setCaptcha($req); + + my $info = $self->captcha->ott->getToken( $req->token, 1 ); + + return $self->sendJSONresponse( + $req, + { + token => $req->token, + img => $req->captcha, + answer => $info->{captcha} + } + ); +} + +sub getCaptcha { + my ( $self, $req ) = @_; + + my ( $token, $image ) = $self->captcha->getCaptcha; + my $info = $self->captcha->ott->getToken( $token, 1 ); + + return $self->sendJSONresponse( $req, + { token => $token, img => $image, answer => $info->{captcha} } ); +} + +sub validateCaptcha { + my ( $self, $req ) = @_; + my $token = $req->param('token'); + my $answer = $req->param('answer'); + + my $result = $self->captcha->validateCaptcha( $token, $answer ); + + return $self->sendJSONresponse( $req, { result => $result } ); + +} + +1;