@ -2,22 +2,22 @@
namespace Tests\Feature;
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Illuminate\Testing\Fluent\AssertableJson;
use App\Models\Role;
use App\Models\Role;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
class RoleTest extends TestCase {
class RoleTest extends TestCase
{
/**
/**
* A basic feature test example.
* A basic feature test example.
*
*
* @return void
* @return void
*/
*/
public function test_list_roles() {
public function test_list_roles()
{
$response = $this->postJson('/api/login', [
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'email' => 'admin@hydra.project',
'password' => 'hydra'
'password' => 'hydra',
]);
]);
$data = json_decode($response->getContent());
$data = json_decode($response->getContent());
@ -36,16 +36,17 @@ class RoleTest extends TestCase {
fn ($json) =>
fn ($json) =>
$json->where('id', 1)
$json->where('id', 1)
->where('name', 'Administrator')
->where('name', 'Administrator')
->where('slug','admin')
->where('slug', 'admin')
->etc()
->etc()
)
)
);
);
}
}
public function test_update_role_name_as_admin() {
public function test_update_role_name_as_admin()
{
$response = $this->postJson('/api/login', [
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'email' => 'admin@hydra.project',
'password' => 'hydra'
'password' => 'hydra',
]);
]);
$data = json_decode($response->getContent());
$data = json_decode($response->getContent());
@ -54,23 +55,24 @@ class RoleTest extends TestCase {
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
->put("/api/roles/4",[
->put("/api/roles/4", [
"name"=>"Chief Editor"
"name" => "Chief Editor",
]);
]);
$response
$response
->assertJson(
->assertJson(
fn (AssertableJson $json) =>
fn (AssertableJson $json) =>
$json->where('name','Chief Editor')
$json->where('name', 'Chief Editor')
->missing('error')
->missing('error')
->etc()
->etc()
);
);
}
}
public function test_update_role_slug_as_admin() {
public function test_update_role_slug_as_admin()
{
$response = $this->postJson('/api/login', [
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'email' => 'admin@hydra.project',
'password' => 'hydra'
'password' => 'hydra',
]);
]);
$data = json_decode($response->getContent());
$data = json_decode($response->getContent());
@ -79,23 +81,24 @@ class RoleTest extends TestCase {
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
->put("/api/roles/4",[
->put("/api/roles/4", [
"slug"=>"chief-editor"
"slug" => "chief-editor",
]);
]);
$response
$response
->assertJson(
->assertJson(
fn (AssertableJson $json) =>
fn (AssertableJson $json) =>
$json->where('slug','chief-editor')
$json->where('slug', 'chief-editor')
->missing('error')
->missing('error')
->etc()
->etc()
);
);
}
}
public function test_update_role_namd_and_slug_as_admin() {
public function test_update_role_namd_and_slug_as_admin()
{
$response = $this->postJson('/api/login', [
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'email' => 'admin@hydra.project',
'password' => 'hydra'
'password' => 'hydra',
]);
]);
$data = json_decode($response->getContent());
$data = json_decode($response->getContent());
@ -104,25 +107,26 @@ class RoleTest extends TestCase {
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
->put("/api/roles/4",[
->put("/api/roles/4", [
"name"=>"Editor X",
"name" => "Editor X",
"slug"=>"editor-x"
"slug" => "editor-x",
]);
]);
$response
$response
->assertJson(
->assertJson(
fn (AssertableJson $json) =>
fn (AssertableJson $json) =>
$json->where('name','Editor X')
$json->where('name', 'Editor X')
->where('slug','editor-x')
->where('slug', 'editor-x')
->missing('error')
->missing('error')
->etc()
->etc()
);
);
}
}
public function test_update_admin_slug_as_admin_should_fail() {
public function test_update_admin_slug_as_admin_should_fail()
{
$response = $this->postJson('/api/login', [
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'email' => 'admin@hydra.project',
'password' => 'hydra'
'password' => 'hydra',
]);
]);
$data = json_decode($response->getContent());
$data = json_decode($response->getContent());
@ -131,23 +135,24 @@ class RoleTest extends TestCase {
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
->put("/api/roles/1",[
->put("/api/roles/1", [
"slug"=>"admin-x"
"slug" => "admin-x",
]);
]);
$response
$response
->assertJson(
->assertJson(
fn (AssertableJson $json) =>
fn (AssertableJson $json) =>
$json
$json
->where('slug','admin')
->where('slug', 'admin')
->etc()
->etc()
);
);
}
}
public function test_create_new_role_as_admin() {
public function test_create_new_role_as_admin()
{
$response = $this->postJson('/api/login', [
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'email' => 'admin@hydra.project',
'password' => 'hydra'
'password' => 'hydra',
]);
]);
$data = json_decode($response->getContent());
$data = json_decode($response->getContent());
@ -156,25 +161,26 @@ class RoleTest extends TestCase {
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
->post("/api/roles",[
->post("/api/roles", [
"name"=>"New Role",
"name" => "New Role",
"slug"=>"new-role"
"slug" => "new-role",
]);
]);
$response
$response
->assertJson(
->assertJson(
fn (AssertableJson $json) =>
fn (AssertableJson $json) =>
$json->where('name','New Role')
$json->where('name', 'New Role')
->where('slug','new-role')
->where('slug', 'new-role')
->missing('error')
->missing('error')
->etc()
->etc()
);
);
}
}
public function test_duplicate_role_will_not_be_created() {
public function test_duplicate_role_will_not_be_created()
{
$response = $this->postJson('/api/login', [
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'email' => 'admin@hydra.project',
'password' => 'hydra'
'password' => 'hydra',
]);
]);
$data = json_decode($response->getContent());
$data = json_decode($response->getContent());
@ -183,30 +189,31 @@ class RoleTest extends TestCase {
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
->post("/api/roles",[
->post("/api/roles", [
"name"=>"New Role",
"name" => "New Role",
"slug"=>"new-role"
"slug" => "new-role",
]);
]);
$response
$response
->assertJson(
->assertJson(
fn (AssertableJson $json) =>
fn (AssertableJson $json) =>
$json->where('error',1)
$json->where('error', 1)
->etc()
->etc()
);
);
}
}
public function test_delete_role_as_admin() {
public function test_delete_role_as_admin()
{
$response = $this->postJson('/api/login', [
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'email' => 'admin@hydra.project',
'password' => 'hydra'
'password' => 'hydra',
]);
]);
$data = json_decode($response->getContent());
$data = json_decode($response->getContent());
$this->token = $data->token;
$this->token = $data->token;
$this->user_id = $data->id;
$this->user_id = $data->id;
$newRole = Role::where('slug','new-role')->first();
$newRole = Role::where('slug', 'new-role')->first();
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
@ -215,22 +222,23 @@ class RoleTest extends TestCase {
$response
$response
->assertJson(
->assertJson(
fn (AssertableJson $json) =>
fn (AssertableJson $json) =>
$json->where('error',0)
$json->where('error', 0)
->has('message')
->has('message')
);
);
}
}
public function test_delete_admin_role_should_fail() {
public function test_delete_admin_role_should_fail()
{
$response = $this->postJson('/api/login', [
$response = $this->postJson('/api/login', [
'email' => 'admin@hydra.project',
'email' => 'admin@hydra.project',
'password' => 'hydra'
'password' => 'hydra',
]);
]);
$data = json_decode($response->getContent());
$data = json_decode($response->getContent());
$this->token = $data->token;
$this->token = $data->token;
$this->user_id = $data->id;
$this->user_id = $data->id;
$newRole = Role::where('slug','admin')->first();
$newRole = Role::where('slug', 'admin')->first();
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
$response = $this->withHeader('Authorization', 'Bearer ' . $this->token)
@ -239,7 +247,7 @@ class RoleTest extends TestCase {
$response
$response
->assertJson(
->assertJson(
fn (AssertableJson $json) =>
fn (AssertableJson $json) =>
$json->where('error',1)
$json->where('error', 1)
->has('message')
->has('message')
);
);
}
}