Skip to content
This repository was archived by the owner on Apr 3, 2023. It is now read-only.

Commit ce44631

Browse files
author
Teddy Roncin
committed
✅ (DELETE /users/{id}) started tests for this route
testing normal calls, when user doesn't have the permissions, when the user is not connected, when the user does not exist and verified sql injections
1 parent 64c2a9d commit ce44631

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

tests/Users/DeleteUser.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace App\Tests\Users;
4+
5+
use App\Entity\User;
6+
use App\Tests\EtuUTTApiTestCase;
7+
use Faker\Provider\Uuid;
8+
use Symfony\Component\HttpFoundation\Response;
9+
10+
class DeleteUser extends EtuUTTApiTestCase
11+
{
12+
13+
public function testNormal() : void
14+
{
15+
$client = static::createClient();
16+
$client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'test' ]]);
17+
$tempUser = new User();
18+
$tempUser->setLogin('foobar');
19+
$tempUser->setFirstName('foo');
20+
$tempUser->setLastName('bar');
21+
$this->em->persist($tempUser);
22+
$this->em->flush();
23+
$client->request('DELETE', '/users/'.$tempUser->getId());
24+
$this->assertResponseStatusCodeSame(Response::HTTP_NO_CONTENT);
25+
$users = $this->em->createQueryBuilder()
26+
->select('user.id')
27+
->from(User::class, 'user')
28+
->where('user.id=\''.$tempUser->getId().'\'')
29+
->getQuery()
30+
->execute();
31+
$this->assertEmpty($users);
32+
}
33+
34+
public function testNoPermission() : void
35+
{
36+
$client = static::createClient();
37+
$client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'test' ]]);
38+
$this->user->removeRole('ROLE_ADMIN');
39+
$tempUser = new User();
40+
$tempUser->setLogin('foobar');
41+
$tempUser->setFirstName('foo');
42+
$tempUser->setLastName('bar');
43+
$this->em->persist($tempUser);
44+
$this->em->flush();
45+
// Test with non existing user
46+
$client->request('DELETE', '/users/'.Uuid::uuid());
47+
$this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED);
48+
// Test with existing user
49+
$client->request('DELETE', '/users/'.$tempUser->getId());
50+
$this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED);
51+
$users = $this->em->createQueryBuilder()
52+
->select('user.id')
53+
->from(User::class, 'user')
54+
->where('user.id=\''.$tempUser->getId().'\'')
55+
->getQuery()
56+
->execute();
57+
$this->assertNotEmpty($users);
58+
}
59+
60+
public function testNotConnected() : void
61+
{
62+
$client = static::createClient();
63+
$testUser = $this->em->createQueryBuilder()
64+
->select('user.id')
65+
->from(User::class, 'user')
66+
->where('user.login = \'test\'')
67+
->getQuery()
68+
->execute();
69+
$client->request('DELETE', '/users/'.($testUser[0]['id']->jsonSerialize()));
70+
$this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED);
71+
$client->request('DELETE', '/users/'.(Uuid::uuid()));
72+
$this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED);
73+
}
74+
75+
public function testNonExistingUser() : void
76+
{
77+
$client = static::createClient();
78+
$client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'test' ]]);
79+
$client->request('DELETE', '/users/'.Uuid::uuid());
80+
$this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
81+
}
82+
83+
public function testSQLInjection() : void
84+
{
85+
$client = static::createClient();
86+
$client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'test' ]]);
87+
$client->request('DELETE', '/users/\'');
88+
$this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
89+
$client->request('DELETE', '/users/"');
90+
$this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
91+
}
92+
93+
}

0 commit comments

Comments
 (0)