koel/tests/Feature/LastfmTest.php

72 lines
2.1 KiB
PHP
Raw Normal View History

<?php
2017-02-14 14:53:02 +08:00
namespace Tests\Feature;
2015-12-20 07:30:28 -05:00
use App\Models\User;
2018-08-18 15:19:40 +02:00
use App\Services\LastfmService;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
2018-08-29 14:05:24 +07:00
use Illuminate\Contracts\Cache\Repository as Cache;
use Mockery;
2015-12-30 11:14:47 +07:00
use Tymon\JWTAuth\JWTAuth;
2017-08-05 17:56:11 +01:00
class LastfmTest extends TestCase
{
2015-12-20 20:17:35 +08:00
public function testGetSessionKey()
{
/** @var Client $client */
2018-08-29 14:05:24 +07:00
$client = Mockery::mock(Client::class, [
2017-02-14 14:53:02 +08:00
'get' => new Response(200, [], file_get_contents(__DIR__.'../../blobs/lastfm/session-key.xml')),
2015-12-20 20:17:35 +08:00
]);
2018-08-29 14:05:24 +07:00
self::assertEquals('foo', (new LastfmService($client, app(Cache::class)))->getSessionKey('bar'));
2015-12-20 20:17:35 +08:00
}
public function testSetSessionKey()
2016-01-26 14:32:29 +08:00
{
$user = factory(User::class)->create();
2018-08-24 17:27:19 +02:00
$this->postAsUser('api/lastfm/session-key', ['key' => 'foo'], $user)
->assertResponseOk();
2017-02-14 14:53:02 +08:00
$user = User::find($user->id);
2018-08-24 17:27:19 +02:00
self::assertEquals('foo', $user->lastfm_session_key);
2016-01-26 14:32:29 +08:00
}
2018-08-19 18:05:10 +02:00
public function testConnectToLastfm()
2015-12-20 20:17:35 +08:00
{
2018-08-19 18:05:10 +02:00
$this->mockIocDependency(JWTAuth::class, [
'parseToken' => null,
'getToken' => 'foo',
2015-12-30 11:14:47 +07:00
]);
2015-12-20 20:17:35 +08:00
2018-08-19 18:05:10 +02: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 20:17:35 +08:00
}
public function testRetrieveAndStoreSessionKey()
2015-12-20 20:17:35 +08:00
{
2018-08-19 18:05:10 +02:00
$lastfm = $this->mockIocDependency(LastfmService::class);
$lastfm->shouldReceive('getSessionKey')
->once()
->with('foo')
->andReturn('bar');
2015-12-20 20:17:35 +08:00
2018-08-19 18:05:10 +02:00
/** @var User $user */
2015-12-20 20:17:35 +08:00
$user = factory(User::class)->create();
2018-08-19 18:05:10 +02:00
$this->getAsUser('api/lastfm/callback?token=foo', $user);
$user->refresh();
2015-12-20 20:17:35 +08:00
$this->assertEquals('bar', $user->lastfm_session_key);
2015-12-20 20:17:35 +08:00
}
public function testDisconnectUser()
2015-12-20 20:17:35 +08:00
{
2018-08-19 18:05:10 +02:00
/** @var User $user */
2015-12-20 20:17:35 +08:00
$user = factory(User::class)->create(['preferences' => ['lastfm_session_key' => 'bar']]);
2017-02-14 14:53:02 +08:00
$this->deleteAsUser('api/lastfm/disconnect', [], $user);
2018-08-19 18:05:10 +02:00
$user->refresh();
2015-12-21 21:49:00 +08:00
2018-08-19 18:05:10 +02:00
$this->assertNull($user->lastfm_session_key);
2015-12-23 14:26:16 +08:00
}
}