AuthControllerTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 testSignupWithEmail()
  22. {
  23. $token = $this->user->generateSystemToken();
  24. $response = $this->request(
  25. 'POST',
  26. '/auth/signup',
  27. ['uid' => 'username1', 'email' => 'email@example.com'],
  28. ['Authorization' => "Bearer $token->key:$token->secret" ]
  29. );
  30. $this->assertSame('username1', $response->getData()->uid);
  31. $this->assertSame('email@example.com', $response->getData()->email);
  32. }
  33. public function testSignupWithUidAsEmail()
  34. {
  35. $token = $this->user->generateSystemToken();
  36. $response = $this->request(
  37. 'POST',
  38. '/auth/signup',
  39. ['uid' => 'email@example.com'],
  40. ['Authorization' => "Bearer $token->key:$token->secret" ]
  41. );
  42. $this->assertSame('email@example.com', $response->getData()->uid);
  43. }
  44. public function testSignupWithReturnToken()
  45. {
  46. $token = $this->user->generateSystemToken();
  47. $response = $this->request(
  48. 'POST',
  49. '/auth/signup',
  50. ['uid' => 'email@example.com', 'return_token' => 'porfa'],
  51. ['Authorization' => "Bearer $token->key:$token->secret" ]
  52. );
  53. $this->assertObjectHasAttribute('secret', $response->getData());
  54. }
  55. public function testLoginWithUid()
  56. {
  57. $this->assertFalse($this->user->amI());
  58. $response = $this->request(
  59. 'POST',
  60. '/auth/login',
  61. ['username' => $this->user->uid, 'password' => 'password']
  62. );
  63. $this->assertSame(
  64. $this->user->uid,
  65. $response->getData()->uid
  66. );
  67. $this->assertTrue($this->user->amI());
  68. }
  69. public function testLoginWithEmail()
  70. {
  71. $this->assertFalse($this->user->amI());
  72. $response = $this->request(
  73. 'POST',
  74. '/auth/login',
  75. ['username' => $this->user->email, 'password' => 'password']
  76. );
  77. $this->assertSame(
  78. $this->user->uid,
  79. $response->getData()->uid
  80. );
  81. $this->assertTrue($this->user->amI());
  82. }
  83. public function testLoginWrongUsername()
  84. {
  85. $response = $this->request(
  86. 'POST',
  87. '/auth/login',
  88. ['username' => 'anything', 'password' => 'password']
  89. );
  90. $this->assertStringContainsString(
  91. 'could not be found',
  92. $response->getData()->error->message
  93. );
  94. }
  95. public function testLoginWrongPassword()
  96. {
  97. $response = $this->request(
  98. 'POST',
  99. '/auth/login',
  100. ['username' => $this->user->email, 'password' => 'not_password']
  101. );
  102. $this->assertStringContainsString(
  103. 'Wrong',
  104. $response->getData()->error->message
  105. );
  106. }
  107. public function testLoginNoInput()
  108. {
  109. $response = $this->request(
  110. 'POST',
  111. '/auth/login',
  112. );
  113. $this->assertStringContainsString(
  114. 'invalid',
  115. $response->getData()->error->message
  116. );
  117. }
  118. public function testEmailTokenWithUid()
  119. {
  120. $this->validOrigin();
  121. $response = $this->request(
  122. 'POST',
  123. '/auth/email-token',
  124. ['username' => $this->user->uid],
  125. ['Referer' => $this->validOrigin()]
  126. );
  127. $this->assertSame(
  128. 'm**l@doma.in',
  129. $response->getData()->To[0][0]
  130. );
  131. }
  132. public function testEmailTokenWithEmail()
  133. {
  134. $this->validOrigin();
  135. $response = $this->request(
  136. 'POST',
  137. "/auth/email-token?origin_url={$this->validOrigin()}",
  138. ['username' => $this->user->email]
  139. );
  140. $this->assertSame(
  141. 'm**l@doma.in',
  142. $response->getData()->To[0][0]
  143. );
  144. }
  145. public function testEmailTokenWrongUsername()
  146. {
  147. $response = $this->request(
  148. 'POST',
  149. '/auth/email-token',
  150. ['username' => 'not_username'],
  151. ['Referer' => $this->validOrigin()]
  152. );
  153. $this->assertStringContainsString(
  154. 'could not be found',
  155. $response->getData()->error->message
  156. );
  157. }
  158. public function testEmailTokenNoInput()
  159. {
  160. $response = $this->request(
  161. 'POST',
  162. '/auth/email-token',
  163. [],
  164. ['Referer' => $this->validOrigin()]
  165. );
  166. $this->assertStringContainsString(
  167. 'invalid',
  168. $response->getData()->error->message
  169. );
  170. }
  171. public function testWhoAmI()
  172. {
  173. $this->user->login();
  174. $this->assertSame(
  175. $this->user->uid,
  176. $this->request('GET', '/auth/whoami')->getData()->uid
  177. );
  178. }
  179. public function testUnauthorizedWhoAmI()
  180. {
  181. $this->assertSame(
  182. \Illuminate\Http\Response::HTTP_UNAUTHORIZED,
  183. $this->request('GET', '/auth/whoami')->getStatusCode()
  184. );
  185. }
  186. public function testLogout()
  187. {
  188. $this->user->login();
  189. $this->assertTrue($this->user->amI());
  190. $this->assertSame(
  191. 'true',
  192. $this->request('POST', '/auth/logout')->getContent()
  193. );
  194. $this->assertFalse($this->user->amI());
  195. }
  196. // AuthServiceProvider tests, use /auth/whoami so I'll leave it here
  197. public function testLoginWithCookie()
  198. {
  199. $token = $this->user->generateHumanToken();
  200. $response = $this->call(
  201. 'GET',
  202. '/auth/whoami',
  203. [],
  204. ['auth_token' => "$token->key:$token->secret"]
  205. );
  206. $this->assertSame($this->user->uid, $response->getData()->uid);
  207. }
  208. public function testLoginWithToken()
  209. {
  210. $token = $this->user->generateHumanToken();
  211. $response = $this->request(
  212. 'GET',
  213. '/auth/whoami',
  214. [],
  215. ['Authorization' => "Bearer $token->key:$token->secret"]
  216. );
  217. $this->assertSame($this->user->uid, $response->getData()->uid);
  218. }
  219. public function testLoginWithInput()
  220. {
  221. $token = $this->user->generateHumanToken();
  222. $response = $this->request(
  223. 'GET',
  224. "/auth/whoami?auth_token=$token->key:$token->secret"
  225. );
  226. $this->assertSame($this->user->uid, $response->getData()->uid);
  227. }
  228. public function testLoginWrongUid()
  229. {
  230. app('session')->set('user_uid', 'nope');
  231. $response = $this->request(
  232. 'GET',
  233. '/auth/whoami',
  234. );
  235. $this->assertSame('Unauthorized.', $response->getData());
  236. }
  237. public function testLoginAsSuperUser()
  238. {
  239. // Does not really belong here. But IDK where to write this test.
  240. $this->assertFalse(app('gate')->allows('anything-and-everything'));
  241. $this->user->uid = 'hawese';
  242. $this->user->update();
  243. app('session')->set('user_uid', $this->user->uid);
  244. $this->assertTrue(app('gate')->allows('anything-and-everything'));
  245. }
  246. }