AuthControllerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. // Copyright 2019 Hackware SpA <human@hackware.cl>
  3. // "Hackware Web Services Core" is released under the MIT License terms.
  4. namespace Hawese\Tests;
  5. use Hawese\Core\User;
  6. use Laravel\Lumen\Testing\DatabaseTransactions;
  7. class AuthControllerTest extends TestCase
  8. {
  9. use DatabaseTransactions;
  10. public function setUp(): void
  11. {
  12. parent::setUp();
  13. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  14. $this->user = new User([
  15. 'uid' => 'username',
  16. 'email' => 'mail@doma.in'
  17. ]);
  18. $this->user->changePassword('password');
  19. $this->user->insert();
  20. }
  21. public function testLoginWithUid()
  22. {
  23. $this->assertFalse($this->user->amI());
  24. $response = $this->request(
  25. 'POST',
  26. '/auth/login',
  27. ['username' => $this->user->uid, 'password' => 'password']
  28. );
  29. $this->assertSame(
  30. $this->user->uid,
  31. $response->getData()->uid
  32. );
  33. $this->assertTrue($this->user->amI());
  34. }
  35. public function testLoginWithEmail()
  36. {
  37. $this->assertFalse($this->user->amI());
  38. $response = $this->request(
  39. 'POST',
  40. '/auth/login',
  41. ['username' => $this->user->email, 'password' => 'password']
  42. );
  43. $this->assertSame(
  44. $this->user->uid,
  45. $response->getData()->uid
  46. );
  47. $this->assertTrue($this->user->amI());
  48. }
  49. public function testLoginWrongUsername()
  50. {
  51. $response = $this->request(
  52. 'POST',
  53. '/auth/login',
  54. ['username' => 'anything', 'password' => 'password']
  55. );
  56. $this->assertStringContainsString(
  57. 'could not be found',
  58. $response->getData()->error->message
  59. );
  60. }
  61. public function testLoginWrongPassword()
  62. {
  63. $response = $this->request(
  64. 'POST',
  65. '/auth/login',
  66. ['username' => $this->user->email, 'password' => 'not_password']
  67. );
  68. $this->assertStringContainsString(
  69. 'Wrong',
  70. $response->getData()->error->message
  71. );
  72. }
  73. public function testLoginNoInput()
  74. {
  75. $response = $this->request(
  76. 'POST',
  77. '/auth/login',
  78. );
  79. $this->assertStringContainsString(
  80. 'invalid',
  81. $response->getData()->error->message
  82. );
  83. }
  84. private function validOrigin()
  85. {
  86. return explode(',', env('CORS_ALLOW_ORIGINS'))[0];
  87. }
  88. public function testEmailTokenWithUid()
  89. {
  90. $this->validOrigin();
  91. $response = $this->request(
  92. 'POST',
  93. '/auth/email-token',
  94. ['username' => $this->user->uid],
  95. ['Referer' => $this->validOrigin()]
  96. );
  97. $this->assertSame(
  98. 'm**l@doma.in',
  99. $response->getData()->To[0][0]
  100. );
  101. }
  102. public function testEmailTokenWithEmail()
  103. {
  104. $this->validOrigin();
  105. $response = $this->request(
  106. 'POST',
  107. "/auth/email-token?origin_url={$this->validOrigin()}",
  108. ['username' => $this->user->email]
  109. );
  110. $this->assertSame(
  111. 'm**l@doma.in',
  112. $response->getData()->To[0][0]
  113. );
  114. }
  115. public function testEmailTokenWrongUsername()
  116. {
  117. $response = $this->request(
  118. 'POST',
  119. '/auth/email-token',
  120. ['username' => 'not_username'],
  121. ['Referer' => $this->validOrigin()]
  122. );
  123. $this->assertStringContainsString(
  124. 'could not be found',
  125. $response->getData()->error->message
  126. );
  127. }
  128. public function testEmailTokenNoInput()
  129. {
  130. $response = $this->request(
  131. 'POST',
  132. '/auth/email-token',
  133. [],
  134. ['Referer' => $this->validOrigin()]
  135. );
  136. $this->assertStringContainsString(
  137. 'invalid',
  138. $response->getData()->error->message
  139. );
  140. }
  141. public function testWhoAmI()
  142. {
  143. $this->user->login();
  144. $this->assertSame(
  145. $this->user->uid,
  146. $this->request('GET', '/auth/whoami')->getData()->uid
  147. );
  148. }
  149. public function testUnauthorizedWhoAmI()
  150. {
  151. $this->assertSame(
  152. \Illuminate\Http\Response::HTTP_UNAUTHORIZED,
  153. $this->request('GET', '/auth/whoami')->getStatusCode()
  154. );
  155. }
  156. public function testLogout()
  157. {
  158. $this->user->login();
  159. $this->assertTrue($this->user->amI());
  160. $this->assertSame(
  161. 'true',
  162. $this->request('POST', '/auth/logout')->getContent()
  163. );
  164. $this->assertFalse($this->user->amI());
  165. }
  166. }