|
| 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\Repository\UserRepository; |
| 9 | +use App\Tests\EtuUTTApiTestCase; |
| 10 | +use DateTimeInterface; |
| 11 | +use Faker\Provider\Address; |
| 12 | +use Faker\Provider\Uuid; |
| 13 | +use Symfony\Component\HttpFoundation\Response; |
| 14 | + |
| 15 | +class UpdateUser extends EtuUTTApiTestCase |
| 16 | +{ |
| 17 | + |
| 18 | + public function testNormal() : void |
| 19 | + { |
| 20 | + $client = static::createClient(); |
| 21 | + $client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'test', 'Content-Type' => 'application/merge-patch+json' ]]); |
| 22 | + $testUser = $this->createUser('foo', 'bar', 'foobar'); |
| 23 | + $testUserId = $testUser->getId(); |
| 24 | + $testUserBirthday = $testUser->getInfos()->getBirthday(); |
| 25 | + $crawler = $client->request('PATCH', '/users/'.$testUser->getId(), [ 'body' => json_encode([ |
| 26 | + 'socialNetwork' => [ |
| 27 | + 'facebook' => 'https://facebook.com/foobar', |
| 28 | + 'twitter' => 'https://twitter.com/foobar', |
| 29 | + 'instagram' => 'https://instagram.com/foobar', |
| 30 | + 'linkedin' => 'https://linkedin.com/foobar', |
| 31 | + 'pseudoDiscord' => 'foobar', |
| 32 | + 'wantDiscordUTT' => true |
| 33 | + ], |
| 34 | + 'RGPD' => [ |
| 35 | + 'isKeepingAccount' => true, |
| 36 | + 'isDeletingEverything' => true |
| 37 | + ], |
| 38 | + 'preferences' => [ |
| 39 | + 'birthdayDisplayOnlyAge' => false, |
| 40 | + 'language' => 'en', |
| 41 | + 'wantDaymail' => false, |
| 42 | + 'wantDayNotif' => false |
| 43 | + ], |
| 44 | + 'infos' => [ |
| 45 | + 'sex' => 'Féminin', |
| 46 | + 'nickname' => 'foobar', |
| 47 | + 'passions' => 'I have no passions :(', |
| 48 | + 'website' => 'https://foobar.com' |
| 49 | + ], |
| 50 | + 'addresses' => [ |
| 51 | + [ |
| 52 | + 'street' => 'avenue Foobar', |
| 53 | + 'postalCode' => '00 000', |
| 54 | + 'city' => 'Foobar City', |
| 55 | + 'country' => 'United States of Foobar' |
| 56 | + ] |
| 57 | + ], |
| 58 | + 'mailsPhones' => [ |
| 59 | + 'mailPersonal' => '[email protected]', |
| 60 | + 'phoneNumber' => '0123456789' |
| 61 | + ] |
| 62 | + ])]); |
| 63 | + $this->assertResponseStatusCodeSame(Response::HTTP_OK); |
| 64 | + $response = json_decode($crawler->getContent()); |
| 65 | + // User checks |
| 66 | + $this->assertEquals($testUserId, $response->{'id'}); |
| 67 | + $this->assertEquals('foobar', $response->{'login'}); |
| 68 | + $this->assertEquals(null, $response->{'studentId'}); |
| 69 | + $this->assertEquals('foo', $response->{'firstName'}); |
| 70 | + $this->assertEquals('bar', $response->{'lastName'}); |
| 71 | + // Social network checks |
| 72 | + $this->assertEquals('https://facebook.com/foobar', $response->{'socialNetwork'}->{'facebook'}); |
| 73 | + $this->assertEquals('https://twitter.com/foobar', $response->{'socialNetwork'}->{'twitter'}); |
| 74 | + $this->assertEquals('https://instagram.com/foobar', $response->{'socialNetwork'}->{'instagram'}); |
| 75 | + $this->assertEquals('https://linkedin.com/foobar', $response->{'socialNetwork'}->{'linkedin'}); |
| 76 | + $this->assertEquals('foobar', $response->{'socialNetwork'}->{'pseudoDiscord'}); |
| 77 | + $this->assertEquals(true, $response->{'socialNetwork'}->{'wantDiscordUTT'}); |
| 78 | + // RGPD checks |
| 79 | + $this->assertEmpty($response->{'RGPD'}); |
| 80 | + // Badges checks |
| 81 | + $this->assertEmpty($response->{'badges'}); |
| 82 | + // Badges checks |
| 83 | + //$this->assertEmpty($response->{'branche'}); |
| 84 | + // Formation checks |
| 85 | + //$this->assertEmpty($response->{'formation'}); |
| 86 | + // Preference checks |
| 87 | + $this->assertEmpty($response->{'preference'}); |
| 88 | + // Infos checks |
| 89 | + $this->assertEquals('Féminin', $response->{'infos'}->{'sex'}); |
| 90 | + $this->assertNull($response->{'infos'}->{'nationality'}); |
| 91 | + $this->assertEquals($testUserBirthday->format(DateTimeInterface::RFC3339), $response->{'infos'}->{'birthday'}); |
| 92 | + $this->assertEquals('/default_user_avatar.png', $response->{'infos'}->{'avatar'}); |
| 93 | + $this->assertEquals('foobar', $response->{'infos'}->{'nickname'}); |
| 94 | + $this->assertEquals('I have no passions :(', $response->{'infos'}->{'passions'}); |
| 95 | + $this->assertEquals('https://foobar.com', $response->{'infos'}->{'website'}); |
| 96 | + // Addresses checks |
| 97 | + $this->assertEmpty($response->{'addresses'}); |
| 98 | + $this->assertEquals(1, $response->{'addresses'}->length()); |
| 99 | + $this->assertEquals("avenue Foobar", $response->{'addresses'}[0]->{'street'}); |
| 100 | + $this->assertEquals("00 000", $response->{'addresses'}[0]->{'street'}); |
| 101 | + $this->assertEquals("Foobar City", $response->{'addresses'}[0]->{'street'}); |
| 102 | + $this->assertEquals("United States of Foobar", $response->{'addresses'}[0]->{'street'}); |
| 103 | + // Mails-phones checks |
| 104 | + $this->assertEquals(1, $response->{'mailsPhones'}->length()); |
| 105 | + $this-> assertEquals( '[email protected]', $response->{ 'mailsPhones'}->{ 'mailPersonal'}); |
| 106 | + $this->assertEquals('0123456789', $response->{'mailsPhones'}->{'phoneNumber'}); |
| 107 | + } |
| 108 | + |
| 109 | + public function testNotConnected() : void |
| 110 | + { |
| 111 | + $client = static::createClient(); |
| 112 | + $client->setDefaultOptions([ 'headers' => ['Content-Type' => 'application/merge-patch+json' ]]); |
| 113 | + $client->request('PATCH', '/users/'.$this->user->getId(), [ 'body' => []]); |
| 114 | + $this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED); |
| 115 | + $client->request('PATCH', '/users/'.Uuid::uuid(), [ 'body' => []]); |
| 116 | + $this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED); |
| 117 | + } |
| 118 | + |
| 119 | + public function testNonExistingUser() : void |
| 120 | + { |
| 121 | + $client = static::createClient(); |
| 122 | + $client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'test', 'Content-Type' => 'application/merge-patch+json' ]]); |
| 123 | + $client->request('PATCH', '/users/'.Uuid::uuid(), [ 'body' => []]); |
| 124 | + $this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND); |
| 125 | + } |
| 126 | + |
| 127 | + public function testNoParameter() : void |
| 128 | + { |
| 129 | + $client = static::createClient(); |
| 130 | + $client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'test', 'Content-Type' => 'application/merge-patch+json' ]]); |
| 131 | + $client->request('PATCH', '/users/'.$this->user->getId()); |
| 132 | + $this->assertResponseStatusCodeSame(Response::HTTP_BAD_REQUEST); |
| 133 | + } |
| 134 | + |
| 135 | + public function testSQLInjection() : void |
| 136 | + { |
| 137 | + $client = static::createClient(); |
| 138 | + $client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'test', 'Content-Type' => 'application/merge-patch+json' ]]); |
| 139 | + $testUser = $this->createUser('foo', 'bar', 'foobar'); |
| 140 | + $client->request('PATCH', '/users/\'', [ 'body' => '{}' ]); |
| 141 | + $this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND); |
| 142 | + $client->request('PATCH', '/users/"', [ 'body' => '{}' ]); |
| 143 | + $this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND); |
| 144 | + $client->request('PATCH', '/users/'.$testUser->getId(), [ 'body' => json_encode([ 'socialNetwork' => ['facebook' => '\''] ])]); |
| 145 | + $this->assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY); |
| 146 | + $client->request('PATCH', '/users/'.$testUser->getId(), [ 'body' => json_encode([ 'socialNetwork' => ['facebook' => '"'] ])]); |
| 147 | + $this->assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY); |
| 148 | + } |
| 149 | + |
| 150 | + public function testInvalidFieldContent() : void |
| 151 | + { |
| 152 | + $client = static::createClient(); |
| 153 | + $client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'test', 'Content-Type' => 'application/merge-patch+json' ]]); |
| 154 | + $testUser = $this->createUser('foo', 'bar', 'foobar'); |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments