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-19 09:05:33 +00:00
|
|
|
use Exception;
|
2020-09-07 20:43:23 +00:00
|
|
|
use Illuminate\Http\Response;
|
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
|
|
|
{
|
2020-12-22 20:11:22 +00:00
|
|
|
/** @var MockInterface|DownloadService */
|
2018-08-18 11:51:52 +00:00
|
|
|
private $downloadService;
|
|
|
|
|
2018-08-19 09:05:33 +00:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
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();
|
|
|
|
$this->downloadService = static::mockIocDependency(DownloadService::class);
|
2016-06-04 16:56:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 20:43:23 +00:00
|
|
|
public function testNonLoggedInUserCannotDownload(): void
|
|
|
|
{
|
|
|
|
$song = Song::first();
|
|
|
|
|
|
|
|
$this->downloadService
|
|
|
|
->shouldReceive('from')
|
|
|
|
->never();
|
|
|
|
|
2020-09-12 15:01:48 +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
|
|
|
{
|
|
|
|
$song = Song::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
|
|
|
|
2018-08-22 20:25:01 +00:00
|
|
|
$songs = Song::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();
|
|
|
|
|
|
|
|
return $requestedIds[0] === $retrievedIds[0] && $requestedIds[1] === $retrievedIds[1];
|
|
|
|
}))
|
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
|
|
|
{
|
|
|
|
$album = Album::first();
|
|
|
|
|
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
|
|
|
{
|
|
|
|
$artist = Artist::first();
|
|
|
|
|
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)
|
2020-09-07 20:43:23 +00:00
|
|
|
->assertStatus(Response::HTTP_FORBIDDEN);
|
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-04-27 18:55:12 +00:00
|
|
|
static::mockIocDependency(InteractionRepository::class)
|
2018-08-22 20:25:01 +00:00
|
|
|
->shouldReceive('getUserFavorites')
|
|
|
|
->once()
|
2020-09-07 20:43:23 +00:00
|
|
|
->with(Mockery::on(static function (User $retrievedUser) use ($user): bool {
|
2018-08-22 20:25:01 +00:00
|
|
|
return $retrievedUser->id === $user->id;
|
|
|
|
}))
|
|
|
|
->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
|
|
|
}
|
|
|
|
}
|