"Fossies" - the Fresh Open Source Software Archive

Member "otrs-5.0.40/scripts/test/ACL/DB/ACLImport.t" (11 Jan 2019, 8888 Bytes) of package /linux/www/otrs-5.0.40.tar.bz2:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "ACLImport.t": rel-6_0_25_vs_rel-6_0_26.

    1 # --
    2 # Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
    3 # --
    4 # This software comes with ABSOLUTELY NO WARRANTY. For details, see
    5 # the enclosed file COPYING for license information (GPL). If you
    6 # did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
    7 # --
    8 
    9 use strict;
   10 use warnings;
   11 use utf8;
   12 
   13 use vars (qw($Self));
   14 
   15 use Kernel::System::VariableCheck qw(:all);
   16 
   17 # get needed objects
   18 my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
   19 my $ACLObject    = $Kernel::OM->Get('Kernel::System::ACL::DB::ACL');
   20 my $YAMLObject   = $Kernel::OM->Get('Kernel::System::YAML');
   21 
   22 # get helper object
   23 $Kernel::OM->ObjectParamAdd(
   24     'Kernel::System::UnitTest::Helper' => {
   25         RestoreDatabase => 1,
   26     },
   27 );
   28 my $Helper = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
   29 
   30 # define needed variables
   31 my $RandomID = $Helper->GetRandomID();
   32 my $Home     = $ConfigObject->Get('Home');
   33 my $UserID   = 1;
   34 
   35 my $CheckACL = sub {
   36     my %Param = @_;
   37 
   38     my $Test          = $Param{Test};
   39     my %ACLDataLookup = %{ $Param{ACLDataLookup} };
   40     my $ACLName       = $Param{ACLName};
   41     my $ACL           = $Param{ACL};
   42 
   43     # update ACL reference from 3.3.x style if needed
   44     if ( ref $ACLDataLookup{$ACLName}->{ConfigChange}->{Possible}->{Action} eq 'HASH' ) {
   45         my %ActionList = %{ $ACLDataLookup{$ACLName}->{ConfigChange}->{Possible}->{Action} };
   46         my @PossibleNotActions;
   47 
   48         ACTION:
   49         for my $Action ( sort keys %ActionList ) {
   50             next ACTION if $ActionList{$Action} ne 0;
   51             push @PossibleNotActions, $Action;
   52         }
   53 
   54         delete $ACLDataLookup{$ACLName}->{ConfigChange}->{Possible}->{Action};
   55         $ACLDataLookup{$ACLName}->{ConfigChange}->{PossibleNot}->{Action} = \@PossibleNotActions;
   56     }
   57 
   58     for my $Attribute (
   59         qw( Name Comment Description StopAfterMatch ConfigMatch ConfigChange ValidID)
   60         )
   61     {
   62         if ( $Attribute eq 'ConfigMatch' || $Attribute eq 'ConfigChange' ) {
   63             $Self->IsDeeply(
   64                 $ACL->{$Attribute},
   65                 $ACLDataLookup{$ACLName}->{$Attribute},
   66                 "ACLImport() $Test->{Name} - $ACLName Expected Attribute $Attribute",
   67             );
   68         }
   69         else {
   70 
   71             # set undefined values as empty (quick fix for ORACLE)
   72             $ACL->{$Attribute} //= '';
   73 
   74             $Self->Is(
   75                 $ACL->{$Attribute},
   76                 $ACLDataLookup{$ACLName}->{$Attribute},
   77                 "ACLImport() $Test->{Name} - $ACLName Expected Attribute $Attribute",
   78             );
   79         }
   80     }
   81 };
   82 
   83 #
   84 # Tests for ACLAdd
   85 #
   86 my @Tests = (
   87     {
   88         Name    => 'ACLImport Test: No Params',
   89         Config  => {},
   90         Success => 0,
   91     },
   92     {
   93         Name    => 'ACLImport Test: No UserID',
   94         Config  => {},
   95         ACLFile => 'Properties.yml',
   96         Success => 0,
   97     },
   98     {
   99         Name   => 'ACLImport Test: No Content',
  100         Config => {
  101             Content => '',
  102             UserID  => $UserID
  103         },
  104         Success => 0,
  105     },
  106     {
  107         Name   => 'ACLImport Test: Properties',
  108         Config => {
  109             UserID => $UserID,
  110         },
  111         ACLFile         => 'Properties.yml',
  112         ExpectedResults => {
  113             AddedACLs   => "100-ACL-Test-Properties$RandomID",
  114             UpdatedACLs => '',
  115             ACLErrors   => '',
  116         },
  117         Success => 1,
  118     },
  119     {
  120         Name   => 'ACLImport Test: PropertiesDatabase',
  121         Config => {
  122             UserID => $UserID,
  123         },
  124         ACLFile         => 'PropertiesDatabase.yml',
  125         ExpectedResults => {
  126             AddedACLs   => "100-ACL-Test-PropertiesDatabase$RandomID",
  127             UpdatedACLs => '',
  128             ACLErrors   => '',
  129         },
  130         Success => 1,
  131     },
  132     {
  133         Name   => 'ACLImport Test: PossibleNot',
  134         Config => {
  135             UserID => $UserID,
  136         },
  137         ACLFile         => 'PossibleNot.yml',
  138         ExpectedResults => {
  139             AddedACLs   => "100-ACL-Test-PossibleNot$RandomID",
  140             UpdatedACLs => '',
  141             ACLErrors   => '',
  142         },
  143         Success => 1,
  144     },
  145     {
  146         Name   => 'ACLImport Test: Multiple',
  147         Config => {
  148             UserID => $UserID,
  149         },
  150         ACLFile         => 'Multiple.yml',
  151         ExpectedResults => {
  152             AddedACLs   => "100-ACL-Test-Multiple1$RandomID, 100-ACL-Test-Multiple2$RandomID",
  153             UpdatedACLs => '',
  154             ACLErrors   => '',
  155         },
  156         Success => 1,
  157     },
  158     {
  159         Name   => 'ACLImport Test: Properties Updated without Overwrite',
  160         Config => {
  161             UserID => $UserID,
  162         },
  163         ACLFile         => 'PropertiesUpdated.yml',
  164         ExpectedResults => {
  165             AddedACLs   => '',
  166             UpdatedACLs => '',
  167             ACLErrors   => "100-ACL-Test-Properties$RandomID",
  168         },
  169         Success => 1,
  170     },
  171     {
  172         Name   => 'ACLImport Test: Properties Updated with Overwrite',
  173         Config => {
  174             OverwriteExistingEntities => 1,
  175             UserID                    => $UserID,
  176         },
  177         ACLFile         => 'PropertiesUpdated.yml',
  178         ExpectedResults => {
  179             AddedACLs   => '',
  180             UpdatedACLs => "100-ACL-Test-Properties$RandomID",
  181             ACLErrors   => '',
  182         },
  183         Success => 1,
  184     },
  185     {
  186         Name   => 'ACLImport Test: Actions from 3.3.x',
  187         Config => {
  188             UserID => $UserID,
  189         },
  190         ACLFile         => 'Actions33.yml',
  191         ExpectedResults => {
  192             AddedACLs   => "100-ACL-Test-Actions33$RandomID",
  193             UpdatedACLs => '',
  194             ACLErrors   => '',
  195         },
  196         Success => 1,
  197     },
  198     {
  199         Name   => 'ACLImport Test: Actions from 3.3.x Empty',
  200         Config => {
  201             UserID => $UserID,
  202         },
  203         ACLFile         => 'Actions33Empty.yml',
  204         ExpectedResults => {
  205             AddedACLs   => "100-ACL-Test-Actions33Empty$RandomID",
  206             UpdatedACLs => '',
  207             ACLErrors   => '',
  208         },
  209         Success => 1,
  210     },
  211 );
  212 
  213 my $MainObject = $Kernel::OM->Get('Kernel::System::Main');
  214 
  215 my %ACLToDelete;
  216 for my $Test (@Tests) {
  217 
  218     # read process for yml file if needed
  219     my $ACLData;
  220     my $FileRef;
  221     if ( $Test->{ACLFile} ) {
  222 
  223         $FileRef = $MainObject->FileRead(
  224             Location => $Home . '/scripts/test/sample/ACL/' . $Test->{ACLFile},
  225         );
  226 
  227         my $RandomID = $Helper->GetRandomID();
  228 
  229         # convert process to Perl for easy handling
  230         $ACLData = $YAMLObject->Load( Data => $$FileRef );
  231     }
  232 
  233     # update all ACL names for easy search
  234     if ( IsArrayRefWithData($ACLData) ) {
  235         for my $ACLItem ( @{$ACLData} ) {
  236             $ACLItem->{Name} .= $RandomID;
  237         }
  238     }
  239 
  240     # convert process back to YAML and set it as part of the config
  241     my $Content = $YAMLObject->Dump( Data => $ACLData );
  242     $Test->{Config}->{Content} = $Content;
  243 
  244     # call import function
  245     my $ACLImport = $ACLObject->ACLImport( %{ $Test->{Config} } );
  246 
  247     if ( $Test->{Success} ) {
  248         $Self->True(
  249             $ACLImport->{Success},
  250             "ACLImport() $Test->{Name} - return value with true",
  251         );
  252 
  253         for my $ResultKey ( sort keys %{ $Test->{ExpectedResults} } ) {
  254             $Self->Is(
  255                 $ACLImport->{$ResultKey},
  256                 $Test->{ExpectedResults}->{$ResultKey},
  257                 "ACLImport() $Test->{Name} - Expected $ResultKey",
  258             );
  259         }
  260 
  261         my %ACLDataLookup = map { $_->{Name} => $_ } @{$ACLData};
  262 
  263         my @AddedACLs = split ',', $ACLImport->{AddedACLs};
  264         for my $ACLName (@AddedACLs) {
  265 
  266             # cleanup possible leading whitespaces in name
  267             $ACLName =~ s{^\s+}{};
  268             my $ACL = $ACLObject->ACLGet(
  269                 Name   => $ACLName,
  270                 UserID => $UserID,
  271             );
  272 
  273             $CheckACL->(
  274                 Test          => $Test,
  275                 ACLDataLookup => \%ACLDataLookup,
  276                 ACLName       => $ACLName,
  277                 ACL           => $ACL
  278             );
  279 
  280             $ACLToDelete{ $ACL->{ID} } = 1;
  281         }
  282 
  283         my @UpdatedACLs = split ',', $ACLImport->{UpdatedACLs};
  284         for my $ACLName (@UpdatedACLs) {
  285 
  286             # cleanup possible leading whitespaces in name
  287             $ACLName =~ s{^\s+}{};
  288             my $ACL = $ACLObject->ACLGet(
  289                 Name   => $ACLName,
  290                 UserID => $UserID,
  291             );
  292 
  293             $CheckACL->(
  294                 Test          => $Test,
  295                 ACLDataLookup => \%ACLDataLookup,
  296                 ACLName       => $ACLName,
  297                 ACL           => $ACL
  298             );
  299         }
  300     }
  301     else {
  302         $Self->False(
  303             $ACLImport->{Success},
  304             "ACLImport() $Test->{Name} - return value with false",
  305         );
  306 
  307     }
  308 }
  309 
  310 # cleanup is done by RestoreDatabase
  311 
  312 1;