feat: read BusProNet base data from the MyE&P API

This commit is contained in:
Björn Fromme authored and Björn Fromme committed 2026-09-29 11:08:04 +02:00
1 parent 1505f9b50b
commit 2e9328fc51
37 files changed
+2302 -148

No files matched your search

+43 -19
View File
@@ -12,14 +12,12 @@ use App\Security\OAuth2\MyEpClient;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Flagception\Manager\FeatureManagerInterface;
use GuzzleHttp\Psr7\Request as Psr7Request;
use GuzzleHttp\Psr7\Response as Psr7Response;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Token\AccessToken;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
@@ -346,6 +344,38 @@ class MyEpAuthenticatorTest extends TestCase
$this->assertTrue($authenticator->supports($request));
}
public function testARejectedUserinfoRequestRefusesTheLogin(): void
{
$httpClient = new MockHttpClient(
new MockResponse('{"message":"Unauthorized"}', ['http_code' => 401]),
'https://my.example.com/',
);
$authenticator = $this->createAuthenticator($this->createUserinfo(['ROLE_TEAMER']), true, $httpClient);
$request = new Request(query: ['code' => 'code', 'state' => 'state']);
$request->setSession(new Session(new MockArraySessionStorage()));
$this->expectException(CustomUserMessageAuthenticationException::class);
$authenticator->authenticate($request);
}
public function testAnUnreadableUserinfoPayloadRefusesTheLogin(): void
{
$httpClient = new MockHttpClient(
new MockResponse('<html>gateway timeout</html>'),
'https://my.example.com/',
);
$authenticator = $this->createAuthenticator($this->createUserinfo(['ROLE_TEAMER']), true, $httpClient);
$request = new Request(query: ['code' => 'code', 'state' => 'state']);
$request->setSession(new Session(new MockArraySessionStorage()));
$this->expectException(CustomUserMessageAuthenticationException::class);
$authenticator->authenticate($request);
}
/**
* @param string[] $roles
*/
@@ -385,31 +415,25 @@ class MyEpAuthenticatorTest extends TestCase
return $user;
}
private function createAuthenticator(array $userinfo, bool $featureActive = true): MyEpAuthenticator
private function userinfoClient(array $userinfo): MockHttpClient
{
$httpClient = $this->createMock(ClientInterface::class);
$httpClient
->method('sendRequest')
->willReturn(new Psr7Response(200, [], json_encode($userinfo)))
;
$provider = $this->createMock(AbstractProvider::class);
$provider->method('getResourceOwnerDetailsUrl')->willReturn('https://my.example.com/api/userinfo');
$provider
->method('getAuthenticatedRequest')
->willReturn(new Psr7Request('GET', 'https://my.example.com/api/userinfo'))
;
$provider->method('getHttpClient')->willReturn($httpClient);
return new MockHttpClient(
new MockResponse(json_encode($userinfo)),
'https://my.example.com/',
);
}
private function createAuthenticator(array $userinfo, bool $featureActive = true, ?MockHttpClient $httpClient = null): MyEpAuthenticator
{
$client = $this->createMock(MyEpClient::class);
$client->method('fetchAccessToken')->willReturn(new AccessToken(['access_token' => 'token']));
$client->method('getProvider')->willReturn($provider);
$featureManager = $this->createMock(FeatureManagerInterface::class);
$featureManager->method('isActive')->willReturn($featureActive);
return new MyEpAuthenticator(
$client,
$httpClient ?? $this->userinfoClient($userinfo),
new UserDataHandler($this->entityManager, $this->createMock(LoggerInterface::class)),
$this->entityManager,
$this->createMock(UrlGeneratorInterface::class),