"Fossies" - the Fresh Open Source Software Archive

Member "ampache-5.0.0/tests/Module/Authorization/AccessListManagerTest.php" (31 Aug 2021, 6620 Bytes) of package /linux/www/ampache-5.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 /*
    3  * vim:set softtabstop=4 shiftwidth=4 expandtab:
    4  *
    5  * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
    6  * Copyright 2001 - 2020 Ampache.org
    7  *
    8  * This program is free software: you can redistribute it and/or modify
    9  * it under the terms of the GNU Affero General Public License as published by
   10  * the Free Software Foundation, either version 3 of the License, or
   11  * (at your option) any later version.
   12  *
   13  * This program is distributed in the hope that it will be useful,
   14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16  * GNU Affero General Public License for more details.
   17  *
   18  * You should have received a copy of the GNU Affero General Public License
   19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
   20  *
   21  */
   22 
   23 declare(strict_types=1);
   24 
   25 namespace Ampache\Module\Authorization;
   26 
   27 use Ampache\MockeryTestCase;
   28 use Ampache\Module\Authorization\Exception\AclItemDuplicationException;
   29 use Ampache\Module\Authorization\Exception\InvalidEndIpException;
   30 use Ampache\Module\Authorization\Exception\InvalidIpRangeException;
   31 use Ampache\Module\Authorization\Exception\InvalidStartIpException;
   32 use Ampache\Repository\AccessRepositoryInterface;
   33 use Mockery\MockInterface;
   34 
   35 class AccessListManagerTest extends MockeryTestCase
   36 {
   37     /** @var MockInterface|AccessRepositoryInterface|null */
   38     private MockInterface $accessRepository;
   39 
   40     private ?AccessListManager $subject;
   41 
   42     public function setUp(): void
   43     {
   44         $this->accessRepository = $this->mock(AccessRepositoryInterface::class);
   45 
   46         $this->subject = new AccessListManager(
   47             $this->accessRepository
   48         );
   49     }
   50 
   51     public function testUpdateThrowsExceptionOnInvalidStartIp(): void
   52     {
   53         $this->expectException(InvalidStartIpException::class);
   54 
   55         $this->subject->update(
   56             111,
   57             '666',
   58             '1.2.3.4',
   59             'some-name',
   60             42,
   61             33,
   62             'some-type'
   63         );
   64     }
   65 
   66     public function testUpdateThrowsExceptionOnInvalidEndIp(): void
   67     {
   68         $this->expectException(InvalidEndIpException::class);
   69 
   70         $this->subject->update(
   71             111,
   72             '1.2.3.4',
   73             '666',
   74             'some-name',
   75             42,
   76             33,
   77             'some-type'
   78         );
   79     }
   80 
   81     public function testUpdateThrowsExceptionOnInvalidIpRange(): void
   82     {
   83         $this->expectException(InvalidIpRangeException::class);
   84 
   85         $this->subject->update(
   86             111,
   87             '::',
   88             '1.2.3.4',
   89             'some-name',
   90             42,
   91             33,
   92             'some-type'
   93         );
   94     }
   95 
   96     public function testUpdateUpdatesEntry(): void
   97     {
   98         $accessId = 111;
   99         $startIp  = '1.2.3.4';
  100         $endIp    = '2.3.4.5';
  101         $name     = 'some-name';
  102         $userId   = 42;
  103         $level    = 666;
  104         $type     = AccessLevelEnum::TYPE_INTERFACE;
  105 
  106         $this->accessRepository->shouldReceive('update')
  107             ->with(
  108                 $accessId,
  109                 inet_pton($startIp),
  110                 inet_pton($endIp),
  111                 $name,
  112                 $userId,
  113                 $level,
  114                 $type
  115             )
  116             ->once();
  117 
  118         $this->subject->update(
  119             $accessId,
  120             $startIp,
  121             $endIp,
  122             $name,
  123             $userId,
  124             $level,
  125             $type
  126         );
  127     }
  128 
  129     public function testCreateCreatesEntry(): void
  130     {
  131         $startIp        = '1.2.3.4';
  132         $endIp          = '2.3.4.5';
  133         $name           = 'some-name';
  134         $userId         = 42;
  135         $level          = 666;
  136         $type           = AccessLevelEnum::TYPE_INTERFACE;
  137         $additionalType = 'all';
  138 
  139         $this->accessRepository->shouldReceive('exists')
  140             ->with(
  141                 inet_pton($startIp),
  142                 inet_pton($endIp),
  143                 $type,
  144                 $userId
  145             )
  146             ->once()
  147             ->andReturnFalse();
  148         $this->accessRepository->shouldReceive('exists')
  149             ->with(
  150                 inet_pton($startIp),
  151                 inet_pton($endIp),
  152                 AccessLevelEnum::TYPE_STREAM,
  153                 $userId
  154             )
  155             ->once()
  156             ->andReturnFalse();
  157         $this->accessRepository->shouldReceive('exists')
  158             ->with(
  159                 inet_pton($startIp),
  160                 inet_pton($endIp),
  161                 AccessLevelEnum::TYPE_INTERFACE,
  162                 $userId
  163             )
  164             ->once()
  165             ->andReturnFalse();
  166         $this->accessRepository->shouldReceive('create')
  167             ->with(
  168                 inet_pton($startIp),
  169                 inet_pton($endIp),
  170                 $name,
  171                 $userId,
  172                 $level,
  173                 $type
  174             )
  175             ->once();
  176         $this->accessRepository->shouldReceive('create')
  177             ->with(
  178                 inet_pton($startIp),
  179                 inet_pton($endIp),
  180                 $name,
  181                 $userId,
  182                 $level,
  183                 AccessLevelEnum::TYPE_STREAM
  184             )
  185             ->once();
  186         $this->accessRepository->shouldReceive('create')
  187             ->with(
  188                 inet_pton($startIp),
  189                 inet_pton($endIp),
  190                 $name,
  191                 $userId,
  192                 $level,
  193                 AccessLevelEnum::TYPE_INTERFACE
  194             )
  195             ->once();
  196 
  197         $this->subject->create(
  198             $startIp,
  199             $endIp,
  200             $name,
  201             $userId,
  202             $level,
  203             $type,
  204             $additionalType
  205         );
  206     }
  207 
  208     public function testCreateThrowsExceptionOnDuplicateEntry(): void
  209     {
  210         $this->expectException(AclItemDuplicationException::class);
  211 
  212         $startIp        = '1.2.3.4';
  213         $endIp          = '2.3.4.5';
  214         $name           = 'some-name';
  215         $userId         = 42;
  216         $level          = 666;
  217         $type           = AccessLevelEnum::TYPE_INTERFACE;
  218         $additionalType = 'all';
  219 
  220         $this->accessRepository->shouldReceive('exists')
  221             ->with(
  222                 inet_pton($startIp),
  223                 inet_pton($endIp),
  224                 $type,
  225                 $userId
  226             )
  227             ->once()
  228             ->andReturnTrue();
  229 
  230         $this->subject->create(
  231             $startIp,
  232             $endIp,
  233             $name,
  234             $userId,
  235             $level,
  236             $type,
  237             $additionalType
  238         );
  239     }
  240 }