Debian Codegraph @

lemonldap-ng-manager/t/60-Cli-JSON.t

raw
1 use Test::More;
2 use Test::Output;
3 use File::Path;
4 use File::Temp;
5 use JSON;
6
7 BEGIN {
8 use_ok 'Lemonldap::NG::Common::Conf';
9 }
10
11 use Lemonldap::NG::Common::Cli;
12 use Lemonldap::NG::Manager::Cli;
13 use Lemonldap::NG::Manager::Conf::Zero;
14
15 my $conf;
16 my $dir = File::Temp::tempdir( CLEANUP => 1 );
17 ok( $conf = &Lemonldap::NG::Manager::Conf::Zero::zeroConf(), 'Build new conf' );
18 $conf->{cfgNum} = 1;
19 my $confAcc = new Lemonldap::NG::Common::Conf( {
20 type => 'File',
21 dirName => $dir,
22 }
23 );
24 ok( $confAcc->store($conf) == 1, "Conf is stored" );
25
26 open my $f, '>', "$dir/lemonldap-ng.ini" or die $!;
27 my $ini = <<"EOF";
28 [ all ]
29 logLevel = debug
30
31 [ configuration ]
32 type = File
33 dirName = $dir
34
35 [ manager ]
36 enabledModules = conf
37 templateDir = $dir
38 EOF
39 print $f $ini;
40 close $f;
41
42 sub runTest {
43 my $type = shift;
44 my $name = shift;
45 my $test = shift;
46 my @ARGS = @_;
47
48 #Lemonldap::NG::Manager::Cli->new(
49 #{ iniFile => "$dir/lemonldap-ng.ini" } )->run(@ARGS);
50 my ($str) = Test::Output::output_from(
51 sub {
52 $type eq 'manager'
53 ? Lemonldap::NG::Manager::Cli->new(
54 { iniFile => "$dir/lemonldap-ng.ini" } )->run(@ARGS)
55 : Lemonldap::NG::Common::Cli->new(
56 { iniFile => "$dir/lemonldap-ng.ini" } )->run(@ARGS);
57 }
58 );
59 chomp $str;
60 my $json;
61 ok( $json = eval { JSON::from_json("[$str]")->[0] }, 'Valid JSON response' )
62 or diag($@);
63 $test->($json);
64 ok( $test->($json), "$name result matches" ) or diag "$str";
65 }
66
67 subtest 'Conf: simple string', sub {
68 runTest( 'manager', 'portal', sub { $_[0] eq 'http://auth.example.com/' },
69 '-json', 'get', 'portal' );
70 };
71 subtest 'Conf: hash ref', sub {
72 runTest( 'manager', 'globalStorageOptions',
73 sub { $_[0]->{Directory} eq '/var/lib/lemonldap-ng/sessions' },
74 '-json', 'get', 'globalStorageOptions' );
75 };
76 subtest 'Conf: metadata', sub {
77 runTest(
78 'common', 'info',
79 sub {
80 $_[0]->{author} eq 'The LemonLDAP::NG team';
81 }, #qr#"author"\s*:\s*"The LemonLDAP::NG team"#,
82 '-json', 'info',
83 );
84 };
85
86 done_testing();