2015-12-19 16:36:44 +00:00
|
|
|
<?php
|
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2018-08-18 13:19:40 +00:00
|
|
|
use App\Services\LastfmService;
|
2020-09-06 18:21:39 +00:00
|
|
|
use App\Services\TokenManager;
|
2020-09-07 20:43:23 +00:00
|
|
|
use Laravel\Sanctum\NewAccessToken;
|
|
|
|
use Laravel\Sanctum\PersonalAccessToken;
|
2018-08-29 07:05:24 +00:00
|
|
|
use Mockery;
|
2024-10-24 10:45:45 +00:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2024-01-09 18:34:40 +00:00
|
|
|
use Tests\TestCase;
|
2015-12-19 16:36:44 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
use function Tests\create_user;
|
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
class LastfmTest extends TestCase
|
2015-12-19 16:36:44 +00:00
|
|
|
{
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function setSessionKey(): void
|
2016-01-26 06:32:29 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2022-07-27 08:49:33 +00:00
|
|
|
$this->postAs('api/lastfm/session-key', ['key' => 'foo'], $user)
|
2022-07-27 15:32:36 +00:00
|
|
|
->assertNoContent();
|
2018-08-24 15:27:19 +00:00
|
|
|
|
2024-01-23 22:50:50 +00:00
|
|
|
self::assertSame('foo', $user->refresh()->preferences->lastFmSessionKey);
|
2016-01-26 06:32:29 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function connectToLastfm(): void
|
2015-12-20 12:17:35 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2020-09-06 18:21:39 +00:00
|
|
|
$token = $user->createToken('Koel')->plainTextToken;
|
2015-12-20 12:17:35 +00:00
|
|
|
|
2020-09-07 20:43:23 +00:00
|
|
|
$temporaryToken = Mockery::mock(NewAccessToken::class);
|
|
|
|
$temporaryToken->plainTextToken = 'tmp-token';
|
|
|
|
|
2020-12-22 23:01:49 +00:00
|
|
|
$tokenManager = self::mock(TokenManager::class);
|
2020-09-07 20:43:23 +00:00
|
|
|
|
|
|
|
$tokenManager->shouldReceive('getUserFromPlainTextToken')
|
|
|
|
->with($token)
|
|
|
|
->andReturn($user);
|
|
|
|
|
|
|
|
$tokenManager->shouldReceive('createToken')
|
|
|
|
->once()
|
|
|
|
->with($user)
|
|
|
|
->andReturn($temporaryToken);
|
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
$this->get('lastfm/connect?api_token=' . $token)
|
2020-09-06 18:21:39 +00:00
|
|
|
->assertRedirect(
|
2020-12-22 20:11:22 +00:00
|
|
|
'https://www.last.fm/api/auth/?api_key=foo&cb=http%3A%2F%2Flocalhost%2Flastfm%2Fcallback%3Fapi_token%3Dtmp-token' // @phpcs-ignore-line
|
2020-09-06 18:21:39 +00:00
|
|
|
);
|
2015-12-20 12:17:35 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
2020-09-07 20:43:23 +00:00
|
|
|
public function testCallback(): void
|
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2020-09-07 20:43:23 +00:00
|
|
|
$token = $user->createToken('Koel')->plainTextToken;
|
|
|
|
|
|
|
|
self::assertNotNull(PersonalAccessToken::findToken($token));
|
|
|
|
|
2021-06-04 16:19:34 +00:00
|
|
|
$lastfm = Mockery::mock(LastfmService::class)->makePartial();
|
2020-09-07 20:43:23 +00:00
|
|
|
|
|
|
|
$lastfm->shouldReceive('getSessionKey')
|
|
|
|
->with('lastfm-token')
|
|
|
|
->once()
|
|
|
|
->andReturn('my-session-key');
|
|
|
|
|
2021-06-04 16:19:34 +00:00
|
|
|
app()->instance(LastfmService::class, $lastfm);
|
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
$this->get('lastfm/callback?token=lastfm-token&api_token=' . urlencode($token))
|
2020-09-07 20:43:23 +00:00
|
|
|
->assertOk();
|
|
|
|
|
2024-01-23 22:50:50 +00:00
|
|
|
self::assertSame('my-session-key', $user->refresh()->preferences->lastFmSessionKey);
|
2020-09-07 20:43:23 +00:00
|
|
|
// make sure the user's api token is deleted
|
|
|
|
self::assertNull(PersonalAccessToken::findToken($token));
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function retrieveAndStoreSessionKey(): void
|
2015-12-20 12:17:35 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2020-09-06 21:11:48 +00:00
|
|
|
|
2021-06-04 16:19:34 +00:00
|
|
|
$lastfm = Mockery::mock(LastfmService::class)->makePartial();
|
|
|
|
|
2018-09-04 06:25:24 +00:00
|
|
|
$lastfm->shouldReceive('getSessionKey')
|
|
|
|
->once()
|
|
|
|
->with('foo')
|
2021-06-04 16:19:34 +00:00
|
|
|
->andReturn('my-session-key');
|
|
|
|
|
|
|
|
app()->instance(LastfmService::class, $lastfm);
|
2015-12-20 12:17:35 +00:00
|
|
|
|
2020-12-22 23:01:49 +00:00
|
|
|
$tokenManager = self::mock(TokenManager::class);
|
2021-06-04 16:19:34 +00:00
|
|
|
|
2020-09-06 21:11:48 +00:00
|
|
|
$tokenManager->shouldReceive('getUserFromPlainTextToken')
|
|
|
|
->once()
|
|
|
|
->with('my-token')
|
|
|
|
->andReturn($user);
|
|
|
|
|
2020-09-12 15:01:48 +00:00
|
|
|
$this->get('lastfm/callback?token=foo&api_token=my-token');
|
2015-12-20 12:17:35 +00:00
|
|
|
|
2024-01-23 22:50:50 +00:00
|
|
|
self::assertSame('my-session-key', $user->refresh()->preferences->lastFmSessionKey);
|
2015-12-20 12:17:35 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 10:45:45 +00:00
|
|
|
#[Test]
|
|
|
|
public function disconnectUser(): void
|
2015-12-20 12:17:35 +00:00
|
|
|
{
|
2024-01-11 12:41:33 +00:00
|
|
|
$user = create_user();
|
2024-01-23 22:50:50 +00:00
|
|
|
self::assertNotNull($user->preferences->lastFmSessionKey);
|
2024-01-11 12:41:33 +00:00
|
|
|
|
2022-07-27 08:49:33 +00:00
|
|
|
$this->deleteAs('api/lastfm/disconnect', [], $user);
|
2015-12-21 13:49:00 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
$user->refresh();
|
2024-01-23 22:50:50 +00:00
|
|
|
self::assertNull($user->preferences->lastFmSessionKey);
|
2015-12-23 06:26:16 +00:00
|
|
|
}
|
2015-12-19 16:36:44 +00:00
|
|
|
}
|