Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): mitigate slow queries #4521

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions api/src/Doctrine/Filter/ContentNodePeriodFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ protected function filterProperty(
$queryBuilder
->join("{$rootAlias}.root", $rootJoinAlias)
->join(Activity::class, $activityJoinAlias, Join::WITH, "{$activityJoinAlias}.rootContentNode = {$rootJoinAlias}.id")
->join("{$activityJoinAlias}.scheduleEntries", $scheduleEntryJoinAlias)
->andWhere($queryBuilder->expr()->eq("{$scheduleEntryJoinAlias}.period", ":{$periodParameterName}"))
->join("{$activityJoinAlias}.scheduleEntries", $scheduleEntryJoinAlias, Join::WITH, $queryBuilder->expr()->eq("{$scheduleEntryJoinAlias}.period", ":{$periodParameterName}"))
;

$queryBuilder->setParameter($periodParameterName, $period);
Expand Down
16 changes: 11 additions & 5 deletions api/src/Repository/FiltersByCampCollaboration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Entity\CampCollaboration;
use App\Entity\User;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;

trait FiltersByCampCollaboration {
Expand All @@ -14,14 +15,19 @@ trait FiltersByCampCollaboration {
* the alias of the camp as the third argument if it's anything other than "camp".
*/
public function filterByCampCollaboration(QueryBuilder $queryBuilder, User $user, string $campAlias = 'camp'): void {
$queryBuilder->leftJoin("{$campAlias}.collaborations", "filter_{$campAlias}_campCollaboration");
$queryBuilder->leftJoin(
"{$campAlias}.collaborations",
"filter_{$campAlias}_campCollaboration",
Join::WITH,
$queryBuilder->expr()->andX(
$queryBuilder->expr()->eq("filter_{$campAlias}_campCollaboration.user", ':current_user'),
$queryBuilder->expr()->eq("filter_{$campAlias}_campCollaboration.status", ':established'),
)
);
$queryBuilder->andWhere(
$queryBuilder->expr()->orX(
// user is established collaborator in the camp
$queryBuilder->expr()->andX(
$queryBuilder->expr()->eq("filter_{$campAlias}_campCollaboration.user", ':current_user'),
$queryBuilder->expr()->eq("filter_{$campAlias}_campCollaboration.status", ':established'),
),
$queryBuilder->expr()->isNotNull("filter_{$campAlias}_campCollaboration.id"),

// camp is a Prototype = all Prototypes are readable
$queryBuilder->expr()->eq("{$campAlias}.isPrototype", ':true'),
Expand Down
88 changes: 86 additions & 2 deletions api/tests/Api/ContentNodes/ContentNode/ListContentNodesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@
* @internal
*/
class ListContentNodesTest extends ECampApiTestCase {
// TODO security tests when not logged in or not collaborator
// TODO add tests for filtering by contentType and root

public function testListContentNodesIsAllowedForCollaborator() {
public function testListContentNodesIsDeniedForAnonymousUser() {
static::createBasicClient()->request('GET', '/content_nodes');
$this->assertResponseStatusCodeSame(401);
$this->assertJsonContains([
'code' => 401,
'message' => 'JWT Token not found',
]);
}

public function testListContentNodesIsAllowedForLoggedInUserButFiltered() {
$response = static::createClientWithCredentials()->request('GET', '/content_nodes');
$this->assertResponseStatusCodeSame(200);
$this->assertJsonContains([
Expand Down Expand Up @@ -46,4 +55,79 @@ public function testListContentNodesIsAllowedForCollaborator() {
['href' => $this->getIriFor('responsiveLayout1')],
], $response->toArray()['_links']['items']);
}

public function testListContentNodesFilteredByPeriodIsAllowedForCollaborator() {
$period = static::getFixture('period1');
$response = static::createClientWithCredentials()->request('GET', '/content_nodes?period=%2Fperiods%2F'.$period->getId());
$this->assertResponseStatusCodeSame(200);
$this->assertJsonContains([
'totalItems' => 12,
'_links' => [
'items' => [],
],
'_embedded' => [
'items' => [],
],
]);
$this->assertEqualsCanonicalizing([
['href' => $this->getIriFor('columnLayout1')],
['href' => $this->getIriFor('columnLayoutChild1')],
['href' => $this->getIriFor('columnLayout3')],
['href' => $this->getIriFor('singleText1')],
['href' => $this->getIriFor('singleText2')],
['href' => $this->getIriFor('safetyConcept1')],
['href' => $this->getIriFor('materialNode1')],
['href' => $this->getIriFor('storyboard1')],
['href' => $this->getIriFor('storyboard2')],
['href' => $this->getIriFor('multiSelect1')],
['href' => $this->getIriFor('multiSelect2')],
['href' => $this->getIriFor('responsiveLayout1')],
], $response->toArray()['_links']['items']);
}

public function testListContentNodesFilteredByPeriodIsDeniedForUnrelatedUser() {
$period = static::getFixture('period1');
$response = static::createClientWithCredentials(['email' => static::$fixtures['user4unrelated']->getEmail()])
->request('GET', '/content_nodes?period=%2Fperiods%2F'.$period->getId())
;

$this->assertResponseStatusCodeSame(400);

$this->assertJsonContains([
'title' => 'An error occurred',
'detail' => 'Item not found for "'.$this->getIriFor('period1').'".',
]);
}

public function testListContentNodesFilteredByPeriodIsDeniedForInactiveCollaborator() {
$period = static::getFixture('period1');
$response = static::createClientWithCredentials(['email' => static::$fixtures['user5inactive']->getEmail()])
->request('GET', '/content_nodes?period=%2Fperiods%2F'.$period->getId())
;

$this->assertResponseStatusCodeSame(400);

$this->assertJsonContains([
'title' => 'An error occurred',
'detail' => 'Item not found for "'.$this->getIriFor('period1').'".',
]);
}

public function testListContentNodesFilteredByPeriodInCampPrototypeIsAllowedForCollaborator() {
$period = static::getFixture('period1campPrototype');
$response = static::createClientWithCredentials()->request('GET', '/content_nodes?period=%2Fperiods%2F'.$period->getId());
$this->assertResponseStatusCodeSame(200);
$this->assertJsonContains([
'totalItems' => 1,
'_links' => [
'items' => [],
],
'_embedded' => [
'items' => [],
],
]);
$this->assertEqualsCanonicalizing([
['href' => $this->getIriFor('columnLayout1campPrototype')],
], $response->toArray()['_links']['items']);
}
}
5 changes: 0 additions & 5 deletions api/tests/Doctrine/Filter/ContentNodePeriodFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@ public function testAddsFilterForPeriodProperty() {
->will($this->returnValue($period))
;

$this->queryBuilderMock
->expects($this->once())
->method('andWhere')
;

$this->queryBuilderMock
->expects($this->once())
->method('setParameter')
Expand Down
Loading