Skip to content

Commit

Permalink
final test
Browse files Browse the repository at this point in the history
Signed-off-by: Sagar <sagargurung1001@gmail.com>
  • Loading branch information
SagarGi committed Dec 24, 2024
1 parent e842164 commit 7b4c74e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/ExchangedTokenRequestedEventHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public function __construct(
$this->config = $config;
}

/**
* @return ExchangedTokenRequestedEvent
*/
public function getEvent(): ExchangedTokenRequestedEvent {
return new ExchangedTokenRequestedEvent(
$this->config->getAppValue(Application::APP_ID, 'targeted_audience_client_id', '')
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/OpenProjectAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1625,10 +1625,10 @@ public function getOIDCToken(): ?string {
return null;
}
try {
$event = $this->exchangedTokenRequestedEventHelper->getEvent();
/** @psalm-suppress InvalidArgument for dispatchTyped($event)
* but new ExchangedTokenRequestedEvent(targeted_audience_client_id) returns event
*/
$event = $this->exchangedTokenRequestedEventHelper->getEvent();
$this->eventDispatcher->dispatchTyped($event);
} catch (TokenExchangeFailedException $e) {
$this->logger->debug('Failed to exchange token: ' . $e->getMessage());
Expand Down
4 changes: 4 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<referencedClass name="OCA\TermsOfService\Db\Mapper\TermsMapper" />
<!-- these classes belong to the user_oidc app, which isn't compulsory, so might not exist while running psalm -->
<referencedClass name="OCA\UserOIDC\Db\ProviderMapper" />
<referencedClass name="OCA\UserOIDC\Model\Token" />
<referencedClass name="OCA\UserOIDC\Event\ExchangedTokenRequestedEvent" />
<referencedClass name="OCA\UserOIDC\Exception\TokenExchangeFailedException" />
<!-- these classes belong to the activity app, which isn't compulsory, so might not exist while running psalm -->
Expand Down Expand Up @@ -87,6 +88,9 @@
<referencedClass name="OC\Http\Client\LocalAddressChecker"/>
<!-- these are classes form terms_of_service app, which isn't cloned while doing static code analysis -->
<referencedClass name="OCA\TermsOfService\Db\Mapper\SignatoryMapper" />
<!-- these classes belong to the user_oidc app, which isn't compulsory, so might not exist while running psalm -->
<referencedClass name="OCA\UserOIDC\Model\Token" />
<referencedClass name="OCA\UserOIDC\Event\ExchangedTokenRequestedEvent" />
<!-- these are classes form activity app, which isn't cloned while doing static code analysis -->
<referencedClass name="OCA\Activity\UserSettings" />
<referencedClass name="OCA\Activity\GroupHelperDisabled" />
Expand Down
47 changes: 42 additions & 5 deletions tests/lib/Service/OpenProjectAPIServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,9 @@ private function getOpenProjectAPIService(
}

/**
* Since our app currently has two authorization methods, the test employing this mock service has no effect on the authorization method.
*
*
* @param array<string> $onlyMethods
* @param IRootFolder|null $rootMock
* @param ICacheFactory|null $cacheFactoryMock
Expand Down Expand Up @@ -4310,6 +4313,8 @@ public function testIsServerSideEncryptionEnabled(
$this->assertEquals($expectedResult, $actualResult);
}



/**
* @return void
*/
Expand Down Expand Up @@ -4342,19 +4347,51 @@ public function testGetOIDCTokenSuccess(): void {
$exchangeTokenEvent
);
$result = $service->getOIDCToken();
self::assertSame('exchanged-access-token', $result);
$this->assertEquals('exchanged-access-token', $result);
}

/**
* @return void
*/

public function testGetOIDCTokenUserOIDCAppNotInstalled(): void {
$configMock = $this->getMockBuilder(IConfig::class)->getMock();
$iManagerMock = $this->getMockBuilder(IManager::class)->getMock();
$iAppManagerMock = $this->getMockBuilder(IAppManager::class)->getMock();
$iAppManagerMock->method('isInstalled')->willReturn(true);
$service = $this->getServiceMock(
[],
null,
null,
null,
null,
$iAppManagerMock,
null,
null,
$configMock,
null,
null,
null,
$iManagerMock,
null
);
$result = $service->getOIDCToken();
$this->assertEquals(null, $result);
}

/**
* @return void
*/

public function testGetOIDCTokenErrorTokenExchange(): void {
public function testGetOIDCTokenExchangeFailedException(): void {
$configMock = $this->getMockBuilder(IConfig::class)->getMock();
$iManagerMock = $this->getMockBuilder(IManager::class)->getMock();
$iAppManagerMock = $this->getMockBuilder(IAppManager::class)->getMock();
$iAppManagerMock->method('isInstalled')->willReturn(true);
$exchangeTokenEvent = $this->getMockBuilder(ExchangedTokenRequestedEventHelper::class)->disableOriginalConstructor()->getMock();
/** @psalm-suppress InvalidArgument for getEvent
* get event can throw TokenExchangeFailedException in case there is failure in token exchange from the user_oidc app
*/
$exchangeTokenEvent->method('getEvent')->willThrowException(new TokenExchangeFailedException('Token exchanged failed'));
$service = $this->getServiceMock(
[],
Expand All @@ -4373,14 +4410,14 @@ public function testGetOIDCTokenErrorTokenExchange(): void {
$exchangeTokenEvent
);
$result = $service->getOIDCToken();
self::assertSame(null, $result);
$this->assertEquals(null, $result);
}

/**
* @return void
*/

public function testGetOIDCTokenErrorTokenNull(): void {
public function testGetOIDCTokenNoToken(): void {
$configMock = $this->getMockBuilder(IConfig::class)->getMock();
$iManagerMock = $this->getMockBuilder(IManager::class)->getMock();
$iAppManagerMock = $this->getMockBuilder(IAppManager::class)->getMock();
Expand All @@ -4406,6 +4443,6 @@ public function testGetOIDCTokenErrorTokenNull(): void {
$exchangeTokenEvent
);
$result = $service->getOIDCToken();
self::assertSame(null, $result);
$this->assertEquals(null, $result);
}
}

0 comments on commit 7b4c74e

Please sign in to comment.