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;
|
|
|
|
use App\Models\Playlist;
|
|
|
|
use App\Models\Song;
|
|
|
|
use App\Models\User;
|
2018-08-29 06:15:11 +00:00
|
|
|
use App\Repositories\InteractionRepository;
|
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;
|
2016-06-04 16:56:38 +00:00
|
|
|
|
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-09-07 20:43:23 +00:00
|
|
|
|
2020-04-27 18:55:12 +00:00
|
|
|
static::createSampleMediaSet();
|
2020-12-22 23:01:49 +00:00
|
|
|
$this->downloadService = self::mock(DownloadService::class);
|
2016-06-04 16:56:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 20:43:23 +00:00
|
|
|
public function testNonLoggedInUserCannotDownload(): void
|
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
/** @var Song $song */
|
|
|
|
$song = Song::query()->first();
|
2020-09-07 20:43:23 +00:00
|
|
|
|
|
|
|
$this->downloadService
|
|
|
|
->shouldReceive('from')
|
|
|
|
->never();
|
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
$this->get("download/songs?songs[]=$song->id")
|
2020-09-07 20:43:23 +00:00
|
|
|
->assertRedirect('/');
|
|
|
|
}
|
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testDownloadOneSong(): void
|
2016-06-04 16:56:38 +00:00
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
/** @var Song $song */
|
|
|
|
$song = Song::query()->first();
|
2018-08-22 20:25:01 +00:00
|
|
|
|
2020-09-07 20:43:23 +00:00
|
|
|
/** @var User $user */
|
2020-11-14 16:57:25 +00:00
|
|
|
$user = User::factory()->create();
|
2020-09-07 20:43:23 +00:00
|
|
|
|
2018-08-18 11:51:52 +00:00
|
|
|
$this->downloadService
|
|
|
|
->shouldReceive('from')
|
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;
|
|
|
|
}))
|
2020-12-22 20:11:22 +00:00
|
|
|
->andReturn($this->mediaPath . '/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
|
|
|
{
|
2020-09-07 20:43:23 +00:00
|
|
|
/** @var User $user */
|
2020-11-14 16:57:25 +00:00
|
|
|
$user = User::factory()->create();
|
2020-09-07 20:43:23 +00:00
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
/** @var array<Song>|Collection $songs */
|
2022-08-09 18:45:11 +00:00
|
|
|
$songs = Song::query()->take(2)->orderBy('id')->get();
|
2018-08-18 11:51:52 +00:00
|
|
|
|
|
|
|
$this->downloadService
|
|
|
|
->shouldReceive('from')
|
2016-06-04 16:56:38 +00:00
|
|
|
->once()
|
2020-09-07 20:43:23 +00:00
|
|
|
->with(Mockery::on(static function (Collection $retrievedSongs) use ($songs): bool {
|
2018-08-22 20:25:01 +00:00
|
|
|
$retrievedIds = $retrievedSongs->pluck('id')->toArray();
|
|
|
|
$requestedIds = $songs->pluck('id')->toArray();
|
2022-10-23 16:05:12 +00:00
|
|
|
self::assertEqualsCanonicalizing($requestedIds, $retrievedIds);
|
2018-08-22 20:25:01 +00:00
|
|
|
|
2022-10-23 16:05:12 +00:00
|
|
|
return true;
|
2018-08-22 20:25:01 +00:00
|
|
|
}))
|
2020-12-22 20:11:22 +00:00
|
|
|
->andReturn($this->mediaPath . '/blank.mp3'); // should be a zip file, but we're testing here…
|
2016-06-04 16:56:38 +00:00
|
|
|
|
2020-09-07 20:43:23 +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-07 20:43:23 +00:00
|
|
|
)
|
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
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
/** @var Album $album */
|
|
|
|
$album = Album::query()->first();
|
2016-06-04 16:56:38 +00:00
|
|
|
|
2020-09-07 20:43:23 +00:00
|
|
|
/** @var User $user */
|
2020-11-14 16:57:25 +00:00
|
|
|
$user = User::factory()->create();
|
2020-09-07 20:43:23 +00:00
|
|
|
|
2018-08-18 11:51:52 +00:00
|
|
|
$this->downloadService
|
|
|
|
->shouldReceive('from')
|
2016-06-04 16:56:38 +00:00
|
|
|
->once()
|
2020-09-07 20:43:23 +00:00
|
|
|
->with(Mockery::on(static function (Album $retrievedAlbum) use ($album): bool {
|
2018-08-22 20:25:01 +00:00
|
|
|
return $retrievedAlbum->id === $album->id;
|
|
|
|
}))
|
2020-12-22 20:11:22 +00:00
|
|
|
->andReturn($this->mediaPath . '/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
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
/** @var Artist $artist */
|
|
|
|
$artist = Artist::query()->first();
|
2016-06-04 16:56:38 +00:00
|
|
|
|
2020-09-07 20:43:23 +00:00
|
|
|
/** @var User $user */
|
2020-11-14 16:57:25 +00:00
|
|
|
$user = User::factory()->create();
|
2020-09-07 20:43:23 +00:00
|
|
|
|
2018-08-18 11:51:52 +00:00
|
|
|
$this->downloadService
|
|
|
|
->shouldReceive('from')
|
2016-06-04 16:56:38 +00:00
|
|
|
->once()
|
2020-09-07 20:43:23 +00:00
|
|
|
->with(Mockery::on(static function (Artist $retrievedArtist) use ($artist): bool {
|
2018-08-22 20:25:01 +00:00
|
|
|
return $retrievedArtist->id === $artist->id;
|
|
|
|
}))
|
2020-12-22 20:11:22 +00:00
|
|
|
->andReturn($this->mediaPath . '/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
|
|
|
{
|
2020-09-06 18:21:39 +00:00
|
|
|
/** @var User $user */
|
2020-11-14 16:57:25 +00:00
|
|
|
$user = User::factory()->create();
|
2016-06-04 16:56:38 +00:00
|
|
|
|
2018-08-22 20:25:01 +00:00
|
|
|
/** @var Playlist $playlist */
|
2020-11-14 16:57:25 +00:00
|
|
|
$playlist = Playlist::factory()->create([
|
2016-06-04 16:56:38 +00:00
|
|
|
'user_id' => $user->id,
|
|
|
|
]);
|
|
|
|
|
2018-08-18 11:51:52 +00:00
|
|
|
$this->downloadService
|
|
|
|
->shouldReceive('from')
|
2020-09-07 20:43:23 +00:00
|
|
|
->with(Mockery::on(static function (Playlist $retrievedPlaylist) use ($playlist): bool {
|
2018-08-22 20:25:01 +00:00
|
|
|
return $retrievedPlaylist->id === $playlist->id;
|
|
|
|
}))
|
2016-06-04 16:56:38 +00:00
|
|
|
->once()
|
2020-12-22 20:11:22 +00:00
|
|
|
->andReturn($this->mediaPath . '/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
|
|
|
|
{
|
|
|
|
/** @var Playlist $playlist */
|
2020-11-14 16:57:25 +00:00
|
|
|
$playlist = Playlist::factory()->create();
|
2020-09-06 18:21:39 +00:00
|
|
|
|
2020-09-07 20:43:23 +00:00
|
|
|
/** @var User $user */
|
2020-11-14 16:57:25 +00:00
|
|
|
$user = User::factory()->create();
|
2020-09-07 20:43:23 +00:00
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
$this->get("download/playlist/{$playlist->id}?api_token=" . $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
|
|
|
{
|
2018-08-22 20:25:01 +00:00
|
|
|
/** @var User $user */
|
2020-11-14 16:57:25 +00:00
|
|
|
$user = User::factory()->create();
|
2018-08-22 20:25:01 +00:00
|
|
|
$favorites = Collection::make();
|
|
|
|
|
2020-12-22 23:01:49 +00:00
|
|
|
self::mock(InteractionRepository::class)
|
2018-08-22 20:25:01 +00:00
|
|
|
->shouldReceive('getUserFavorites')
|
|
|
|
->once()
|
2022-07-27 08:49:33 +00:00
|
|
|
->with(Mockery::on(static fn (User $retrievedUser) => $retrievedUser->is($user)))
|
2018-08-22 20:25:01 +00:00
|
|
|
->andReturn($favorites);
|
|
|
|
|
2018-08-18 11:51:52 +00:00
|
|
|
$this->downloadService
|
|
|
|
->shouldReceive('from')
|
2018-08-22 20:25:01 +00:00
|
|
|
->with($favorites)
|
2016-06-04 16:56:38 +00:00
|
|
|
->once()
|
2020-12-22 20:11:22 +00:00
|
|
|
->andReturn($this->mediaPath . '/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)
|
2020-09-07 20:43:23 +00:00
|
|
|
->assertOk();
|
2016-06-04 16:56:38 +00:00
|
|
|
}
|
|
|
|
}
|