|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Superdesk Web Publisher Core Bundle. |
| 7 | + * |
| 8 | + * Copyright 2017 Sourcefabric z.ú. and contributors. |
| 9 | + * |
| 10 | + * For the full copyright and license information, please see the |
| 11 | + * AUTHORS and LICENSE files distributed with this source code. |
| 12 | + * |
| 13 | + * @copyright 2017 Sourcefabric z.ú |
| 14 | + * @license http://www.superdesk.org/license |
| 15 | + */ |
| 16 | + |
| 17 | +namespace SWP\Bundle\CoreBundle\Tests\Functional; |
| 18 | + |
| 19 | +use SWP\Bundle\FixturesBundle\WebTestCase; |
| 20 | + |
| 21 | +final class ArticlePreviewTest extends WebTestCase |
| 22 | +{ |
| 23 | + private $router; |
| 24 | + |
| 25 | + public function setUp() |
| 26 | + { |
| 27 | + self::bootKernel(); |
| 28 | + $this->initDatabase(); |
| 29 | + $this->loadCustomFixtures(['tenant']); |
| 30 | + $this->loadFixtureFiles([ |
| 31 | + '@SWPFixturesBundle/Resources/fixtures/ORM/test/article_preview.yml', |
| 32 | + ], true); |
| 33 | + |
| 34 | + $this->router = $this->getContainer()->get('router'); |
| 35 | + } |
| 36 | + |
| 37 | + public function testArticlePreview() |
| 38 | + { |
| 39 | + $route = $this->createRoute(); |
| 40 | + |
| 41 | + $this->ensureArticleIsNotAccessible(); |
| 42 | + |
| 43 | + $client = static::createClient([], ['HTTP_Authorization' => null]); |
| 44 | + $crawler = $client->request('GET', $this->router->generate( |
| 45 | + 'swp_article_preview', |
| 46 | + ['routeId' => $route['id'], 'slug' => 'art1-not-published', 'auth_token' => base64_encode('test_token:')] |
| 47 | + )); |
| 48 | + |
| 49 | + self::assertTrue($client->getResponse()->isSuccessful()); |
| 50 | + self::assertGreaterThan(0, $crawler->filter('html:contains("Slug: art1-not-published")')->count()); |
| 51 | + self::assertGreaterThan(0, $crawler->filter('html:contains("Current tenant: Default tenant")')->count()); |
| 52 | + } |
| 53 | + |
| 54 | + public function testArticlePreviewWithoutToken() |
| 55 | + { |
| 56 | + $route = $this->createRoute(); |
| 57 | + $client = static::createClient([], ['HTTP_Authorization' => null]); |
| 58 | + $client->request('GET', $this->router->generate( |
| 59 | + 'swp_article_preview', |
| 60 | + ['routeId' => $route['id'], 'slug' => 'art1-not-published'] |
| 61 | + )); |
| 62 | + |
| 63 | + self::assertEquals(401, $client->getResponse()->getStatusCode()); |
| 64 | + } |
| 65 | + |
| 66 | + public function testArticlePreviewWithFakeToken() |
| 67 | + { |
| 68 | + $route = $this->createRoute(); |
| 69 | + |
| 70 | + $this->ensureArticleIsNotAccessible(); |
| 71 | + |
| 72 | + $client = static::createClient([], ['HTTP_Authorization' => null]); |
| 73 | + $client->request('GET', $this->router->generate( |
| 74 | + 'swp_article_preview', |
| 75 | + ['routeId' => $route['id'], 'slug' => 'art1-not-published', 'auth_token' => 'fake'] |
| 76 | + )); |
| 77 | + |
| 78 | + self::assertEquals(403, $client->getResponse()->getStatusCode()); |
| 79 | + } |
| 80 | + |
| 81 | + public function testArticlePreviewWithNotExistingArticle() |
| 82 | + { |
| 83 | + $route = $this->createRoute(); |
| 84 | + |
| 85 | + $this->ensureArticleIsNotAccessible(); |
| 86 | + |
| 87 | + $client = static::createClient([], ['HTTP_Authorization' => null]); |
| 88 | + $client->request('GET', $this->router->generate( |
| 89 | + 'swp_article_preview', |
| 90 | + ['routeId' => $route['id'], 'slug' => 'fake-article', 'auth_token' => base64_encode('test_token:')] |
| 91 | + )); |
| 92 | + |
| 93 | + self::assertFalse($client->getResponse()->isSuccessful()); |
| 94 | + self::assertEquals($client->getResponse()->getStatusCode(), 404); |
| 95 | + } |
| 96 | + |
| 97 | + public function testArticlePreviewWithNotExistingRoute() |
| 98 | + { |
| 99 | + $this->ensureArticleIsNotAccessible(); |
| 100 | + |
| 101 | + $client = static::createClient([], ['HTTP_Authorization' => null]); |
| 102 | + $client->request('GET', $this->router->generate( |
| 103 | + 'swp_article_preview', |
| 104 | + ['routeId' => 9999, 'slug' => 'art1-not-published', 'auth_token' => base64_encode('test_token:')] |
| 105 | + )); |
| 106 | + |
| 107 | + self::assertFalse($client->getResponse()->isSuccessful()); |
| 108 | + self::assertEquals($client->getResponse()->getStatusCode(), 404); |
| 109 | + } |
| 110 | + |
| 111 | + public function testArticlePreviewWithoutRouteTemplate() |
| 112 | + { |
| 113 | + $route = $this->createRouteWithoutTemplate(); |
| 114 | + |
| 115 | + $this->ensureArticleIsNotAccessible(); |
| 116 | + |
| 117 | + $client = static::createClient([], ['HTTP_Authorization' => null]); |
| 118 | + $client->request('GET', $this->router->generate( |
| 119 | + 'swp_article_preview', |
| 120 | + ['routeId' => $route['id'], 'slug' => 'art1-not-published', 'auth_token' => base64_encode('test_token:')] |
| 121 | + )); |
| 122 | + |
| 123 | + self::assertFalse($client->getResponse()->isSuccessful()); |
| 124 | + self::assertEquals($client->getResponse()->getStatusCode(), 404); |
| 125 | + } |
| 126 | + |
| 127 | + public function testTenantsFromDifferentOrganizationsCantPreviewArticlesOfEachother() |
| 128 | + { |
| 129 | + $route = $this->createRoute(); |
| 130 | + $this->ensureArticleIsNotAccessible(); |
| 131 | + |
| 132 | + $client = static::createClient([], ['HTTP_Authorization' => null]); |
| 133 | + $crawler = $client->request('GET', $this->router->generate( |
| 134 | + 'swp_article_preview', |
| 135 | + ['routeId' => $route['id'], 'slug' => 'art1-not-published', 'auth_token' => base64_encode('test_token:')] |
| 136 | + )); |
| 137 | + |
| 138 | + self::assertTrue($client->getResponse()->isSuccessful()); |
| 139 | + self::assertGreaterThan(0, $crawler->filter('html:contains("Slug: art1-not-published")')->count()); |
| 140 | + self::assertGreaterThan(0, $crawler->filter('html:contains("Current tenant: Default tenant")')->count()); |
| 141 | + |
| 142 | + $client->request('GET', $this->router->generate( |
| 143 | + 'swp_article_preview', |
| 144 | + ['routeId' => $route['id'], 'slug' => 'art1-not-published', 'auth_token' => base64_encode('client1_token')] |
| 145 | + )); |
| 146 | + |
| 147 | + self::assertEquals(403, $client->getResponse()->getStatusCode()); |
| 148 | + } |
| 149 | + |
| 150 | + public function testOneTenantCanPreviewArticlesOfOtherTenantsUsingTokensWithinSameOrganization() |
| 151 | + { |
| 152 | + $route = $this->createRoute(); |
| 153 | + $this->ensureArticleIsNotAccessible(); |
| 154 | + |
| 155 | + $client = static::createClient([], ['HTTP_Authorization' => null]); |
| 156 | + $crawler = $client->request('GET', $this->router->generate( |
| 157 | + 'swp_article_preview', |
| 158 | + ['routeId' => $route['id'], 'slug' => 'art1-not-published', 'auth_token' => base64_encode('test_token:')] |
| 159 | + )); |
| 160 | + |
| 161 | + self::assertTrue($client->getResponse()->isSuccessful()); |
| 162 | + self::assertGreaterThan(0, $crawler->filter('html:contains("Slug: art1-not-published")')->count()); |
| 163 | + self::assertGreaterThan(0, $crawler->filter('html:contains("Current tenant: Default tenant")')->count()); |
| 164 | + |
| 165 | + $client->request('GET', $this->router->generate( |
| 166 | + 'swp_article_preview', |
| 167 | + ['routeId' => $route['id'], 'slug' => 'art1-not-published', 'auth_token' => base64_encode('client2_token')] |
| 168 | + )); |
| 169 | + |
| 170 | + self::assertTrue($client->getResponse()->isSuccessful()); |
| 171 | + self::assertGreaterThan(0, $crawler->filter('html:contains("Slug: art1-not-published")')->count()); |
| 172 | + self::assertGreaterThan(0, $crawler->filter('html:contains("Current tenant: Default tenant")')->count()); |
| 173 | + } |
| 174 | + |
| 175 | + private function createRoute() |
| 176 | + { |
| 177 | + $client = static::createClient(); |
| 178 | + $client->request('POST', $this->router->generate('swp_api_content_create_routes'), [ |
| 179 | + 'route' => [ |
| 180 | + 'name' => 'news', |
| 181 | + 'type' => 'collection', |
| 182 | + 'content' => null, |
| 183 | + 'templateName' => 'news.html.twig', |
| 184 | + 'articlesTemplateName' => 'article.html.twig', |
| 185 | + ], |
| 186 | + ]); |
| 187 | + |
| 188 | + self::assertEquals(201, $client->getResponse()->getStatusCode()); |
| 189 | + |
| 190 | + return json_decode($client->getResponse()->getContent(), true); |
| 191 | + } |
| 192 | + |
| 193 | + private function ensureArticleIsNotAccessible() |
| 194 | + { |
| 195 | + $client = static::createClient(); |
| 196 | + $client->request('GET', '/news/art1-not-published'); |
| 197 | + |
| 198 | + self::assertFalse($client->getResponse()->isSuccessful()); |
| 199 | + self::assertEquals(404, $client->getResponse()->getStatusCode()); |
| 200 | + } |
| 201 | + |
| 202 | + private function createRouteWithoutTemplate() |
| 203 | + { |
| 204 | + $client = static::createClient(); |
| 205 | + $client->request('POST', $this->router->generate('swp_api_content_create_routes'), [ |
| 206 | + 'route' => [ |
| 207 | + 'name' => 'news', |
| 208 | + 'type' => 'collection', |
| 209 | + 'content' => null, |
| 210 | + 'templateName' => 'news.html.twig', |
| 211 | + ], |
| 212 | + ]); |
| 213 | + |
| 214 | + self::assertEquals(201, $client->getResponse()->getStatusCode()); |
| 215 | + |
| 216 | + return json_decode($client->getResponse()->getContent(), true); |
| 217 | + } |
| 218 | +} |
0 commit comments