#!/usr/bin/perl -w
our
$AUTHDB_WORKDIR
=
$ENV
{AUTHDB_WORKDIR} || getcwd();
my
$file
= File::Spec->catfile(
$AUTHDB_WORKDIR
,
'testauth.db'
);
my
$authdb
= WWW::Suffit::AuthDB->with_roles(
'+AAA'
)->new(
ds
=>
qq{sqlite://$file?RaiseError=0&PrintError=0&sqlite_unicode=1}
,
);
plan
skip_all
=>
"Please run previous tests in numeric order first"
unless
-e
$file
;
$authdb
->
connect
;
ok(!
$authdb
->error,
"Connect to database"
) or diag
$authdb
->error;
unless
(
$authdb
->model->ping) {
fail
sprintf
(
qq{Can't connect to database "%s"}
,
$authdb
->model->dsn);
diag
$authdb
->model->error;
goto
DONE;
}
subtest
'Authentication'
=>
sub
{
my
$alice
=
$authdb
->authn(
u
=>
"alice"
,
p
=>
"alice"
,
a
=>
"127.0.0.1"
);
ok
$alice
,
"Check password for alice (correct)"
or diag
$authdb
->error;
ok !
$authdb
->authn(
u
=>
"test"
,
p
=>
"123"
),
"Incorrect password"
and note
$authdb
->error;
ok !
$authdb
->authn(
u
=>
"nobody"
,
p
=>
"alice"
),
"User not found"
and note
$authdb
->error;
};
subtest
'Authentication'
=>
sub
{
my
$alice
=
$authdb
->authz(
u
=>
"alice"
);
ok
$alice
&&
$alice
->is_authorized,
"Authorized"
or diag
$authdb
->error;
my
$user
=
$authdb
->authz(
u
=>
"anon"
);
ok !(
$user
&&
$user
->is_authorized),
"Not authorized"
and note
$authdb
->error;
};
subtest
'Access'
=>
sub
{
ok(
$authdb
->access(
controller
=> Mojolicious::Controller->new,
username
=>
"alice"
,
method
=>
"GET"
,
path
=>
"/foo/bar"
,
remote_ip
=>
"127.0.0.1"
,
routename
=>
"root"
,
),
"Access granted for Alice user"
) or diag
$authdb
->error;;
};
DONE: done_testing;
$authdb
->model->disconnect;