Skip to content

ISSUE-357: use api-client #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
],
"homepage": "https://www.phplist.com/",
"license": "AGPL-3.0-or-later",
"repositories": [
{
"type": "vcs",
"url": "https://github.com/tatevikgr/phplist-api-client"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"authors": [
{
"name": "Xheni Myrtaj",
Expand All @@ -37,7 +45,8 @@
"php": "^8.1",
"phplist/core": "v5.0.0-alpha8",
"symfony/twig-bundle": "^6.4",
"symfony/webpack-encore-bundle": "^2.2"
"symfony/webpack-encore-bundle": "^2.2",
"tatevikgr/rest-api-client": "dev-master"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
9 changes: 4 additions & 5 deletions src/Controller/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

use Exception;
use GuzzleHttp\Exception\GuzzleException;
use PhpList\WebFrontend\Service\ApiClient;
use PhpList\RestApiClient\Endpoint\AuthClient;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class AuthController extends AbstractController
{
private ApiClient $apiClient;
private AuthClient $apiClient;

public function __construct(ApiClient $apiClient)
public function __construct(AuthClient $apiClient)
{
$this->apiClient = $apiClient;
}
Expand All @@ -40,10 +40,9 @@ public function login(Request $request): Response
$password = $request->request->get('password');

try {
$authData = $this->apiClient->authenticate($username, $password);
$authData = $this->apiClient->login($username, $password);
$request->getSession()->set('auth_token', $authData['key']);
$request->getSession()->set('auth_expiry_date', $authData['key']);
$this->apiClient->setAuthToken($authData['key']);

return $this->redirectToRoute('empty_start_page');
} catch (Exception $e) {
Expand Down
19 changes: 9 additions & 10 deletions tests/Unit/Controller/AuthControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace PhpList\WebFrontend\Tests\Unit\Controller;

use PhpList\WebFrontend\Controller\AuthController;
use PhpList\WebFrontend\Service\ApiClient;
use PhpList\RestApiClient\Endpoint\AuthClient;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use RuntimeException;
Expand All @@ -16,12 +16,12 @@

class AuthControllerTest extends TestCase
{
private ApiClient&MockObject $apiClient;
private AuthClient&MockObject $apiClient;
private AuthController $controller;

protected function setUp(): void
{
$this->apiClient = $this->createMock(ApiClient::class);
$this->apiClient = $this->createMock(AuthClient::class);

$this->controller = $this->getMockBuilder(AuthController::class)
->setConstructorArgs([$this->apiClient])
Expand Down Expand Up @@ -118,14 +118,10 @@ public function testLoginWithPostRequestSuccess(): void
]);
$request->setSession($session);

$this->apiClient->method('authenticate')
$this->apiClient->method('login')
->with('testuser', 'testpass')
->willReturn(['key' => 'test-token']);

$this->apiClient->expects($this->once())
->method('setAuthToken')
->with('test-token');

$response = $this->controller->login($request);

$this->assertInstanceOf(RedirectResponse::class, $response);
Expand All @@ -147,14 +143,17 @@ public function testLoginWithPostRequestFailure(): void
]);
$request->setSession($session);

$this->apiClient->method('authenticate')
$this->apiClient->method('login')
->with('testuser', 'testpass')
->willThrowException(new RuntimeException('Invalid credentials'));

$response = $this->controller->login($request);

$this->assertStringContainsString('auth/login.html.twig', $response->getContent());
$this->assertStringContainsString('Invalid credentials', $response->getContent());
$this->assertStringContainsString(
'Invalid credentials or server error: Invalid credentials',
$response->getContent(),
);
}

public function testLoginWithExistingSession(): void
Expand Down
Loading