DefaultControllerTest::testCleaning()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\KI\CoreBundle\Controller;
4
5
use Tests\KI\CoreBundle\WebTestCase;
6
7
class DefaultControllerTest extends WebTestCase
8
{
9
    public function testCleaning()
10
    {
11
        $this->client->request('GET', '/clean');
12
        $this->assertJsonResponse($this->client->getResponse(), 204);
13
    }
14
15
    public function testDirty()
16
    {
17
        $this->client->request('GET', '/dirty');
18
        $this->assertJsonResponse($this->client->getResponse(), 302);
19
    }
20
21
    public function testLoginFailure()
22
    {
23
        $client = static::createClient();
24
        $client->request('POST', '/login', ['username' => 'user', 'password' => 'userwrongpass']);
25
        $this->assertJsonResponse($client->getResponse(), 401, true);
26
    }
27
28
    public function testLoginSuccess()
29
    {
30
        $client = static::createClient();
31
        $client->request('POST', '/login', ['username' => 'trancara', 'password' => 'password']);
32
33
        $this->assertJsonResponse($client->getResponse(), 200, true);
34
        $response = json_decode($client->getResponse()->getContent(), true);
35
        $this->assertArrayHasKey('token', $response);
36
        $this->assertArrayHasKey('data', $response);
37
38
        // On vérifie que le token de la requête marche bien
39
        $client = static::createClient();
40
        $client->request('HEAD', $this->getUrl('ping', [$this->queryParameterName => $response['token']]));
41
        $this->assertJsonResponse($client->getResponse(), 204);
42
43
        // On vérifie que le token reçu marche bien
44
        $client = static::createClient();
45
        $client->setServerParameter('HTTP_Authorization', sprintf('%s %s', $this->authorizationHeaderPrefix, $response['token']));
46
        $client->request('HEAD', '/ping');
47
        $this->assertJsonResponse($client->getResponse(), 204);
48
49
        // On vérifie que le token reçu marche plusieurs fois tant qu'il est valide
50
        $client = static::createClient();
51
        $client->setServerParameter('HTTP_Authorization', sprintf('%s %s', $this->authorizationHeaderPrefix, $response['token']));
52
        $client->request('HEAD', '/ping');
53
        $this->assertJsonResponse($client->getResponse(), 204);
54
55
        // On vérifie qu'un mauvais token ne marche pas
56
        $client = static::createClient();
57
        $client->setServerParameter('HTTP_Authorization', sprintf('%s %s', $this->authorizationHeaderPrefix, $response['token'].'changed'));
58
        $client->request('GET', '/movies');
59
        $this->assertJsonResponse($client->getResponse(), 401);
60
61
        // On vérifie qu'une erreur est retournée si l'on ne précise pas le header d'autorisation
62
        $client = static::createClient();
63
        $client->request('GET', '/movies');
64
        $this->assertJsonResponse($client->getResponse(), 401);
65
    }
66
67
    public function testMaintenance()
68
    {
69
        $this->client->request('DELETE', '/maintenance');
70
        $this->assertJsonResponse($this->client->getResponse(), 400);
71
72
        $this->client->request('POST', '/maintenance', ['until' => time()]);
73
        $this->assertJsonResponse($this->client->getResponse(), 204);
74
75
        $this->client->request('HEAD', '/ping');
76
        $this->assertJsonResponse($this->client->getResponse(), 503);
77
78
        $this->client->request('DELETE', '/maintenance');
79
        $this->assertJsonResponse($this->client->getResponse(), 204);
80
81
        $this->client->request('HEAD', '/ping');
82
        $this->assertJsonResponse($this->client->getResponse(), 204);
83
    }
84
85
    public function testPing()
86
    {
87
        $this->client->request('HEAD', '/ping');
88
        $this->assertJsonResponse($this->client->getResponse(), 204);
89
90
        $this->client->request('GET', '/ping');
91
        $this->assertJsonResponse($this->client->getResponse(), 204);
92
93
        $client = static::createClient();
94
        $client->request('HEAD', '/ping');
95
        $this->assertJsonResponse($client->getResponse(), 204);
96
    }
97
98 View Code Duplication
    public function testSearch()
99
    {
100
        $this->client->request('POST', '/search', ['search' => 'User/al']);
101
        $response = $this->client->getResponse();
102
        $this->assertJsonResponse($response, 200);
103
104
        $this->client->request('POST', '/search', ['search' => '']);
105
        $response = $this->client->getResponse();
106
        $this->assertJsonResponse($response, 400);
107
108
        $this->client->request('POST', '/search', ['search' => 'Users/']);
109
        $response = $this->client->getResponse();
110
        $this->assertJsonResponse($response, 400);
111
112
        $this->client->request('POST', '/search', ['search' => 'al']);
113
        $response = $this->client->getResponse();
114
        $this->assertJsonResponse($response, 400);
115
116
        $this->client->request('POST', '/search', ['search' => 'Miam/']);
117
        $response = $this->client->getResponse();
118
        $this->assertJsonResponse($response, 400);
119
120
        $this->client->request('POST', '/search', ['search' => 'Miam/ps']);
121
        $response = $this->client->getResponse();
122
        $this->assertJsonResponse($response, 400);
123
    }
124
125
    public function testVersion()
126
    {
127
        $this->client->request('GET', '/version');
128
        $this->assertJsonResponse($this->client->getResponse(), 200);
129
        $response = json_decode($this->client->getResponse()->getContent(), true);
130
        $this->assertArrayHasKey('version', $response);
131
        $this->assertArrayHasKey('major', $response);
132
        $this->assertArrayHasKey('minor', $response);
133
        $this->assertArrayHasKey('build', $response);
134
    }
135
}
136