Skip to content

Commit

Permalink
Add telegram tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zorn-v committed Nov 30, 2023
1 parent 25d89d0 commit a0905de
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/OAuth/ResourceOwner/TelegramResourceOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace HWI\Bundle\OAuthBundle\OAuth\ResourceOwner;

use HWI\Bundle\OAuthBundle\OAuth\Response\PathUserResponse;
use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -90,6 +91,7 @@ public function getUserInformation(array $accessToken, array $extraParameters =
$response = $this->getUserResponse();
$response->setData($data);
$response->setResourceOwner($this);
$response->setOAuthToken(new OAuthToken($accessToken));

return $response;
}
Expand Down
192 changes: 192 additions & 0 deletions tests/OAuth/ResourceOwner/TelegramResourceOwnerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php

/*
* This file is part of the HWIOAuthBundle package.
*
* (c) Hardware Info <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace HWI\Bundle\OAuthBundle\Tests\OAuth\ResourceOwner;

use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\TelegramResourceOwner;
use HWI\Bundle\OAuthBundle\Test\Fixtures\CustomUserResponse;
use HWI\Bundle\OAuthBundle\Test\OAuth\ResourceOwner\GenericOAuth2ResourceOwnerTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\LazyResponseException;

/**
* @author zorn-v
*/
final class TelegramResourceOwnerTest extends GenericOAuth2ResourceOwnerTestCase
{
protected string $resourceOwnerClass = TelegramResourceOwner::class;

protected array $options = [
'client_id' => 'clientid',
'client_secret' => 'client:secret',

'authorization_url' => 'http://user.auth/?test=2',
];

protected array $paths = [
'identifier' => 'id',
'nickname' => 'username',
];

protected array $tokenData = ['access_token' => 'eyJpZCI6MSwidXNlcm5hbWUiOiJiYXIifQ=='];

protected string $authorizationUrlBasePart = 'http://user.auth/?test=2&bot_id=client&origin=http%3A%2F%2Fredirect.to%2F';
protected string $redirectUrlPart = '&return_to=http%3A%2F%2Fredirect.to%2F';

public function testHandleRequest(): void
{
$resourceOwner = $this->createResourceOwner();

$request = new Request(['code' => 'test']);
$this->assertTrue($resourceOwner->handles($request));

$request = new Request(['code' => 'test', 'test' => 'test']);
$this->assertTrue($resourceOwner->handles($request));

$request = new Request(['test' => 'test']);
$this->expectException(LazyResponseException::class);
$resourceOwner->handles($request);
}

private function getAuthToken(array $authData, string $secret)
{
ksort($authData);
$dataStr = '';
foreach ($authData as $k => $v) {
$dataStr .= sprintf("\n%s=%s", $k, $v);
}
$dataStr = substr($dataStr, 1);
$secretKey = hash('sha256', $secret, true);
$authData['hash'] = hash_hmac('sha256', $dataStr, $secretKey);

return base64_encode(json_encode($authData));
}

public function testGetAccessToken(string $response = '', string $contentType = ''): void
{
$resourceOwner = $this->createResourceOwner(
[],
[],
[
$this->createMockResponse($response, $contentType),
]
);
$token = $this->getAuthToken(['id' => 1, 'auth_date' => time()], $this->options['client_secret']);

$request = new Request(['code' => $token]);

$this->assertEquals(
$token,
$resourceOwner->getAccessToken($request, 'http://redirect.to/')
);
}

public function testGetAccessTokenExpired(string $response = '', string $contentType = ''): void
{
$resourceOwner = $this->createResourceOwner(
[],
[],
[
$this->createMockResponse($response, $contentType),
]
);
$token = $this->getAuthToken(['id' => 1, 'auth_date' => 123], $this->options['client_secret']);

$request = new Request(['code' => $token]);

$this->expectExceptionMessage('Telegram auth data expired');
$resourceOwner->getAccessToken($request, 'http://redirect.to/');
}

public function testGetAccessTokenInvalidHash(string $response = '', string $contentType = ''): void
{
$resourceOwner = $this->createResourceOwner(
[],
[],
[
$this->createMockResponse($response, $contentType),
]
);
$token = $this->getAuthToken(['id' => 1, 'auth_date' => time()], 'invalid');

$request = new Request(['code' => $token]);

$this->expectExceptionMessage('Telegram auth data check failed');
$resourceOwner->getAccessToken($request, 'http://redirect.to/');
}

public function testRefreshAccessToken($response = '', $contentType = ''): void
{
$this->markTestSkipped('There is no refresh tokens');
}

public function testRefreshAccessTokenInvalid(string $response = '', string $exceptionClass = ''): void
{
$this->markTestSkipped('There is no refresh tokens');
}

public function testGetUserInformation(): void
{
$resourceOwner = $this->createResourceOwner(
[],
[],
[
$this->createMockResponse($this->userResponse),
]
);

/** @var AbstractUserResponse $userResponse */
$userResponse = $resourceOwner->getUserInformation($this->tokenData);

Check failure on line 147 in tests/OAuth/ResourceOwner/TelegramResourceOwnerTest.php

View workflow job for this annotation

GitHub Actions / phpstan

PHPDoc tag @var for variable $userResponse contains unknown class HWI\Bundle\OAuthBundle\Tests\OAuth\ResourceOwner\AbstractUserResponse.

$this->assertEquals('1', $userResponse->getUsername());

Check failure on line 149 in tests/OAuth/ResourceOwner/TelegramResourceOwnerTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getUsername() on an unknown class HWI\Bundle\OAuthBundle\Tests\OAuth\ResourceOwner\AbstractUserResponse.
$this->assertEquals('bar', $userResponse->getNickname());

Check failure on line 150 in tests/OAuth/ResourceOwner/TelegramResourceOwnerTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getNickname() on an unknown class HWI\Bundle\OAuthBundle\Tests\OAuth\ResourceOwner\AbstractUserResponse.
$this->assertEquals($this->tokenData['access_token'], $userResponse->getAccessToken());

Check failure on line 151 in tests/OAuth/ResourceOwner/TelegramResourceOwnerTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getAccessToken() on an unknown class HWI\Bundle\OAuthBundle\Tests\OAuth\ResourceOwner\AbstractUserResponse.
$this->assertNull($userResponse->getRefreshToken());

Check failure on line 152 in tests/OAuth/ResourceOwner/TelegramResourceOwnerTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getRefreshToken() on an unknown class HWI\Bundle\OAuthBundle\Tests\OAuth\ResourceOwner\AbstractUserResponse.
$this->assertNull($userResponse->getExpiresIn());

Check failure on line 153 in tests/OAuth/ResourceOwner/TelegramResourceOwnerTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getExpiresIn() on an unknown class HWI\Bundle\OAuthBundle\Tests\OAuth\ResourceOwner\AbstractUserResponse.
}

public function testInvalidOptionValueThrowsException(): void
{

}

public function testGetUserInformationFailure(): void
{
$this->markTestSkipped('There is no extra http request for get user information');
}

public function testGetAuthorizationUrlWithEnabledCsrf(): void
{
$this->markTestSkipped('No CSRF is available for this Resource Owner.');
}

public function testCustomResponseClass(): void
{
$class = CustomUserResponse::class;

$resourceOwner = $this->createResourceOwner(
['user_response_class' => $class],
[],
[
$this->createMockResponse($this->userResponse),
]
);

$userResponse = $resourceOwner->getUserInformation($this->tokenData);

$this->assertInstanceOf($class, $userResponse);
$this->assertEquals('foo666', $userResponse->getUsername());
$this->assertEquals('foo', $userResponse->getNickname());
$this->assertEquals($this->tokenData['access_token'], $userResponse->getAccessToken());
$this->assertNull($userResponse->getRefreshToken());
$this->assertNull($userResponse->getExpiresIn());
}
}

0 comments on commit a0905de

Please sign in to comment.