lemonldap-ng/lemonldap-ng-portal/t/36-Combination.t

122 lines
3.7 KiB
Perl
Raw Normal View History

2017-02-05 14:11:14 +01:00
use Test::More;
use strict;
use IO::String;
require 't/test-lib.pm';
my $res;
my $maintests = 3;
2017-02-05 18:05:33 +01:00
my $client;
2017-02-05 14:11:14 +01:00
2019-08-29 10:04:06 +02:00
my $userdb = tempdb();
2017-02-05 14:11:14 +01:00
SKIP: {
eval { require DBI; require DBD::SQLite; };
if ($@) {
2018-02-08 21:55:21 +01:00
skip 'DBD::SQLite not found', $maintests;
2017-02-05 14:11:14 +01:00
}
2019-08-29 10:04:06 +02:00
my $dbh = DBI->connect("dbi:SQLite:dbname=$userdb");
2017-02-05 14:11:14 +01:00
$dbh->do('CREATE TABLE users (user text,password text,name text)');
2017-02-05 22:12:06 +01:00
$dbh->do("INSERT INTO users VALUES ('dvador','dvador','Test user 1')");
$dbh->do("INSERT INTO users VALUES ('rtyler','rtyler','Test user 1')");
2017-02-05 14:11:14 +01:00
2017-02-05 18:05:33 +01:00
$client = iniCmb('[Dm] or [DB]');
2020-04-23 15:36:48 +02:00
$client->logout( expectCookie( try('dwho') ) );
2017-02-05 22:12:06 +01:00
expectCookie( try('dvador') );
2017-02-05 18:05:33 +01:00
2017-02-05 22:12:06 +01:00
$client = iniCmb('[Dm] and [DB]');
2020-04-23 15:36:48 +02:00
$client->logout( expectCookie( try('rtyler') ) );
2017-02-05 22:12:06 +01:00
expectReject( try('dwho') );
$client = iniCmb('if($env->{HTTP_X} eq "dwho") then [Dm] else [DB]');
2020-04-23 15:36:48 +02:00
$client->logout( expectCookie( try('dwho') ) );
$client->logout( expectCookie( try('dvador') ) );
2017-02-05 22:12:06 +01:00
$client = iniCmb(
'if($env->{HTTP_X} eq "rtyler") then [Dm] and [DB] else if($env->{HTTP_X} eq "dvador") then [DB] else [DB]'
);
my $id = expectCookie( try('rtyler') );
my $res;
ok( $res = $client->_get("/sessions/global/$id"), 'Get session content' );
ok( $res = eval { JSON::from_json( $res->[2]->[0] ) }, ' GET JSON' )
or print STDERR $@;
ok(
( $res->{demo} eq 'rtyler' and $res->{dbi} eq 'rtyler' ),
2019-06-20 21:27:49 +02:00
' Demo and DBI exported variables exist in session'
);
2017-02-05 22:12:06 +01:00
expectCookie( try('dvador') );
expectReject( try('dwho') );
2020-04-21 21:17:44 +02:00
$client = iniCmb(
'if($env->{REMOTE_ADDR} =~ /^(127\.)/) then [Dm] or [DB] else [DB]');
expectCookie( try('rtyler') );
expectCookie( try('dwho') );
$client = iniCmb(
'if($env->{REMOTE_ADDR} =~ /^(128\.)/) then [Dm,Dm] or [DB,DB] else [DB,DB]'
);
expectCookie( try('rtyler') );
expectReject( try('dwho') );
2017-02-05 18:05:33 +01:00
}
2018-02-08 21:55:21 +01:00
count($maintests);
2017-02-05 18:05:33 +01:00
clean_sessions();
done_testing( count() );
sub try {
my $user = shift;
my $s = "user=$user&password=$user";
my $res;
ok(
2017-02-05 22:12:06 +01:00
$res = $client->_post(
'/', IO::String->new($s),
length => length($s),
custom => { HTTP_X => $user }
),
" Try to connect with login $user"
2017-02-05 18:05:33 +01:00
);
count(1);
return $res;
}
sub iniCmb {
my $expr = shift;
&Lemonldap::NG::Handler::Main::cfgNum( 0, 0 );
2017-02-05 22:12:06 +01:00
if (
2019-02-07 09:27:56 +01:00
my $res = LLNG::Manager::Test->new( {
2017-02-05 22:12:06 +01:00
ini => {
logLevel => 'error',
useSafeJail => 1,
authentication => 'Combination',
userDB => 'Same',
restSessionServer => 1,
2017-02-05 14:11:14 +01:00
2017-02-05 22:12:06 +01:00
combination => $expr,
2017-02-06 13:36:27 +01:00
combModules => {
DB => {
2017-02-05 22:12:06 +01:00
for => 0,
type => 'DBI',
},
2017-02-06 13:36:27 +01:00
Dm => {
2017-02-05 22:12:06 +01:00
for => 0,
type => 'Demo',
},
2017-02-06 13:36:27 +01:00
},
2017-02-05 14:11:14 +01:00
2019-08-29 10:04:06 +02:00
dbiAuthChain => "dbi:SQLite:dbname=$userdb",
2017-02-05 22:12:06 +01:00
dbiAuthUser => '',
dbiAuthPassword => '',
dbiAuthTable => 'users',
dbiAuthLoginCol => 'user',
dbiAuthPasswordCol => 'password',
dbiAuthPasswordHash => '',
2022-02-16 17:43:29 +01:00
dbiExportedVars => { dbi => 'user' },
demoExportedVars => { demo => 'uid' },
2017-02-05 22:12:06 +01:00
}
2017-02-05 14:11:14 +01:00
}
2017-02-05 22:12:06 +01:00
)
)
{
pass(qq'Expression loaded: "$expr"');
count(1);
return $res;
}
2017-02-05 14:11:14 +01:00
}