koel/tests/Feature/LastfmTest.php

110 lines
3.2 KiB
PHP
Raw Normal View History

<?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;
use Laravel\Sanctum\NewAccessToken;
use Laravel\Sanctum\PersonalAccessToken;
2018-08-29 07:05:24 +00:00
use Mockery;
2024-01-09 18:34:40 +00:00
use Tests\TestCase;
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
{
2018-08-31 13:47:15 +00:00
public function testSetSessionKey(): 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
self::assertSame('foo', $user->refresh()->preferences->lastFmSessionKey);
2016-01-26 06:32:29 +00:00
}
2018-08-31 13:47:15 +00:00
public function testConnectToLastfm(): 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
$temporaryToken = Mockery::mock(NewAccessToken::class);
$temporaryToken->plainTextToken = 'tmp-token';
2020-12-22 23:01:49 +00:00
$tokenManager = self::mock(TokenManager::class);
$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
}
public function testCallback(): void
{
2024-01-11 12:41:33 +00:00
$user = create_user();
$token = $user->createToken('Koel')->plainTextToken;
self::assertNotNull(PersonalAccessToken::findToken($token));
$lastfm = Mockery::mock(LastfmService::class)->makePartial();
$lastfm->shouldReceive('getSessionKey')
->with('lastfm-token')
->once()
->andReturn('my-session-key');
app()->instance(LastfmService::class, $lastfm);
2020-12-22 20:11:22 +00:00
$this->get('lastfm/callback?token=lastfm-token&api_token=' . urlencode($token))
->assertOk();
self::assertSame('my-session-key', $user->refresh()->preferences->lastFmSessionKey);
// make sure the user's api token is deleted
self::assertNull(PersonalAccessToken::findToken($token));
}
2018-08-31 13:47:15 +00:00
public function testRetrieveAndStoreSessionKey(): 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
$lastfm = Mockery::mock(LastfmService::class)->makePartial();
2018-09-04 06:25:24 +00:00
$lastfm->shouldReceive('getSessionKey')
->once()
->with('foo')
->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);
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
self::assertSame('my-session-key', $user->refresh()->preferences->lastFmSessionKey);
2015-12-20 12:17:35 +00:00
}
2018-08-31 13:47:15 +00:00
public function testDisconnectUser(): void
2015-12-20 12:17:35 +00:00
{
2024-01-11 12:41:33 +00:00
$user = create_user();
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();
self::assertNull($user->preferences->lastFmSessionKey);
2015-12-23 06:26:16 +00:00
}
}