apt source loading and tests for Packages::Apt probe
[scpubgit/System-Introspector.git] / t / users.t
1 use strictures 1;
2 use Test::More;
3 use FindBin;
4
5 use System::Introspector::Probe::Users;
6
7 my $passwd_file = "$FindBin::Bin/test-passwd";
8 my $home = "$FindBin::Bin/data/home-testfoo";
9 my $nohome = "$FindBin::Bin/data/home-doesnotexist";
10
11 open my $passwd_fh, '>', $passwd_file
12     or die "Unable to write $passwd_file: $!\n";
13 printf $passwd_fh "%s\n", join ':', @$_
14     for [qw( testfoo x 23 42 comment ), $home, '/bin/false'],
15         [qw( testbar x 24 43 comment ), $nohome, '/bin/false'];
16 close $passwd_fh;
17
18 local $ENV{PATH} = join ':',
19     "$FindBin::Bin/bin",
20     $ENV{PATH};
21
22 my $probe = System::Introspector::Probe::Users->new(
23     passwd_file => $passwd_file,
24 );
25 my $data = $probe->gather;
26
27 do {
28     my $user = $data->{users}{testfoo};
29     ok $user, 'found first user';
30
31     my $keys = $user->{ssh}{keys};
32     ok $keys, 'found ssh keys structure';
33
34     is_deeply $keys, {
35         files => {
36             'first.pub' => {
37                 file_name => "$home/.ssh/first.pub",
38                 body => "pubkey\n",
39             },
40         },
41         authorized => {
42             file_name => "$home/.ssh/authorized_keys",
43             body => "keyA\nkeyB\n",
44         },
45     }, 'ssh key data';
46
47     is_deeply $user->{groups}, {
48         list => [qw( testfoo_group_A testfoo_group_B testfoo_group_C )],
49     }, 'groups list';
50
51     my $tab = $user->{crontab};
52     ok $tab, 'got crontab results';
53     like $tab->{body}, qr{-u\s*testfoo}, 'crontab called with user option';
54     like $tab->{body}, qr{-l}, 'crontab asked for list';
55 };
56
57 do {
58     my $user = $data->{users}{testbar};
59     ok $user, 'found second user';
60
61     my $keys = $user->{ssh}{keys};
62     ok $keys, 'found ssh keys structure';
63
64     is_deeply $keys, {
65         files => {},
66         authorized => {
67             file_name => "$nohome/.ssh/authorized_keys",
68             body => '',
69         },
70     }, 'ssh key data';
71 };
72
73 #ok((my $user = $data->{users}{ +getlogin }), 'found own user');
74 #ok(defined($user->{ $_ }), "$_ is defined")
75 #    for qw( comment crontab gid groups home shell ssh uid username );
76 #ok(not(exists $user->{crontab}{error}), 'no crontab error');
77 #is($user->{ssh}{keys}{error}, undef, 'no ssh keys error');
78
79
80 unlink $passwd_file;
81 done_testing;