|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace vendor\statamic\cms\tests\API; |
| 4 | + |
| 5 | +use PHPUnit\Framework\Attributes\Test; |
| 6 | +use Statamic\Facades; |
| 7 | +use Tests\PreventSavingStacheItemsToDisk; |
| 8 | +use Tests\TestCase; |
| 9 | + |
| 10 | +class AuthenticationTest extends TestCase |
| 11 | +{ |
| 12 | + use PreventSavingStacheItemsToDisk; |
| 13 | + |
| 14 | + private $collection; |
| 15 | + |
| 16 | + public function setUp(): void |
| 17 | + { |
| 18 | + parent::setUp(); |
| 19 | + |
| 20 | + Facades\Config::set('statamic.api.enabled', true); |
| 21 | + Facades\Config::set('statamic.api.resources.collections', true); |
| 22 | + |
| 23 | + $this->collection = Facades\Collection::make('articles')->save(); |
| 24 | + } |
| 25 | + |
| 26 | + #[Test] |
| 27 | + public function it_can_authenticate_using_auth_token() |
| 28 | + { |
| 29 | + Facades\Config::set('statamic.api.auth_token', 'foobar'); |
| 30 | + |
| 31 | + $this |
| 32 | + ->withToken('foobar') |
| 33 | + ->getJson('/api/collections/articles/entries') |
| 34 | + ->assertOk(); |
| 35 | + } |
| 36 | + |
| 37 | + #[Test] |
| 38 | + public function it_cant_authenticate_with_invalid_auth_token() |
| 39 | + { |
| 40 | + Facades\Config::set('statamic.api.auth_token', 'foobar'); |
| 41 | + |
| 42 | + $this |
| 43 | + ->withToken($token = 'invalid') |
| 44 | + ->getJson($url = '/api/collections/articles/entries') |
| 45 | + ->assertUnauthorized(); |
| 46 | + |
| 47 | + $this |
| 48 | + ->withToken($token) |
| 49 | + ->get($url) |
| 50 | + ->assertUnauthorized(); |
| 51 | + } |
| 52 | + |
| 53 | + #[Test] |
| 54 | + public function it_cant_authenticate_without_auth_token() |
| 55 | + { |
| 56 | + Facades\Config::set('statamic.api.auth_token', 'foobar'); |
| 57 | + |
| 58 | + $this |
| 59 | + ->getJson($url = '/api/collections/articles/entries') |
| 60 | + ->assertUnauthorized(); |
| 61 | + |
| 62 | + $this |
| 63 | + ->get($url) |
| 64 | + ->assertUnauthorized(); |
| 65 | + } |
| 66 | + |
| 67 | + #[Test] |
| 68 | + public function authentication_only_required_when_auth_token_is_set() |
| 69 | + { |
| 70 | + Facades\Config::set('statamic.api.auth_token', null); |
| 71 | + |
| 72 | + $this |
| 73 | + ->getJson($url = '/api/collections/articles/entries') |
| 74 | + ->assertOk(); |
| 75 | + |
| 76 | + $this |
| 77 | + ->get($url) |
| 78 | + ->assertOk(); |
| 79 | + } |
| 80 | +} |
0 commit comments