123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- // Copyright 2019 Hackware SpA <human@hackware.cl>
- // "Hackware Web Services Core" is released under the MIT License terms.
- namespace Hawese\Tests;
- use Hawese\Core\User;
- use Laravel\Lumen\Testing\DatabaseTransactions;
- class AuthControllerTest extends TestCase
- {
- use DatabaseTransactions;
- public function setUp(): void
- {
- parent::setUp();
- $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
- $this->user = new User([
- 'uid' => 'username',
- 'email' => 'mail@doma.in'
- ]);
- $this->user->changePassword('password');
- $this->user->insert();
- }
- public function testSignupWithEmail()
- {
- $token = $this->user->generateSystemToken();
- $response = $this->request(
- 'POST',
- '/auth/signup',
- ['uid' => 'username1', 'email' => 'email@example.com'],
- ['Authorization' => "Bearer $token->key:$token->secret" ]
- );
- $this->assertSame('username1', $response->getData()->uid);
- $this->assertSame('email@example.com', $response->getData()->email);
- }
- public function testSignupWithUidAsEmail()
- {
- $token = $this->user->generateSystemToken();
- $response = $this->request(
- 'POST',
- '/auth/signup',
- ['uid' => 'email@example.com'],
- ['Authorization' => "Bearer $token->key:$token->secret" ]
- );
- $this->assertSame('email@example.com', $response->getData()->uid);
- }
- public function testSignupWithReturnToken()
- {
- $token = $this->user->generateSystemToken();
- $response = $this->request(
- 'POST',
- '/auth/signup',
- ['uid' => 'email@example.com', 'return_token' => 'porfa'],
- ['Authorization' => "Bearer $token->key:$token->secret" ]
- );
- $this->assertObjectHasAttribute('secret', $response->getData());
- }
- public function testLoginWithUid()
- {
- $this->assertFalse($this->user->amI());
- $response = $this->request(
- 'POST',
- '/auth/login',
- ['username' => $this->user->uid, 'password' => 'password']
- );
- $this->assertSame(
- $this->user->uid,
- $response->getData()->uid
- );
- $this->assertTrue($this->user->amI());
- }
- public function testLoginWithEmail()
- {
- $this->assertFalse($this->user->amI());
- $response = $this->request(
- 'POST',
- '/auth/login',
- ['username' => $this->user->email, 'password' => 'password']
- );
- $this->assertSame(
- $this->user->uid,
- $response->getData()->uid
- );
- $this->assertTrue($this->user->amI());
- }
- public function testLoginWrongUsername()
- {
- $response = $this->request(
- 'POST',
- '/auth/login',
- ['username' => 'anything', 'password' => 'password']
- );
- $this->assertStringContainsString(
- 'could not be found',
- $response->getData()->error->message
- );
- }
- public function testLoginWrongPassword()
- {
- $response = $this->request(
- 'POST',
- '/auth/login',
- ['username' => $this->user->email, 'password' => 'not_password']
- );
- $this->assertStringContainsString(
- 'Wrong',
- $response->getData()->error->message
- );
- }
- public function testLoginNoInput()
- {
- $response = $this->request(
- 'POST',
- '/auth/login',
- );
- $this->assertStringContainsString(
- 'invalid',
- $response->getData()->error->message
- );
- }
- public function testEmailTokenWithUid()
- {
- $this->validOrigin();
- $response = $this->request(
- 'POST',
- '/auth/email-token',
- ['username' => $this->user->uid],
- ['Referer' => $this->validOrigin()]
- );
- $this->assertSame(
- 'm**l@doma.in',
- $response->getData()->To[0][0]
- );
- }
- public function testEmailTokenWithEmail()
- {
- $this->validOrigin();
- $response = $this->request(
- 'POST',
- "/auth/email-token?origin_url={$this->validOrigin()}",
- ['username' => $this->user->email]
- );
- $this->assertSame(
- 'm**l@doma.in',
- $response->getData()->To[0][0]
- );
- }
- public function testEmailTokenWrongUsername()
- {
- $response = $this->request(
- 'POST',
- '/auth/email-token',
- ['username' => 'not_username'],
- ['Referer' => $this->validOrigin()]
- );
- $this->assertStringContainsString(
- 'could not be found',
- $response->getData()->error->message
- );
- }
- public function testEmailTokenNoInput()
- {
- $response = $this->request(
- 'POST',
- '/auth/email-token',
- [],
- ['Referer' => $this->validOrigin()]
- );
- $this->assertStringContainsString(
- 'invalid',
- $response->getData()->error->message
- );
- }
- public function testWhoAmI()
- {
- $this->user->login();
- $this->assertSame(
- $this->user->uid,
- $this->request('GET', '/auth/whoami')->getData()->uid
- );
- }
- public function testUnauthorizedWhoAmI()
- {
- $this->assertSame(
- \Illuminate\Http\Response::HTTP_UNAUTHORIZED,
- $this->request('GET', '/auth/whoami')->getStatusCode()
- );
- }
- public function testLogout()
- {
- $this->user->login();
- $this->assertTrue($this->user->amI());
- $this->assertSame(
- 'true',
- $this->request('POST', '/auth/logout')->getContent()
- );
- $this->assertFalse($this->user->amI());
- }
- // AuthServiceProvider tests, use /auth/whoami so I'll leave it here
- public function testLoginWithCookie()
- {
- $token = $this->user->generateHumanToken();
- $response = $this->call(
- 'GET',
- '/auth/whoami',
- [],
- ['auth_token' => "$token->key:$token->secret"]
- );
- $this->assertSame($this->user->uid, $response->getData()->uid);
- }
- public function testLoginWithToken()
- {
- $token = $this->user->generateHumanToken();
- $response = $this->request(
- 'GET',
- '/auth/whoami',
- [],
- ['Authorization' => "Bearer $token->key:$token->secret"]
- );
- $this->assertSame($this->user->uid, $response->getData()->uid);
- }
- public function testLoginWithInput()
- {
- $token = $this->user->generateHumanToken();
- $response = $this->request(
- 'GET',
- "/auth/whoami?auth_token=$token->key:$token->secret"
- );
- $this->assertSame($this->user->uid, $response->getData()->uid);
- }
- public function testLoginWrongUid()
- {
- app('session')->set('user_uid', 'nope');
- $response = $this->request(
- 'GET',
- '/auth/whoami',
- );
- $this->assertSame('Unauthorized.', $response->getData());
- }
- public function testLoginAsSuperUser()
- {
- // Does not really belong here. But IDK where to write this test.
- $this->assertFalse(app('gate')->allows('anything-and-everything'));
- $this->user->uid = 'hawese';
- $this->user->update();
- app('session')->set('user_uid', $this->user->uid);
- $this->assertTrue(app('gate')->allows('anything-and-everything'));
- }
- }
|