source: tags/5.2.15/LATMOS-Accounts/t/05_utils.t @ 2626

Last change on this file since 2626 was 1985, checked in by nanardon, 7 years ago

Add tools to create multiple object from csv file

File size: 3.2 KB
Line 
1use strict;
2use warnings;
3use Test::More tests => 32;
4use File::Temp qw(mkstemp);
5
6use_ok('LATMOS::Accounts::Utils');
7use_ok('LATMOS::Accounts::Mail');
8use LATMOS::Accounts::Log;
9
10ok(la_log(LA_NOTICE, "a notice"), "can run la_log");
11
12{
13my ($fh, $file) = mkstemp( "tmpfileXXXXX" );
14print $fh <<EOF;
15attr1: attr1
16attr: val1
17attr: val2
18EOF
19
20seek($fh, 0, 0);
21
22my %attributes = LATMOS::Accounts::Utils::parse_obj_file($fh);
23$fh = undef;
24unlink($file);
25
26# now testing
27is($attributes{'attr1'}, 'attr1', "can get attribute from file");
28ok(eq_set($attributes{'attr'}, [ qw(val1 val2) ]),
29        "can get multiple values attribute");
30
31$ENV{EDITOR} = 'sleep 1; /bin/touch';
32
33ok(LATMOS::Accounts::Utils::dump_read_temp_file(
34        sub { 1; }, sub { 1; }), "Can edit temp file");
35
36}
37{
38my ($fh, $file) = mkstemp( "tmpfileXXXXX" );
39binmode($fh, ":encoding(UTF-8)");
40print $fh <<EOF;
41sn,givenName
42Nom,Prénom
43EOF
44
45seek($fh, 0, 0);
46
47my $res = LATMOS::Accounts::Utils::loadCSV($fh);
48$fh = undef;
49unlink($file);
50
51# now testing
52is(scalar(@{ $res }), 1, "loadCSV return same number result");
53is($res->[0]{givenName}, "Prénom", "Attribute givenName is properly return");
54
55}
56
57ok(!LATMOS::Accounts::Utils::check_oid_validity('toto'), '"toto" is valid oid');
58ok(!LATMOS::Accounts::Utils::check_oid_validity('toTo'), '"toTo" is valid oid');
59ok(LATMOS::Accounts::Utils::check_oid_validity('to to'), '"to to" is invalid oid');
60ok(LATMOS::Accounts::Utils::check_oid_validity(' toto'), '" toto" is invalid oid');
61
62ok(!LATMOS::Accounts::Utils::check_ug_validity('toto'), '"toto" is valid username');
63ok(LATMOS::Accounts::Utils::check_ug_validity('toTo'), '"toTo" is invalid username');
64ok(LATMOS::Accounts::Utils::check_ug_validity('to to'), '"to to" is invalid username');
65ok(LATMOS::Accounts::Utils::check_ug_validity(' toto'), '" toto" is invalid username');
66ok(LATMOS::Accounts::Utils::check_ug_validity('5toto'), '"5toto" is invalid username');
67ok(!LATMOS::Accounts::Utils::check_ug_validity('t5oto'), '"t5oto" is valid username');
68
69is(LATMOS::Accounts::Utils::buildLogin('Toto'), 'toto', "buildLogin return toto");
70is(LATMOS::Accounts::Utils::buildLogin('To\'to'), 'toto', "buildLogin return toto");
71is(LATMOS::Accounts::Utils::buildLogin('#'), undef, "Impossible name return undef");
72is(LATMOS::Accounts::Utils::buildLogin('TotoTataTiti'), 'tototata', "buildLogin return 8 byte length login");
73is(LATMOS::Accounts::Utils::buildLogin(sub { $_[0] =~ /^toto$/ ? 0 : 1 }, 'toto'), undef, "buildLogin: cb works");
74is(LATMOS::Accounts::Utils::buildLogin(sub { $_[0] =~ /^toto$/ ? 0 : 1 }, 'Toto', 'Titi'), 'totot', "buildLogin: try others login");
75is(LATMOS::Accounts::Utils::buildLogin(sub { $_[0] =~ /^totot?$/ ? 0 : 1 }, 'toto', 'titi'), 'tototi', "buildLogin: try others login");
76is(LATMOS::Accounts::Utils::buildLogin(sub { $_[0] =~ /^toto/ ? 0 : 1 }, 'toto', 'titi'), undef, "buildLogin: return undef if no possibility");
77is(LATMOS::Accounts::Utils::buildLogin(sub { length($_[0]) > 9 }, 'TotoTataTiti'), 'tototatati', "buildLogin return 8 byte length login");
78
79is(yesno('yes'),  1, 'yes is true');
80is(yesno('true'), 1, 'true is true');
81is(yesno('23'),   1, '23 is true');
82is(yesno('no'),   0, 'no is false');
83is(yesno('what'), 'what', 'what is what?');
Note: See TracBrowser for help on using the repository browser.