Skip to content

Commit

Permalink
feat(oauth2): Add a test for skipping auth page
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc committed Dec 19, 2024
1 parent ecdbc26 commit cdd4dec
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions apps/oauth2/tests/Controller/LoginRedirectorControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace OCA\OAuth2\Tests\Controller;

use OC\Core\Controller\ClientFlowLoginController;
use OCA\OAuth2\Controller\LoginRedirectorController;
use OCA\OAuth2\Db\Client;
use OCA\OAuth2\Db\ClientMapper;
Expand Down Expand Up @@ -84,6 +85,53 @@ public function testAuthorize(): void {
$this->assertEquals($expected, $this->loginRedirectorController->authorize('MyClientId', 'MyState', 'code'));
}

public function testAuthorizeSkipGrant(): void {
$client = new Client();
$client->setName('MyClientName');
$client->setClientIdentifier('MyClientIdentifier');
$this->clientMapper
->expects($this->once())
->method('getByIdentifier')
->with('MyClientId')
->willReturn($client);
$this->session
->expects(static::exactly(2))
->method('set')
->willReturnCallback(function (string $key, string $value): void {
switch ([$key, $value]) {
case ['oauth.state', 'MyState']:
case [ClientFlowLoginController::STATE_NAME, 'MyStateToken']:
/* Expected */
break;
default:
throw new LogicException();
}
});
$this->appConfig
->expects(static::once())
->method('getValueArray')
->with('oauth2', 'autoGrantApplications', [])
->willReturn(['MyClientName']);
$this->random
->expects(static::once())
->method('generate')
->willReturn('MyStateToken');
$this->urlGenerator
->expects($this->once())
->method('linkToRouteAbsolute')
->with(
'core.ClientFlowLogin.grantPage',
[
'stateToken' => 'MyStateToken',
'clientIdentifier' => 'MyClientIdentifier',
]
)
->willReturn('https://example.com/?clientIdentifier=foo');

$expected = new RedirectResponse('https://example.com/?clientIdentifier=foo');
$this->assertEquals($expected, $this->loginRedirectorController->authorize('MyClientId', 'MyState', 'code'));
}

public function testAuthorizeWrongResponseType(): void {
$client = new Client();
$client->setClientIdentifier('MyClientIdentifier');
Expand Down

0 comments on commit cdd4dec

Please sign in to comment.