SecurityTest.php 6.9 KB

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