koel/tests/Feature/DownloadTest.php

167 lines
5.5 KiB
PHP
Raw Normal View History

2016-06-04 16:56:38 +00:00
<?php
2017-02-14 06:53:02 +00:00
namespace Tests\Feature;
2016-06-04 16:56:38 +00:00
use App\Models\Album;
use App\Models\Artist;
2024-01-09 18:34:40 +00:00
use App\Models\Interaction;
2016-06-04 16:56:38 +00:00
use App\Models\Playlist;
use App\Models\Song;
2018-08-18 12:27:17 +00:00
use App\Services\DownloadService;
2018-08-22 20:25:01 +00:00
use Illuminate\Support\Collection;
use Mockery;
2018-08-18 11:51:52 +00:00
use Mockery\MockInterface;
2024-01-09 18:34:40 +00:00
use Tests\TestCase;
2016-06-04 16:56:38 +00:00
2024-01-11 12:41:33 +00:00
use function Tests\create_user;
use function Tests\test_path;
2017-08-05 16:56:11 +00:00
class DownloadTest extends TestCase
2016-06-04 16:56:38 +00:00
{
2022-07-26 20:08:31 +00:00
private MockInterface|DownloadService $downloadService;
2018-08-18 11:51:52 +00:00
2019-07-22 07:03:23 +00:00
public function setUp(): void
2016-06-04 16:56:38 +00:00
{
parent::setUp();
2020-12-22 23:01:49 +00:00
$this->downloadService = self::mock(DownloadService::class);
2016-06-04 16:56:38 +00:00
}
public function testNonLoggedInUserCannotDownload(): void
{
$this->downloadService->shouldNotReceive('getDownloadablePath');
2024-04-24 21:58:19 +00:00
$this->get('download/songs?songs[]=' . Song::factory()->create()->id)
2022-11-16 17:57:38 +00:00
->assertUnauthorized();
}
2019-07-22 07:03:23 +00:00
public function testDownloadOneSong(): void
2016-06-04 16:56:38 +00:00
{
2024-01-10 23:11:45 +00:00
$song = Song::factory()->create();
2024-01-11 12:41:33 +00:00
$user = create_user();
2018-08-18 11:51:52 +00:00
$this->downloadService
2024-01-10 23:11:45 +00:00
->shouldReceive('getDownloadablePath')
2016-06-04 16:56:38 +00:00
->once()
2018-08-22 20:25:01 +00:00
->with(Mockery::on(static function (Collection $retrievedSongs) use ($song) {
return $retrievedSongs->count() === 1 && $retrievedSongs->first()->id === $song->id;
}))
2024-01-10 23:11:45 +00:00
->andReturn(test_path('songs/blank.mp3'));
2016-06-04 16:56:38 +00:00
2020-12-22 20:11:22 +00:00
$this->get("download/songs?songs[]={$song->id}&api_token=" . $user->createToken('Koel')->plainTextToken)
2020-09-06 18:21:39 +00:00
->assertOk();
2016-06-04 16:56:38 +00:00
}
2019-07-22 07:03:23 +00:00
public function testDownloadMultipleSongs(): void
2016-06-04 16:56:38 +00:00
{
2024-01-10 23:11:45 +00:00
$songs = Song::factory(2)->create();
2024-01-11 12:41:33 +00:00
$user = create_user();
2018-08-18 11:51:52 +00:00
$this->downloadService
2024-01-10 23:11:45 +00:00
->shouldReceive('getDownloadablePath')
2016-06-04 16:56:38 +00:00
->once()
->with(Mockery::on(static function (Collection $retrievedSongs) use ($songs): bool {
2024-01-10 23:11:45 +00:00
self::assertEqualsCanonicalizing($retrievedSongs->pluck('id')->all(), $songs->pluck('id')->all());
2018-08-22 20:25:01 +00:00
return true;
2018-08-22 20:25:01 +00:00
}))
2024-01-10 23:11:45 +00:00
->andReturn(test_path('songs/blank.mp3')); // should be a zip file, but we're testing here…
2016-06-04 16:56:38 +00:00
$this->get(
2020-09-12 15:01:48 +00:00
"download/songs?songs[]={$songs[0]->id}&songs[]={$songs[1]->id}&api_token="
2020-12-22 20:11:22 +00:00
. $user->createToken('Koel')->plainTextToken
)
2020-09-06 18:21:39 +00:00
->assertOk();
2016-06-04 16:56:38 +00:00
}
2019-07-22 07:03:23 +00:00
public function testDownloadAlbum(): void
2016-06-04 16:56:38 +00:00
{
2024-01-10 23:11:45 +00:00
$album = Album::factory()->create();
$songs = Song::factory(3)->for($album)->create();
2024-01-11 12:41:33 +00:00
$user = create_user();
2018-08-18 11:51:52 +00:00
$this->downloadService
2024-01-10 23:11:45 +00:00
->shouldReceive('getDownloadablePath')
2016-06-04 16:56:38 +00:00
->once()
2024-01-09 18:34:40 +00:00
->with(Mockery::on(static function (Collection $retrievedSongs) use ($songs): bool {
self::assertEqualsCanonicalizing($retrievedSongs->pluck('id')->all(), $songs->pluck('id')->all());
return true;
2018-08-22 20:25:01 +00:00
}))
2024-01-10 23:11:45 +00:00
->andReturn(test_path('songs/blank.mp3'));
2016-06-04 16:56:38 +00:00
2020-12-22 20:11:22 +00:00
$this->get("download/album/{$album->id}?api_token=" . $user->createToken('Koel')->plainTextToken)
2020-09-06 18:21:39 +00:00
->assertOk();
2016-06-04 16:56:38 +00:00
}
2019-07-22 07:03:23 +00:00
public function testDownloadArtist(): void
2016-06-04 16:56:38 +00:00
{
2024-01-10 23:11:45 +00:00
$artist = Artist::factory()->create();
$songs = Song::factory(3)->for($artist)->create();
2024-01-11 12:41:33 +00:00
$user = create_user();
2018-08-18 11:51:52 +00:00
$this->downloadService
2024-01-10 23:11:45 +00:00
->shouldReceive('getDownloadablePath')
2016-06-04 16:56:38 +00:00
->once()
2024-01-09 18:34:40 +00:00
->with(Mockery::on(static function (Collection $retrievedSongs) use ($songs): bool {
self::assertEqualsCanonicalizing($retrievedSongs->pluck('id')->all(), $songs->pluck('id')->all());
return true;
2018-08-22 20:25:01 +00:00
}))
2024-01-10 23:11:45 +00:00
->andReturn(test_path('songs/blank.mp3'));
2016-06-04 16:56:38 +00:00
2020-12-22 20:11:22 +00:00
$this->get("download/artist/{$artist->id}?api_token=" . $user->createToken('Koel')->plainTextToken)
2020-09-06 18:21:39 +00:00
->assertOk();
2016-06-04 16:56:38 +00:00
}
2019-07-22 07:03:23 +00:00
public function testDownloadPlaylist(): void
2016-06-04 16:56:38 +00:00
{
2024-01-11 12:41:33 +00:00
$user = create_user();
2024-01-09 18:34:40 +00:00
$songs = Song::factory(3)->create();
2018-08-22 20:25:01 +00:00
/** @var Playlist $playlist */
2023-06-05 21:46:41 +00:00
$playlist = Playlist::factory()->for($user)->create();
2024-01-18 11:13:05 +00:00
$playlist->addSongs($songs);
2024-01-09 18:34:40 +00:00
2018-08-18 11:51:52 +00:00
$this->downloadService
2024-01-10 23:11:45 +00:00
->shouldReceive('getDownloadablePath')
2024-01-09 18:34:40 +00:00
->with(Mockery::on(static function (Collection $retrievedSongs) use ($songs): bool {
self::assertEqualsCanonicalizing($retrievedSongs->pluck('id')->all(), $songs->pluck('id')->all());
return true;
2018-08-22 20:25:01 +00:00
}))
2016-06-04 16:56:38 +00:00
->once()
2024-01-10 23:11:45 +00:00
->andReturn(test_path('songs/blank.mp3'));
2016-06-04 16:56:38 +00:00
2020-12-22 20:11:22 +00:00
$this->get("download/playlist/{$playlist->id}?api_token=" . $user->createToken('Koel')->plainTextToken)
2020-09-06 18:21:39 +00:00
->assertOk();
}
public function testNonOwnerCannotDownloadPlaylist(): void
{
$playlist = Playlist::factory()->create();
2024-04-24 21:58:19 +00:00
$this->get("download/playlist/{$playlist->id}?api_token=" . create_user()->createToken('Koel')->plainTextToken)
2022-07-27 15:32:36 +00:00
->assertForbidden();
2016-06-04 16:56:38 +00:00
}
2019-07-22 07:03:23 +00:00
public function testDownloadFavorites(): void
2016-06-04 16:56:38 +00:00
{
2024-01-11 12:41:33 +00:00
$user = create_user();
2024-01-09 18:34:40 +00:00
$favorites = Interaction::factory(3)->for($user)->create(['liked' => true]);
2018-08-22 20:25:01 +00:00
2018-08-18 11:51:52 +00:00
$this->downloadService
2024-01-10 23:11:45 +00:00
->shouldReceive('getDownloadablePath')
2024-01-09 18:34:40 +00:00
->with(Mockery::on(static function (Collection $songs) use ($favorites): bool {
self::assertEqualsCanonicalizing($songs->pluck('id')->all(), $favorites->pluck('song_id')->all());
return true;
}))
2016-06-04 16:56:38 +00:00
->once()
2024-01-10 23:11:45 +00:00
->andReturn(test_path('songs/blank.mp3'));
2016-06-04 16:56:38 +00:00
2020-12-22 20:11:22 +00:00
$this->get('download/favorites?api_token=' . $user->createToken('Koel')->plainTextToken)
->assertOk();
2016-06-04 16:56:38 +00:00
}
}