File Coverage: t/bin-user-tools.t

# File Coverage

File: | t/bin-user-tools.t
Coverage: | 92.3%

line | stmt | bran | cond | sub | pod | time | code
--- | --- | --- | --- | --- | --- | --- | ---
1 |  |  |  |  |  |  | #!/usr/bin/env perl
2 | 1 1 |  |  | 1 |  | 1840 3 | use 5.12.1;
3 |
4 | 1 1 1 |  |  | 1 |  | 512 454 5 | use lib 't/lib';
5 | 1 1 1 |  |  | 1 |  | 526 4 2 | use Yukki::Test;
6 |
7 | 1 1 1 |  |  | 1 |  | 322 1 22 | use Path::Tiny qw( tempdir );
8 | 1 1 1 |  |  | 1 |  | 3 1 3 | use Test2::V0;
9 | 1 1 1 |  |  | 1 |  | 1698 7329 4 | use Test::Script;
10 | 1 1 1 |  |  | 1 |  | 243 1 34 | use Try::Tiny;
11 | 1 1 1 |  |  | 1 |  | 216 3078 167 | use YAML qw( LoadFile );
12 |
13 | 1 | 50 |  |  |  | 88707 | $File::Temp::KEEP_ALL = 1 if $ENV{YUKKI_TEST_KEEP_FILES};
14 |
15 | 1 |  |  |  |  | 3 | script_compiles('bin/yukki-setup', 'yukki-setup compiles');
16 | 1 |  |  |  |  | 394397 | script_compiles('bin/yukki-add-user', 'yukki-add-user compiles');
17 | 1 |  |  |  |  | 244804 | script_compiles('bin/yukki-passwd', 'yukki-passwd compiles');
18 |
19 | 1 |  |  |  |  | 261559 | my $tempdir = tempdir;
20 | 1 | 50 |  |  |  | 678 | diag("TEMPDIR = $tempdir") if $ENV{YUKKI_TEST_KEEP_FILES};
21 |
22 | 1 |  |  |  |  | 4 | script_runs([ 'bin/yukki-setup', "$tempdir/yukki-test", 'skel' ],
23 |  |  |  |  |  |  | 'yukki-setup runs');
24 |
25 | 1 |  |  |  |  | 456063 | ok(-d "$tempdir/yukki-test", 'created the test directory');
26 | 1 |  |  |  |  | 176 | ok(!-f "$tempdir/yukki-test/var/db/users/foo",
27 |  |  |  |  |  |  | 'the user we are about to create does not exist yet');
28 |
29 | 1 |  |  |  |  | 180 | $ENV{YUKKI_CONFIG} = "$tempdir/yukki-test/etc/yukki.conf";
30 |
31 |  |  |  |  |  |  | try {
32 | 1 |  |  | 1 |  | 65 | yukki('add-user', qq[foo
33 |  |  |  |  |  |  | secret
34 |  |  |  |  |  |  | Foo Bar
35 |  |  |  |  |  |  | foo\@bar.com
36 |  |  |  |  |  |  | some_group
37 |  |  |  |  |  |  | another_group
38 |  |  |  |  |  |  | ]);
39 | 1 |  |  |  |  | 14 | pass('ran yukki-add-user');
40 |  |  |  |  |  |  | }
41 |  |  |  |  |  |  | catch {
42 | 0 |  |  | 0 |  | 0 | fail("ran yukki-add-user: $_");
43 | 1 |  |  |  |  | 33 | };
44 |
45 | 1 |  |  |  |  | 521 | ok(-f "$tempdir/yukki-test/var/db/users/foo",
46 |  |  |  |  |  |  | 'the user file has been created');
47 |
48 | 1 |  |  |  |  | 201 | my $user = LoadFile("$tempdir/yukki-test/var/db/users/foo");
49 | 1 |  |  |  |  | 9204 | my $password = delete $user->{password};
50 | 1 |  |  |  |  | 18 | is($user, {
51 |  |  |  |  |  |  | login_name => 'foo',
52 |  |  |  |  |  |  | name => 'Foo Bar',
53 |  |  |  |  |  |  | email => 'foo@bar.com',
54 |  |  |  |  |  |  | groups => [ 'some_group', 'another_group' ],
55 |  |  |  |  |  |  | }, 'the user was created correctly');
56 |
57 | 1 1 1 1 1 1 1 |  |  | 1 1 |  | 169 11587 6 536 2 2 9 | use ok('Yukki');
58 | 1 |  |  |  |  | 1125 | my $app = Yukki->new;
59 | 1 |  |  |  |  | 1781 | my $digest = $app->hasher;
60 |
61 | 1 |  |  |  |  | 1757 | ok(scalar $digest->validate($password, 'secret'), 'password is valid');
62 |
63 |  |  |  |  |  |  | try {
64 | 1 |  |  | 1 |  | 45 | yukki('passwd', "foo\nMy Other Secret\n");
65 | 1 |  |  |  |  | 19 | pass("ran yukki-passwd");
66 |  |  |  |  |  |  | }
67 |  |  |  |  |  |  | catch {
68 | 0 |  |  | 0 |  | 0 | fail("ran yukki-passwd: $_");
69 | 1 |  |  |  |  | 403 | };
70 |
71 | 1 |  |  |  |  | 446 | my $user_again = LoadFile("$tempdir/yukki-test/var/db/users/foo");
72 | 1 |  |  |  |  | 3058 | my $new_password = delete $user_again->{password};
73 | 1 |  |  |  |  | 38 | is($user_again, {
74 |  |  |  |  |  |  | login_name => 'foo',
75 |  |  |  |  |  |  | name => 'Foo Bar',
76 |  |  |  |  |  |  | email => 'foo@bar.com',
77 |  |  |  |  |  |  | groups => [ 'some_group', 'another_group' ],
78 |  |  |  |  |  |  | }, 'the user is unchanged');
79 |
80 | 1 |  |  |  |  | 1119 | ok(scalar $digest->validate($new_password, 'My Other Secret'), 'password is valid, but different');
81 |
82 | 1 |  |  |  |  | 422 | done_testing;