From 944235693951cd7565e5c0415e69f3691bb260ed Mon Sep 17 00:00:00 2001 From: formidablae <81068781+formidablae@users.noreply.github.com> Date: Tue, 2 Nov 2021 12:50:44 +0100 Subject: [PATCH] JWT is working, removed unused stuff. Implemented a simple phpunit test. --- .gitignore | 3 +- .../Controllers/AuthenticationController.php | 9 ----- app/Http/Controllers/CommentController.php | 2 +- app/Http/Controllers/UserController.php | 1 - app/Models/User.php | 7 ++-- app/Providers/AuthServiceProvider.php | 6 ---- composer.json | 4 ++- config/app.php | 7 ---- config/auth.php | 2 +- config/jwt.php | 3 -- database/factories/UserFactory.php | 3 +- .../migrations/1970_01_01_000001_init.php | 33 +++++++++++++++++++ ..._11_02_105203_add_jwt_remove_api_token.php | 32 ++++++++++++++++++ database/migrations/init/down.sql | 2 ++ database/migrations/init/up.sql | 2 ++ phpunit.xml | 24 ++++++++++---- tests/ExampleTest.php | 21 ------------ tests/TestCase.php | 26 +++++++++++++++ tests/UserTest.php | 18 ++++++++++ 19 files changed, 143 insertions(+), 62 deletions(-) delete mode 100644 config/app.php delete mode 100644 config/jwt.php create mode 100644 database/migrations/1970_01_01_000001_init.php create mode 100644 database/migrations/2021_11_02_105203_add_jwt_remove_api_token.php create mode 100644 database/migrations/init/down.sql create mode 100644 database/migrations/init/up.sql delete mode 100644 tests/ExampleTest.php create mode 100644 tests/UserTest.php diff --git a/.gitignore b/.gitignore index 3adebc1..a80a423 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ Homestead.yaml !.env.example .phpunit.result.cache +.phpunit.cache/ .vscode/* !.vscode/settings.json @@ -16,4 +17,4 @@ private/tmp /tmp -.DS_Store \ No newline at end of file +.DS_Store diff --git a/app/Http/Controllers/AuthenticationController.php b/app/Http/Controllers/AuthenticationController.php index d1af458..08356e4 100644 --- a/app/Http/Controllers/AuthenticationController.php +++ b/app/Http/Controllers/AuthenticationController.php @@ -8,15 +8,6 @@ use Laravel\Lumen\Routing\Controller as BaseController; class AuthenticationController extends BaseController { - /** - * create a new AuthenticationController instance. - * - * @return void - */ - public function __construct() { - $this->middleware('auth:api', ['except' => ['login']]); - } - /** * login a user */ diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php index 8f3445c..f3da0b9 100644 --- a/app/Http/Controllers/CommentController.php +++ b/app/Http/Controllers/CommentController.php @@ -74,7 +74,7 @@ public function editComment(Request $request, $comment_id) { $user = Auth::user(); - Gate::authorize('isPremiumUser', $user); // check if user has premium subscription, thus can edit own comments + Gate::authorize('isPremiumUser'); // check if user has premium subscription, thus can edit own comments $comment = $this->getComment($comment_id); diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 5ce5354..bc9df9f 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -43,7 +43,6 @@ public function newUser(Request $request) { $data = $request->all(); $user->fill($data); $user->password = Hash::make($data["password"]); - $user->api_token = Str::random(64); $user->save(); return $user; } diff --git a/app/Models/User.php b/app/Models/User.php index b7b2232..6583ba9 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -28,7 +28,7 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac * @var array */ protected $hidden = [ - 'password', 'updated_at', 'api_token' + 'password', 'updated_at' ]; /** @@ -50,7 +50,10 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac * obtain full name attribute */ public function getFullNameAttribute(): string { - return $this->attributes['first_name'] . ' ' . $this->attributes['last_name']; + if (isset($this->attributes['first_name']) && isset($this->attributes['last_name'])) { + return $this->attributes['first_name'] . ' ' . $this->attributes['last_name']; + } + return ""; } /** diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index a551174..5bace7e 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -35,12 +35,6 @@ public function boot() // should return either a User instance or null. You're free to obtain // the User instance via an API token or any other method necessary. - $this->app['auth']->viaRequest('api', function ($request) { - if ($request->header('Authorization')) { - return User::where('api_token', $request->header('Authorization'))->first(); - } - }); - Gate::policy(Post::class, PostPolicy::class); Gate::policy(Comment::class, CommentPolicy::class); Gate::policy(User::class, PremiumPolicy::class); diff --git a/composer.json b/composer.json index 267e3e4..b01f492 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,8 @@ "scripts": { "post-root-package-install": [ "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" - ] + ], + "test": "phpunit", + "test-coverage": "phpunit --coverage-html /tmp" } } diff --git a/config/app.php b/config/app.php deleted file mode 100644 index b85271f..0000000 --- a/config/app.php +++ /dev/null @@ -1,7 +0,0 @@ - [ - Tymon\JWTAuth\Providers\LaravelServiceProvider::class - ] -]; diff --git a/config/auth.php b/config/auth.php index ba73f54..6b42780 100644 --- a/config/auth.php +++ b/config/auth.php @@ -88,4 +88,4 @@ // ], -]; \ No newline at end of file +]; diff --git a/config/jwt.php b/config/jwt.php deleted file mode 100644 index 2af0cbe..0000000 --- a/config/jwt.php +++ /dev/null @@ -1,3 +0,0 @@ - $this->faker->firstName(), 'last_name' => $this->faker->lastName(), 'email' => $this->faker->unique()->safeEmail, - 'password' => Hash::make('password'), - 'api_token' => Str::random(64) + 'password' => Hash::make('password') ]; } } diff --git a/database/migrations/1970_01_01_000001_init.php b/database/migrations/1970_01_01_000001_init.php new file mode 100644 index 0000000..5f8661d --- /dev/null +++ b/database/migrations/1970_01_01_000001_init.php @@ -0,0 +1,33 @@ +doMigration()) { + DB::unprepared(file_get_contents(__DIR__ . '/init/up.sql')); + } + } + + /** + * Reverse the migrations. + */ + public function down() + { + if ($this->doMigration()) { + DB::unprepared(file_get_contents(__DIR__ . '/init/down.sql')); + } + } + + private function doMigration() + { + return App::environment(['local']); + } +} diff --git a/database/migrations/2021_11_02_105203_add_jwt_remove_api_token.php b/database/migrations/2021_11_02_105203_add_jwt_remove_api_token.php new file mode 100644 index 0000000..0cb4408 --- /dev/null +++ b/database/migrations/2021_11_02_105203_add_jwt_remove_api_token.php @@ -0,0 +1,32 @@ +dropColumn('api_token'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('users', function (Blueprint $table) { + $table->string('api_token', 64)->after('password'); + }); + } +} diff --git a/database/migrations/init/down.sql b/database/migrations/init/down.sql new file mode 100644 index 0000000..b2c2587 --- /dev/null +++ b/database/migrations/init/down.sql @@ -0,0 +1,2 @@ +DROP DATABASE IF EXISTS a_lumen_blog_sviluppo; +DROP DATABASE IF EXISTS a_lumen_blog_testing; \ No newline at end of file diff --git a/database/migrations/init/up.sql b/database/migrations/init/up.sql new file mode 100644 index 0000000..d0cef3e --- /dev/null +++ b/database/migrations/init/up.sql @@ -0,0 +1,2 @@ +CREATE DATABASE IF NOT EXISTS a_lumen_blog_sviluppo; +CREATE DATABASE IF NOT EXISTS a_lumen_blog_testing; \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index 853e786..5379eee 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,17 +1,27 @@ + cacheResultFile=".phpunit.cache/test-results" + executionOrder="depends,defects" + beStrictAboutOutputDuringTests="true" + beStrictAboutTodoAnnotatedTests="true" + convertDeprecationsToExceptions="true" + verbose="true"> - - ./tests + + tests + + + + app + + - - + diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php deleted file mode 100644 index 1bad6ef..0000000 --- a/tests/ExampleTest.php +++ /dev/null @@ -1,21 +0,0 @@ -get('/'); - - $this->assertEquals( - $this->app->version(), $this->response->getContent() - ); - } -} diff --git a/tests/TestCase.php b/tests/TestCase.php index 136846b..8361d00 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,9 +1,14 @@ app->instance(ExceptionHandler::class, new class extends Handler { + public function __construct() {} + + public function report(Throwable $e) + { + // no-op + } + + public function render($request, Throwable $e) { + throw $e; + } + }); + } + + protected function setUp(): void { + parent::setUp(); + $this->disableExceptionHandling(); + } } diff --git a/tests/UserTest.php b/tests/UserTest.php new file mode 100644 index 0000000..f25902f --- /dev/null +++ b/tests/UserTest.php @@ -0,0 +1,18 @@ +notSeeInDatabase('users', ['email' => $email]); + $this->post('auth/register', ['email' => $email, 'password' => $pwd])->seeStatusCode(200); + $this->seeInDatabase('users', ['email' => $email]); + } +}