|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Tests\Users; |
| 4 | + |
| 5 | +use App\DataFixtures\UserSeeder; |
| 6 | +use App\Entity\User; |
| 7 | +use App\Entity\UserAddress; |
| 8 | +use App\Tests\EtuUTTApiTestCase; |
| 9 | +use DateTimeInterface; |
| 10 | +use Faker\Provider\Address; |
| 11 | +use Faker\Provider\Uuid; |
| 12 | +use Symfony\Component\HttpFoundation\Response; |
| 13 | + |
| 14 | +class GetUserFromId extends EtuUTTApiTestCase |
| 15 | +{ |
| 16 | + |
| 17 | + public function testNormal() : void |
| 18 | + { |
| 19 | + $this->loadFixtures(new UserSeeder()); |
| 20 | + $client = static::createClient(); |
| 21 | + $client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'test' ]]); |
| 22 | + $users = $this->em->createQueryBuilder() |
| 23 | + ->select('user.id, user.login, user.studentId, user.firstName, user.lastName, |
| 24 | + socials.facebook, socials.twitter, socials.instagram, socials.linkedin, socials.pseudoDiscord, socials.wantDiscordUTT, |
| 25 | + infos.sex, infos.nationality, infos.birthday, infos.avatar, infos.nickname, infos.passions, infos.website, |
| 26 | + addresses.street, addresses.postalCode, addresses.city, addresses.country, |
| 27 | + mailsPhones.mailPersonal, mailsPhones.phoneNumber') |
| 28 | + ->from(User::class, 'user') |
| 29 | + ->innerJoin('user.socialNetwork', 'socials') |
| 30 | + ->innerJoin('user.infos', 'infos') |
| 31 | + ->innerJoin('user.addresses', 'addresses') |
| 32 | + ->innerJoin('user.mailsPhones', 'mailsPhones') |
| 33 | + ->getQuery() |
| 34 | + ->execute(); |
| 35 | + foreach ($users as $user) { |
| 36 | + $crawler = $client->request('GET', '/users/'.($user['id']->jsonSerialize())); |
| 37 | + $this->assertResponseStatusCodeSame(Response::HTTP_OK); |
| 38 | + $response = json_decode($crawler->getContent()); |
| 39 | + $this->assertEquals($user['id']->jsonSerialize(), $response->{'id'}); |
| 40 | + $this->assertEquals($user['login'], $response->{'login'}); |
| 41 | + $this->assertEquals($user['studentId'], $response->{'studentId'}); |
| 42 | + $this->assertEquals($user['firstName'], $response->{'firstName'}); |
| 43 | + $this->assertEquals($user['lastName'], $response->{'lastName'}); |
| 44 | + $this->assertEquals($user['facebook'], $response->{'socialNetwork'}->{'facebook'}); |
| 45 | + $this->assertEquals($user['twitter'], $response->{'socialNetwork'}->{'twitter'}); |
| 46 | + $this->assertEquals($user['instagram'], $response->{'socialNetwork'}->{'instagram'}); |
| 47 | + $this->assertEquals($user['linkedin'], $response->{'socialNetwork'}->{'linkedin'}); |
| 48 | + $this->assertEquals($user['pseudoDiscord'], $response->{'socialNetwork'}->{'pseudoDiscord'}); |
| 49 | + $this->assertEquals($user['wantDiscordUTT'], $response->{'socialNetwork'}->{'wantDiscordUTT'}); |
| 50 | + $this->assertEquals($user['sex'], $response->{'infos'}->{'sex'}); |
| 51 | + $this->assertEquals($user['nationality'], $response->{'infos'}->{'nationality'}); |
| 52 | + // RFC3339 is the default normalization format of the date with symfony : |
| 53 | + // https://github.com/symfony/symfony/blob/60b1a2af0d819a98cde0b2144b3b22415f30d6c1/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php#L29 |
| 54 | + $this->assertEquals($user['birthday']->format(DateTimeInterface::RFC3339), $response->{'infos'}->{'birthday'}); |
| 55 | + $this->assertEquals($user['avatar'], $response->{'infos'}->{'avatar'}); |
| 56 | + $this->assertEquals($user['nickname'], $response->{'infos'}->{'nickname'}); |
| 57 | + $this->assertEquals($user['passions'], $response->{'infos'}->{'passions'}); |
| 58 | + $this->assertEquals($user['website'], $response->{'infos'}->{'website'}); |
| 59 | + $this->assertEquals($user['street'], $response->{'addresses'}[0]->{'street'}); |
| 60 | + $this->assertEquals($user['postalCode'], $response->{'addresses'}[0]->{'postalCode'}); |
| 61 | + $this->assertEquals($user['city'], $response->{'addresses'}[0]->{'city'}); |
| 62 | + $this->assertEquals($user['country'], $response->{'addresses'}[0]->{'country'}); |
| 63 | + $this->assertEquals($user['mailPersonal'], $response->{'mailsPhones'}->{'mailPersonal'}); |
| 64 | + $this->assertEquals($user['phoneNumber'], $response->{'mailsPhones'}->{'phoneNumber'}); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public function testNotConnected() : void |
| 69 | + { |
| 70 | + $client = static::createClient(); |
| 71 | + $client->request('GET', '/users/'.$this->user->getId()); |
| 72 | + $this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED); |
| 73 | + $client->request('GET', '/users/'.(Uuid::uuid())); |
| 74 | + $this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED); |
| 75 | + } |
| 76 | + |
| 77 | + public function testNonExistingUser() : void |
| 78 | + { |
| 79 | + $client = static::createClient(); |
| 80 | + $client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'test' ]]); |
| 81 | + $client->request('GET', '/users/'.Uuid::uuid()); |
| 82 | + $this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND); |
| 83 | + } |
| 84 | + |
| 85 | + public function testSQLInjection() : void |
| 86 | + { |
| 87 | + $client = static::createClient(); |
| 88 | + $client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'test' ]]); |
| 89 | + $client->request('GET', '/users/\''); |
| 90 | + $this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND); |
| 91 | + $client->request('GET', '/users/"'); |
| 92 | + $this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND); |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments