SecurityTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\AdminAudit\Tests\Actions;
  26. use OCA\AdminAudit\Actions\Security;
  27. use OCA\AdminAudit\AuditLogger;
  28. use OCP\Authentication\TwoFactorAuth\IProvider;
  29. use OCP\IUser;
  30. use PHPUnit\Framework\MockObject\MockObject;
  31. use Test\TestCase;
  32. class SecurityTest extends TestCase {
  33. private AuditLogger|MockObject $logger;
  34. private Security $security;
  35. private MockObject|IUser $user;
  36. protected function setUp(): void {
  37. parent::setUp();
  38. $this->logger = $this->createMock(AuditLogger::class);
  39. $this->security = new Security($this->logger);
  40. $this->user = $this->createMock(IUser::class);
  41. $this->user->method('getUID')->willReturn('myuid');
  42. $this->user->method('getDisplayName')->willReturn('mydisplayname');
  43. }
  44. public function testTwofactorFailed() {
  45. $this->logger->expects($this->once())
  46. ->method('info')
  47. ->with(
  48. $this->equalTo('Failed two factor attempt by user mydisplayname (myuid) with provider myprovider'),
  49. ['app' => 'admin_audit']
  50. );
  51. $provider = $this->createMock(IProvider::class);
  52. $provider->method('getDisplayName')
  53. ->willReturn('myprovider');
  54. $this->security->twofactorFailed($this->user, $provider);
  55. }
  56. public function testTwofactorSuccess() {
  57. $this->logger->expects($this->once())
  58. ->method('info')
  59. ->with(
  60. $this->equalTo('Successful two factor attempt by user mydisplayname (myuid) with provider myprovider'),
  61. ['app' => 'admin_audit']
  62. );
  63. $provider = $this->createMock(IProvider::class);
  64. $provider->method('getDisplayName')
  65. ->willReturn('myprovider');
  66. $this->security->twofactorSuccess($this->user, $provider);
  67. }
  68. }