From a92083ee98c8467b90606c6edd5511d682004ad3 Mon Sep 17 00:00:00 2001 From: Jeremy Postlethwaite Date: Sun, 3 Mar 2024 12:07:15 -0800 Subject: [PATCH 1/4] GH-26 --- .../Resource/Playground/CreateJsonTrait.php | 19 +++++-- .../Resource/Playground/CreateTrait.php | 20 ++++++-- .../Resource/Playground/DestroyJsonTrait.php | 42 ++++------------ .../Resource/Playground/DestroyTrait.php | 35 ++++--------- .../Resource/Playground/EditJsonTrait.php | 18 +++++-- .../Resource/Playground/EditTrait.php | 17 +++++-- .../Resource/Playground/IndexJsonTrait.php | 5 +- .../Resource/Playground/IndexTrait.php | 4 +- .../Resource/Playground/LockJsonTrait.php | 28 ++++------- .../Resource/Playground/LockTrait.php | 28 ++++------- .../Resource/Playground/RestoreJsonTrait.php | 19 +++---- .../Playground/RestoreRevisionJsonTrait.php | 4 +- .../Playground/RestoreRevisionTrait.php | 4 +- .../Resource/Playground/RestoreTrait.php | 19 +++---- .../Resource/Playground/RevisionJsonTrait.php | 4 +- .../Resource/Playground/RevisionTrait.php | 4 +- .../Playground/RevisionsJsonTrait.php | 4 +- .../Resource/Playground/RevisionsTrait.php | 4 +- .../Resource/Playground/ShowJsonTrait.php | 5 +- .../Resource/Playground/ShowTrait.php | 4 +- .../Resource/Playground/StoreJsonTrait.php | 45 +++++++++++------ .../Resource/Playground/StoreTrait.php | 50 +++++++++++++------ .../Resource/Playground/UnlockJsonTrait.php | 19 +++---- .../Resource/Playground/UnlockTrait.php | 19 +++---- .../Resource/Playground/UpdateJsonTrait.php | 29 ++++++----- .../Resource/Playground/UpdateTrait.php | 34 +++++++------ 26 files changed, 255 insertions(+), 228 deletions(-) diff --git a/src/Feature/Http/Controllers/Resource/Playground/CreateJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/CreateJsonTrait.php index b883135..29a91b0 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/CreateJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/CreateJsonTrait.php @@ -12,6 +12,12 @@ */ trait CreateJsonTrait { + protected int $status_code_guest_create = 403; + + protected string $create_info_parameter = 'owned_by_id'; + + protected string $create_info_invalid_value = '[duck]'; + /** * @return class-string */ @@ -39,7 +45,7 @@ public function test_json_guest_cannot_get_create_info() $response = $this->getJson($url); // $response->dump(); - $response->assertStatus(403); + $response->assertStatus($this->status_code_json_guest_create); } public function test_json_admin_can_get_create_info() @@ -73,9 +79,12 @@ public function test_json_get_create_info_as_admin_with_invalid_parameter_and_fa $packageInfo['model_route'] )); - $response = $this->actingAs($user) - ->from($url) - ->getJson($url.'?owned_by_id=[duck]'); + $response = $this->actingAs($user)->from($url)->getJson(sprintf( + '%1$s?%2$s=%3$s', + $url, + $this->create_info_parameter, + $this->create_info_invalid_value + )); $this->assertAuthenticated(); @@ -85,6 +94,6 @@ public function test_json_get_create_info_as_admin_with_invalid_parameter_and_fa 'errors', ]); - $response->assertJsonValidationErrorFor('owned_by_id'); + $response->assertJsonValidationErrorFor($this->create_info_parameter); } } diff --git a/src/Feature/Http/Controllers/Resource/Playground/CreateTrait.php b/src/Feature/Http/Controllers/Resource/Playground/CreateTrait.php index 250e49d..be04de1 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/CreateTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/CreateTrait.php @@ -12,6 +12,12 @@ */ trait CreateTrait { + protected int $status_code_guest_create = 403; + + protected string $create_form_parameter = 'owned_by_id'; + + protected string $create_form_invalid_value = '[duck]'; + /** * @return class-string */ @@ -32,7 +38,8 @@ public function test_guest_cannot_render_create_view() )); $response = $this->get($url); - $response->assertStatus(403); + + $response->assertStatus($this->status_code_guest_create); } public function test_admin_can_render_create_view() @@ -91,9 +98,12 @@ public function test_create_view_as_admin_with_invalid_parameter_and_fail_valida $packageInfo['model_route'] )); - $response = $this->actingAs($user) - ->from($url) - ->get($url.'?owned_by_id=[duck]'); + $response = $this->actingAs($user)->from($url)->get(sprintf( + '%1$s?%2$s=%3$s', + $url, + $this->create_form_parameter, + $this->create_form_invalid_value + )); // $response->dump(); // $response->dumpHeaders(); @@ -102,7 +112,7 @@ public function test_create_view_as_admin_with_invalid_parameter_and_fail_valida // // The owned by id field must be a valid UUID. $response->assertSessionHasErrors([ - 'owned_by_id', + $this->create_form_parameter, ]); $this->assertAuthenticated(); diff --git a/src/Feature/Http/Controllers/Resource/Playground/DestroyJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/DestroyJsonTrait.php index bec842f..cb0b9ae 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/DestroyJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/DestroyJsonTrait.php @@ -12,6 +12,10 @@ */ trait DestroyJsonTrait { + protected int $status_code_json_guest_destroy = 403; + + protected int $status_code_json_user_destroy = 401; + /** * @return class-string */ @@ -32,7 +36,6 @@ public function test_json_guest_cannot_destroy() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => null, 'deleted_at' => null, ]); @@ -45,11 +48,10 @@ public function test_json_guest_cannot_destroy() $response = $this->deleteJson($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_json_guest_destroy); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => null, 'deleted_at' => null, ]); } @@ -62,13 +64,10 @@ public function test_json_destroy_as_admin_and_succeed() $user = User::factory()->admin()->create(); - $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, - ]); + $model = $fqdn::factory()->create(); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -88,11 +87,9 @@ public function test_json_destroy_as_admin_and_succeed() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, ]); $this->assertDatabaseMissing($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); $response->assertNoContent(); @@ -106,13 +103,10 @@ public function test_json_destroy_as_admin_and_succeed_with_force_delete() $user = User::factory()->admin()->create(); - $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, - ]); + $model = $fqdn::factory()->create(); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -141,13 +135,10 @@ public function test_json_destroy_as_admin_and_succeed_with_no_content() $user = User::factory()->admin()->create(); - $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, - ]); + $model = $fqdn::factory()->create(); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -162,11 +153,9 @@ public function test_json_destroy_as_admin_and_succeed_with_no_content() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, ]); $this->assertDatabaseMissing($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -181,13 +170,10 @@ public function test_json_destroy_as_admin_and_succeed_and_ignore_redirect() $user = User::factory()->admin()->create(); - $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, - ]); + $model = $fqdn::factory()->create(); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -209,11 +195,9 @@ public function test_json_destroy_as_admin_and_succeed_and_ignore_redirect() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, ]); $this->assertDatabaseMissing($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -228,13 +212,10 @@ public function test_json_destroy_as_user_and_get_denied_and_no_force_delete_all $user = User::factory()->create(); - $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, - ]); + $model = $fqdn::factory()->create(); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -248,11 +229,10 @@ public function test_json_destroy_as_user_and_get_denied_and_no_force_delete_all $response = $this->actingAs($user)->deleteJson($url); - $response->assertStatus(401); + $response->assertStatus($this->status_code_json_user_destroy); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); } diff --git a/src/Feature/Http/Controllers/Resource/Playground/DestroyTrait.php b/src/Feature/Http/Controllers/Resource/Playground/DestroyTrait.php index 49cfdae..daa2ce8 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/DestroyTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/DestroyTrait.php @@ -12,6 +12,10 @@ */ trait DestroyTrait { + protected int $status_code_guest_destroy = 403; + + protected int $status_code_user_destroy = 401; + /** * @return class-string */ @@ -32,7 +36,6 @@ public function test_guest_cannot_destroy() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => null, 'deleted_at' => null, ]); @@ -45,11 +48,10 @@ public function test_guest_cannot_destroy() $response = $this->delete($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_destroy); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => null, 'deleted_at' => null, ]); } @@ -62,13 +64,10 @@ public function test_destroy_as_admin_and_succeed() $user = User::factory()->admin()->create(); - $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, - ]); + $model = $fqdn::factory()->create(); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -83,12 +82,10 @@ public function test_destroy_as_admin_and_succeed() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, ]); $this->assertDatabaseMissing($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -103,13 +100,10 @@ public function test_destroy_as_admin_and_succeed_with_force_delete() $user = User::factory()->admin()->create(); - $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, - ]); + $model = $fqdn::factory()->create(); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -138,13 +132,10 @@ public function test_destroy_as_admin_and_succeed_with_redirect_to_index_with_tr $user = User::factory()->admin()->create(); - $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, - ]); + $model = $fqdn::factory()->create(); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -166,11 +157,9 @@ public function test_destroy_as_admin_and_succeed_with_redirect_to_index_with_tr $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, ]); $this->assertDatabaseMissing($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -185,13 +174,10 @@ public function test_destroy_as_user_and_get_denied_and_no_force_delete_allowed( $user = User::factory()->create(); - $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, - ]); + $model = $fqdn::factory()->create(); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -205,11 +191,10 @@ public function test_destroy_as_user_and_get_denied_and_no_force_delete_allowed( $response = $this->actingAs($user)->delete($url); - $response->assertStatus(401); + $response->assertStatus($this->status_code_user_destroy); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); } diff --git a/src/Feature/Http/Controllers/Resource/Playground/EditJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/EditJsonTrait.php index 4fd0134..c1f1f5f 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/EditJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/EditJsonTrait.php @@ -12,6 +12,12 @@ */ trait EditJsonTrait { + protected int $status_code_json_guest_edit = 403; + + protected string $edit_info_parameter = 'owned_by_id'; + + protected string $edit_info_invalid_value = '[duck]'; + /** * @return class-string */ @@ -43,7 +49,8 @@ public function test_json_guest_cannot_get_edit_info() ]); $response = $this->getJson($url); - $response->assertStatus(403); + + $response->assertStatus($this->status_code_json_guest_edit); } public function test_json_admin_can_get_edit_info() @@ -92,7 +99,12 @@ public function test_json_edit_as_admin_and_fail_validation_with_invalid_paramet $packageInfo['model_slug'] => $model->id, ]); - $response = $this->actingAs($user)->getJson($url.'?owned_by_id=[duck]'); + $response = $this->actingAs($user)->from($url)->getJson(sprintf( + '%1$s?%2$s=%3$s', + $url, + $this->edit_info_parameter, + $this->edit_info_invalid_value + )); // $response->dump(); // $response->dumpHeaders(); @@ -102,7 +114,7 @@ public function test_json_edit_as_admin_and_fail_validation_with_invalid_paramet $response->assertJsonStructure([ 'errors', ]); - $response->assertJsonValidationErrorFor('owned_by_id'); + $response->assertJsonValidationErrorFor($this->edit_info_parameter); $this->assertAuthenticated(); } diff --git a/src/Feature/Http/Controllers/Resource/Playground/EditTrait.php b/src/Feature/Http/Controllers/Resource/Playground/EditTrait.php index 88368d0..4b0270b 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/EditTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/EditTrait.php @@ -12,6 +12,12 @@ */ trait EditTrait { + protected int $status_code_guest_edit = 403; + + protected string $edit_form_parameter = 'owned_by_id'; + + protected string $edit_form_invalid_value = '[duck]'; + /** * @return class-string */ @@ -39,7 +45,7 @@ public function test_guest_cannot_render_edit_view() $response = $this->get($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_edit); } public function test_admin_can_render_edit_view() @@ -115,13 +121,18 @@ public function test_edit_view_as_admin_with_invalid_parameter_and_fail_validati $packageInfo['model_slug'] => $model->id, ]); - $response = $this->actingAs($user)->get($url.'?owned_by_id=[duck]'); + $response = $this->actingAs($user)->from($url)->get(sprintf( + '%1$s?%2$s=%3$s', + $url, + $this->edit_form_parameter, + $this->edit_form_invalid_value + )); $response->assertStatus(302); // // The owned by id field must be a valid UUID. $response->assertSessionHasErrors([ - 'owned_by_id', + $this->edit_form_parameter, ]); $this->assertAuthenticated(); diff --git a/src/Feature/Http/Controllers/Resource/Playground/IndexJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/IndexJsonTrait.php index ddf7a18..40d497b 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/IndexJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/IndexJsonTrait.php @@ -12,6 +12,8 @@ */ trait IndexJsonTrait { + protected int $status_code_json_guest_index = 403; + /** * @return class-string */ @@ -38,7 +40,8 @@ public function test_json_guest_cannot_get_index() $url = route($packageInfo['model_route']); $response = $this->getJson($url); - $response->assertStatus(403); + + $response->assertStatus($this->status_code_json_guest_index); } public function test_json_admin_can_get_index() diff --git a/src/Feature/Http/Controllers/Resource/Playground/IndexTrait.php b/src/Feature/Http/Controllers/Resource/Playground/IndexTrait.php index 4e2e02c..5502bd1 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/IndexTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/IndexTrait.php @@ -12,6 +12,8 @@ */ trait IndexTrait { + protected int $status_code_guest_index = 403; + /** * @return class-string */ @@ -34,7 +36,7 @@ public function test_guest_cannot_render_index_view() $response = $this->get($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_index); } public function test_admin_can_render_index_view() diff --git a/src/Feature/Http/Controllers/Resource/Playground/LockJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/LockJsonTrait.php index 0608559..07a67a2 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/LockJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/LockJsonTrait.php @@ -12,6 +12,10 @@ */ trait LockJsonTrait { + protected int $status_code_json_guest_lock = 403; + + protected int $status_code_json_user_lock = 401; + /** * @return class-string */ @@ -37,7 +41,6 @@ public function test_json_guest_cannot_lock() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => null, 'locked' => false, ]); @@ -50,11 +53,10 @@ public function test_json_guest_cannot_lock() $response = $this->putJson($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_json_guest_lock); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => null, 'locked' => false, ]); } @@ -67,13 +69,10 @@ public function test_json_lock_as_admin_and_succeed() $user = User::factory()->admin()->create(); - $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, - ]); + $model = $fqdn::factory()->create(); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => false, ]); @@ -88,7 +87,6 @@ public function test_json_lock_as_admin_and_succeed() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => true, ]); @@ -103,13 +101,10 @@ public function test_json_lock_as_admin_and_succeed_with_no_redirect() $user = User::factory()->admin()->create(); - $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, - ]); + $model = $fqdn::factory()->create(); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => false, ]); @@ -135,7 +130,6 @@ public function test_json_lock_as_admin_and_succeed_with_no_redirect() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => true, ]); @@ -153,13 +147,10 @@ public function test_json_lock_as_user_and_get_denied() $user = User::factory()->create(); // $user = User::factory()->admin()->create(); - $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, - ]); + $model = $fqdn::factory()->create(); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => false, ]); @@ -172,11 +163,10 @@ public function test_json_lock_as_user_and_get_denied() $response = $this->actingAs($user)->putJson($url); - $response->assertStatus(401); + $response->assertStatus($this->status_code_json_user_lock); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => false, ]); } diff --git a/src/Feature/Http/Controllers/Resource/Playground/LockTrait.php b/src/Feature/Http/Controllers/Resource/Playground/LockTrait.php index d7976a5..4012726 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/LockTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/LockTrait.php @@ -12,6 +12,10 @@ */ trait LockTrait { + protected int $status_code_guest_lock = 403; + + protected int $status_code_user_lock = 401; + /** * @return class-string */ @@ -32,7 +36,6 @@ public function test_guest_cannot_lock() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => null, 'locked' => false, ]); @@ -45,11 +48,10 @@ public function test_guest_cannot_lock() $response = $this->put($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_lock); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => null, 'locked' => false, ]); } @@ -62,13 +64,10 @@ public function test_lock_as_admin_and_succeed() $user = User::factory()->admin()->create(); - $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, - ]); + $model = $fqdn::factory()->create(); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => false, ]); @@ -83,7 +82,6 @@ public function test_lock_as_admin_and_succeed() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => true, ]); @@ -100,13 +98,10 @@ public function test_lock_as_admin_and_succeed_with_redirect_to_index_with_sorte $user = User::factory()->admin()->create(); - $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, - ]); + $model = $fqdn::factory()->create(); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => false, ]); @@ -126,7 +121,6 @@ public function test_lock_as_admin_and_succeed_with_redirect_to_index_with_sorte $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => true, ]); @@ -141,13 +135,10 @@ public function test_lock_as_user_and_get_denied() $user = User::factory()->create(); - $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, - ]); + $model = $fqdn::factory()->create(); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => false, ]); @@ -160,11 +151,10 @@ public function test_lock_as_user_and_get_denied() $response = $this->actingAs($user)->put($url); - $response->assertStatus(401); + $response->assertStatus($this->status_code_user_lock); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => false, ]); } diff --git a/src/Feature/Http/Controllers/Resource/Playground/RestoreJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/RestoreJsonTrait.php index db88d89..fa5a17e 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/RestoreJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/RestoreJsonTrait.php @@ -13,6 +13,10 @@ */ trait RestoreJsonTrait { + protected int $status_code_json_guest_restore = 403; + + protected int $status_code_json_user_restore = 401; + /** * @return class-string */ @@ -35,7 +39,6 @@ public function test_json_guest_cannot_restore() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => null, 'deleted_at' => Carbon::now(), ]); @@ -48,11 +51,10 @@ public function test_json_guest_cannot_restore() $response = $this->putJson($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_json_guest_restore); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => null, 'deleted_at' => Carbon::now(), ]); } @@ -66,13 +68,11 @@ public function test_json_restore_as_admin_and_succeed() $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, 'deleted_at' => Carbon::now(), ]); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => Carbon::now(), ]); @@ -87,7 +87,6 @@ public function test_json_restore_as_admin_and_succeed() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -105,13 +104,11 @@ public function test_json_restore_as_admin_and_succeed_with_no_redirect() $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, 'deleted_at' => Carbon::now(), ]); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => Carbon::now(), ]); @@ -133,7 +130,6 @@ public function test_json_restore_as_admin_and_succeed_with_no_redirect() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -151,13 +147,11 @@ public function test_json_restore_as_user_and_get_denied() $user = User::factory()->create(['role' => 'user']); $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, 'deleted_at' => Carbon::now(), ]); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => Carbon::now(), ]); @@ -170,11 +164,10 @@ public function test_json_restore_as_user_and_get_denied() $response = $this->actingAs($user)->putJson($url); - $response->assertStatus(401); + $response->assertStatus($this->status_code_json_user_restore); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => Carbon::now(), ]); } diff --git a/src/Feature/Http/Controllers/Resource/Playground/RestoreRevisionJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/RestoreRevisionJsonTrait.php index 708776d..afa1f59 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/RestoreRevisionJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/RestoreRevisionJsonTrait.php @@ -12,6 +12,8 @@ */ trait RestoreRevisionJsonTrait { + protected int $status_code_json_guest_restore_revision = 403; + /** * @return class-string */ @@ -64,7 +66,7 @@ public function test_json_guest_cannot_restore_revision() $response = $this->putJson($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_json_guest_restore_revision); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, diff --git a/src/Feature/Http/Controllers/Resource/Playground/RestoreRevisionTrait.php b/src/Feature/Http/Controllers/Resource/Playground/RestoreRevisionTrait.php index e0dc6ab..21977a9 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/RestoreRevisionTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/RestoreRevisionTrait.php @@ -12,6 +12,8 @@ */ trait RestoreRevisionTrait { + protected int $status_code_guest_restore_revision = 403; + /** * @return class-string */ @@ -59,7 +61,7 @@ public function test_guest_cannot_restore_revision() $response = $this->put($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_restore_revision); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, diff --git a/src/Feature/Http/Controllers/Resource/Playground/RestoreTrait.php b/src/Feature/Http/Controllers/Resource/Playground/RestoreTrait.php index e30f971..b730899 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/RestoreTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/RestoreTrait.php @@ -13,6 +13,10 @@ */ trait RestoreTrait { + protected int $status_code_guest_restore = 403; + + protected int $status_code_user_restore = 401; + /** * @return class-string */ @@ -35,7 +39,6 @@ public function test_guest_cannot_restore() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => null, 'deleted_at' => Carbon::now(), ]); @@ -48,11 +51,10 @@ public function test_guest_cannot_restore() $response = $this->put($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_restore); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => null, 'deleted_at' => Carbon::now(), ]); } @@ -66,13 +68,11 @@ public function test_restore_as_admin_and_succeed() $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, 'deleted_at' => Carbon::now(), ]); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => Carbon::now(), ]); @@ -87,7 +87,6 @@ public function test_restore_as_admin_and_succeed() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -105,13 +104,11 @@ public function test_restore_as_admin_and_succeed_with_redirect_to_index_with_on $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, 'deleted_at' => Carbon::now(), ]); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => Carbon::now(), ]); @@ -138,7 +135,6 @@ public function test_restore_as_admin_and_succeed_with_redirect_to_index_with_on $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => null, ]); @@ -154,13 +150,11 @@ public function test_restore_as_user_and_get_denied() $user = User::factory()->create(['role' => 'user']); $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, 'deleted_at' => Carbon::now(), ]); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => Carbon::now(), ]); @@ -173,11 +167,10 @@ public function test_restore_as_user_and_get_denied() $response = $this->actingAs($user)->put($url); - $response->assertStatus(401); + $response->assertStatus($this->status_code_user_restore); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'deleted_at' => Carbon::now(), ]); } diff --git a/src/Feature/Http/Controllers/Resource/Playground/RevisionJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/RevisionJsonTrait.php index 32a1959..73fc217 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/RevisionJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/RevisionJsonTrait.php @@ -12,6 +12,8 @@ */ trait RevisionJsonTrait { + protected int $status_code_guest_json_revision = 403; + /** * @return class-string */ @@ -59,7 +61,7 @@ public function test_json_guest_cannot_render_revision_view() $response = $this->getJson($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_json_revision); } public function test_json_admin_can_get_revision_info() diff --git a/src/Feature/Http/Controllers/Resource/Playground/RevisionTrait.php b/src/Feature/Http/Controllers/Resource/Playground/RevisionTrait.php index 4ef1177..f573b5d 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/RevisionTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/RevisionTrait.php @@ -12,6 +12,8 @@ */ trait RevisionTrait { + protected int $status_code_guest_revision = 403; + /** * @return class-string */ @@ -59,7 +61,7 @@ public function test_guest_cannot_render_revision_view() $response = $this->get($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_revision); } public function test_revision_view_rendered_by_admin() diff --git a/src/Feature/Http/Controllers/Resource/Playground/RevisionsJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/RevisionsJsonTrait.php index cd44d55..b6876fc 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/RevisionsJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/RevisionsJsonTrait.php @@ -12,6 +12,8 @@ */ trait RevisionsJsonTrait { + protected int $status_code_guest_json_revisions = 403; + /** * @return class-string */ @@ -53,7 +55,7 @@ public function test_json_guest_cannot_get_revisions() $response = $this->getJson($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_json_revisions); } public function test_json_admin_can_get_revisions() diff --git a/src/Feature/Http/Controllers/Resource/Playground/RevisionsTrait.php b/src/Feature/Http/Controllers/Resource/Playground/RevisionsTrait.php index f1047b9..d4aac60 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/RevisionsTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/RevisionsTrait.php @@ -12,6 +12,8 @@ */ trait RevisionsTrait { + protected int $status_code_guest_revisions = 403; + /** * @return class-string */ @@ -53,7 +55,7 @@ public function test_guest_cannot_render_revisions_view() $response = $this->get($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_revisions); } public function test_admin_can_render_revisions_view() diff --git a/src/Feature/Http/Controllers/Resource/Playground/ShowJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/ShowJsonTrait.php index 07780cd..916c646 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/ShowJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/ShowJsonTrait.php @@ -12,6 +12,8 @@ */ trait ShowJsonTrait { + protected int $status_code_json_guest_show = 403; + /** * @return class-string */ @@ -38,7 +40,8 @@ public function test_json_guest_cannot_see_info() ]); $response = $this->getJson($url); - $response->assertStatus(403); + + $response->assertStatus($this->status_code_json_guest_show); } public function test_json_show_info_for_admin() diff --git a/src/Feature/Http/Controllers/Resource/Playground/ShowTrait.php b/src/Feature/Http/Controllers/Resource/Playground/ShowTrait.php index 3a02c78..13b37eb 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/ShowTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/ShowTrait.php @@ -12,6 +12,8 @@ */ trait ShowTrait { + protected int $status_code_guest_show = 403; + /** * @return class-string */ @@ -39,7 +41,7 @@ public function test_guest_cannot_render_show_view() $response = $this->get($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_show); } public function test_show_view_rendered_by_admin() diff --git a/src/Feature/Http/Controllers/Resource/Playground/StoreJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/StoreJsonTrait.php index 42e57ae..edd1395 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/StoreJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/StoreJsonTrait.php @@ -12,6 +12,14 @@ */ trait StoreJsonTrait { + protected int $status_code_guest_json_store = 403; + + protected string $store_json_parameter = 'title'; + + protected array $store_json_without_payload_errors = [ + 'title', + ]; + /** * @return class-string */ @@ -36,24 +44,20 @@ public function test_json_guest_cannot_store() $model = $fqdn::factory()->make(); $this->assertDatabaseMissing($packageInfo['table'], [ - 'title' => $model->title, + $this->store_json_parameter => $model->getAttributeValue($this->store_json_parameter), ]); $url = route(sprintf('%1$s.post', $packageInfo['model_route'])); $response = $this->postJson($url, $model->toArray()); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_json_store); $this->assertDatabaseMissing($packageInfo['table'], [ - 'title' => $model->title, + $this->store_json_parameter => $model->getAttributeValue($this->store_json_parameter), ]); } - protected array $store_without_payload_errors = [ - 'title', - ]; - public function test_json_store_as_admin_without_payload_and_fail_validation() { $packageInfo = $this->getPackageInfo(); @@ -65,18 +69,18 @@ public function test_json_store_as_admin_without_payload_and_fail_validation() $model = $fqdn::factory()->make(); $this->assertDatabaseMissing($packageInfo['table'], [ - 'title' => $model->title, + $this->store_json_parameter => $model->getAttributeValue($this->store_json_parameter), ]); $url = route(sprintf('%1$s.post', $packageInfo['model_route'])); $response = $this->actingAs($user)->postJson($url); - $response->assertInvalid($this->store_without_payload_errors); + $response->assertInvalid($this->store_json_without_payload_errors); $response->assertStatus(422); $this->assertDatabaseMissing($packageInfo['table'], [ - 'title' => $model->title, + $this->store_json_parameter => $model->getAttributeValue($this->store_json_parameter), ]); } @@ -89,20 +93,33 @@ public function test_json_store_as_admin_and_succeed() $user = User::factory()->admin()->create(); $model = $fqdn::factory()->make(); - + // dump([ + // '__METHOD__' => __METHOD__, + // '$model->toArray()' => $model->toArray(), + // '$this->store_json_parameter' => $this->store_json_parameter, + // '$model->getAttributeValue($this->store_json_parameter)' => $model->getAttributeValue($this->store_json_parameter), + // ]); $this->assertDatabaseMissing($packageInfo['table'], [ - 'title' => $model->title, + $this->store_json_parameter => $model->getAttributeValue($this->store_json_parameter), ]); $url = route(sprintf('%1$s.post', $packageInfo['model_route'])); + // dump([ + // '__METHOD__' => __METHOD__, + // '$url' => $url, + // ]); $response = $this->actingAs($user)->postJson($url, $model->toArray()); + // $response->dump(); $this->assertDatabaseHas($packageInfo['table'], [ - 'title' => $model->title, + $this->store_json_parameter => $model->getAttributeValue($this->store_json_parameter), ]); - $created = $fqdn::where('title', $model->title)->firstOrFail(); + $created = $fqdn::where( + $this->store_json_parameter, + $model->getAttributeValue($this->store_json_parameter) + )->firstOrFail(); $response->assertStatus(201); } diff --git a/src/Feature/Http/Controllers/Resource/Playground/StoreTrait.php b/src/Feature/Http/Controllers/Resource/Playground/StoreTrait.php index c194fce..d49a835 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/StoreTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/StoreTrait.php @@ -12,6 +12,14 @@ */ trait StoreTrait { + protected int $status_code_guest_store = 403; + + protected string $store_parameter = 'title'; + + protected array $store_without_payload_errors = [ + 'title', + ]; + /** * @return class-string */ @@ -31,24 +39,20 @@ public function test_guest_cannot_store() $model = $fqdn::factory()->make(); $this->assertDatabaseMissing($packageInfo['table'], [ - 'title' => $model->title, + $this->store_parameter => $model->getAttributeValue($this->store_parameter), ]); $url = route(sprintf('%1$s.post', $packageInfo['model_route'])); $response = $this->post($url, $model->toArray()); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_store); $this->assertDatabaseMissing($packageInfo['table'], [ - 'title' => $model->title, + $this->store_parameter => $model->getAttributeValue($this->store_parameter), ]); } - protected array $store_without_payload_errors = [ - 'title', - ]; - public function test_store_as_admin_without_payload_and_fail_validation() { $packageInfo = $this->getPackageInfo(); @@ -60,7 +64,7 @@ public function test_store_as_admin_without_payload_and_fail_validation() $model = $fqdn::factory()->make(); $this->assertDatabaseMissing($packageInfo['table'], [ - 'title' => $model->title, + $this->store_parameter => $model->getAttributeValue($this->store_parameter), ]); $url = route(sprintf('%1$s.post', $packageInfo['model_route'])); @@ -73,7 +77,7 @@ public function test_store_as_admin_without_payload_and_fail_validation() $response->assertStatus(302); $this->assertDatabaseMissing($packageInfo['table'], [ - 'title' => $model->title, + $this->store_parameter => $model->getAttributeValue($this->store_parameter), ]); // $response->assertStatus(422); } @@ -87,12 +91,22 @@ public function test_store_as_admin_and_succeed() $user = User::factory()->admin()->create(); $model = $fqdn::factory()->make(); + // dump([ + // '__METHOD__' => __METHOD__, + // '$model->toArray()' => $model->toArray(), + // '$this->store_json_parameter' => $this->store_parameter, + // '$model->getAttributeValue($this->store_json_parameter)' => $model->getAttributeValue($this->store_parameter), + // ]); $this->assertDatabaseMissing($packageInfo['table'], [ - 'title' => $model->title, + $this->store_parameter => $model->getAttributeValue($this->store_parameter), ]); $url = route(sprintf('%1$s.post', $packageInfo['model_route'])); + // dump([ + // '__METHOD__' => __METHOD__, + // '$url' => $url, + // ]); $response = $this->actingAs($user)->post($url, $model->toArray()); @@ -100,10 +114,13 @@ public function test_store_as_admin_and_succeed() // $response->dumpSession(); $this->assertDatabaseHas($packageInfo['table'], [ - 'title' => $model->title, + $this->store_parameter => $model->getAttributeValue($this->store_parameter), ]); - $created = $fqdn::where('title', $model->title)->firstOrFail(); + $created = $fqdn::where( + $this->store_parameter, + $model->getAttributeValue($this->store_parameter) + )->firstOrFail(); $response->assertRedirect(route(sprintf('%1$s.show', $packageInfo['model_route']), [ $packageInfo['model_slug'] => $created->id, @@ -121,7 +138,7 @@ public function test_store_as_admin_and_succeed_with_return_url() $model = $fqdn::factory()->make(); $this->assertDatabaseMissing($packageInfo['table'], [ - 'title' => $model->title, + $this->store_parameter => $model->getAttributeValue($this->store_parameter), ]); $_return_url = route($packageInfo['model_route'], [ @@ -142,10 +159,13 @@ public function test_store_as_admin_and_succeed_with_return_url() // $response->dumpSession(); $this->assertDatabaseHas($packageInfo['table'], [ - 'title' => $model->title, + $this->store_parameter => $model->getAttributeValue($this->store_parameter), ]); - $created = $fqdn::where('title', $model->title)->firstOrFail(); + $created = $fqdn::where( + $this->store_parameter, + $model->getAttributeValue($this->store_parameter) + )->firstOrFail(); $response->assertRedirect($_return_url); } diff --git a/src/Feature/Http/Controllers/Resource/Playground/UnlockJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/UnlockJsonTrait.php index 7b02d10..c4549ee 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/UnlockJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/UnlockJsonTrait.php @@ -12,6 +12,10 @@ */ trait UnlockJsonTrait { + protected int $status_code_guest_json_unlock = 403; + + protected int $status_code_user_json_unlock = 401; + /** * @return class-string */ @@ -39,7 +43,6 @@ public function test_json_guest_cannot_unlock() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => null, 'locked' => true, ]); @@ -52,11 +55,10 @@ public function test_json_guest_cannot_unlock() $response = $this->deleteJson($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_json_unlock); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => null, 'locked' => true, ]); } @@ -70,13 +72,11 @@ public function test_json_unlock_as_admin_and_succeed() $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, 'locked' => true, ]); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => true, ]); @@ -91,7 +91,6 @@ public function test_json_unlock_as_admin_and_succeed() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => false, ]); @@ -109,13 +108,11 @@ public function test_json_unlock_as_admin_and_succeed_with_no_redirect() $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, 'locked' => true, ]); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => true, ]); @@ -135,7 +132,6 @@ public function test_json_unlock_as_admin_and_succeed_with_no_redirect() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => false, ]); @@ -153,13 +149,11 @@ public function test_json_unlock_as_user_and_get_denied() $user = User::factory()->admin()->create(['role' => 'user']); $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, 'locked' => true, ]); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => true, ]); @@ -172,11 +166,10 @@ public function test_json_unlock_as_user_and_get_denied() $response = $this->actingAs($user)->deleteJson($url); - $response->assertStatus(401); + $response->assertStatus($this->status_code_user_json_unlock); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => true, ]); } diff --git a/src/Feature/Http/Controllers/Resource/Playground/UnlockTrait.php b/src/Feature/Http/Controllers/Resource/Playground/UnlockTrait.php index 23147dc..d051e57 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/UnlockTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/UnlockTrait.php @@ -12,6 +12,10 @@ */ trait UnlockTrait { + protected int $status_code_guest_unlock = 403; + + protected int $status_code_user_unlock = 401; + /** * @return class-string */ @@ -39,7 +43,6 @@ public function test_guest_cannot_unlock() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => null, 'locked' => true, ]); @@ -52,11 +55,10 @@ public function test_guest_cannot_unlock() $response = $this->delete($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_unlock); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => null, 'locked' => true, ]); } @@ -70,13 +72,11 @@ public function test_unlock_as_admin_and_succeed() $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, 'locked' => true, ]); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => true, ]); @@ -96,7 +96,6 @@ public function test_unlock_as_admin_and_succeed() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => false, ]); @@ -114,13 +113,11 @@ public function test_unlock_as_admin_and_succeed_with_redirect_to_index_with_sor $user = User::factory()->admin()->create(); $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, 'locked' => true, ]); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => true, ]); @@ -140,7 +137,6 @@ public function test_unlock_as_admin_and_succeed_with_redirect_to_index_with_sor $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => false, ]); @@ -156,13 +152,11 @@ public function test_unlock_as_user_and_get_denied() $user = User::factory()->admin()->create(['role' => 'user']); $model = $fqdn::factory()->create([ - 'owned_by_id' => $user->id, 'locked' => true, ]); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => true, ]); @@ -175,11 +169,10 @@ public function test_unlock_as_user_and_get_denied() $response = $this->actingAs($user)->delete($url); - $response->assertStatus(401); + $response->assertStatus($this->status_code_user_unlock); $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'owned_by_id' => $user->id, 'locked' => true, ]); } diff --git a/src/Feature/Http/Controllers/Resource/Playground/UpdateJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/UpdateJsonTrait.php index 6db7d42..3012d11 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/UpdateJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/UpdateJsonTrait.php @@ -12,6 +12,18 @@ */ trait UpdateJsonTrait { + protected int $status_code_guest_json_update = 403; + + protected string $update_json_parameter = 'title'; + + protected array $json_update_without_payload_errors = [ + 'title', + ]; + + protected array $json_update_payload = [ + 'title' => 'change to new title', + ]; + /** * @return class-string */ @@ -39,17 +51,9 @@ public function test_json_guest_cannot_update() $response = $this->patchJson($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_json_update); } - protected array $json_update_without_payload_errors = [ - 'title', - ]; - - protected array $json_update_payload = [ - 'title' => 'change to new title', - ]; - public function test_json_update_as_admin_without_payload_and_fail_validation() { $packageInfo = $this->getPackageInfo(); @@ -85,14 +89,14 @@ public function test_json_admin_can_update() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'title' => $model->title, + $this->update_json_parameter => $model->getAttributeValue($this->update_json_parameter), ]); $payload = $this->json_update_payload + $model->toArray(); $this->assertDatabaseMissing($packageInfo['table'], [ 'id' => $model->id, - 'title' => $payload['title'], + $this->update_json_parameter => $payload[$this->update_json_parameter], ]); $user = User::factory()->admin()->create(); @@ -105,6 +109,7 @@ public function test_json_admin_can_update() ]); $response = $this->actingAs($user)->patchJson($url, $payload); + // $response->dump(); $response->assertStatus(200); $response->assertJsonStructure($this->getStructureData()); @@ -113,7 +118,7 @@ public function test_json_admin_can_update() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'title' => $payload['title'], + $this->update_json_parameter => $payload[$this->update_json_parameter], ]); } } diff --git a/src/Feature/Http/Controllers/Resource/Playground/UpdateTrait.php b/src/Feature/Http/Controllers/Resource/Playground/UpdateTrait.php index c1e4a47..cfb7e9f 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/UpdateTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/UpdateTrait.php @@ -12,6 +12,18 @@ */ trait UpdateTrait { + protected int $status_code_guest_update = 403; + + protected string $update_parameter = 'title'; + + protected array $update_without_payload_errors = [ + 'title', + ]; + + protected array $update_payload = [ + 'title' => 'change to new title', + ]; + /** * @return class-string */ @@ -39,17 +51,9 @@ public function test_guest_cannot_update() $response = $this->patch($url); - $response->assertStatus(403); + $response->assertStatus($this->status_code_guest_update); } - protected array $update_without_payload_errors = [ - 'title', - ]; - - protected array $update_payload = [ - 'title' => 'change to new title', - ]; - public function test_update_as_admin_without_payload_and_fail_validation() { $packageInfo = $this->getPackageInfo(); @@ -85,14 +89,14 @@ public function test_admin_can_update() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'title' => $model->title, + $this->update_parameter => $model->getAttributeValue($this->update_parameter), ]); $payload = $this->update_payload + $model->toArray(); $this->assertDatabaseMissing($packageInfo['table'], [ 'id' => $model->id, - 'title' => $payload['title'], + $this->update_parameter => $payload[$this->update_parameter], ]); $user = User::factory()->admin()->create(); @@ -114,7 +118,7 @@ public function test_admin_can_update() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'title' => $payload['title'], + $this->update_parameter => $payload[$this->update_parameter], ]); } @@ -128,14 +132,14 @@ public function test_admin_can_update_view_with_return_url() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'title' => $model->title, + $this->update_parameter => $model->getAttributeValue($this->update_parameter), ]); $payload = $this->update_payload + $model->toArray(); $this->assertDatabaseMissing($packageInfo['table'], [ 'id' => $model->id, - 'title' => $payload['title'], + $this->update_parameter => $payload[$this->update_parameter], ]); $user = User::factory()->admin()->create(); @@ -158,7 +162,7 @@ public function test_admin_can_update_view_with_return_url() $this->assertDatabaseHas($packageInfo['table'], [ 'id' => $model->id, - 'title' => $payload['title'], + $this->update_parameter => $payload[$this->update_parameter], ]); } } From 96df65cd8989bc8396840f0bf4b7a9df0194806b Mon Sep 17 00:00:00 2001 From: Jeremy Postlethwaite Date: Sun, 3 Mar 2024 12:36:16 -0800 Subject: [PATCH 2/4] GH-26 --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 998ee5b..679b6dc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,7 +30,7 @@ jobs: echo "TIMESTAMP_START=$(date +'%s')" >> $GITHUB_OUTPUT - name: "Slack notification: IN PROGRESS" id: slack - uses: slackapi/slack-github-action@v1.24.0 + uses: slackapi/slack-github-action@v1.25.0 with: channel-id: 'C068A06PV43' payload: | @@ -131,7 +131,7 @@ jobs: echo "DATE_END=$(date +'%Y-%m-%d %H:%M:%S')" >> $GITHUB_OUTPUT echo "DURATION_PHRASE=$(($(date +'%s')-$TIMESTAMP_START)) seconds" >> $GITHUB_OUTPUT - name: "Slack notification: Done" - uses: slackapi/slack-github-action@v1.24.0 + uses: slackapi/slack-github-action@v1.25.0 with: channel-id: 'C068A06PV43' update-ts: ${{ steps.slack.outputs.ts }} @@ -192,7 +192,7 @@ jobs: SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} - name: "Send a notification for failures" if: ${{ failure() }} - uses: slackapi/slack-github-action@v1.24.0 + uses: slackapi/slack-github-action@v1.25.0 with: channel-id: 'C068A06PV43' update-ts: ${{ steps.slack.outputs.ts }} From b236358bed4a4ec1520167c156029ab053131384 Mon Sep 17 00:00:00 2001 From: Jeremy Postlethwaite Date: Sun, 3 Mar 2024 12:59:56 -0800 Subject: [PATCH 3/4] GH-26 --- .../Http/Controllers/Resource/Playground/CreateJsonTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Feature/Http/Controllers/Resource/Playground/CreateJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/CreateJsonTrait.php index 29a91b0..cb3868e 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/CreateJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/CreateJsonTrait.php @@ -12,7 +12,7 @@ */ trait CreateJsonTrait { - protected int $status_code_guest_create = 403; + protected int $status_code_json_guest_create = 403; protected string $create_info_parameter = 'owned_by_id'; From bc6232fcf36f131318621f2aa98c49d12474f714 Mon Sep 17 00:00:00 2001 From: Jeremy Postlethwaite Date: Sun, 3 Mar 2024 13:31:53 -0800 Subject: [PATCH 4/4] GH-26 --- .../Http/Controllers/Resource/Playground/StoreJsonTrait.php | 3 +++ .../Http/Controllers/Resource/Playground/StoreTrait.php | 3 +++ .../Controllers/Resource/Playground/UpdateJsonTrait.php | 6 ++++++ .../Http/Controllers/Resource/Playground/UpdateTrait.php | 6 ++++++ 4 files changed, 18 insertions(+) diff --git a/src/Feature/Http/Controllers/Resource/Playground/StoreJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/StoreJsonTrait.php index edd1395..7768c34 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/StoreJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/StoreJsonTrait.php @@ -16,6 +16,9 @@ trait StoreJsonTrait protected string $store_json_parameter = 'title'; + /** + * @var array + */ protected array $store_json_without_payload_errors = [ 'title', ]; diff --git a/src/Feature/Http/Controllers/Resource/Playground/StoreTrait.php b/src/Feature/Http/Controllers/Resource/Playground/StoreTrait.php index d49a835..049e8d1 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/StoreTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/StoreTrait.php @@ -16,6 +16,9 @@ trait StoreTrait protected string $store_parameter = 'title'; + /** + * @var array + */ protected array $store_without_payload_errors = [ 'title', ]; diff --git a/src/Feature/Http/Controllers/Resource/Playground/UpdateJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/UpdateJsonTrait.php index 3012d11..329dbd6 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/UpdateJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/UpdateJsonTrait.php @@ -16,10 +16,16 @@ trait UpdateJsonTrait protected string $update_json_parameter = 'title'; + /** + * @var array + */ protected array $json_update_without_payload_errors = [ 'title', ]; + /** + * @var array + */ protected array $json_update_payload = [ 'title' => 'change to new title', ]; diff --git a/src/Feature/Http/Controllers/Resource/Playground/UpdateTrait.php b/src/Feature/Http/Controllers/Resource/Playground/UpdateTrait.php index cfb7e9f..b88fbbe 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/UpdateTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/UpdateTrait.php @@ -16,10 +16,16 @@ trait UpdateTrait protected string $update_parameter = 'title'; + /** + * @var array + */ protected array $update_without_payload_errors = [ 'title', ]; + /** + * @var array + */ protected array $update_payload = [ 'title' => 'change to new title', ];