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