diff --git a/app/Http/Controllers/API/AuthController.php b/app/Http/Controllers/API/AuthController.php new file mode 100644 index 00000000..3be655b6 --- /dev/null +++ b/app/Http/Controllers/API/AuthController.php @@ -0,0 +1,54 @@ +only('email', 'password'))) { + return response()->json(['error' => 'invalid_credentials'], 401); + } + } catch (JWTException $e) { + Log:error($e); + + return response()->json(['error' => 'could_not_create_token'], 500); + } + + return response()->json(compact('token')); + } + + /** + * Log the current user out. + * + * @return \Illuminate\Http\JsonResponse + */ + public function logout() + { + if ($token = JWTAuth::getToken()) { + try { + JWTAuth::invalidate($token); + } catch (Exception $e) { + Log::error($e); + } + } + + return response()->json(); + } +} diff --git a/app/Http/Controllers/API/ProfileController.php b/app/Http/Controllers/API/ProfileController.php new file mode 100644 index 00000000..01d98ab5 --- /dev/null +++ b/app/Http/Controllers/API/ProfileController.php @@ -0,0 +1,28 @@ +only('name', 'email'); + + if ($password = $request->input('password')) { + $data['password'] = Hash::make($password); + } + + return response()->json(auth()->user()->update($data)); + } +} diff --git a/app/Http/Controllers/API/ScrobbleController.php b/app/Http/Controllers/API/ScrobbleController.php new file mode 100644 index 00000000..6dba8e4c --- /dev/null +++ b/app/Http/Controllers/API/ScrobbleController.php @@ -0,0 +1,21 @@ +json($song->scrobble($timestamp)); + } +} diff --git a/app/Http/Controllers/API/SettingController.php b/app/Http/Controllers/API/SettingController.php index 1a9f98de..db2f9558 100644 --- a/app/Http/Controllers/API/SettingController.php +++ b/app/Http/Controllers/API/SettingController.php @@ -15,7 +15,7 @@ class SettingController extends Controller * * @return \Illuminate\Http\JsonResponse */ - public function save(SettingRequest $request) + public function store(SettingRequest $request) { // For right now there's only one setting to be saved Setting::set('media_path', rtrim(trim($request->input('media_path')), '/')); diff --git a/app/Http/Controllers/API/SongController.php b/app/Http/Controllers/API/SongController.php index f7b437fe..ee3d4381 100644 --- a/app/Http/Controllers/API/SongController.php +++ b/app/Http/Controllers/API/SongController.php @@ -41,7 +41,7 @@ class SongController extends Controller * * @return \Illuminate\Http\JsonResponse */ - public function getInfo(Song $song) + public function show(Song $song) { return response()->json([ 'lyrics' => $song->lyrics, @@ -50,19 +50,6 @@ class SongController extends Controller ]); } - /** - * Scrobble a song. - * - * @param Song $song - * @param string $timestamp The UNIX timestamp when the song started playing. - * - * @return \Illuminate\Http\JsonResponse - */ - public function scrobble(Song $song, $timestamp) - { - return response()->json($song->scrobble($timestamp)); - } - /** * Update songs info. * diff --git a/app/Http/Controllers/API/UserController.php b/app/Http/Controllers/API/UserController.php index 42378f2c..0cb1b170 100644 --- a/app/Http/Controllers/API/UserController.php +++ b/app/Http/Controllers/API/UserController.php @@ -2,59 +2,13 @@ namespace App\Http\Controllers\API; -use App\Http\Requests\API\ProfileUpdateRequest; -use App\Http\Requests\API\UserLoginRequest; use App\Http\Requests\API\UserStoreRequest; use App\Http\Requests\API\UserUpdateRequest; use App\Models\User; -use Exception; use Hash; -use JWTAuth; -use Log; -use Tymon\JWTAuth\Exceptions\JWTException; class UserController extends Controller { - /** - * Log a user in. - * - * @param UserLoginRequest $request - * - * @return \Illuminate\Http\JsonResponse - */ - public function login(UserLoginRequest $request) - { - try { - if (!$token = JWTAuth::attempt($request->only('email', 'password'))) { - return response()->json(['error' => 'invalid_credentials'], 401); - } - } catch (JWTException $e) { - Log:error($e); - - return response()->json(['error' => 'could_not_create_token'], 500); - } - - return response()->json(compact('token')); - } - - /** - * Log the current user out. - * - * @return \Illuminate\Http\JsonResponse - */ - public function logout() - { - if ($token = JWTAuth::getToken()) { - try { - JWTAuth::invalidate($token); - } catch (Exception $e) { - Log::error($e); - } - } - - return response()->json(); - } - /** * Create a new user. * @@ -103,22 +57,4 @@ class UserController extends Controller return response()->json($user->delete()); } - - /** - * Update the current user's profile. - * - * @param ProfileUpdateRequest $request - * - * @return \Illuminate\Http\JsonResponse - */ - public function updateProfile(ProfileUpdateRequest $request) - { - $data = $request->only('name', 'email'); - - if ($password = $request->input('password')) { - $data['password'] = Hash::make($password); - } - - return response()->json(auth()->user()->update($data)); - } } diff --git a/app/Http/routes.php b/app/Http/routes.php index 2674705a..c58a3c7a 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -11,8 +11,8 @@ Route::get('/♫', function () { Route::group(['prefix' => 'api', 'namespace' => 'API'], function () { - Route::post('me', 'UserController@login'); - Route::delete('me', 'UserController@logout'); + Route::post('me', 'AuthController@login'); + Route::delete('me', 'AuthController@logout'); Route::group(['middleware' => 'jwt.auth'], function () { Route::get('/', function () { @@ -21,14 +21,13 @@ Route::group(['prefix' => 'api', 'namespace' => 'API'], function () { Route::get('data', 'DataController@index'); - Route::post('settings', 'SettingController@save'); + Route::post('settings', 'SettingController@store'); Route::get('{song}/play', 'SongController@play'); - Route::post('{song}/scrobble/{timestamp}', 'SongController@scrobble')->where([ + Route::post('{song}/scrobble/{timestamp}', 'ScrobbleController@store')->where([ 'timestamp' => '\d+', ]); - Route::get('{song}/info', 'SongController@getInfo'); - + Route::get('{song}/info', 'SongController@show'); Route::put('songs', 'SongController@update'); Route::post('interaction/play', 'InteractionController@play'); @@ -40,7 +39,7 @@ Route::group(['prefix' => 'api', 'namespace' => 'API'], function () { Route::put('playlist/{playlist}/sync', 'PlaylistController@sync')->where(['playlist' => '\d+']); Route::resource('user', 'UserController', ['only' => ['store', 'update', 'destroy']]); - Route::put('me', 'UserController@updateProfile'); + Route::put('me', 'ProfileController@update'); Route::get('lastfm/connect', 'LastfmController@connect'); Route::post('lastfm/session-key', 'LastfmController@setSessionKey'); diff --git a/tests/LastfmTest.php b/tests/LastfmTest.php index 15f8dd14..16b3b880 100644 --- a/tests/LastfmTest.php +++ b/tests/LastfmTest.php @@ -203,21 +203,4 @@ class LastfmTest extends TestCase (new UpdateLastfmNowPlaying($lastfm))->handle(new SongStartedPlaying($song, $user)); } - - public function testScrobble() - { - $this->withoutEvents(); - $this->createSampleMediaSet(); - - $user = factory(User::class)->create(['preferences' => ['lastfm_session_key' => 'bar']]); - $song = Song::first(); - - $ts = time(); - - $lastfm = m::mock(Lastfm::class, ['enabled' => true]); - $lastfm->shouldReceive('scrobble') - ->with($song->album->artist->name, $song->title, $ts, $song->album->name, 'bar'); - - $this->post("/api/{$song->id}/scrobble/$ts"); - } } diff --git a/tests/ProfileTest.php b/tests/ProfileTest.php new file mode 100644 index 00000000..c86bbf3d --- /dev/null +++ b/tests/ProfileTest.php @@ -0,0 +1,18 @@ +actingAs(factory(User::class)->create()) + ->put('api/me', ['name' => 'Foo', 'email' => 'bar@baz.com']); + + $this->seeInDatabase('users', ['name' => 'Foo', 'email' => 'bar@baz.com']); + } +} diff --git a/tests/ScrobbleTest.php b/tests/ScrobbleTest.php new file mode 100644 index 00000000..1f776e73 --- /dev/null +++ b/tests/ScrobbleTest.php @@ -0,0 +1,30 @@ +withoutEvents(); + $this->createSampleMediaSet(); + + $user = factory(User::class)->create(['preferences' => ['lastfm_session_key' => 'bar']]); + $song = Song::first(); + + $ts = time(); + + $lastfm = m::mock(Lastfm::class, ['enabled' => true]); + $lastfm->shouldReceive('scrobble') + ->with($song->album->artist->name, $song->title, $ts, $song->album->name, 'bar'); + + $this->post("/api/{$song->id}/scrobble/$ts"); + } +} diff --git a/tests/UserTest.php b/tests/UserTest.php index eb099107..a24cad4d 100644 --- a/tests/UserTest.php +++ b/tests/UserTest.php @@ -30,16 +30,6 @@ class UserTest extends TestCase $this->seeInDatabase('users', ['name' => 'Foo']); } - public function testUpdateProfile() - { - $user = factory(User::class)->create(); - - $this->actingAs($user) - ->put('api/me', ['name' => 'Foo', 'email' => 'bar@baz.com']); - - $this->seeInDatabase('users', ['name' => 'Foo', 'email' => 'bar@baz.com']); - } - public function testUpdateUser() { $user = factory(User::class)->create();