-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor to avoid duplication of code in SessionVerifier and A…
…uthModule
- Loading branch information
1 parent
7e2e586
commit af61681
Showing
9 changed files
with
209 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ | |
|
||
namespace OCA\OpenIdConnect\Tests\Unit; | ||
|
||
use JsonException; | ||
use Jumbojett\OpenIDConnectClientException; | ||
use OCA\OpenIdConnect\Client; | ||
use OCP\IConfig; | ||
use OCP\ISession; | ||
|
@@ -76,7 +78,7 @@ protected function setUp(): void { | |
|
||
$this->client = $this->getMockBuilder(Client::class) | ||
->setConstructorArgs([$this->config, $this->urlGenerator, $this->session, $this->logger]) | ||
->setMethods(['fetchURL']) | ||
->onlyMethods(['fetchURL']) | ||
->getMock(); | ||
} | ||
|
||
|
@@ -91,7 +93,7 @@ public function testGetConfig(): void { | |
*/ | ||
public function testGetAppConfig($expectedData, $dataInConfig, $expectedErrorMessage = null): void { | ||
$this->config->method('getSystemValue')->willReturn('from system config'); | ||
$this->config->expects(self::once())->method('getAppValue')->willReturnCallback(function ($app, $key, $default) use ($dataInConfig) { | ||
$this->config->expects(self::once())->method('getAppValue')->willReturnCallback(function () use ($dataInConfig) { | ||
return $dataInConfig; | ||
}); | ||
if ($expectedErrorMessage) { | ||
|
@@ -101,13 +103,20 @@ public function testGetAppConfig($expectedData, $dataInConfig, $expectedErrorMes | |
self::assertEquals($expectedData, $return); | ||
} | ||
|
||
/** | ||
* @throws OpenIDConnectClientException | ||
* @throws JsonException | ||
*/ | ||
public function testGetWellKnown(): void { | ||
$this->client->setProviderURL('https://example.net'); | ||
$this->client->expects(self::once())->method('fetchURL')->with('https://example.net/.well-known/openid-configuration')->willReturn('{"foo": "bar"}'); | ||
$return = $this->client->getWellKnownConfig(); | ||
self::assertEquals((object)['foo' => 'bar'], $return); | ||
} | ||
|
||
/** | ||
* @throws OpenIDConnectClientException | ||
*/ | ||
public function testCtor(): void { | ||
$providerUrl = 'https://example.net'; | ||
|
||
|
@@ -132,14 +141,17 @@ public function testCtor(): void { | |
}); | ||
$this->client = $this->getMockBuilder(Client::class) | ||
->setConstructorArgs([$this->config, $this->urlGenerator, $this->session, $this->logger]) | ||
->setMethods(['fetchURL']) | ||
->onlyMethods(['fetchURL']) | ||
->getMock(); | ||
|
||
self::assertEquals($providerUrl, $this->client->getProviderURL()); | ||
self::assertEquals(true, $this->client->getVerifyHost()); | ||
self::assertEquals(true, $this->client->getVerifyPeer()); | ||
} | ||
|
||
/** | ||
* @throws OpenIDConnectClientException | ||
*/ | ||
public function testCtorInsecure(): void { | ||
$providerUrl = 'https://example.net'; | ||
|
||
|
@@ -165,7 +177,7 @@ public function testCtorInsecure(): void { | |
}); | ||
$this->client = $this->getMockBuilder(Client::class) | ||
->setConstructorArgs([$this->config, $this->urlGenerator, $this->session, $this->logger]) | ||
->setMethods(['fetchURL']) | ||
->onlyMethods(['fetchURL']) | ||
->getMock(); | ||
|
||
self::assertEquals($providerUrl, $this->client->getProviderURL()); | ||
|
@@ -176,15 +188,16 @@ public function testCtorInsecure(): void { | |
/** | ||
* @dataProvider providesGetUserInfoData | ||
* @param $useAccessTokenPayloadForUserInfo | ||
* @throws JsonException | ||
* @throws OpenIDConnectClientException | ||
*/ | ||
public function testGetUserInfo($useAccessTokenPayloadForUserInfo): void { | ||
$this->config->method('getSystemValue')->willReturnCallback(static function ($key) use ($useAccessTokenPayloadForUserInfo) { | ||
$this->config->method('getSystemValue')->willReturnCallback(static function ($key) { | ||
if ($key === 'openid-connect') { | ||
return [ | ||
'provider-url' => '$providerUrl', | ||
'client-id' => 'client-id', | ||
'client-secret' => 'secret', | ||
'use-access-token-payload-for-user-info' => $useAccessTokenPayloadForUserInfo | ||
]; | ||
} | ||
if ($key === 'proxy') { | ||
|
@@ -202,18 +215,18 @@ public function testGetUserInfo($useAccessTokenPayloadForUserInfo): void { | |
->getMock(); | ||
if ($useAccessTokenPayloadForUserInfo) { | ||
$this->client->expects(self::never())->method('requestUserInfo'); | ||
$this->client->expects(self::once())->method('getAccessTokenPayload')->willReturn([ | ||
$this->client->expects(self::once())->method('getAccessTokenPayload')->willReturn((object)[ | ||
'preferred_username' => '[email protected]' | ||
]); | ||
} else { | ||
$this->client->expects(self::never())->method('getAccessTokenPayload'); | ||
$this->client->expects(self::once())->method('requestUserInfo')->willReturn([ | ||
$this->client->expects(self::once())->method('getAccessTokenPayload')->willReturn(null); | ||
$this->client->expects(self::once())->method('requestUserInfo')->willReturn((object)[ | ||
'preferred_username' => '[email protected]' | ||
]); | ||
} | ||
|
||
$info = $this->client->getUserInfo(); | ||
self::assertEquals([ | ||
self::assertEquals((object)[ | ||
'preferred_username' => '[email protected]' | ||
], $info); | ||
} | ||
|
Oops, something went wrong.