-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
101 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/* | ||
* UserFrosting Vue Demo (http://www.userfrosting.com) | ||
* | ||
* @link https://github.com/userfrosting/demo-vue | ||
* @copyright Copyright (c) 2023 Louis Charette | ||
* @license https://github.com/userfrosting/demo-vue/blob/main/LICENSE.md (MIT License) | ||
*/ | ||
|
||
namespace UserFrosting\Demo\Controller; | ||
|
||
use Psr\Http\Message\ResponseInterface as Response; | ||
use Psr\Http\Message\ServerRequestInterface as Request; | ||
use UserFrosting\Sprinkle\Account\Controller\LoginAction as AccountLoginAction; | ||
|
||
class LoginAction extends AccountLoginAction | ||
{ | ||
/** | ||
* Receive the request, dispatch to the handler, and return the payload to | ||
* the response. | ||
* | ||
* @param Request $request | ||
* @param Response $response | ||
*/ | ||
public function __invoke(Request $request, Response $response): Response | ||
{ | ||
$this->handle($request); | ||
|
||
// Write response | ||
$payload = json_encode($this->authenticator->user(), JSON_THROW_ON_ERROR); | ||
$response->getBody()->write($payload); | ||
|
||
return $response->withHeader('Content-Type', 'application/json'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* UserFrosting Vue Demo (http://www.userfrosting.com) | ||
* | ||
* @link https://github.com/userfrosting/demo-vue | ||
* @copyright Copyright (c) 2023 Louis Charette | ||
* @license https://github.com/userfrosting/demo-vue/blob/main/LICENSE.md (MIT License) | ||
*/ | ||
|
||
namespace UserFrosting\Tests\Demo\Controller; | ||
|
||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; | ||
use UserFrosting\Demo\Demo; | ||
use UserFrosting\Sprinkle\Account\Authenticate\Authenticator; | ||
use UserFrosting\Sprinkle\Account\Database\Models\User; | ||
use UserFrosting\Sprinkle\Core\Testing\RefreshDatabase; | ||
use UserFrosting\Testing\TestCase; | ||
|
||
class LoginActionTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
use MockeryPHPUnitIntegration; | ||
|
||
protected string $mainSprinkle = Demo::class; | ||
|
||
/** | ||
* Setup test database for controller tests | ||
*/ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->refreshDatabase(); | ||
} | ||
|
||
public function testLogin(): void | ||
{ | ||
/** @var User */ | ||
$user = User::factory([ | ||
'password' => 'test' | ||
])->create(); | ||
|
||
// Create request with method and url and fetch response | ||
$request = $this->createJsonRequest('POST', '/auth/login', [ | ||
'user_name' => $user->user_name, | ||
'password' => 'test', | ||
]); | ||
$response = $this->handleRequest($request); | ||
|
||
// Assert response status & body | ||
$this->assertResponseStatus(200, $response); | ||
$this->assertJsonResponse($user->refresh()->toArray(), $response); | ||
|
||
// We have to logout the user to avoid problem | ||
/** @var Authenticator */ | ||
$authenticator = $this->ci->get(Authenticator::class); | ||
$authenticator->logout(); | ||
} | ||
} |