koel/tests/Feature/LastfmTest.php

82 lines
2.5 KiB
PHP
Raw Normal View History

<?php
2017-02-14 06:53:02 +00:00
namespace Tests\Feature;
2015-12-20 12:30:28 +00:00
use App\Models\User;
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 GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
2018-09-04 06:25:24 +00:00
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Log\Logger;
2018-08-29 07:05:24 +00:00
use Mockery;
2017-08-05 16:56:11 +00:00
class LastfmTest extends TestCase
{
2018-09-04 06:25:24 +00:00
public function testGetSessionKey(): void
2018-09-04 05:34:02 +00:00
{
2018-09-04 06:25:24 +00:00
/** @var Client $client */
$client = Mockery::mock(Client::class, [
'get' => new Response(200, [], file_get_contents(__DIR__.'../../blobs/lastfm/session-key.json')),
2018-09-04 06:25:24 +00:00
]);
$service = new LastfmService($client, app(Cache::class), app(Logger::class));
self::assertEquals('foo', $service->getSessionKey('bar'));
2015-12-20 12:17:35 +00:00
}
2018-08-31 13:47:15 +00:00
public function testSetSessionKey(): void
2016-01-26 06:32:29 +00:00
{
2020-09-06 21:11:48 +00:00
/** @var User $user */
2016-01-26 06:32:29 +00:00
$user = factory(User::class)->create();
2018-08-24 15:27:19 +00:00
$this->postAsUser('api/lastfm/session-key', ['key' => 'foo'], $user)
2020-09-06 18:21:39 +00:00
->assertOk();
2018-08-24 15:27:19 +00:00
2020-09-06 21:11:48 +00:00
self::assertEquals('foo', $user->refresh()->lastfm_session_key);
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
{
2020-09-06 18:21:39 +00:00
/** @var User $user */
$user = factory(User::class)->create();
$token = $user->createToken('Koel')->plainTextToken;
2015-12-20 12:17:35 +00:00
2020-09-06 21:11:48 +00:00
$this->get('lastfm/connect?api_token='.$token)
2020-09-06 18:21:39 +00:00
->assertRedirect(
2020-09-06 21:11:48 +00:00
'https://www.last.fm/api/auth/?api_key=foo&cb=http%3A%2F%2Flocalhost%2Flastfm%2Fcallback%3Fapi_token%3D'
2020-09-06 18:21:39 +00:00
.urlencode($token)
);
2015-12-20 12:17:35 +00:00
}
2018-08-31 13:47:15 +00:00
public function testRetrieveAndStoreSessionKey(): void
2015-12-20 12:17:35 +00:00
{
2020-09-06 21:11:48 +00:00
/** @var User $user */
$user = factory(User::class)->create();
$lastfm = static::mockIocDependency(LastfmService::class);
2018-09-04 06:25:24 +00:00
$lastfm->shouldReceive('getSessionKey')
->once()
->with('foo')
->andReturn('bar');
2015-12-20 12:17:35 +00:00
2020-09-06 21:11:48 +00:00
$tokenManager = static::mockIocDependency(TokenManager::class);
$tokenManager->shouldReceive('getUserFromPlainTextToken')
->once()
->with('my-token')
->andReturn($user);
$this->get('lastfm/callback?token=foo&api_token=my-token');
2015-12-20 12:17:35 +00:00
2020-09-06 21:11:48 +00:00
self::assertEquals('bar', $user->refresh()->lastfm_session_key);
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
{
2018-08-19 16:05:10 +00:00
/** @var User $user */
2015-12-20 12:17:35 +00:00
$user = factory(User::class)->create(['preferences' => ['lastfm_session_key' => 'bar']]);
2017-02-14 06:53:02 +00:00
$this->deleteAsUser('api/lastfm/disconnect', [], $user);
2018-08-19 16:05:10 +00:00
$user->refresh();
2015-12-21 13:49:00 +00:00
2020-09-06 18:21:39 +00:00
self::assertNull($user->lastfm_session_key);
2015-12-23 06:26:16 +00:00
}
}