SecurityTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace App\Tests\Controller;
  20. use App\Util\GNUsocialTestCase;
  21. class SecurityTest extends GNUsocialTestCase
  22. {
  23. // --------- Login --------------
  24. private function testLogin(string $nickname, string $password)
  25. {
  26. // This calls static::bootKernel(), and creates a "client" that is acting as the browser
  27. $client = static::createClient();
  28. $crawler = $client->request('GET', '/main/login');
  29. $this->assertResponseIsSuccessful();
  30. // $form = $crawler->selectButton('Sign in')->form();
  31. $crawler = $client->submitForm('Sign in', [
  32. '_username' => $nickname,
  33. '_password' => $password,
  34. ]);
  35. $this->assertResponseStatusCodeSame(302);
  36. $crawler = $client->followRedirect();
  37. return [$client, $crawler];
  38. }
  39. public function testLoginSuccess()
  40. {
  41. [$client, $crawler] = self::testLogin($nickname = 'taken_user', 'foobar');
  42. $this->assertRouteSame('root');
  43. $client->followRedirect();
  44. $this->assertSelectorNotExists('.alert');
  45. $this->assertSelectorTextContains('.profile-info-url-nickname', $nickname);
  46. }
  47. public function testLoginAttemptAlreadyLoggedIn()
  48. {
  49. [$client] = self::testLogin('taken_user', 'foobar'); // Normal login
  50. $crawler = $client->request('GET', '/main/login'); // attempt to login again
  51. $client->followRedirect();
  52. $this->assertRouteSame('root');
  53. }
  54. public function testLoginFailure()
  55. {
  56. self::testLogin('taken_user', 'wrong password');
  57. $this->assertResponseIsSuccessful();
  58. $this->assertSelectorTextContains('.alert', 'The presented password is invalid.');
  59. $this->assertRouteSame('security_login');
  60. }
  61. public function testLoginEmail()
  62. {
  63. [$client, $crawler] = self::testLogin('taken_user@provider.any', 'foobar');
  64. $this->assertRouteSame('root');
  65. $client->followRedirect();
  66. $this->assertSelectorNotExists('.alert');
  67. $this->assertSelectorTextContains('.profile-info-url-nickname', 'taken_user');
  68. }
  69. // --------- Register --------------
  70. private function testRegister(string $nickname, string $email, string $password)
  71. {
  72. $client = static::createClient();
  73. $crawler = $client->request('GET', '/main/register');
  74. $this->assertResponseIsSuccessful();
  75. $crawler = $client->submitForm('Register', [
  76. 'register[nickname]' => $nickname,
  77. 'register[email]' => $email,
  78. 'register[password][first]' => $password,
  79. 'register[password][second]' => $password,
  80. ]);
  81. return [$client, $crawler];
  82. }
  83. public function testRegisterSuccess()
  84. {
  85. [$client,] = self::testRegister('new_nickname', 'new_email@provider.any', 'foobar');
  86. $this->assertResponseStatusCodeSame(302);
  87. $crawler = $client->followRedirect();
  88. $this->assertRouteSame('security_login');
  89. $this->assertSelectorNotExists('.alert');
  90. }
  91. public function testRegisterDifferentPassword()
  92. {
  93. $client = static::createClient();
  94. $crawler = $client->request('GET', '/main/register');
  95. $this->assertResponseIsSuccessful();
  96. $crawler = $client->submitForm('Register', [
  97. 'register[nickname]' => 'new_user',
  98. 'register[email]' => 'new_email@provider.any',
  99. 'register[password][first]' => 'fooobar',
  100. 'register[password][second]' => 'barquux',
  101. ]);
  102. $this->assertSelectorTextContains('form[name=register] ul li', 'The password fields must match');
  103. $this->assertResponseStatusCodeSame(200);
  104. $this->assertRouteSame('security_register');
  105. }
  106. private function testRegisterPasswordLength(string $password, string $error)
  107. {
  108. [$client, ] = self::testRegister('new_nickname', 'email@provider.any', $password);
  109. $this->assertResponseIsSuccessful();
  110. $this->assertSelectorTextContains('.form-error', $error);
  111. $this->assertRouteSame('security_register');
  112. }
  113. public function testRegisterPasswordEmpty()
  114. {
  115. self::testRegisterPasswordLength('', error: 'Please enter a password');
  116. }
  117. public function testRegisterPasswordShort()
  118. {
  119. self::testRegisterPasswordLength('f', error: 'Your password should be at least');
  120. }
  121. public function testRegisterPasswordLong()
  122. {
  123. self::testRegisterPasswordLength(str_repeat('f', 128), error: 'Your password should be at most');
  124. }
  125. private function testRegisterNoEmail()
  126. {
  127. [$client, ] = self::testRegister('new_nickname', '', 'foobar');
  128. $this->assertResponseIsSuccessful();
  129. $this->assertSelectorTextContains('.form-error', 'Please enter an email');
  130. $this->assertRouteSame('security_register');
  131. }
  132. private function testRegisterNicknameLength(string $nickname, string $error)
  133. {
  134. [$client, ] = self::testRegister($nickname, 'email@provider.any', 'foobar');
  135. $this->assertResponseIsSuccessful();
  136. $this->assertSelectorTextContains('.form-error', $error);
  137. $this->assertRouteSame('security_register');
  138. }
  139. public function testRegisterNicknameEmpty()
  140. {
  141. self::testRegisterNicknameLength('', error: 'Please enter a nickname');
  142. }
  143. public function testRegisterNicknameLong()
  144. {
  145. self::testRegisterNicknameLength(str_repeat('f', 128), error: 'Your nickname must be at most');
  146. }
  147. public function testRegisterExistingNickname()
  148. {
  149. [$client, ] = self::testRegister('taken_user', 'new_new_email@provider.any', 'foobar');
  150. $this->assertSelectorTextContains('.stacktrace', 'App\Util\Exception\NicknameTakenException');
  151. }
  152. public function testRegisterExistingEmail()
  153. {
  154. [$client, ] = self::testRegister('other_new_nickname', 'taken_user@provider.any', 'foobar');
  155. $this->assertSelectorTextContains('.stacktrace', 'App\Util\Exception\EmailTakenException');
  156. }
  157. }