-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
JsonLoginTest.php
90 lines (77 loc) · 3.82 KB
/
JsonLoginTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class JsonLoginTest extends AbstractWebTestCase
{
/**
* @dataProvider provideSecuritySystems
*/
public function testDefaultJsonLoginSuccess(array $options)
{
$client = $this->createClient($options + ['test_case' => 'JsonLogin', 'root_config' => 'config.yml']);
$client->request('POST', '/chk', [], [], ['CONTENT_TYPE' => 'application/json'], '{"user": {"login": "dunglas", "password": "foo"}}');
$response = $client->getResponse();
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame(['message' => 'Welcome @dunglas!'], json_decode($response->getContent(), true));
}
/**
* @dataProvider provideSecuritySystems
*/
public function testDefaultJsonLoginFailure(array $options)
{
$client = $this->createClient($options + ['test_case' => 'JsonLogin', 'root_config' => 'config.yml']);
$client->request('POST', '/chk', [], [], ['CONTENT_TYPE' => 'application/json'], '{"user": {"login": "dunglas", "password": "bad"}}');
$response = $client->getResponse();
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertSame(401, $response->getStatusCode());
$this->assertSame(['error' => 'Invalid credentials.'], json_decode($response->getContent(), true));
}
/**
* @dataProvider provideSecuritySystems
*/
public function testCustomJsonLoginSuccess(array $options)
{
$client = $this->createClient($options + ['test_case' => 'JsonLogin', 'root_config' => 'custom_handlers.yml']);
$client->request('POST', '/chk', [], [], ['CONTENT_TYPE' => 'application/json'], '{"user": {"login": "dunglas", "password": "foo"}}');
$response = $client->getResponse();
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame(['message' => 'Good game @dunglas!'], json_decode($response->getContent(), true));
}
/**
* @dataProvider provideSecuritySystems
*/
public function testCustomJsonLoginFailure(array $options)
{
$client = $this->createClient($options + ['test_case' => 'JsonLogin', 'root_config' => 'custom_handlers.yml']);
$client->request('POST', '/chk', [], [], ['CONTENT_TYPE' => 'application/json'], '{"user": {"login": "dunglas", "password": "bad"}}');
$response = $client->getResponse();
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertSame(500, $response->getStatusCode());
$this->assertSame(['message' => 'Something went wrong'], json_decode($response->getContent(), true));
}
/**
* @dataProvider provideSecuritySystems
*/
public function testDefaultJsonLoginBadRequest(array $options)
{
$client = $this->createClient($options + ['test_case' => 'JsonLogin', 'root_config' => 'config.yml']);
$client->request('POST', '/chk', [], [], ['CONTENT_TYPE' => 'application/json'], 'Not a json content');
$response = $client->getResponse();
$this->assertSame(400, $response->getStatusCode());
$this->assertSame('application/json', $response->headers->get('Content-Type'));
$this->assertSame(['type' => 'https://tools.ietf.org/html/rfc2616#section-10', 'title' => 'An error occurred', 'status' => 400, 'detail' => 'Bad Request'], json_decode($response->getContent(), true));
}
}