@@ -19,6 +19,10 @@ class AccessDeniedListenerTest extends WebTestCase
1919
2020 public static function setUpBeforeClass ()
2121 {
22+ if (!interface_exists (ErrorRendererInterface::class)) {
23+ self ::markTestSkipped ();
24+ }
25+
2226 parent ::setUpBeforeClass ();
2327 static ::$ client = static ::createClient (['test_case ' => 'AccessDeniedListener ' ]);
2428 }
@@ -29,65 +33,39 @@ public static function tearDownAfterClass()
2933 parent ::tearDownAfterClass ();
3034 }
3135
32- protected function setUp ()
33- {
34- if (!interface_exists (ErrorRendererInterface::class)) {
35- $ this ->markTestSkipped ();
36- }
37- }
38-
39- public function testBundleListenerHandlesExceptionsInRestZonesWithoutLogin ()
36+ public function testNoCredentialsGives403 ()
4037 {
41- static ::$ client ->request ('GET ' , '/api/comments ' );
38+ static ::$ client ->request ('POST ' , '/api/login ' , [], [], ['CONTENT_TYPE ' => 'application/json ' ]);
39+ $ response = static ::$ client ->getResponse ();
4240
43- $ this ->assertEquals (401 , static :: $ client -> getResponse () ->getStatusCode ());
44- $ this ->assertEquals ('application/json ' , static :: $ client -> getResponse () ->headers ->get ('Content-Type ' ));
41+ $ this ->assertEquals (403 , $ response ->getStatusCode ());
42+ $ this ->assertEquals ('application/json ' , $ response ->headers ->get ('Content-Type ' ));
4543 }
4644
47- public function testBundleListenerHandlesExceptionsInRestZonesWithLogin ()
45+ public function testWrongLoginGives401 ()
4846 {
49- $ credentials = [
50- 'PHP_AUTH_USER ' => 'restapi ' ,
51- 'PHP_AUTH_PW ' => 'secretpw ' ,
52- ];
47+ static ::$ client ->request ('POST ' , '/api/login ' , [], [], ['HTTP_X-FOO ' => 'BAR ' , 'CONTENT_TYPE ' => 'application/json ' ]);
48+ $ response = static ::$ client ->getResponse ();
5349
54- static ::$ client ->request ('GET ' , '/api/comments ' , [], [], $ credentials );
55-
56- $ this ->assertEquals (200 , static ::$ client ->getResponse ()->getStatusCode ());
57- $ this ->assertEquals ('application/json ' , static ::$ client ->getResponse ()->headers ->get ('Content-Type ' ));
50+ $ this ->assertEquals (401 , $ response ->getStatusCode ());
51+ $ this ->assertEquals ('application/json ' , $ response ->headers ->get ('Content-Type ' ));
5852 }
5953
60- public function testBundleListenerHandlesExceptionsInRestZonesWrongLogin ()
54+ public function testSuccessfulLogin ()
6155 {
62- $ credentials = [
63- 'PHP_AUTH_USER ' => 'admin ' ,
64- 'PHP_AUTH_PW ' => 'secretpw ' ,
65- ];
66-
67- static ::$ client ->request ('GET ' , '/api/comments ' , [], [], $ credentials );
68-
69- $ this ->assertEquals (403 , static ::$ client ->getResponse ()->getStatusCode ());
70- $ this ->assertEquals ('application/json ' , static ::$ client ->getResponse ()->headers ->get ('Content-Type ' ));
71- }
72-
73- public function testBundleListenerHandlesExceptionsInRestZonesWithIncorrectLogin ()
74- {
75- $ credentials = [
76- 'PHP_AUTH_USER ' => 'restapi ' ,
77- 'PHP_AUTH_PW ' => 'foobar ' ,
78- ];
79-
80- static ::$ client ->request ('GET ' , '/api/comments ' , [], [], $ credentials );
56+ static ::$ client ->request ('POST ' , '/api/login ' , [], [], ['HTTP_X-FOO ' => 'FOOBAR ' , 'CONTENT_TYPE ' => 'application/json ' ]);
57+ $ response = static ::$ client ->getResponse ();
8158
82- $ this ->assertEquals (401 , static :: $ client -> getResponse () ->getStatusCode ());
83- $ this ->assertEquals ('application/json ' , static :: $ client -> getResponse () ->headers ->get ('Content-Type ' ));
59+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
60+ $ this ->assertEquals ('application/json ' , $ response ->headers ->get ('Content-Type ' ));
8461 }
8562
86- public function testSymfonyListenerHandlesExceptionsOutsideRestZones ()
63+ public function testAccessDeniedExceptionGives403 ()
8764 {
88- static ::$ client ->request ('GET ' , '/admin/comments ' );
65+ static ::$ client ->request ('GET ' , '/api/comments ' , [], [], ['CONTENT_TYPE ' => 'application/json ' ]);
66+ $ response = static ::$ client ->getResponse ();
8967
90- $ this ->assertEquals (302 , static :: $ client -> getResponse () ->getStatusCode ());
91- $ this ->assertEquals ('text/html; charset=UTF-8 ' , static :: $ client -> getResponse () ->headers ->get ('Content-Type ' ));
68+ $ this ->assertEquals (403 , $ response ->getStatusCode ());
69+ $ this ->assertEquals ('application/json ' , $ response ->headers ->get ('Content-Type ' ));
9270 }
9371}
0 commit comments