TokenControllerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @author Christoph Wurst <christoph@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Tests\Core\Controller;
  22. use OC\AppFramework\Http;
  23. use OC\Authentication\Token\IToken;
  24. use OC\Core\Controller\TokenController;
  25. use OCP\AppFramework\Http\JSONResponse;
  26. use Test\TestCase;
  27. class TokenControllerTest extends TestCase {
  28. /** \OC\Core\Controller\TokenController */
  29. private $tokenController;
  30. private $request;
  31. private $userManager;
  32. private $tokenProvider;
  33. private $twoFactorAuthManager;
  34. private $secureRandom;
  35. protected function setUp() {
  36. parent::setUp();
  37. $this->request = $this->getMock('\OCP\IRequest');
  38. $this->userManager = $this->getMockBuilder('\OC\User\Manager')
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->tokenProvider = $this->getMock('\OC\Authentication\Token\IProvider');
  42. $this->twoFactorAuthManager = $this->getMockBuilder('\OC\Authentication\TwoFactorAuth\Manager')
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->secureRandom = $this->getMock('\OCP\Security\ISecureRandom');
  46. $this->tokenController = new TokenController('core', $this->request, $this->userManager, $this->tokenProvider, $this->twoFactorAuthManager, $this->secureRandom);
  47. }
  48. public function testWithoutCredentials() {
  49. $expected = new JSONResponse();
  50. $expected->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY);
  51. $actual = $this->tokenController->generateToken(null, null);
  52. $this->assertEquals($expected, $actual);
  53. }
  54. public function testWithInvalidCredentials() {
  55. $this->userManager->expects($this->once())
  56. ->method('checkPassword')
  57. ->with('john', 'passme')
  58. ->will($this->returnValue(false));
  59. $expected = new JSONResponse();
  60. $expected->setStatus(Http::STATUS_UNAUTHORIZED);
  61. $actual = $this->tokenController->generateToken('john', 'passme');
  62. $this->assertEquals($expected, $actual);
  63. }
  64. public function testWithValidCredentials() {
  65. $user = $this->getMock('\OCP\IUser');
  66. $this->userManager->expects($this->once())
  67. ->method('checkPassword')
  68. ->with('john', '123456')
  69. ->will($this->returnValue($user));
  70. $user->expects($this->once())
  71. ->method('getUID')
  72. ->will($this->returnValue('john'));
  73. $this->twoFactorAuthManager->expects($this->once())
  74. ->method('isTwoFactorAuthenticated')
  75. ->with($user)
  76. ->will($this->returnValue(false));
  77. $this->secureRandom->expects($this->once())
  78. ->method('generate')
  79. ->with(128)
  80. ->will($this->returnValue('verysecurerandomtoken'));
  81. $this->tokenProvider->expects($this->once())
  82. ->method('generateToken')
  83. ->with('verysecurerandomtoken', 'john', 'john', '123456', 'unknown client', IToken::PERMANENT_TOKEN);
  84. $expected = [
  85. 'token' => 'verysecurerandomtoken'
  86. ];
  87. $actual = $this->tokenController->generateToken('john', '123456');
  88. $this->assertEquals($expected, $actual);
  89. }
  90. public function testWithValidCredentialsBut2faEnabled() {
  91. $user = $this->getMock('\OCP\IUser');
  92. $this->userManager->expects($this->once())
  93. ->method('checkPassword')
  94. ->with('john', '123456')
  95. ->will($this->returnValue($user));
  96. $this->twoFactorAuthManager->expects($this->once())
  97. ->method('isTwoFactorAuthenticated')
  98. ->with($user)
  99. ->will($this->returnValue(true));
  100. $this->secureRandom->expects($this->never())
  101. ->method('generate');
  102. $expected = new JSONResponse();
  103. $expected->setStatus(Http::STATUS_UNAUTHORIZED);
  104. $actual = $this->tokenController->generateToken('john', '123456');
  105. $this->assertEquals($expected, $actual);
  106. }
  107. }