aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorChristoph Wurst <christoph@winzerhof-wurst.at>2017-01-03 09:57:52 +0100
committerRoeland Jago Douma <roeland@famdouma.nl>2017-01-11 11:01:54 +0100
commit243c9c09419252b379a9ad5e460d77a65e948b58 (patch)
tree2372d23463b72ed0616301d1cea5bb4a98ce0de3 /tests
parentb8d41752ca550763e23b29eca5e0ec4ec7d7ecb6 (diff)
downloadnextcloud-server-243c9c09419252b379a9ad5e460d77a65e948b58.tar.gz
nextcloud-server-243c9c09419252b379a9ad5e460d77a65e948b58.zip
fix coding style and increase code coverage
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests')
-rw-r--r--tests/Core/Controller/TwoFactorChallengeControllerTest.php151
1 files changed, 111 insertions, 40 deletions
diff --git a/tests/Core/Controller/TwoFactorChallengeControllerTest.php b/tests/Core/Controller/TwoFactorChallengeControllerTest.php
index 3486ae55e2e..bef343f9043 100644
--- a/tests/Core/Controller/TwoFactorChallengeControllerTest.php
+++ b/tests/Core/Controller/TwoFactorChallengeControllerTest.php
@@ -22,32 +22,52 @@
namespace Test\Core\Controller;
+use OC\Authentication\TwoFactorAuth\Manager;
use OC\Core\Controller\TwoFactorChallengeController;
+use OC_Util;
+use OCP\AppFramework\Http\RedirectResponse;
+use OCP\AppFramework\Http\TemplateResponse;
+use OCP\Authentication\TwoFactorAuth\IProvider;
+use OCP\Authentication\TwoFactorAuth\TwoFactorException;
+use OCP\IRequest;
+use OCP\ISession;
+use OCP\IURLGenerator;
+use OCP\IUser;
+use OCP\IUserSession;
+use OCP\Template;
+use PHPUnit_Framework_MockObject_MockObject;
use Test\TestCase;
class TwoFactorChallengeControllerTest extends TestCase {
+ /** @var IRequest|PHPUnit_Framework_MockObject_MockObject */
private $request;
+
+ /** @var Manager|PHPUnit_Framework_MockObject_MockObject */
private $twoFactorManager;
+
+ /** @var IUserSession|PHPUnit_Framework_MockObject_MockObject */
private $userSession;
+
+ /** @var ISession|PHPUnit_Framework_MockObject_MockObject */
private $session;
+
+ /** @var IURLGenerator|PHPUnit_Framework_MockObject_MockObject */
private $urlGenerator;
- /** @var TwoFactorChallengeController|\PHPUnit_Framework_MockObject_MockObject */
+ /** @var TwoFactorChallengeController|PHPUnit_Framework_MockObject_MockObject */
private $controller;
protected function setUp() {
parent::setUp();
- $this->request = $this->getMockBuilder('\OCP\IRequest')->getMock();
- $this->twoFactorManager = $this->getMockBuilder('\OC\Authentication\TwoFactorAuth\Manager')
- ->disableOriginalConstructor()
- ->getMock();
- $this->userSession = $this->getMockBuilder('\OCP\IUserSession')->getMock();
- $this->session = $this->getMockBuilder('\OCP\ISession')->getMock();
- $this->urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')->getMock();
+ $this->request = $this->createMock(IRequest::class);
+ $this->twoFactorManager = $this->createMock(Manager::class);
+ $this->userSession = $this->createMock(IUserSession::class);
+ $this->session = $this->createMock(ISession::class);
+ $this->urlGenerator = $this->createMock(IURLGenerator::class);
- $this->controller = $this->getMockBuilder('OC\Core\Controller\TwoFactorChallengeController')
+ $this->controller = $this->getMockBuilder(TwoFactorChallengeController::class)
->setConstructorArgs([
'core',
$this->request,
@@ -64,7 +84,7 @@ class TwoFactorChallengeControllerTest extends TestCase {
}
public function testSelectChallenge() {
- $user = $this->getMockBuilder('\OCP\IUser')->getMock();
+ $user = $this->getMockBuilder(IUser::class)->getMock();
$providers = [
'prov1',
'prov2',
@@ -82,27 +102,21 @@ class TwoFactorChallengeControllerTest extends TestCase {
->with($user)
->will($this->returnValue('backup'));
- $expected = new \OCP\AppFramework\Http\TemplateResponse('core', 'twofactorselectchallenge', [
+ $expected = new TemplateResponse('core', 'twofactorselectchallenge', [
'providers' => $providers,
'backupProvider' => 'backup',
'redirect_url' => '/some/url',
'logout_attribute' => 'logoutAttribute',
- ], 'guest');
+ ], 'guest');
$this->assertEquals($expected, $this->controller->selectChallenge('/some/url'));
}
public function testShowChallenge() {
- $user = $this->getMockBuilder('\OCP\IUser')->getMock();
- $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')
- ->disableOriginalConstructor()
- ->getMock();
- $backupProvider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')
- ->disableOriginalConstructor()
- ->getMock();
- $tmpl = $this->getMockBuilder('\OCP\Template')
- ->disableOriginalConstructor()
- ->getMock();
+ $user = $this->createMock(IUser::class);
+ $provider = $this->createMock(IProvider::class);
+ $backupProvider = $this->createMock(IProvider::class);
+ $tmpl = $this->createMock(Template::class);
$this->userSession->expects($this->once())
->method('getUser')
@@ -128,9 +142,7 @@ class TwoFactorChallengeControllerTest extends TestCase {
->will($this->returnValue(true));
$this->session->expects($this->exactly(2))
->method('remove')
- ->with($this->logicalOr(
- $this->equalTo('two_factor_auth_error'),
- $this->equalTo('two_factor_auth_error_message')));
+ ->with($this->logicalOr($this->equalTo('two_factor_auth_error'), $this->equalTo('two_factor_auth_error_message')));
$provider->expects($this->once())
->method('getTemplate')
->with($user)
@@ -139,7 +151,7 @@ class TwoFactorChallengeControllerTest extends TestCase {
->method('fetchPage')
->will($this->returnValue('<html/>'));
- $expected = new \OCP\AppFramework\Http\TemplateResponse('core', 'twofactorshowchallenge', [
+ $expected = new TemplateResponse('core', 'twofactorshowchallenge', [
'error' => true,
'provider' => $provider,
'backupProvider' => $backupProvider,
@@ -153,7 +165,7 @@ class TwoFactorChallengeControllerTest extends TestCase {
}
public function testShowInvalidChallenge() {
- $user = $this->getMockBuilder('\OCP\IUser')->getMock();
+ $user = $this->createMock(IUser::class);
$this->userSession->expects($this->once())
->method('getUser')
@@ -167,16 +179,14 @@ class TwoFactorChallengeControllerTest extends TestCase {
->with('core.TwoFactorChallenge.selectChallenge')
->will($this->returnValue('select/challenge/url'));
- $expected = new \OCP\AppFramework\Http\RedirectResponse('select/challenge/url');
+ $expected = new RedirectResponse('select/challenge/url');
$this->assertEquals($expected, $this->controller->showChallenge('myprovider', 'redirect/url'));
}
public function testSolveChallenge() {
- $user = $this->getMockBuilder('\OCP\IUser')->getMock();
- $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')
- ->disableOriginalConstructor()
- ->getMock();
+ $user = $this->createMock(IUser::class);
+ $provider = $this->createMock(IProvider::class);
$this->userSession->expects($this->once())
->method('getUser')
@@ -191,12 +201,37 @@ class TwoFactorChallengeControllerTest extends TestCase {
->with('myprovider', $user, 'token')
->will($this->returnValue(true));
- $expected = new \OCP\AppFramework\Http\RedirectResponse(\OC_Util::getDefaultPageUrl());
+ $expected = new RedirectResponse(OC_Util::getDefaultPageUrl());
$this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token'));
}
+ public function testSolveValidChallengeAndRedirect() {
+ $user = $this->createMock(IUser::class);
+ $provider = $this->createMock(IProvider::class);
+
+ $this->userSession->expects($this->once())
+ ->method('getUser')
+ ->will($this->returnValue($user));
+ $this->twoFactorManager->expects($this->once())
+ ->method('getProvider')
+ ->with($user, 'myprovider')
+ ->will($this->returnValue($provider));
+
+ $this->twoFactorManager->expects($this->once())
+ ->method('verifyChallenge')
+ ->with('myprovider', $user, 'token')
+ ->willReturn(true);
+ $this->urlGenerator->expects($this->once())
+ ->method('getAbsoluteURL')
+ ->with('redirect url')
+ ->willReturn('redirect/url');
+
+ $expected = new RedirectResponse('redirect/url');
+ $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token', 'redirect%20url'));
+ }
+
public function testSolveChallengeInvalidProvider() {
- $user = $this->getMockBuilder('\OCP\IUser')->getMock();
+ $user = $this->getMockBuilder(IUser::class)->getMock();
$this->userSession->expects($this->once())
->method('getUser')
@@ -210,16 +245,14 @@ class TwoFactorChallengeControllerTest extends TestCase {
->with('core.TwoFactorChallenge.selectChallenge')
->will($this->returnValue('select/challenge/url'));
- $expected = new \OCP\AppFramework\Http\RedirectResponse('select/challenge/url');
+ $expected = new RedirectResponse('select/challenge/url');
$this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token'));
}
public function testSolveInvalidChallenge() {
- $user = $this->getMockBuilder('\OCP\IUser')->getMock();
- $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')
- ->disableOriginalConstructor()
- ->getMock();
+ $user = $this->createMock(IUser::class);
+ $provider = $this->createMock(IProvider::class);
$this->userSession->expects($this->once())
->method('getUser')
@@ -247,7 +280,45 @@ class TwoFactorChallengeControllerTest extends TestCase {
->method('getId')
->will($this->returnValue('myprovider'));
- $expected = new \OCP\AppFramework\Http\RedirectResponse('files/index/url');
+ $expected = new RedirectResponse('files/index/url');
+ $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token', '/url'));
+ }
+
+ public function testSolveChallengeTwoFactorException() {
+ $user = $this->createMock(IUser::class);
+ $provider = $this->createMock(IProvider::class);
+ $exception = new TwoFactorException("2FA failed");
+
+ $this->userSession->expects($this->once())
+ ->method('getUser')
+ ->will($this->returnValue($user));
+ $this->twoFactorManager->expects($this->once())
+ ->method('getProvider')
+ ->with($user, 'myprovider')
+ ->will($this->returnValue($provider));
+
+ $this->twoFactorManager->expects($this->once())
+ ->method('verifyChallenge')
+ ->with('myprovider', $user, 'token')
+ ->will($this->throwException($exception));
+ $this->session->expects($this->at(0))
+ ->method('set')
+ ->with('two_factor_auth_error_message', "2FA failed");
+ $this->session->expects($this->at(1))
+ ->method('set')
+ ->with('two_factor_auth_error', true);
+ $this->urlGenerator->expects($this->once())
+ ->method('linkToRoute')
+ ->with('core.TwoFactorChallenge.showChallenge', [
+ 'challengeProviderId' => 'myprovider',
+ 'redirect_url' => '/url',
+ ])
+ ->will($this->returnValue('files/index/url'));
+ $provider->expects($this->once())
+ ->method('getId')
+ ->will($this->returnValue('myprovider'));
+
+ $expected = new RedirectResponse('files/index/url');
$this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token', '/url'));
}