Skip to content

Commit 7b1bbb2

Browse files
committed
feat(tests): Add link security behat tests
1 parent 637167c commit 7b1bbb2

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

features/authorization/deny.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,21 @@ Feature: Authorization checking
210210
Then the response status code should be 200
211211
And the response should contain "ownerOnlyProperty"
212212
And the JSON node "ownerOnlyProperty" should be equal to the string "updated"
213+
214+
Scenario: An user can get related linked dummies for an secured dummy they own
215+
Given there are 1 SecuredDummy objects owned by dunglas with related dummies
216+
When I add "Accept" header equal to "application/ld+json"
217+
And I add "Content-Type" header equal to "application/ld+json"
218+
And I add "Authorization" header equal to "Basic ZHVuZ2xhczprZXZpbg=="
219+
And I send a "GET" request to "/secured_dummies/4/related"
220+
Then the response status code should be 200
221+
And the response should contain "securedDummy"
222+
And the JSON node "hydra:member[0].id" should be equal to 1"
223+
224+
Scenario: An user can not get related linked dummies for an secured dummy they do not own
225+
Given there are 1 SecuredDummy objects owned by someone with related dummies
226+
When I add "Accept" header equal to "application/ld+json"
227+
And I add "Content-Type" header equal to "application/ld+json"
228+
And I add "Authorization" header equal to "Basic ZHVuZ2xhczprZXZpbg=="
229+
And I send a "GET" request to "/secured_dummies/5/related"
230+
Then the response status code should be 403

tests/Behat/DoctrineContext.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
use ApiPlatform\Tests\Fixtures\TestBundle\Document\Program as ProgramDocument;
7878
use ApiPlatform\Tests\Fixtures\TestBundle\Document\Question as QuestionDocument;
7979
use ApiPlatform\Tests\Fixtures\TestBundle\Document\RelatedDummy as RelatedDummyDocument;
80+
use ApiPlatform\Tests\Fixtures\TestBundle\Document\RelatedLinkedDummy as RelatedLinkedDummyDocument;
8081
use ApiPlatform\Tests\Fixtures\TestBundle\Document\RelatedOwnedDummy as RelatedOwnedDummyDocument;
8182
use ApiPlatform\Tests\Fixtures\TestBundle\Document\RelatedOwningDummy as RelatedOwningDummyDocument;
8283
use ApiPlatform\Tests\Fixtures\TestBundle\Document\RelatedSecuredDummy as RelatedSecuredDummyDocument;
@@ -156,6 +157,7 @@
156157
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Question;
157158
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RamseyUuidDummy;
158159
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
160+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedLinkedDummy;
159161
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedOwnedDummy;
160162
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedOwningDummy;
161163
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedSecuredDummy;
@@ -1125,12 +1127,16 @@ public function thereAreSecuredDummyObjectsOwnedByWithRelatedDummies(int $nb, st
11251127
$publicRelatedSecuredDummy = $this->buildRelatedSecureDummy();
11261128
$this->manager->persist($publicRelatedSecuredDummy);
11271129

1130+
$relatedLinkedDummy = $this->buildRelatedLinkedDummy();
1131+
$this->manager->persist($relatedLinkedDummy);
1132+
11281133
$securedDummy->addRelatedDummy($relatedDummy);
11291134
$securedDummy->setRelatedDummy($relatedDummy);
11301135
$securedDummy->addRelatedSecuredDummy($relatedSecuredDummy);
11311136
$securedDummy->setRelatedSecuredDummy($relatedSecuredDummy);
11321137
$securedDummy->addPublicRelatedSecuredDummy($publicRelatedSecuredDummy);
11331138
$securedDummy->setPublicRelatedSecuredDummy($publicRelatedSecuredDummy);
1139+
$relatedLinkedDummy->setSecuredDummy($securedDummy);
11341140

11351141
$this->manager->persist($securedDummy);
11361142
}
@@ -2246,6 +2252,11 @@ private function buildRelatedToDummyFriend(): RelatedToDummyFriend|RelatedToDumm
22462252
return $this->isOrm() ? new RelatedToDummyFriend() : new RelatedToDummyFriendDocument();
22472253
}
22482254

2255+
private function buildRelatedLinkedDummy(): RelatedLinkedDummy|RelatedLinkedDummyDocument
2256+
{
2257+
return $this->isOrm() ? new RelatedLinkedDummy() : new RelatedLinkedDummyDocument();
2258+
}
2259+
22492260
private function buildRelationEmbedder(): RelationEmbedder|RelationEmbedderDocument
22502261
{
22512262
return $this->isOrm() ? new RelationEmbedder() : new RelationEmbedderDocument();
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Document;
15+
16+
use ApiPlatform\Metadata\ApiProperty;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\GetCollection;
19+
use ApiPlatform\Metadata\Link;
20+
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
21+
22+
#[ApiResource()]
23+
#[ApiResource(
24+
uriTemplate: '/secured_dummies/{securedDummyId}/related',
25+
operations: [new GetCollection()],
26+
uriVariables: [
27+
'securedDummyId' => new Link(toProperty: 'securedDummy', fromClass: SecuredDummy::class, security: "is_granted('ROLE_USER') and securedDummy.getOwner() == user"),
28+
]
29+
)]
30+
#[ODM\Document]
31+
class RelatedLinkedDummy
32+
{
33+
#[ApiProperty(writable: false)]
34+
#[ODM\Id(strategy: 'INCREMENT', type: 'int')]
35+
private $id;
36+
37+
#[ODM\ReferenceOne(targetDocument: SecuredDummy::class)]
38+
private SecuredDummy $securedDummy;
39+
40+
public function getId()
41+
{
42+
return $this->id;
43+
}
44+
45+
public function setId($id): void
46+
{
47+
$this->id = $id;
48+
}
49+
50+
public function getSecuredDummy(): SecuredDummy
51+
{
52+
return $this->securedDummy;
53+
}
54+
55+
public function setSecuredDummy(SecuredDummy $securedDummy): void
56+
{
57+
$this->securedDummy = $securedDummy;
58+
}
59+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\GetCollection;
18+
use ApiPlatform\Metadata\Link;
19+
use Doctrine\ORM\Mapping as ORM;
20+
use Doctrine\ORM\Mapping\Entity;
21+
22+
#[ApiResource()]
23+
#[ApiResource(
24+
uriTemplate: '/secured_dummies/{securedDummyId}/related',
25+
operations: [new GetCollection()],
26+
uriVariables: [
27+
'securedDummyId' => new Link(toProperty: 'securedDummy', fromClass: SecuredDummy::class, security: "is_granted('ROLE_USER') and securedDummy.getOwner() == user"),
28+
]
29+
)]
30+
#[Entity]
31+
class RelatedLinkedDummy
32+
{
33+
/**
34+
* @var int
35+
*/
36+
#[ORM\Column(type: 'integer')]
37+
#[ORM\Id]
38+
#[ORM\GeneratedValue(strategy: 'AUTO')]
39+
private $id;
40+
41+
#[ORM\ManyToOne(targetEntity: SecuredDummy::class)]
42+
private SecuredDummy $securedDummy;
43+
44+
public function getId()
45+
{
46+
return $this->id;
47+
}
48+
49+
public function setId($id): void
50+
{
51+
$this->id = $id;
52+
}
53+
54+
public function getSecuredDummy(): SecuredDummy
55+
{
56+
return $this->securedDummy;
57+
}
58+
59+
public function setSecuredDummy(SecuredDummy $securedDummy): void
60+
{
61+
$this->securedDummy = $securedDummy;
62+
}
63+
}

0 commit comments

Comments
 (0)