From eab75c872079ed715d9dd4de176755dde609c54a Mon Sep 17 00:00:00 2001 From: Hasin Hayder Date: Sun, 22 May 2022 00:20:06 +0600 Subject: [PATCH] Role Tests --- tests/Feature/AdminLoginFailTest.php | 32 ---- tests/Feature/AdminLoginTest.php | 17 +- tests/Feature/ExampleTest.php | 21 --- tests/Feature/RoleTest.php | 246 +++++++++++++++++++++++++++ 4 files changed, 262 insertions(+), 54 deletions(-) delete mode 100644 tests/Feature/AdminLoginFailTest.php delete mode 100644 tests/Feature/ExampleTest.php create mode 100644 tests/Feature/RoleTest.php diff --git a/tests/Feature/AdminLoginFailTest.php b/tests/Feature/AdminLoginFailTest.php deleted file mode 100644 index ef4add0..0000000 --- a/tests/Feature/AdminLoginFailTest.php +++ /dev/null @@ -1,32 +0,0 @@ -postJson('/api/login',[ - 'email'=>'admin@hydra.project', - 'password'=>'hydrax' - ]); - - $response - ->assertJson(fn (AssertableJson $json) => - $json->where('error', 1) - ->missing('token') - ->has('message') - ); - } -} diff --git a/tests/Feature/AdminLoginTest.php b/tests/Feature/AdminLoginTest.php index 52f0c40..55a6003 100644 --- a/tests/Feature/AdminLoginTest.php +++ b/tests/Feature/AdminLoginTest.php @@ -15,7 +15,7 @@ class AdminLoginTest extends TestCase * * @return void */ - public function test_example() + public function test_admin_login() { $response = $this->postJson('/api/login',[ 'email'=>'admin@hydra.project', @@ -29,4 +29,19 @@ class AdminLoginTest extends TestCase ->etc() ); } + + public function test_admin_login_fail() + { + $response = $this->postJson('/api/login',[ + 'email'=>'admin@hydra.project', + 'password'=>'hydrax' + ]); + + $response + ->assertJson(fn (AssertableJson $json) => + $json->where('error', 1) + ->missing('token') + ->has('message') + ); + } } diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php deleted file mode 100644 index 78ccc21..0000000 --- a/tests/Feature/ExampleTest.php +++ /dev/null @@ -1,21 +0,0 @@ -get('/'); - - $response->assertStatus(200); - } -} diff --git a/tests/Feature/RoleTest.php b/tests/Feature/RoleTest.php new file mode 100644 index 0000000..b494427 --- /dev/null +++ b/tests/Feature/RoleTest.php @@ -0,0 +1,246 @@ +postJson('/api/login', [ + 'email' => 'admin@hydra.project', + 'password' => 'hydra' + ]); + + $data = json_decode($response->getContent()); + $this->token = $data->token; + $this->user_id = $data->id; + + + $response = $this->withHeader('Authorization', 'Bearer ' . $this->token) + ->get("/api/roles"); + + $response + ->assertJson( + fn (AssertableJson $json) => + $json->has(6) + ->first( + fn ($json) => + $json->where('id', 1) + ->where('name', 'Administrator') + ->where('slug','admin') + ->etc() + ) + ); + } + + public function test_update_role_name_as_admin() { + $response = $this->postJson('/api/login', [ + 'email' => 'admin@hydra.project', + 'password' => 'hydra' + ]); + + $data = json_decode($response->getContent()); + $this->token = $data->token; + $this->user_id = $data->id; + + + $response = $this->withHeader('Authorization', 'Bearer ' . $this->token) + ->put("/api/roles/4",[ + "name"=>"Chief Editor" + ]); + + $response + ->assertJson( + fn (AssertableJson $json) => + $json->where('name','Chief Editor') + ->missing('error') + ->etc() + ); + } + + public function test_update_role_slug_as_admin() { + $response = $this->postJson('/api/login', [ + 'email' => 'admin@hydra.project', + 'password' => 'hydra' + ]); + + $data = json_decode($response->getContent()); + $this->token = $data->token; + $this->user_id = $data->id; + + + $response = $this->withHeader('Authorization', 'Bearer ' . $this->token) + ->put("/api/roles/4",[ + "slug"=>"chief-editor" + ]); + + $response + ->assertJson( + fn (AssertableJson $json) => + $json->where('slug','chief-editor') + ->missing('error') + ->etc() + ); + } + + public function test_update_role_namd_and_slug_as_admin() { + $response = $this->postJson('/api/login', [ + 'email' => 'admin@hydra.project', + 'password' => 'hydra' + ]); + + $data = json_decode($response->getContent()); + $this->token = $data->token; + $this->user_id = $data->id; + + + $response = $this->withHeader('Authorization', 'Bearer ' . $this->token) + ->put("/api/roles/4",[ + "name"=>"Editor X", + "slug"=>"editor-x" + ]); + + $response + ->assertJson( + fn (AssertableJson $json) => + $json->where('name','Editor X') + ->where('slug','editor-x') + ->missing('error') + ->etc() + ); + } + + public function test_update_admin_slug_as_admin_should_fail() { + $response = $this->postJson('/api/login', [ + 'email' => 'admin@hydra.project', + 'password' => 'hydra' + ]); + + $data = json_decode($response->getContent()); + $this->token = $data->token; + $this->user_id = $data->id; + + + $response = $this->withHeader('Authorization', 'Bearer ' . $this->token) + ->put("/api/roles/1",[ + "slug"=>"admin-x" + ]); + + $response + ->assertJson( + fn (AssertableJson $json) => + $json + ->where('slug','admin') + ->etc() + ); + } + + public function test_create_new_role_as_admin() { + $response = $this->postJson('/api/login', [ + 'email' => 'admin@hydra.project', + 'password' => 'hydra' + ]); + + $data = json_decode($response->getContent()); + $this->token = $data->token; + $this->user_id = $data->id; + + + $response = $this->withHeader('Authorization', 'Bearer ' . $this->token) + ->post("/api/roles",[ + "name"=>"New Role", + "slug"=>"new-role" + ]); + + $response + ->assertJson( + fn (AssertableJson $json) => + $json->where('name','New Role') + ->where('slug','new-role') + ->missing('error') + ->etc() + ); + } + + public function test_duplicate_role_will_not_be_created() { + $response = $this->postJson('/api/login', [ + 'email' => 'admin@hydra.project', + 'password' => 'hydra' + ]); + + $data = json_decode($response->getContent()); + $this->token = $data->token; + $this->user_id = $data->id; + + + $response = $this->withHeader('Authorization', 'Bearer ' . $this->token) + ->post("/api/roles",[ + "name"=>"New Role", + "slug"=>"new-role" + ]); + + $response + ->assertJson( + fn (AssertableJson $json) => + $json->where('error',1) + ->etc() + ); + } + + public function test_delete_role_as_admin() { + $response = $this->postJson('/api/login', [ + 'email' => 'admin@hydra.project', + 'password' => 'hydra' + ]); + + $data = json_decode($response->getContent()); + $this->token = $data->token; + $this->user_id = $data->id; + + $newRole = Role::where('slug','new-role')->first(); + + + $response = $this->withHeader('Authorization', 'Bearer ' . $this->token) + ->delete("/api/roles/{$newRole->id}"); + + $response + ->assertJson( + fn (AssertableJson $json) => + $json->where('error',0) + ->has('message') + ); + } + + public function test_delete_admin_role_should_fail() { + $response = $this->postJson('/api/login', [ + 'email' => 'admin@hydra.project', + 'password' => 'hydra' + ]); + + $data = json_decode($response->getContent()); + $this->token = $data->token; + $this->user_id = $data->id; + + $newRole = Role::where('slug','admin')->first(); + + + $response = $this->withHeader('Authorization', 'Bearer ' . $this->token) + ->delete("/api/roles/{$newRole->id}"); + + $response + ->assertJson( + fn (AssertableJson $json) => + $json->where('error',1) + ->has('message') + ); + } +}