|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
| 4 | + * |
| 5 | + * Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics) |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Affero General Public License as published |
| 9 | + * by the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +declare(strict_types=1); |
| 22 | + |
| 23 | +namespace App\Tests\Controller; |
| 24 | + |
| 25 | +use App\DataFixtures\APITokenFixtures; |
| 26 | +use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
| 27 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
| 28 | + |
| 29 | +final class KiCadApiV2ControllerTest extends WebTestCase |
| 30 | +{ |
| 31 | + private const BASE_URL = '/en/kicad-api/v2'; |
| 32 | + |
| 33 | + protected function createClientWithCredentials(string $token = APITokenFixtures::TOKEN_READONLY): KernelBrowser |
| 34 | + { |
| 35 | + return static::createClient([], ['headers' => ['authorization' => 'Bearer '.$token]]); |
| 36 | + } |
| 37 | + |
| 38 | + public function testRootReturnsEndpointLinks(): void |
| 39 | + { |
| 40 | + $client = $this->createClientWithCredentials(); |
| 41 | + $client->request('GET', self::BASE_URL.'/'); |
| 42 | + |
| 43 | + self::assertResponseIsSuccessful(); |
| 44 | + $content = $client->getResponse()->getContent(); |
| 45 | + self::assertJson($content); |
| 46 | + |
| 47 | + $array = json_decode($content, true); |
| 48 | + self::assertArrayHasKey('categories', $array); |
| 49 | + self::assertArrayHasKey('parts', $array); |
| 50 | + |
| 51 | + // Root endpoint should return link to categories endpoint |
| 52 | + self::assertStringContainsString('categories.json', $array['categories']); |
| 53 | + } |
| 54 | + |
| 55 | + public function testCategories(): void |
| 56 | + { |
| 57 | + $client = $this->createClientWithCredentials(); |
| 58 | + $client->request('GET', self::BASE_URL.'/categories.json'); |
| 59 | + |
| 60 | + self::assertResponseIsSuccessful(); |
| 61 | + $content = $client->getResponse()->getContent(); |
| 62 | + self::assertJson($content); |
| 63 | + |
| 64 | + $data = json_decode($content, true); |
| 65 | + self::assertCount(1, $data); |
| 66 | + |
| 67 | + $category = $data[0]; |
| 68 | + self::assertArrayHasKey('name', $category); |
| 69 | + self::assertArrayHasKey('id', $category); |
| 70 | + } |
| 71 | + |
| 72 | + public function testCategoryParts(): void |
| 73 | + { |
| 74 | + $client = $this->createClientWithCredentials(); |
| 75 | + $client->request('GET', self::BASE_URL.'/parts/category/1.json'); |
| 76 | + |
| 77 | + self::assertResponseIsSuccessful(); |
| 78 | + $content = $client->getResponse()->getContent(); |
| 79 | + self::assertJson($content); |
| 80 | + |
| 81 | + $data = json_decode($content, true); |
| 82 | + self::assertCount(3, $data); |
| 83 | + |
| 84 | + $part = $data[0]; |
| 85 | + self::assertArrayHasKey('name', $part); |
| 86 | + self::assertArrayHasKey('id', $part); |
| 87 | + self::assertArrayHasKey('description', $part); |
| 88 | + } |
| 89 | + |
| 90 | + public function testCategoryPartsMinimal(): void |
| 91 | + { |
| 92 | + $client = $this->createClientWithCredentials(); |
| 93 | + $client->request('GET', self::BASE_URL.'/parts/category/1.json?minimal=true'); |
| 94 | + |
| 95 | + self::assertResponseIsSuccessful(); |
| 96 | + $content = $client->getResponse()->getContent(); |
| 97 | + self::assertJson($content); |
| 98 | + |
| 99 | + $data = json_decode($content, true); |
| 100 | + self::assertCount(3, $data); |
| 101 | + } |
| 102 | + |
| 103 | + public function testPartDetailsHasVolatileFields(): void |
| 104 | + { |
| 105 | + $client = $this->createClientWithCredentials(); |
| 106 | + $client->request('GET', self::BASE_URL.'/parts/1.json'); |
| 107 | + |
| 108 | + self::assertResponseIsSuccessful(); |
| 109 | + $content = $client->getResponse()->getContent(); |
| 110 | + self::assertJson($content); |
| 111 | + |
| 112 | + $data = json_decode($content, true); |
| 113 | + |
| 114 | + // V2 should have volatile flag on Stock field |
| 115 | + self::assertArrayHasKey('fields', $data); |
| 116 | + self::assertArrayHasKey('Stock', $data['fields']); |
| 117 | + self::assertArrayHasKey('volatile', $data['fields']['Stock']); |
| 118 | + self::assertEquals('True', $data['fields']['Stock']['volatile']); |
| 119 | + } |
| 120 | + |
| 121 | + public function testPartDetailsV2VsV1Difference(): void |
| 122 | + { |
| 123 | + $client = $this->createClientWithCredentials(); |
| 124 | + |
| 125 | + // Get v1 response |
| 126 | + $client->request('GET', '/en/kicad-api/v1/parts/1.json'); |
| 127 | + self::assertResponseIsSuccessful(); |
| 128 | + $v1Data = json_decode($client->getResponse()->getContent(), true); |
| 129 | + |
| 130 | + // Get v2 response |
| 131 | + $client->request('GET', self::BASE_URL.'/parts/1.json'); |
| 132 | + self::assertResponseIsSuccessful(); |
| 133 | + $v2Data = json_decode($client->getResponse()->getContent(), true); |
| 134 | + |
| 135 | + // V1 should NOT have volatile on Stock |
| 136 | + self::assertArrayNotHasKey('volatile', $v1Data['fields']['Stock']); |
| 137 | + |
| 138 | + // V2 should have volatile on Stock |
| 139 | + self::assertArrayHasKey('volatile', $v2Data['fields']['Stock']); |
| 140 | + |
| 141 | + // Both should have the same stock value |
| 142 | + self::assertEquals($v1Data['fields']['Stock']['value'], $v2Data['fields']['Stock']['value']); |
| 143 | + } |
| 144 | + |
| 145 | + public function testCategoriesHasCacheHeaders(): void |
| 146 | + { |
| 147 | + $client = $this->createClientWithCredentials(); |
| 148 | + $client->request('GET', self::BASE_URL.'/categories.json'); |
| 149 | + |
| 150 | + self::assertResponseIsSuccessful(); |
| 151 | + $response = $client->getResponse(); |
| 152 | + self::assertNotNull($response->headers->get('ETag')); |
| 153 | + self::assertStringContainsString('max-age=', $response->headers->get('Cache-Control')); |
| 154 | + } |
| 155 | + |
| 156 | + public function testConditionalRequestReturns304(): void |
| 157 | + { |
| 158 | + $client = $this->createClientWithCredentials(); |
| 159 | + $client->request('GET', self::BASE_URL.'/categories.json'); |
| 160 | + |
| 161 | + $etag = $client->getResponse()->headers->get('ETag'); |
| 162 | + self::assertNotNull($etag); |
| 163 | + |
| 164 | + $client->request('GET', self::BASE_URL.'/categories.json', [], [], [ |
| 165 | + 'HTTP_IF_NONE_MATCH' => $etag, |
| 166 | + ]); |
| 167 | + |
| 168 | + self::assertResponseStatusCodeSame(304); |
| 169 | + } |
| 170 | + |
| 171 | + public function testUnauthenticatedAccessDenied(): void |
| 172 | + { |
| 173 | + $client = static::createClient(); |
| 174 | + $client->request('GET', self::BASE_URL.'/categories.json'); |
| 175 | + |
| 176 | + // Anonymous user has default read permissions in Part-DB, |
| 177 | + // so this returns 200 rather than a redirect |
| 178 | + self::assertResponseIsSuccessful(); |
| 179 | + } |
| 180 | +} |
0 commit comments