From 71f5e1d80457f6f18d67e39ca22088d02bdbdd28 Mon Sep 17 00:00:00 2001 From: Phan An Date: Tue, 16 Jan 2024 22:14:14 +0100 Subject: [PATCH] feat(test): add tests song visibility --- .../API/MakeSongsPrivateController.php | 28 ------------ ...oller.php => PrivatizeSongsController.php} | 13 ++---- .../API/PublicizeSongsController.php | 24 ++++++++++ app/Services/SongService.php | 4 +- resources/assets/js/stores/songStore.ts | 4 +- routes/api.base.php | 8 ++-- tests/Feature/KoelPlus/SongTest.php | 45 +++++++++++++++++++ tests/Feature/KoelPlus/SongVisibilityTest.php | 8 ++-- tests/Feature/SongVisibilityTest.php | 4 +- 9 files changed, 87 insertions(+), 51 deletions(-) delete mode 100644 app/Http/Controllers/API/MakeSongsPrivateController.php rename app/Http/Controllers/API/{MakeSongsPublicController.php => PrivatizeSongsController.php} (59%) create mode 100644 app/Http/Controllers/API/PublicizeSongsController.php diff --git a/app/Http/Controllers/API/MakeSongsPrivateController.php b/app/Http/Controllers/API/MakeSongsPrivateController.php deleted file mode 100644 index 8e91ef62..00000000 --- a/app/Http/Controllers/API/MakeSongsPrivateController.php +++ /dev/null @@ -1,28 +0,0 @@ -getMany(ids: $request->songs, scopedUser: $user); - $songs->each(fn ($song) => $this->authorize('own', $song)); - - $songService->makeSongsPrivate($songs); - - return response()->noContent(); - } -} diff --git a/app/Http/Controllers/API/MakeSongsPublicController.php b/app/Http/Controllers/API/PrivatizeSongsController.php similarity index 59% rename from app/Http/Controllers/API/MakeSongsPublicController.php rename to app/Http/Controllers/API/PrivatizeSongsController.php index 7ddcb631..fb8462ad 100644 --- a/app/Http/Controllers/API/MakeSongsPublicController.php +++ b/app/Http/Controllers/API/PrivatizeSongsController.php @@ -6,23 +6,18 @@ use App\Http\Controllers\Controller; use App\Http\Requests\API\ChangeSongsVisibilityRequest; use App\Models\Song; use App\Models\User; -use App\Repositories\SongRepository; use App\Services\SongService; use Illuminate\Contracts\Auth\Authenticatable; -class MakeSongsPublicController extends Controller +class PrivatizeSongsController extends Controller { /** @param User $user */ - public function __invoke( - ChangeSongsVisibilityRequest $request, - SongRepository $repository, - SongService $songService, - Authenticatable $user - ) { + public function __invoke(ChangeSongsVisibilityRequest $request, SongService $songService, Authenticatable $user) + { $songs = Song::query()->findMany($request->songs); $songs->each(fn ($song) => $this->authorize('own', $song)); - $songService->makeSongsPublic($songs); + $songService->privatizeSongs($songs); return response()->noContent(); } diff --git a/app/Http/Controllers/API/PublicizeSongsController.php b/app/Http/Controllers/API/PublicizeSongsController.php new file mode 100644 index 00000000..44657e22 --- /dev/null +++ b/app/Http/Controllers/API/PublicizeSongsController.php @@ -0,0 +1,24 @@ +findMany($request->songs); + $songs->each(fn ($song) => $this->authorize('own', $song)); + + $songService->publicizeSongs($songs); + + return response()->noContent(); + } +} diff --git a/app/Services/SongService.php b/app/Services/SongService.php index fc83a43c..5ffbe2f6 100644 --- a/app/Services/SongService.php +++ b/app/Services/SongService.php @@ -82,12 +82,12 @@ class SongService return $this->songRepository->getOne($song->id); } - public function makeSongsPublic(Collection $songs): void + public function publicizeSongs(Collection $songs): void { Song::query()->whereIn('id', $songs->pluck('id'))->update(['is_public' => true]); } - public function makeSongsPrivate(Collection $songs): void + public function privatizeSongs(Collection $songs): void { Song::query()->whereIn('id', $songs->pluck('id'))->update(['is_public' => false]); } diff --git a/resources/assets/js/stores/songStore.ts b/resources/assets/js/stores/songStore.ts index 90b34a0b..bcae5dce 100644 --- a/resources/assets/js/stores/songStore.ts +++ b/resources/assets/js/stores/songStore.ts @@ -243,7 +243,7 @@ export const songStore = { }, async makePublic (songs: Song[]) { - await http.put('songs/make-public', { + await http.put('songs/publicize', { songs: songs.map(song => song.id) }) @@ -251,7 +251,7 @@ export const songStore = { }, async makePrivate (songs: Song[]) { - await http.put('songs/make-private', { + await http.put('songs/privatize', { songs: songs.map(song => song.id) }) diff --git a/routes/api.base.php b/routes/api.base.php index e4b013d8..b484c7dc 100644 --- a/routes/api.base.php +++ b/routes/api.base.php @@ -23,14 +23,14 @@ use App\Http\Controllers\API\FetchSongsForQueueController; use App\Http\Controllers\API\GenreController; use App\Http\Controllers\API\GenreSongController; use App\Http\Controllers\API\LikeMultipleSongsController; -use App\Http\Controllers\API\MakeSongsPrivateController; -use App\Http\Controllers\API\MakeSongsPublicController; use App\Http\Controllers\API\ObjectStorage\S3\SongController as S3SongController; use App\Http\Controllers\API\PlaylistController; use App\Http\Controllers\API\PlaylistFolderController; use App\Http\Controllers\API\PlaylistFolderPlaylistController; use App\Http\Controllers\API\PlaylistSongController; +use App\Http\Controllers\API\PrivatizeSongsController; use App\Http\Controllers\API\ProfileController; +use App\Http\Controllers\API\PublicizeSongsController; use App\Http\Controllers\API\QueueStateController; use App\Http\Controllers\API\RegisterPlayController; use App\Http\Controllers\API\ScrobbleController; @@ -160,8 +160,8 @@ Route::prefix('api')->middleware('api')->group(static function (): void { Route::post('invitations', [UserInvitationController::class, 'invite']); Route::delete('invitations', [UserInvitationController::class, 'revoke']); - Route::put('songs/make-public', MakeSongsPublicController::class); - Route::put('songs/make-private', MakeSongsPrivateController::class); + Route::put('songs/publicize', PublicizeSongsController::class); + Route::put('songs/privatize', PrivatizeSongsController::class); Route::post('licenses/activate', ActivateLicenseController::class); }); diff --git a/tests/Feature/KoelPlus/SongTest.php b/tests/Feature/KoelPlus/SongTest.php index e052772d..3c68c1bb 100644 --- a/tests/Feature/KoelPlus/SongTest.php +++ b/tests/Feature/KoelPlus/SongTest.php @@ -133,4 +133,49 @@ class SongTest extends TestCase $this->deleteAs('api/songs', ['songs' => $ownSongs->pluck('id')->toArray()], $currentUser) ->assertSuccessful(); } + + public function testPublicizeSongs(): void + { + $user = create_user(); + + /** @var Song $songs */ + $songs = Song::factory(3)->for($user, 'owner')->private()->create(); + + $this->putAs('api/songs/publicize', ['songs' => $songs->pluck('id')->toArray()], $user) + ->assertSuccessful(); + + $songs->each(static function (Song $song): void { + $song->refresh(); + self::assertTrue($song->is_public); + }); + } + + public function testPrivatizeSongs(): void + { + $user = create_user(); + + /** @var Song $songs */ + $songs = Song::factory(3)->for($user, 'owner')->public()->create(); + + $this->putAs('api/songs/privatize', ['songs' => $songs->pluck('id')->toArray()], $user) + ->assertSuccessful(); + + $songs->each(static function (Song $song): void { + $song->refresh(); + self::assertFalse($song->is_public); + }); + } + + public function testPublicizingOrPrivatizingSongsRequiresOwnership(): void + { + $songs = Song::factory(3)->public()->create(); + + $this->putAs('api/songs/privatize', ['songs' => $songs->pluck('id')->toArray()]) + ->assertForbidden(); + + $otherSongs = Song::factory(3)->private()->create(); + + $this->putAs('api/songs/publicize', ['songs' => $otherSongs->pluck('id')->toArray()]) + ->assertForbidden(); + } } diff --git a/tests/Feature/KoelPlus/SongVisibilityTest.php b/tests/Feature/KoelPlus/SongVisibilityTest.php index c3a09f87..7fdc5640 100644 --- a/tests/Feature/KoelPlus/SongVisibilityTest.php +++ b/tests/Feature/KoelPlus/SongVisibilityTest.php @@ -27,13 +27,13 @@ class SongVisibilityTest extends TestCase $externalSongs = Song::factory(3)->for($anotherUser, 'owner')->private()->create(); // We can't make public songs that are not ours. - $this->putAs('api/songs/make-public', ['songs' => $externalSongs->pluck('id')->toArray()], $currentUser) + $this->putAs('api/songs/publicize', ['songs' => $externalSongs->pluck('id')->toArray()], $currentUser) ->assertForbidden(); // But we can our own songs. $ownSongs = Song::factory(3)->for($currentUser, 'owner')->create(); - $this->putAs('api/songs/make-public', ['songs' => $ownSongs->pluck('id')->toArray()], $currentUser) + $this->putAs('api/songs/publicize', ['songs' => $ownSongs->pluck('id')->toArray()], $currentUser) ->assertSuccessful(); $ownSongs->each(static fn (Song $song) => self::assertTrue($song->refresh()->is_public)); @@ -48,13 +48,13 @@ class SongVisibilityTest extends TestCase $externalSongs = Song::factory(3)->for($anotherUser, 'owner')->public()->create(); // We can't make private songs that are not ours. - $this->putAs('api/songs/make-private', ['songs' => $externalSongs->pluck('id')->toArray()], $currentUser) + $this->putAs('api/songs/privatize', ['songs' => $externalSongs->pluck('id')->toArray()], $currentUser) ->assertForbidden(); // But we can our own songs. $ownSongs = Song::factory(3)->for($currentUser, 'owner')->create(); - $this->putAs('api/songs/make-private', ['songs' => $ownSongs->pluck('id')->toArray()], $currentUser) + $this->putAs('api/songs/privatize', ['songs' => $ownSongs->pluck('id')->toArray()], $currentUser) ->assertSuccessful(); $ownSongs->each(static fn (Song $song) => self::assertFalse($song->refresh()->is_public)); diff --git a/tests/Feature/SongVisibilityTest.php b/tests/Feature/SongVisibilityTest.php index b1919b38..202fb167 100644 --- a/tests/Feature/SongVisibilityTest.php +++ b/tests/Feature/SongVisibilityTest.php @@ -14,10 +14,10 @@ class SongVisibilityTest extends TestCase $owner = create_admin(); Song::factory(3)->create(); - $this->putAs('api/songs/make-public', ['songs' => Song::query()->pluck('id')->all()], $owner) + $this->putAs('api/songs/publicize', ['songs' => Song::query()->pluck('id')->all()], $owner) ->assertForbidden(); - $this->putAs('api/songs/make-private', ['songs' => Song::query()->pluck('id')->all()], $owner) + $this->putAs('api/songs/privatize', ['songs' => Song::query()->pluck('id')->all()], $owner) ->assertForbidden(); } }