koel/tests/Feature/LastfmTest.php

74 lines
2.2 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;
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;
2015-12-30 04:14:47 +00:00
use Tymon\JWTAuth\JWTAuth;
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.xml')),
]);
$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
{
$user = factory(User::class)->create();
2018-08-24 15:27:19 +00:00
$this->postAsUser('api/lastfm/session-key', ['key' => 'foo'], $user)
->assertResponseOk();
2018-09-04 06:25:24 +00:00
$user = User::find($user->id);
self::assertEquals('foo', $user->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
{
2018-08-19 16:05:10 +00:00
$this->mockIocDependency(JWTAuth::class, [
'parseToken' => null,
'getToken' => 'foo',
2015-12-30 04:14:47 +00:00
]);
2015-12-20 12:17:35 +00:00
2018-08-19 16:05:10 +00:00
$this->getAsUser('api/lastfm/connect')
->assertRedirectedTo('https://www.last.fm/api/auth/?api_key=foo&cb=http%3A%2F%2Flocalhost%2Fapi%2Flastfm%2Fcallback%3Fjwt-token%3Dfoo');
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
{
2018-09-04 06:25:24 +00:00
$lastfm = $this->mockIocDependency(LastfmService::class);
$lastfm->shouldReceive('getSessionKey')
->once()
->with('foo')
->andReturn('bar');
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();
2018-08-19 16:05:10 +00:00
$this->getAsUser('api/lastfm/callback?token=foo', $user);
$user->refresh();
2015-12-20 12:17:35 +00:00
2018-09-04 06:25:24 +00:00
$this->assertEquals('bar', $user->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
2018-09-04 06:25:24 +00:00
$this->assertNull($user->lastfm_session_key);
2015-12-23 06:26:16 +00:00
}
}