| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- // tests/AuthenticationTest.php
- namespace App\Tests;
- use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
- use App\Entity\Access\Access;
- use App\Entity\Organization\Organization;
- use App\Entity\Person\Person;
- use Hautelook\AliceBundle\PhpUnit\ReloadDatabaseTrait;
- class AuthentificationTest extends ApiTestCase
- {
- use ReloadDatabaseTrait;
- public function testLogin(): void
- {
- $client = self::createClient();
- $container = self::getContainer();
- $organization = new Organization();
- $access = new Access();
- $user = new Person();
- $user->setUsername('foo');
- $user->setPassword(
- $container->get('security.user_password_hasher')->hashPassword($user, '$3CR3T')
- );
- $access->setPerson($user);
- $organization->addAccess($access);
- $manager = $container->get('doctrine')->getManager();
- $manager->persist($organization);
- $manager->flush();
- // retrieve a token
- $response = $client->request('POST', '/login_check', [
- 'headers' => ['Content-Type' => 'application/json'],
- 'json' => [
- 'username' => 'foo',
- 'password' => '$3CR3T',
- ],
- ]);
- $json = $response->toArray();
- self::assertResponseIsSuccessful();
- $this->assertArrayHasKey('token', $json);
- // test not authorized
- $client->request('GET', '/my_profile/1');
- self::assertResponseStatusCodeSame(401);
- // test authorized
- $client->request('GET', '/my_profile/1',
- [
- 'Content-Type' => 'application/ld+json',
- 'authorization' => sprintf('BEARER %s', $json['token']),
- 'x-accessid' => 1
- ]);
- self::assertResponseIsSuccessful();
- }
- }
|