Correctly report the calling location in test functions

With this change, failure in a test lib function (expectForm,
expectJWT...) will be reported at the calling location in the original
*.t test file, instead of being reported in the test lib

    t/30-SAML-ReAuth-with-choice.t .. 1/?
    #   Failed test ' URI match'
>>> #   at t/30-SAML-ReAuth-with-choice.t line 72.
This commit is contained in:
Maxime Besson 2022-07-12 16:14:56 +02:00
parent a62abf7bc3
commit 5a47d7e9bc
3 changed files with 24 additions and 0 deletions

View File

@ -48,6 +48,7 @@ sub id_token_payload {
}
sub login {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ( $op, $uid ) = @_;
my $res;
my $query = buildForm( {
@ -65,6 +66,7 @@ sub login {
}
sub authorize {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ( $op, $id, $params ) = @_;
my $query = buildForm($params);
my $res = $op->_get(
@ -153,6 +155,7 @@ sub introspect {
}
sub expectJWT {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ( $token, %claims ) = @_;
my $payload = getJWTPayload($token);
ok( $payload, "Token is a JWT" );

View File

@ -1363,6 +1363,7 @@ Match a XPath expression against the provided string, and verify that the correc
=cut
sub expectXPath {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ( $xml_string, $xpath, $value, $message ) = @_;
my $dom = XML::LibXML->load_xml( string => $xml_string );
return unless ok( $dom, 'XML successfully parsed' );
@ -1399,6 +1400,7 @@ sub expectXPath {
}
sub expectSamlRequest {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ($string) = @_;
my ($sr) = $string =~ m/SAMLRequest=([^&]*)/;
ok( $sr, "Found SAMLRequest" );
@ -1406,6 +1408,7 @@ sub expectSamlRequest {
}
sub expectSamlResponse {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ($string) = @_;
my ($sr) = $string =~ m/SAMLResponse=([^&]*)/;
ok( $sr, "Found SAMLResponse" );

View File

@ -238,6 +238,7 @@ matching strings are returned. Example:
=cut
sub expectRedirection {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ( $res, $location ) = @_;
ok( $res->[0] == 302, ' Get redirection' )
or explain( $res->[0], 302 );
@ -266,6 +267,8 @@ TODO: verify javascript
=cut
sub expectAutoPost {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my @r = expectForm(@_);
my $method = pop @r;
ok( $method =~ /^post$/i, ' Method is POST' ) or explain( $method, 'POST' );
@ -297,6 +300,7 @@ in $query
=cut
sub expectForm {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ( $res, $hostRe, $uriRe, @requiredFields ) = @_;
expectOK($res);
count(1);
@ -372,6 +376,7 @@ Verify that result has a C<Lm-Remote-User> header and value is $user
=cut
sub expectAuthenticatedAs {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ( $res, $user ) = @_;
is( getHeader( $res, 'Lm-Remote-User' ), $user, "Authenticated as $user" );
count(1);
@ -384,6 +389,7 @@ Verify that the session contains attributes with these values
=cut
sub expectSessionAttributes {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ( $app, $id, %attributes ) = @_;
my $res;
ok(
@ -409,6 +415,7 @@ Verify that returned code is 200
=cut
sub expectOK {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ($res) = @_;
ok( $res->[0] == 200, ' HTTP code is 200' ) or explain( $res, 200 );
count(1);
@ -421,6 +428,7 @@ Verify that the HTTP response contains valid JSON and returns the corresponding
=cut
sub expectJSON {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ($res) = @_;
is( $res->[0], 200, ' HTTP code is 200' ) or explain( $res, 200 );
my %hdr = @{ $res->[1] };
@ -441,6 +449,7 @@ Verify that returned code is 403.
=cut
sub expectForbidden {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ($res) = @_;
ok( $res->[0] == 403, ' HTTP code is 403' ) or explain( $res->[0], 403 );
count(1);
@ -454,6 +463,7 @@ Verify that returned code is 400. Note that it works only for Ajax request
=cut
sub expectBadRequest {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ($res) = @_;
ok( $res->[0] == 400, ' HTTP code is 400' ) or explain( $res->[0], 400 );
count(1);
@ -465,6 +475,7 @@ Verify that an error is displayed on the portal
=cut
sub expectPortalError {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ( $res, $errnum, $message ) = @_;
$errnum ||= 9;
like( $res->[2]->[0], qr/<span trmsg="$errnum">/, $message );
@ -479,6 +490,7 @@ C<error:"$code">. Note that it works only for Ajax request (see below).
=cut
sub expectReject {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ( $res, $status, $code ) = @_;
$status ||= 401;
cmp_ok( $res->[0], '==', $status, " Response status is $status" );
@ -502,6 +514,7 @@ its value.
=cut
sub expectCookie {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ( $res, $cookieName ) = @_;
$cookieName ||= 'lemonldap';
my $cookies = getCookies($res);
@ -521,6 +534,7 @@ Check if the pdata cookie exists and returns its deserialized value.
=cut
sub expectPdata {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ($res) = @_;
my $val = expectCookie( $res, "lemonldappdata" );
ok( $val, "Pdata is not empty" );
@ -538,6 +552,7 @@ Verify that C<Content-Security-Policy> header allows one to connect to $host.
=cut
sub exceptCspFormOK {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ( $res, $host ) = @_;
return 1 unless ($host);
my $csp = getHeader( $res, 'Content-Security-Policy' );
@ -560,6 +575,7 @@ sub exceptCspFormOK {
}
sub expectCspChildOK {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ( $res, $host ) = @_;
return 1 unless ($host);
my $csp = getHeader( $res, 'Content-Security-Policy' );
@ -650,6 +666,7 @@ Registers a new LLNG instance
=cut
sub register {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ( $type, $constructor ) = @_;
my $obj;
@Lemonldap::NG::Handler::Main::_onReload = ();
@ -873,6 +890,7 @@ Launch a C</?logout=1> request an test:
=cut
sub logout {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ( $self, $id, $cookieName ) = @_;
my $res;
$cookieName ||= 'lemonldap';