Skip to content
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

Fix session auth check #271

Merged
merged 1 commit into from
Apr 26, 2023
Merged
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
2 changes: 1 addition & 1 deletion src/Session/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(RequestStack $requestStack)

public function isAuthenticated(): bool
{
return (bool) $this->getHttpSession()->get(static::SESSION_LASTFM_TOKEN);
return $this->getHttpSession()->has(static::SESSION_LASTFM_TOKEN);
}

public function getUsername(): ?string
Expand Down
63 changes: 14 additions & 49 deletions tests/Session/SessionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,21 @@

use Nucleos\LastFm\Session\Session as LastFmSession;
use Nucleos\LastFmBundle\Session\SessionManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;

final class SessionManagerTest extends TestCase
{
/**
* @var MockObject&Session
*/
private Session $session;

private RequestStack $requestStack;

protected function setUp(): void
{
$this->session = $this->createMock(Session::class);
$this->session = new Session(new MockArraySessionStorage());

$request = new Request();
$request->setSession($this->session);
Expand All @@ -41,41 +38,28 @@ protected function setUp(): void

public function testIsAuthenticated(): void
{
$this->session->method('get')->with('LASTFM_TOKEN')
->willReturn(true)
;
$this->session->set('LASTFM_TOKEN', 'TheToken');

$manager = new SessionManager($this->requestStack);
static::assertTrue($manager->isAuthenticated());
}

public function testIsNotAuthenticated(): void
{
$this->session->method('get')->with('LASTFM_TOKEN')
->willReturn(false)
;

$manager = new SessionManager($this->requestStack);
static::assertFalse($manager->isAuthenticated());
}

public function testGetUsername(): void
{
$this->session->method('get')->with('LASTFM_NAME')
->willReturn('MyUser')
;
$this->session->set('LASTFM_NAME', 'MyUser');

$manager = new SessionManager($this->requestStack);
static::assertSame('MyUser', $manager->getUsername());
}

public function testGetUsernameNotExist(): void
{
$this->session = $this->createMock(Session::class);
$this->session->method('get')->with('LASTFM_NAME')
->willReturn(null)
;

$manager = new SessionManager($this->requestStack);
static::assertNull($manager->getUsername());
}
Expand All @@ -84,44 +68,29 @@ public function testStore(): void
{
$lastfmSession = new LastFmSession('YourName', 'YourToken');

$this->session->expects(static::exactly(2))->method('set')
->withConsecutive(
['LASTFM_NAME', 'YourName'],
['LASTFM_TOKEN', 'YourToken'],
)
;

$manager = new SessionManager($this->requestStack);
$manager->store($lastfmSession);

static::assertTrue($this->session->has('LASTFM_TOKEN'));
static::assertTrue($this->session->has('LASTFM_NAME'));
}

public function testClear(): void
{
$this->session->expects(static::exactly(2))->method('remove')
->withConsecutive(
['LASTFM_NAME'],
['LASTFM_TOKEN'],
)
;
$this->session->set('LASTFM_TOKEN', 'TheToken');
$this->session->set('LASTFM_NAME', 'MyUser');

$manager = new SessionManager($this->requestStack);
$manager->clear();

static::assertFalse($this->session->has('LASTFM_TOKEN'));
static::assertFalse($this->session->has('LASTFM_NAME'));
}

public function testGetSession(): void
{
$this->session->expects(static::exactly(3))->method('get')
->withConsecutive(
['LASTFM_TOKEN'],
['LASTFM_NAME'],
['LASTFM_TOKEN'],
)
->willReturn(
'TheToken',
'MyUser',
'TheToken'
)
;
$this->session->set('LASTFM_TOKEN', 'TheToken');
$this->session->set('LASTFM_NAME', 'MyUser');

$manager = new SessionManager($this->requestStack);

Expand All @@ -134,10 +103,6 @@ public function testGetSession(): void

public function testGetSessionWithNoAuth(): void
{
$this->session->method('get')->with('LASTFM_TOKEN')
->willReturn(null)
;

$manager = new SessionManager($this->requestStack);

static::assertNull($manager->getSession());
Expand Down