Skip to content

Commit

Permalink
Adding more Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khushboo-singhvi committed Nov 7, 2024
1 parent 3386fc2 commit 2707735
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
17 changes: 6 additions & 11 deletions Controller/Webhook/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ private function isDuplicate(array $response)
*/
private function fixCgiHttpAuthentication()
{
if ($this->request->getServer('PHP_AUTH_USER') && $this->request->getServer('PHP_AUTH_PW')) {
if (!empty($this->request->getServer('PHP_AUTH_USER')) &&
!empty($this->request->getServer('PHP_AUTH_PW'))) {
return;
}

Expand All @@ -393,16 +394,10 @@ private function fixCgiHttpAuthentication()
];

foreach ($authorizationHeaders as $header) {
$authHeader = $this->request->getServer($header);

if ($authHeader) {
list(
$phpAuthUser, $phpAuthPw
) = explode(':', base64_decode(substr($authHeader, 6)), 2);

$params = $this->request->getServer();
$params->set('PHP_AUTH_USER', $phpAuthUser);
$params->set('PHP_AUTH_PW', $phpAuthPw);
$authValue = $this->request->getServer($header);
if (!empty($authValue)) {
list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) =
explode(':', base64_decode(substr((string) $authValue, 6)), 2);
return;
}
}
Expand Down
49 changes: 24 additions & 25 deletions Test/Unit/Controller/Webhook/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,38 +159,37 @@ public function testLoadNotificationFromRequest()
);
}

public function testFixCgiHttpAuthenticationWithExistingAuthUser()
public function fixCgiHttpAuthenticationDataProvider()
{
// Mock the scenario where PHP_AUTH_USER and PHP_AUTH_PW are set
$this->httpMock->method('getServer')
->will($this->returnCallback(function ($header) {
if ($header === 'PHP_AUTH_USER') {
return 'user';
} elseif ($header === 'PHP_AUTH_PW') {
return 'password';
}
return null; // Return null for all other headers
}));

// Call the private method
$this->invokeMethod($this->indexController, 'fixCgiHttpAuthentication');

// No assertion needed, just ensuring no exception is thrown
$this->assertTrue(true);
return [
'valid_auth_header' => [
'PHP_AUTH_VALUE' => base64_encode('user:password'), // Encoded base64 value
'expectedUser' => 'user',
'expectedPassword' => 'password'
],
'no_auth_header' => [
null, // No authorization header
null, // Expected user
null // Expected password
]
];
}

public function testFixCgiHttpAuthenticationWithNoAuthHeaders()

/**
* @dataProvider fixCgiHttpAuthenticationDataProvider
*/
public function testFixCgiHttpAuthentication($phpAuthHeader, $expectedUser, $expectedPassword)
{
// Mock all relevant server variables to return null
// Mock the getServer method to return the provided PHP_AUTH value
$this->httpMock->method('getServer')
->will($this->returnValue(null)); // All headers return null
->willReturn($phpAuthHeader);

// Call the private method
// Call the method you want to test
$this->invokeMethod($this->indexController, 'fixCgiHttpAuthentication');

// No assertion needed, just ensuring no exception is thrown
$this->assertTrue(true);
// Assert the values are set in the $_SERVER global
$this->assertEquals($expectedUser, $_SERVER['PHP_AUTH_USER']);
$this->assertEquals($expectedPassword, $_SERVER['PHP_AUTH_PW']);
}


}

0 comments on commit 2707735

Please sign in to comment.