Skip to content

Commit de5116f

Browse files
committed
Introduce test case for OAuthClient
1 parent c5b1f5c commit de5116f

File tree

3 files changed

+125
-2
lines changed

3 files changed

+125
-2
lines changed

Classes/OAuthClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ public function startAuthorization(string $clientId, string $clientSecret, UriIn
267267
if ($oldAuthorization !== null) {
268268
$authorization = $oldAuthorization;
269269
}
270-
$authorization->setClientSecret($clientSecret);
271270
$this->entityManager->persist($authorization);
272271
$this->entityManager->flush();
273272
} catch (ORMException $exception) {
@@ -365,11 +364,13 @@ public function finishAuthorization(string $stateIdentifier, string $code, strin
365364
*/
366365
public function refreshAuthorization(string $authorizationId, string $clientId, string $returnToUri): string
367366
{
367+
throw new OAuthClientException('refreshAuthorization is currently not implemented', 1602519541);
368+
368369
$authorization = $this->entityManager->find(Authorization::class, ['authorizationId' => $authorizationId]);
369370
if (!$authorization instanceof Authorization) {
370371
throw new OAuthClientException(sprintf('OAuth2: Could not refresh OAuth token because authorization %s was not found in our database.', $authorization), 1505317044316);
371372
}
372-
$oAuthProvider = $this->createOAuthProvider($clientId, $authorization->getClientSecret());
373+
$oAuthProvider = $this->createOAuthProvider($clientId, $authorization->getClientSecret()); // FIXME
373374

374375
$this->logger->info(sprintf('OAuth (%s): Refreshing authorization %s for client "%s" using a %s bytes long secret and refresh token "%s".', $this->getServiceType(), $authorizationId, $clientId, strlen($authorization->getClientSecret()), $authorization->refreshToken));
375376

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Flownative\OAuth2\Client\Tests\Unit\Fixtures;
5+
6+
/*
7+
* This file is part of the Flownative.OAuth2.Client package.
8+
*
9+
* (c) Robert Lemke, Flownative GmbH - www.flownative.com
10+
*
11+
* This package is Open Source Software. For the full copyright and license
12+
* information, please view the LICENSE file which was distributed with this
13+
* source code.
14+
*/
15+
16+
use Flownative\OAuth2\Client\OAuthClient;
17+
18+
final class OAuthTestClient extends OAuthClient
19+
{
20+
public const TEST_SERVICE_TYPE = 'TestServiceType';
21+
public const TEST_BASE_URI = 'https://localbeach.net/';
22+
public const TEST_CLIENT_ID = 'my-client-id';
23+
24+
public function getServiceType(): string
25+
{
26+
return self::TEST_SERVICE_TYPE;
27+
}
28+
29+
public function getBaseUri(): string
30+
{
31+
return self::TEST_BASE_URI;
32+
}
33+
34+
public function getClientId(): string
35+
{
36+
return self::TEST_CLIENT_ID;
37+
}
38+
}

Tests/Unit/OAuthClientTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Flownative\OAuth2\Client;
5+
6+
require_once('Fixtures/OAuthTestClient.php');
7+
8+
/*
9+
* This file is part of the Flownative.OAuth2.Client package.
10+
*
11+
* (c) Robert Lemke, Flownative GmbH - www.flownative.com
12+
*
13+
* This package is Open Source Software. For the full copyright and license
14+
* information, please view the LICENSE file which was distributed with this
15+
* source code.
16+
*/
17+
18+
use Doctrine\ORM\EntityManagerInterface;
19+
use Doctrine\Persistence\ObjectRepository;
20+
use Flownative\OAuth2\Client\Tests\Unit\Fixtures\OAuthTestClient;
21+
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
22+
use Neos\Flow\Persistence\Repository;
23+
use Neos\Flow\Tests\UnitTestCase;
24+
25+
class OAuthClientTest extends UnitTestCase
26+
{
27+
/**
28+
* @test
29+
*/
30+
public function constructorSetsServiceName(): void
31+
{
32+
$client = new OAuthTestClient('my-service-name');
33+
self::assertSame('my-service-name', $client->getServiceName());
34+
}
35+
36+
/**
37+
* @test
38+
*/
39+
public function baseUriIsUsedForConstructingEndpointUris(): void
40+
{
41+
$client = new OAuthTestClient('my-service-name');
42+
43+
$actualUri = $client->getAccessTokenUri();
44+
$expectedUri = OAuthTestClient::TEST_BASE_URI . 'oauth/token';
45+
self::assertSame($expectedUri, $actualUri);
46+
47+
$actualUri = $client->getAuthorizeTokenUri();
48+
$expectedUri = OAuthTestClient::TEST_BASE_URI . 'oauth/token/authorize';
49+
self::assertSame($expectedUri, $actualUri);
50+
51+
$actualUri = $client->getResourceOwnerUri();
52+
$expectedUri = OAuthTestClient::TEST_BASE_URI . 'oauth/token/resource';
53+
self::assertSame($expectedUri, $actualUri);
54+
}
55+
56+
/**
57+
* @test
58+
*/
59+
public function generateAuthorizationIdQueryParameterName(): void
60+
{
61+
self::assertSame('flownative_oauth2_authorization_id_test-service-type', OAuthTestClient::generateAuthorizationIdQueryParameterName('test-service-type'));
62+
}
63+
64+
/**
65+
* @test
66+
*/
67+
public function getAuthorizationFetchesAuthorizationFromRepository(): void
68+
{
69+
$authorizationId = '3d47f0eafd6a8b49e32b55103d817b6e4ef489e7';
70+
$expectedAuthorization = new Authorization($authorizationId, 'service', 'clientId',Authorization::GRANT_AUTHORIZATION_CODE, 'profile');
71+
72+
$mockRepository = $this->createMock(ObjectRepository::class);
73+
$mockRepository->method('find')->with(['authorizationId' => $authorizationId])->willReturn($expectedAuthorization);
74+
75+
$mockEntityManager = $this->createMock(EntityManagerInterface::class);
76+
$mockEntityManager->method('getRepository')->with(Authorization::class)->willReturn($mockRepository);
77+
78+
$client = new OAuthTestClient('my-service-name');
79+
$client->injectEntityManager($mockEntityManager);
80+
81+
$actualAuthorization = $client->getAuthorization($authorizationId);
82+
self::assertSame($expectedAuthorization, $actualAuthorization);
83+
}
84+
}

0 commit comments

Comments
 (0)