Skip to content

Commit

Permalink
Add tests for missing identity
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpustulka committed Aug 21, 2023
1 parent 4a08022 commit 0163abf
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/TestCase/Controller/Component/AuthorizationComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,33 @@ public function testApplyScopeImplicitAction()
$this->assertSame($query, $result);
}

public function testApplyScopeNoUser()
{
$this->request = $this->request
->withoutAttribute('identity');

$controller = new Controller($this->request);
$componentRegistry = new ComponentRegistry($controller);
$auth = new AuthorizationComponent($componentRegistry);

$articles = new ArticlesTable();
$query = $this->createMock(QueryInterface::class);
$query->method('getRepository')
->willReturn($articles);

$query->expects($this->once())
->method('where')
->with([
'visibility' => 'public',
])
->willReturn($query);

$result = $auth->applyScope($query);

$this->assertInstanceOf(QueryInterface::class, $result);
$this->assertSame($query, $result);
}

public function testApplyScopeMappedAction()
{
$articles = new ArticlesTable();
Expand Down Expand Up @@ -470,6 +497,20 @@ public function testCan()
$this->assertFalse($this->Auth->can($article, 'delete'));
}

public function testCanWithoutUser()
{
$this->request = $this->request
->withoutAttribute('identity');

$controller = new Controller($this->request);
$componentRegistry = new ComponentRegistry($controller);
$auth = new AuthorizationComponent($componentRegistry);

$article = new Article(['user_id' => 1, 'visibility' => 'public']);
$this->assertFalse($auth->can($article, 'edit'));
$this->assertTrue($auth->can($article, 'view'));
}

public function testCanWithResult()
{
$article = new Article(['user_id' => 1]);
Expand Down

0 comments on commit 0163abf

Please sign in to comment.