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