File tree Expand file tree Collapse file tree 2 files changed +47
-4
lines changed
Test/Integration/Controller Expand file tree Collapse file tree 2 files changed +47
-4
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+ declare (strict_types=1 );
3
+
4
+ namespace RunAsRoot \PrometheusExporter \Test \Integration \Controller ;
5
+
6
+ use Magento \TestFramework \TestCase \AbstractController ;
7
+ use Magento \Framework \App \Request ;
8
+ use Magento \Framework \App \Response ;
9
+
10
+ /**
11
+ * @method Request\Http getRequest()
12
+ * @method Response\Http getResponse()
13
+ */
14
+ class IndexControllerTest extends AbstractController
15
+ {
16
+ /**
17
+ * @magentoAppArea frontend
18
+ */
19
+ public function testUnauthorizedResponse ()
20
+ {
21
+ $ this ->dispatch ('metrics/index/index ' );
22
+ $ this ->assertEquals (401 , $ this ->getResponse ()->getStatusCode (), 'Status code should be 401 Unauthorized ' );
23
+ $ this ->assertEquals (
24
+ 'You are not allowed to see these metrics. ' ,
25
+ $ this ->getResponse ()->getBody (),
26
+ 'Body should be error message '
27
+ );
28
+ }
29
+
30
+ /**
31
+ * @magentoAppArea frontend
32
+ * @magentoConfigFixture current_store metric_configuration/security/token supersecrettokenxxx
33
+ */
34
+ public function testAuthorizedResponse ()
35
+ {
36
+ $ this ->getRequest ()->getHeaders ()->addHeaderLine ('Authorization: Bearer supersecrettokenxxx ' );
37
+ $ this ->dispatch ('metrics/index/index ' );
38
+ $ this ->assertEquals (200 , $ this ->getResponse ()->getStatusCode (), 'Status code should be 200 OK ' );
39
+ }
40
+ }
Original file line number Diff line number Diff line change 4
4
5
5
namespace RunAsRoot \PrometheusExporter \Controller \Index ;
6
6
7
+ use Laminas \Http \Response ;
7
8
use Magento \Framework \App \Action \Action ;
8
9
use Magento \Framework \App \Action \Context ;
10
+ use Magento \Framework \App \Response \Http ;
9
11
use Magento \Framework \Controller \ResultFactory ;
10
12
use Magento \Framework \Controller \ResultInterface ;
11
13
use RunAsRoot \PrometheusExporter \Data \Config ;
@@ -33,11 +35,12 @@ public function execute(): ResultInterface
33
35
$ authorizationHeader = $ this ->getRequest ()->getHeader ('Authorization ' );
34
36
35
37
if ($ token !== $ authorizationHeader ) {
36
- /** @var \Magento\Framework\Controller\Result\Raw $response */
37
- $ response = $ this ->resultFactory ->create (ResultFactory::TYPE_RAW );
38
- $ response ->setContents ('You are not allowed to see these metrics. ' );
38
+ /** @var \Magento\Framework\Controller\Result\Raw $result */
39
+ $ result = $ this ->resultFactory ->create (ResultFactory::TYPE_RAW );
40
+ $ result ->setHttpResponseCode (Http::STATUS_CODE_401 );
41
+ $ result ->setContents ('You are not allowed to see these metrics. ' );
39
42
40
- return $ response ;
43
+ return $ result ;
41
44
}
42
45
43
46
return $ this ->prometheusResultFactory ->create ();
You can’t perform that action at this time.
0 commit comments