1 | use strict; |
---|
2 | use warnings; |
---|
3 | use Test::More tests => 46; |
---|
4 | use File::Temp qw(mkstemp); |
---|
5 | |
---|
6 | use_ok('LATMOS::Accounts::Utils'); |
---|
7 | use_ok('LATMOS::Accounts::Mail'); |
---|
8 | use LATMOS::Accounts::Log; |
---|
9 | |
---|
10 | ok(la_log(LA_NOTICE, "a notice"), "can run la_log"); |
---|
11 | |
---|
12 | { |
---|
13 | my ($fh, $file) = mkstemp( "tmpfileXXXXX" ); |
---|
14 | print $fh <<EOF; |
---|
15 | attr1: attr1 |
---|
16 | attr: val1 |
---|
17 | attr: val2 |
---|
18 | EOF |
---|
19 | |
---|
20 | seek($fh, 0, 0); |
---|
21 | |
---|
22 | my %attributes = LATMOS::Accounts::Utils::parse_obj_file($fh); |
---|
23 | $fh = undef; |
---|
24 | unlink($file); |
---|
25 | |
---|
26 | # now testing |
---|
27 | is($attributes{'attr1'}, 'attr1', "can get attribute from file"); |
---|
28 | ok(eq_set($attributes{'attr'}, [ qw(val1 val2) ]), |
---|
29 | "can get multiple values attribute"); |
---|
30 | |
---|
31 | $ENV{EDITOR} = 'sleep 1; /bin/touch'; |
---|
32 | |
---|
33 | ok(LATMOS::Accounts::Utils::dump_read_temp_file( |
---|
34 | sub { 1; }, sub { 1; }), "Can edit temp file"); |
---|
35 | |
---|
36 | } |
---|
37 | { |
---|
38 | my ($fh, $file) = mkstemp( "tmpfileXXXXX" ); |
---|
39 | binmode($fh, ":encoding(UTF-8)"); |
---|
40 | print $fh <<EOF; |
---|
41 | sn,givenName |
---|
42 | Nom,Prénom |
---|
43 | EOF |
---|
44 | |
---|
45 | seek($fh, 0, 0); |
---|
46 | |
---|
47 | my $res = LATMOS::Accounts::Utils::loadCSV($fh); |
---|
48 | $fh = undef; |
---|
49 | unlink($file); |
---|
50 | |
---|
51 | # now testing |
---|
52 | is(scalar(@{ $res }), 1, "loadCSV return same number result"); |
---|
53 | is($res->[0]{givenName}, "Prénom", "Attribute givenName is properly return"); |
---|
54 | |
---|
55 | } |
---|
56 | |
---|
57 | ok(!LATMOS::Accounts::Utils::check_oid_validity('toto'), '"toto" is valid oid'); |
---|
58 | ok(!LATMOS::Accounts::Utils::check_oid_validity('toTo'), '"toTo" is valid oid'); |
---|
59 | ok(LATMOS::Accounts::Utils::check_oid_validity('to to'), '"to to" is invalid oid'); |
---|
60 | ok(LATMOS::Accounts::Utils::check_oid_validity(' toto'), '" toto" is invalid oid'); |
---|
61 | |
---|
62 | ok(!LATMOS::Accounts::Utils::check_ug_validity('toto'), '"toto" is valid username'); |
---|
63 | ok(LATMOS::Accounts::Utils::check_ug_validity('toTo'), '"toTo" is invalid username'); |
---|
64 | ok(LATMOS::Accounts::Utils::check_ug_validity('to to'), '"to to" is invalid username'); |
---|
65 | ok(LATMOS::Accounts::Utils::check_ug_validity(' toto'), '" toto" is invalid username'); |
---|
66 | ok(LATMOS::Accounts::Utils::check_ug_validity('5toto'), '"5toto" is invalid username'); |
---|
67 | ok(!LATMOS::Accounts::Utils::check_ug_validity('t5oto'), '"t5oto" is valid username'); |
---|
68 | |
---|
69 | is(LATMOS::Accounts::Utils::buildLogin('Toto'), 'toto', "buildLogin return toto"); |
---|
70 | is(LATMOS::Accounts::Utils::buildLogin('To\'to'), 'toto', "buildLogin return toto"); |
---|
71 | is(LATMOS::Accounts::Utils::buildLogin('#'), undef, "Impossible name return undef"); |
---|
72 | is(LATMOS::Accounts::Utils::buildLogin('TotoTataTiti'), 'tototata', "buildLogin return 8 byte length login"); |
---|
73 | is(LATMOS::Accounts::Utils::buildLogin(sub { $_[0] =~ /^toto$/ ? 0 : 1 }, 'toto'), undef, "buildLogin: cb works"); |
---|
74 | is(LATMOS::Accounts::Utils::buildLogin(sub { $_[0] =~ /^toto$/ ? 0 : 1 }, 'Toto', 'Titi'), 'totot', "buildLogin: try others login"); |
---|
75 | is(LATMOS::Accounts::Utils::buildLogin(sub { $_[0] =~ /^totot?$/ ? 0 : 1 }, 'toto', 'titi'), 'tototi', "buildLogin: try others login"); |
---|
76 | is(LATMOS::Accounts::Utils::buildLogin(sub { $_[0] =~ /^toto/ ? 0 : 1 }, 'toto', 'titi'), undef, "buildLogin: return undef if no possibility"); |
---|
77 | is(LATMOS::Accounts::Utils::buildLogin(sub { length($_[0]) > 9 }, 'TotoTataTiti'), 'tototatati', "buildLogin return 8 byte length login"); |
---|
78 | |
---|
79 | ok(LATMOS::Accounts::Utils::genpassword(), "can generate a password"); |
---|
80 | ok(LATMOS::Accounts::Utils::genpassword( |
---|
81 | checkpassword => sub { 1 }, |
---|
82 | ), "can generate a password"); |
---|
83 | |
---|
84 | my $clearpass = 'passwd'; |
---|
85 | my $password = ''; |
---|
86 | $password = LATMOS::Accounts::Utils::Crypt($clearpass, 'DES'); |
---|
87 | like($password, qr/^\w\w.*/, 'Password encrypted using DES'); |
---|
88 | is(crypt($clearpass, $password), $password); |
---|
89 | |
---|
90 | $password = LATMOS::Accounts::Utils::Crypt($clearpass, 'MD5'); |
---|
91 | like($password, qr/^\$1\$.*/, 'Password encrypted using MD5'); |
---|
92 | is(crypt($clearpass, $password), $password); |
---|
93 | |
---|
94 | $password = LATMOS::Accounts::Utils::Crypt($clearpass, 'sha-256'); |
---|
95 | like($password, qr/^\$5\$.*/, 'Password encrypted using SHA-256'); |
---|
96 | is(crypt($clearpass, $password), $password); |
---|
97 | |
---|
98 | $password = LATMOS::Accounts::Utils::Crypt($clearpass, 'SHA-512'); |
---|
99 | like($password, qr/^\$6\$.*/, 'Password encrypted using SHA-512'); |
---|
100 | is(crypt($clearpass, $password), $password); |
---|
101 | |
---|
102 | $password = LATMOS::Accounts::Utils::Crypt($clearpass, '5'); |
---|
103 | like($password, qr/^\$5\$.*/, 'Password encrypted using SHA-256 (number)'); |
---|
104 | is(crypt($clearpass, $password), $password); |
---|
105 | |
---|
106 | $password = LATMOS::Accounts::Utils::Crypt($clearpass); |
---|
107 | like($password, qr/^\$1\$.*/, 'Password encrypted using MD5 (default)'); |
---|
108 | is(crypt($clearpass, $password), $password); |
---|
109 | |
---|
110 | is(yesno('yes'), 1, 'yes is true'); |
---|
111 | is(yesno('true'), 1, 'true is true'); |
---|
112 | is(yesno('23'), 1, '23 is true'); |
---|
113 | is(yesno('no'), 0, 'no is false'); |
---|
114 | is(yesno('what'), 'what', 'what is what?'); |
---|