The clean & modern RSS server that doesn't give you any crap. https://thearsse.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestInternal.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /** @license MIT
  3. * Copyright 2017 J. King, Dustin Wilson et al.
  4. * See LICENSE and AUTHORS files for details */
  5. declare(strict_types=1);
  6. namespace JKingWeb\Arsse\TestCase\User;
  7. use JKingWeb\Arsse\Arsse;
  8. use JKingWeb\Arsse\Conf;
  9. use JKingWeb\Arsse\Database;
  10. use JKingWeb\Arsse\User;
  11. use JKingWeb\Arsse\AbstractException as Exception;
  12. use JKingWeb\Arsse\User\Driver as DriverInterface;
  13. use JKingWeb\Arsse\User\Internal\Driver;
  14. use Phake;
  15. /** @covers \JKingWeb\Arsse\User\Internal\Driver */
  16. class TestInternal extends \JKingWeb\Arsse\Test\AbstractTest {
  17. public function setUp() {
  18. self::clearData();
  19. self::setConf();
  20. // create a mock database interface
  21. Arsse::$db = Phake::mock(Database::class);
  22. Phake::when(Arsse::$db)->begin->thenReturn(Phake::mock(\JKingWeb\Arsse\Db\Transaction::class));
  23. }
  24. public function testConstruct() {
  25. $this->assertInstanceOf(DriverInterface::class, new Driver);
  26. }
  27. public function testFetchDriverName() {
  28. $this->assertTrue(strlen(Driver::driverName()) > 0);
  29. }
  30. /**
  31. * @dataProvider provideAuthentication
  32. * @group slow
  33. */
  34. public function testAuthenticateAUser(bool $authorized, string $user, string $password, bool $exp) {
  35. if ($authorized) {
  36. Phake::when(Arsse::$db)->userPasswordGet("john.doe@example.com")->thenReturn('$2y$10$1zbqRJhxM8uUjeSBPp4IhO90xrqK0XjEh9Z16iIYEFRV4U.zeAFom'); // hash of "secret"
  37. Phake::when(Arsse::$db)->userPasswordGet("jane.doe@example.com")->thenReturn('$2y$10$bK1ljXfTSyc2D.NYvT.Eq..OpehLRXVbglW.23ihVuyhgwJCd.7Im'); // hash of "superman"
  38. Phake::when(Arsse::$db)->userPasswordGet("owen.hardy@example.com")->thenReturn("");
  39. Phake::when(Arsse::$db)->userPasswordGet("kira.nerys@example.com")->thenThrow(new \JKingWeb\Arsse\User\Exception("doesNotExist"));
  40. } else {
  41. Phake::when(Arsse::$db)->userPasswordGet->thenThrow(new \JKingWeb\Arsse\User\ExceptionAuthz("notAuthorized"));
  42. }
  43. $this->assertSame($exp, (new Driver)->auth($user, $password));
  44. }
  45. public function provideAuthentication() {
  46. $john = "john.doe@example.com";
  47. $jane = "jane.doe@example.com";
  48. $owen = "owen.hardy@example.com";
  49. $kira = "kira.nerys@example.com";
  50. return [
  51. [false, $john, "secret", false],
  52. [false, $jane, "superman", false],
  53. [false, $owen, "", false],
  54. [false, $kira, "ashalla", false],
  55. [true, $john, "secret", true],
  56. [true, $jane, "superman", true],
  57. [true, $owen, "", true],
  58. [true, $kira, "ashalla", false],
  59. [true, $john, "top secret", false],
  60. [true, $jane, "clark kent", false],
  61. [true, $owen, "watchmaker", false],
  62. [true, $kira, "singha", false],
  63. [true, $john, "", false],
  64. [true, $jane, "", false],
  65. [true, $kira, "", false],
  66. ];
  67. }
  68. public function testAuthorizeAnAction() {
  69. Phake::verifyNoFurtherInteraction(Arsse::$db);
  70. $this->assertTrue((new Driver)->authorize("someone", "something"));
  71. }
  72. public function testListUsers() {
  73. $john = "john.doe@example.com";
  74. $jane = "jane.doe@example.com";
  75. Phake::when(Arsse::$db)->userList->thenReturn([$john, $jane])->thenReturn([$jane, $john]);
  76. $driver = new Driver;
  77. $this->assertSame([$john, $jane], $driver->userList());
  78. $this->assertSame([$jane, $john], $driver->userList());
  79. Phake::verify(Arsse::$db, Phake::times(2))->userList;
  80. }
  81. public function testCheckThatAUserExists() {
  82. $john = "john.doe@example.com";
  83. $jane = "jane.doe@example.com";
  84. Phake::when(Arsse::$db)->userExists($john)->thenReturn(true);
  85. Phake::when(Arsse::$db)->userExists($jane)->thenReturn(false);
  86. $driver = new Driver;
  87. $this->assertTrue($driver->userExists($john));
  88. Phake::verify(Arsse::$db)->userExists($john);
  89. $this->assertFalse($driver->userExists($jane));
  90. Phake::verify(Arsse::$db)->userExists($jane);
  91. }
  92. public function testAddAUser() {
  93. $john = "john.doe@example.com";
  94. Phake::when(Arsse::$db)->userAdd->thenReturnCallback(function($user, $pass) {
  95. return $pass;
  96. });
  97. $driver = new Driver;
  98. $this->assertNull($driver->userAdd($john));
  99. $this->assertNull($driver->userAdd($john, null));
  100. $this->assertSame("secret", $driver->userAdd($john, "secret"));
  101. Phake::verify(Arsse::$db)->userAdd($john, "secret");
  102. Phake::verify(Arsse::$db)->userAdd;
  103. }
  104. public function testRemoveAUser() {
  105. $john = "john.doe@example.com";
  106. Phake::when(Arsse::$db)->userRemove->thenReturn(true)->thenThrow(new \JKingWeb\Arsse\User\Exception("doesNotExist"));
  107. $driver = new Driver;
  108. $this->assertTrue($driver->userRemove($john));
  109. Phake::verify(Arsse::$db, Phake::times(1))->userRemove($john);
  110. $this->assertException("doesNotExist", "User");
  111. try {
  112. $this->assertFalse($driver->userRemove($john));
  113. } finally {
  114. Phake::verify(Arsse::$db, Phake::times(2))->userRemove($john);
  115. }
  116. }
  117. public function testSetAPassword() {
  118. $john = "john.doe@example.com";
  119. Phake::verifyNoFurtherInteraction(Arsse::$db);
  120. $this->assertSame("superman", (new Driver)->userPasswordSet($john, "superman"));
  121. $this->assertSame(null, (new Driver)->userPasswordSet($john, null));
  122. }
  123. }